1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHP version of mysqldump cli that comes with MySQL. |
5
|
|
|
* |
6
|
|
|
* Tags: mysql mysqldump pdo php7 php5 database php sql hhvm mariadb mysql-backup. |
7
|
|
|
* |
8
|
|
|
* @category Library |
|
|
|
|
9
|
|
|
* @package Ifsnop\Mysqldump |
|
|
|
|
10
|
|
|
* @author Diego Torres <[email protected]> |
|
|
|
|
11
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License |
|
|
|
|
12
|
|
|
* @link https://github.com/ifsnop/mysqldump-php |
13
|
|
|
* |
14
|
|
|
*/ |
|
|
|
|
15
|
|
|
|
16
|
|
|
namespace Ifsnop\Mysqldump; |
17
|
|
|
|
18
|
|
|
use Exception; |
19
|
|
|
use PDO; |
20
|
|
|
use PDOException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Mysqldump. |
24
|
|
|
* |
25
|
|
|
* @category Library |
|
|
|
|
26
|
|
|
* @author Diego Torres <[email protected]> |
|
|
|
|
27
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License |
|
|
|
|
28
|
|
|
* @link https://github.com/ifsnop/mysqldump-php |
|
|
|
|
29
|
|
|
* |
30
|
|
|
*/ |
|
|
|
|
31
|
|
|
class Mysqldump |
32
|
|
|
{ |
|
|
|
|
33
|
|
|
|
34
|
|
|
// Same as mysqldump. |
35
|
|
|
const MAXLINESIZE = 1000000; |
36
|
|
|
|
37
|
|
|
// List of available compression methods as constants. |
38
|
|
|
const GZIP = 'Gzip'; |
|
|
|
|
39
|
|
|
const BZIP2 = 'Bzip2'; |
|
|
|
|
40
|
|
|
const NONE = 'None'; |
|
|
|
|
41
|
|
|
const GZIPSTREAM = 'Gzipstream'; |
42
|
|
|
|
43
|
|
|
// List of available connection strings. |
44
|
|
|
const UTF8 = 'utf8'; |
45
|
|
|
const UTF8MB4 = 'utf8mb4'; |
46
|
|
|
const BINARY = 'binary'; |
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Database username. |
50
|
|
|
* @var string |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
public $user; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Database password. |
56
|
|
|
* @var string |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
public $pass; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Connection string for PDO. |
62
|
|
|
* @var string |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
public $dsn; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Destination filename, defaults to stdout. |
68
|
|
|
* @var string |
|
|
|
|
69
|
|
|
*/ |
70
|
|
|
public $fileName = 'php://stdout'; |
71
|
|
|
|
72
|
|
|
// Internal stuff. |
73
|
|
|
private $tables = array(); |
|
|
|
|
74
|
|
|
private $views = array(); |
|
|
|
|
75
|
|
|
private $triggers = array(); |
|
|
|
|
76
|
|
|
private $procedures = array(); |
|
|
|
|
77
|
|
|
private $functions = array(); |
|
|
|
|
78
|
|
|
private $events = array(); |
|
|
|
|
79
|
|
|
private $dbHandler = null; |
|
|
|
|
80
|
|
|
private $dbType = ""; |
|
|
|
|
81
|
|
|
private $compressManager; |
|
|
|
|
82
|
|
|
private $typeAdapter; |
|
|
|
|
83
|
|
|
private $dumpSettings = array(); |
|
|
|
|
84
|
|
|
private $pdoSettings = array(); |
|
|
|
|
85
|
|
|
private $version; |
|
|
|
|
86
|
|
|
private $tableColumnTypes = array(); |
|
|
|
|
87
|
|
|
private $transformTableRowCallable; |
|
|
|
|
88
|
|
|
private $transformColumnValueCallable; |
|
|
|
|
89
|
|
|
private $infoCallable; |
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Database name, parsed from dsn. |
93
|
|
|
* @var string |
|
|
|
|
94
|
|
|
*/ |
95
|
|
|
private $dbName; |
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Host name, parsed from dsn. |
99
|
|
|
* @var string |
|
|
|
|
100
|
|
|
*/ |
101
|
|
|
private $host; |
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Dsn string parsed as an array. |
105
|
|
|
* @var array |
|
|
|
|
106
|
|
|
*/ |
107
|
|
|
private $dsnArray = array(); |
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Keyed on table name, with the value as the conditions. |
111
|
|
|
* e.g. - 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH' |
112
|
|
|
* |
113
|
|
|
* @var array |
114
|
|
|
*/ |
115
|
|
|
private $tableWheres = array(); |
|
|
|
|
116
|
|
|
private $tableLimits = array(); |
|
|
|
|
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Constructor of Mysqldump. Note that in the case of an SQLite database |
121
|
|
|
* connection, the filename must be in the $db parameter. |
122
|
|
|
* |
123
|
|
|
* @param string $dsn PDO DSN connection string |
|
|
|
|
124
|
|
|
* @param string $user SQL account username |
|
|
|
|
125
|
|
|
* @param string $pass SQL account password |
|
|
|
|
126
|
|
|
* @param array $dumpSettings SQL database settings |
|
|
|
|
127
|
|
|
* @param array $pdoSettings PDO configured attributes |
|
|
|
|
128
|
|
|
*/ |
|
|
|
|
129
|
|
|
public function __construct( |
|
|
|
|
130
|
|
|
$dsn = '', |
131
|
|
|
$user = '', |
132
|
|
|
$pass = '', |
133
|
|
|
$dumpSettings = array(), |
|
|
|
|
134
|
|
|
$pdoSettings = array() |
|
|
|
|
135
|
|
|
) { |
|
|
|
|
136
|
|
|
$dumpSettingsDefault = array( |
|
|
|
|
137
|
|
|
'include-tables' => array(), |
|
|
|
|
138
|
|
|
'exclude-tables' => array(), |
|
|
|
|
139
|
|
|
'include-views' => array(), |
|
|
|
|
140
|
|
|
'compress' => Mysqldump::NONE, |
|
|
|
|
141
|
|
|
'init_commands' => array(), |
|
|
|
|
142
|
|
|
'no-data' => array(), |
|
|
|
|
143
|
|
|
'if-not-exists' => false, |
|
|
|
|
144
|
|
|
'reset-auto-increment' => false, |
|
|
|
|
145
|
|
|
'add-drop-database' => false, |
|
|
|
|
146
|
|
|
'add-drop-table' => false, |
|
|
|
|
147
|
|
|
'add-drop-trigger' => true, |
|
|
|
|
148
|
|
|
'add-locks' => true, |
|
|
|
|
149
|
|
|
'complete-insert' => false, |
|
|
|
|
150
|
|
|
'databases' => false, |
|
|
|
|
151
|
|
|
'default-character-set' => Mysqldump::UTF8, |
|
|
|
|
152
|
|
|
'disable-keys' => true, |
|
|
|
|
153
|
|
|
'extended-insert' => true, |
|
|
|
|
154
|
|
|
'events' => false, |
|
|
|
|
155
|
|
|
'hex-blob' => true, /* faster than escaped content */ |
|
|
|
|
156
|
|
|
'insert-ignore' => false, |
|
|
|
|
157
|
|
|
'net_buffer_length' => self::MAXLINESIZE, |
|
|
|
|
158
|
|
|
'no-autocommit' => true, |
|
|
|
|
159
|
|
|
'no-create-info' => false, |
|
|
|
|
160
|
|
|
'lock-tables' => true, |
|
|
|
|
161
|
|
|
'routines' => false, |
|
|
|
|
162
|
|
|
'single-transaction' => true, |
|
|
|
|
163
|
|
|
'skip-triggers' => false, |
|
|
|
|
164
|
|
|
'skip-tz-utc' => false, |
|
|
|
|
165
|
|
|
'skip-comments' => false, |
|
|
|
|
166
|
|
|
'skip-dump-date' => false, |
|
|
|
|
167
|
|
|
'skip-definer' => false, |
|
|
|
|
168
|
|
|
'where' => '', |
|
|
|
|
169
|
|
|
/* deprecated */ |
|
|
|
|
170
|
|
|
'disable-foreign-keys-check' => true |
|
|
|
|
171
|
|
|
); |
|
|
|
|
172
|
|
|
|
173
|
|
|
$pdoSettingsDefault = array( |
|
|
|
|
174
|
|
|
PDO::ATTR_PERSISTENT => true, |
|
|
|
|
175
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
|
|
|
|
176
|
|
|
); |
|
|
|
|
177
|
|
|
|
178
|
|
|
$this->user = $user; |
179
|
|
|
$this->pass = $pass; |
180
|
|
|
$this->parseDsn($dsn); |
181
|
|
|
|
182
|
|
|
// This drops MYSQL dependency, only use the constant if it's defined. |
183
|
|
|
if ("mysql" === $this->dbType) { |
|
|
|
|
184
|
|
|
$pdoSettingsDefault[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = false; |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$this->pdoSettings = array_replace_recursive($pdoSettingsDefault, $pdoSettings); |
|
|
|
|
188
|
|
|
$this->dumpSettings = array_replace_recursive($dumpSettingsDefault, $dumpSettings); |
|
|
|
|
189
|
|
|
$this->dumpSettings['init_commands'][] = "SET NAMES ".$this->dumpSettings['default-character-set']; |
|
|
|
|
190
|
|
|
|
191
|
|
|
if (false === $this->dumpSettings['skip-tz-utc']) { |
|
|
|
|
192
|
|
|
$this->dumpSettings['init_commands'][] = "SET TIME_ZONE='+00:00'"; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$diff = array_diff(array_keys($this->dumpSettings), array_keys($dumpSettingsDefault)); |
196
|
|
|
if (count($diff) > 0) { |
197
|
|
|
throw new Exception("Unexpected value in dumpSettings: (".implode(",", $diff).")"); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if (!is_array($this->dumpSettings['include-tables']) || |
|
|
|
|
201
|
|
|
!is_array($this->dumpSettings['exclude-tables'])) { |
|
|
|
|
202
|
|
|
throw new Exception("Include-tables and exclude-tables should be arrays"); |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// If no include-views is passed in, dump the same views as tables, mimic mysqldump behaviour. |
|
|
|
|
206
|
|
|
if (!isset($dumpSettings['include-views'])) { |
|
|
|
|
207
|
|
|
$this->dumpSettings['include-views'] = $this->dumpSettings['include-tables']; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// Create a new compressManager to manage compressed output |
|
|
|
|
211
|
|
|
$this->compressManager = CompressManagerFactory::create($this->dumpSettings['compress']); |
212
|
|
|
} |
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Destructor of Mysqldump. Unsets dbHandlers and database objects. |
216
|
|
|
*/ |
217
|
|
|
public function __destruct() |
218
|
|
|
{ |
|
|
|
|
219
|
|
|
$this->dbHandler = null; |
|
|
|
|
220
|
|
|
} |
|
|
|
|
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Keyed by table name, with the value as the conditions: |
224
|
|
|
* e.g. 'users' => 'date_registered > NOW() - INTERVAL 6 MONTH AND deleted=0' |
225
|
|
|
* |
226
|
|
|
* @param array $tableWheres |
|
|
|
|
227
|
|
|
*/ |
|
|
|
|
228
|
|
|
public function setTableWheres(array $tableWheres) |
229
|
|
|
{ |
|
|
|
|
230
|
|
|
$this->tableWheres = $tableWheres; |
231
|
|
|
} |
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
|
|
|
|
234
|
|
|
* @param $tableName |
|
|
|
|
235
|
|
|
* |
236
|
|
|
* @return boolean|mixed |
237
|
|
|
*/ |
238
|
|
|
public function getTableWhere($tableName) |
|
|
|
|
239
|
|
|
{ |
|
|
|
|
240
|
|
|
if (!empty($this->tableWheres[$tableName])) { |
|
|
|
|
241
|
|
|
return $this->tableWheres[$tableName]; |
242
|
|
|
} elseif ($this->dumpSettings['where']) { |
|
|
|
|
243
|
|
|
return $this->dumpSettings['where']; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return false; |
|
|
|
|
247
|
|
|
} |
|
|
|
|
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Keyed by table name, with the value as the numeric limit: |
251
|
|
|
* e.g. 'users' => 3000 |
252
|
|
|
* |
253
|
|
|
* @param array $tableLimits |
|
|
|
|
254
|
|
|
*/ |
|
|
|
|
255
|
|
|
public function setTableLimits(array $tableLimits) |
256
|
|
|
{ |
|
|
|
|
257
|
|
|
$this->tableLimits = $tableLimits; |
258
|
|
|
} |
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
|
|
|
|
261
|
|
|
* Returns the LIMIT for the table. Must be numeric to be returned. |
262
|
|
|
* @param $tableName |
|
|
|
|
263
|
|
|
* @return boolean |
|
|
|
|
264
|
|
|
*/ |
265
|
|
|
public function getTableLimit($tableName) |
|
|
|
|
266
|
|
|
{ |
|
|
|
|
267
|
|
|
if (!isset($this->tableLimits[$tableName])) { |
|
|
|
|
268
|
|
|
return false; |
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$limit = $this->tableLimits[$tableName]; |
272
|
|
|
if (!is_numeric($limit)) { |
|
|
|
|
273
|
|
|
return false; |
|
|
|
|
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $limit; |
277
|
|
|
} |
|
|
|
|
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Parse DSN string and extract dbname value |
281
|
|
|
* Several examples of a DSN string |
282
|
|
|
* mysql:host=localhost;dbname=testdb |
283
|
|
|
* mysql:host=localhost;port=3307;dbname=testdb |
284
|
|
|
* mysql:unix_socket=/tmp/mysql.sock;dbname=testdb |
285
|
|
|
* |
286
|
|
|
* @param string $dsn dsn string to parse |
|
|
|
|
287
|
|
|
* @return boolean |
|
|
|
|
288
|
|
|
*/ |
|
|
|
|
289
|
|
|
private function parseDsn($dsn) |
|
|
|
|
290
|
|
|
{ |
|
|
|
|
291
|
|
|
if (empty($dsn) || (false === ($pos = strpos($dsn, ":")))) { |
|
|
|
|
292
|
|
|
throw new Exception("Empty DSN string"); |
|
|
|
|
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$this->dsn = $dsn; |
|
|
|
|
296
|
|
|
$this->dbType = strtolower(substr($dsn, 0, $pos)); // always returns a string |
|
|
|
|
297
|
|
|
|
298
|
|
|
if (empty($this->dbType)) { |
299
|
|
|
throw new Exception("Missing database type from DSN string"); |
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$dsn = substr($dsn, $pos + 1); |
|
|
|
|
303
|
|
|
|
304
|
|
|
foreach (explode(";", $dsn) as $kvp) { |
|
|
|
|
305
|
|
|
$kvpArr = explode("=", $kvp); |
|
|
|
|
306
|
|
|
$this->dsnArray[strtolower($kvpArr[0])] = $kvpArr[1]; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
if (empty($this->dsnArray['host']) && |
310
|
|
|
empty($this->dsnArray['unix_socket'])) { |
|
|
|
|
311
|
|
|
throw new Exception("Missing host from DSN string"); |
|
|
|
|
312
|
|
|
} |
|
|
|
|
313
|
|
|
$this->host = (!empty($this->dsnArray['host'])) ? |
|
|
|
|
314
|
|
|
$this->dsnArray['host'] : $this->dsnArray['unix_socket']; |
315
|
|
|
|
316
|
|
|
if (empty($this->dsnArray['dbname'])) { |
317
|
|
|
throw new Exception("Missing database name from DSN string"); |
|
|
|
|
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
$this->dbName = $this->dsnArray['dbname']; |
321
|
|
|
|
322
|
|
|
return true; |
|
|
|
|
323
|
|
|
} |
|
|
|
|
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Connect with PDO. |
327
|
|
|
* |
328
|
|
|
* @return null |
|
|
|
|
329
|
|
|
*/ |
|
|
|
|
330
|
|
|
private function connect() |
|
|
|
|
331
|
|
|
{ |
|
|
|
|
332
|
|
|
// Connecting with PDO. |
333
|
|
|
try { |
334
|
|
|
switch ($this->dbType) { |
335
|
|
|
case 'sqlite': |
|
|
|
|
336
|
|
|
$this->dbHandler = @new PDO("sqlite:".$this->dbName, null, null, $this->pdoSettings); |
|
|
|
|
337
|
|
|
break; |
|
|
|
|
338
|
|
|
case 'mysql': |
|
|
|
|
339
|
|
|
case 'pgsql': |
|
|
|
|
340
|
|
|
case 'dblib': |
|
|
|
|
341
|
|
|
$this->dbHandler = @new PDO( |
|
|
|
|
342
|
|
|
$this->dsn, |
343
|
|
|
$this->user, |
344
|
|
|
$this->pass, |
345
|
|
|
$this->pdoSettings |
346
|
|
|
); |
347
|
|
|
// Execute init commands once connected |
|
|
|
|
348
|
|
|
foreach ($this->dumpSettings['init_commands'] as $stmt) { |
|
|
|
|
349
|
|
|
$this->dbHandler->exec($stmt); |
350
|
|
|
} |
|
|
|
|
351
|
|
|
// Store server version |
|
|
|
|
352
|
|
|
$this->version = $this->dbHandler->getAttribute(PDO::ATTR_SERVER_VERSION); |
353
|
|
|
break; |
|
|
|
|
354
|
|
|
default: |
|
|
|
|
355
|
|
|
throw new Exception("Unsupported database type (".$this->dbType.")"); |
|
|
|
|
356
|
|
|
} |
|
|
|
|
357
|
|
|
} catch (PDOException $e) { |
358
|
|
|
throw new Exception( |
359
|
|
|
"Connection to ".$this->dbType." failed with message: ". |
|
|
|
|
360
|
|
|
$e->getMessage() |
361
|
|
|
); |
362
|
|
|
} |
|
|
|
|
363
|
|
|
|
364
|
|
|
if (is_null($this->dbHandler)) { |
365
|
|
|
throw new Exception("Connection to ".$this->dbType."failed"); |
|
|
|
|
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
$this->dbHandler->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); |
369
|
|
|
$this->typeAdapter = TypeAdapterFactory::create($this->dbType, $this->dbHandler, $this->dumpSettings); |
|
|
|
|
370
|
|
|
} |
|
|
|
|
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Primary function, triggers dumping. |
374
|
|
|
* |
375
|
|
|
* @param string $filename Name of file to write sql dump to |
|
|
|
|
376
|
|
|
* @return null |
|
|
|
|
377
|
|
|
* @throws \Exception |
|
|
|
|
378
|
|
|
*/ |
|
|
|
|
379
|
|
|
public function start($filename = '') |
380
|
|
|
{ |
|
|
|
|
381
|
|
|
// Output file can be redefined here |
|
|
|
|
382
|
|
|
if (!empty($filename)) { |
|
|
|
|
383
|
|
|
$this->fileName = $filename; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
// Connect to database |
|
|
|
|
387
|
|
|
$this->connect(); |
388
|
|
|
|
389
|
|
|
// Create output file |
|
|
|
|
390
|
|
|
$this->compressManager->open($this->fileName); |
391
|
|
|
|
392
|
|
|
// Write some basic info to output file |
|
|
|
|
393
|
|
|
$this->compressManager->write($this->getDumpFileHeader()); |
394
|
|
|
|
395
|
|
|
// Store server settings and use sanner defaults to dump |
|
|
|
|
396
|
|
|
$this->compressManager->write( |
397
|
|
|
$this->typeAdapter->backup_parameters() |
398
|
|
|
); |
399
|
|
|
|
400
|
|
|
if ($this->dumpSettings['databases']) { |
401
|
|
|
$this->compressManager->write( |
402
|
|
|
$this->typeAdapter->getDatabaseHeader($this->dbName) |
403
|
|
|
); |
404
|
|
|
if ($this->dumpSettings['add-drop-database']) { |
405
|
|
|
$this->compressManager->write( |
406
|
|
|
$this->typeAdapter->add_drop_database($this->dbName) |
407
|
|
|
); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
// Get table, view, trigger, procedures, functions and events structures from |
412
|
|
|
// database. |
413
|
|
|
$this->getDatabaseStructureTables(); |
414
|
|
|
$this->getDatabaseStructureViews(); |
415
|
|
|
$this->getDatabaseStructureTriggers(); |
416
|
|
|
$this->getDatabaseStructureProcedures(); |
417
|
|
|
$this->getDatabaseStructureFunctions(); |
418
|
|
|
$this->getDatabaseStructureEvents(); |
419
|
|
|
|
420
|
|
|
if ($this->dumpSettings['databases']) { |
421
|
|
|
$this->compressManager->write( |
422
|
|
|
$this->typeAdapter->databases($this->dbName) |
423
|
|
|
); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
// If there still are some tables/views in include-tables array, |
427
|
|
|
// that means that some tables or views weren't found. |
428
|
|
|
// Give proper error and exit. |
429
|
|
|
// This check will be removed once include-tables supports regexps. |
430
|
|
|
if (0 < count($this->dumpSettings['include-tables'])) { |
431
|
|
|
$name = implode(",", $this->dumpSettings['include-tables']); |
|
|
|
|
432
|
|
|
throw new Exception("Table (".$name.") not found in database"); |
|
|
|
|
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
$this->exportTables(); |
436
|
|
|
$this->exportTriggers(); |
437
|
|
|
$this->exportFunctions(); |
438
|
|
|
$this->exportProcedures(); |
439
|
|
|
$this->exportViews(); |
440
|
|
|
$this->exportEvents(); |
441
|
|
|
|
442
|
|
|
// Restore saved parameters. |
443
|
|
|
$this->compressManager->write( |
444
|
|
|
$this->typeAdapter->restore_parameters() |
445
|
|
|
); |
446
|
|
|
// Write some stats to output file. |
447
|
|
|
$this->compressManager->write($this->getDumpFileFooter()); |
448
|
|
|
// Close output file. |
449
|
|
|
$this->compressManager->close(); |
450
|
|
|
|
451
|
|
|
return; |
|
|
|
|
452
|
|
|
} |
|
|
|
|
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Returns header for dump file. |
456
|
|
|
* |
457
|
|
|
* @return string |
458
|
|
|
*/ |
459
|
|
|
private function getDumpFileHeader() |
|
|
|
|
460
|
|
|
{ |
|
|
|
|
461
|
|
|
$header = ''; |
462
|
|
|
if (!$this->dumpSettings['skip-comments']) { |
|
|
|
|
463
|
|
|
// Some info about software, source and time |
|
|
|
|
464
|
|
|
$header = "-- mysqldump-php https://github.com/ifsnop/mysqldump-php".PHP_EOL. |
|
|
|
|
465
|
|
|
"--".PHP_EOL. |
|
|
|
|
466
|
|
|
"-- Host: {$this->host}\tDatabase: {$this->dbName}".PHP_EOL. |
|
|
|
|
467
|
|
|
"-- ------------------------------------------------------".PHP_EOL; |
|
|
|
|
468
|
|
|
|
469
|
|
|
if (!empty($this->version)) { |
|
|
|
|
470
|
|
|
$header .= "-- Server version \t".$this->version.PHP_EOL; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
if (!$this->dumpSettings['skip-dump-date']) { |
|
|
|
|
474
|
|
|
$header .= "-- Date: ".date('r').PHP_EOL.PHP_EOL; |
|
|
|
|
475
|
|
|
} |
476
|
|
|
} |
|
|
|
|
477
|
|
|
return $header; |
478
|
|
|
} |
|
|
|
|
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* Returns footer for dump file. |
482
|
|
|
* |
483
|
|
|
* @return string |
484
|
|
|
*/ |
485
|
|
|
private function getDumpFileFooter() |
|
|
|
|
486
|
|
|
{ |
|
|
|
|
487
|
|
|
$footer = ''; |
488
|
|
|
if (!$this->dumpSettings['skip-comments']) { |
|
|
|
|
489
|
|
|
$footer .= '-- Dump completed'; |
490
|
|
|
if (!$this->dumpSettings['skip-dump-date']) { |
|
|
|
|
491
|
|
|
$footer .= ' on: '.date('r'); |
492
|
|
|
} |
|
|
|
|
493
|
|
|
$footer .= PHP_EOL; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
return $footer; |
497
|
|
|
} |
|
|
|
|
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* Reads table names from database. |
501
|
|
|
* Fills $this->tables array so they will be dumped later. |
502
|
|
|
* |
503
|
|
|
* @return null |
504
|
|
|
*/ |
505
|
|
|
private function getDatabaseStructureTables() |
|
|
|
|
506
|
|
|
{ |
|
|
|
|
507
|
|
|
// Listing all tables from database |
|
|
|
|
508
|
|
|
if (empty($this->dumpSettings['include-tables'])) { |
509
|
|
|
// include all tables for now, blacklisting happens later |
|
|
|
|
510
|
|
|
foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
|
|
|
|
511
|
|
|
array_push($this->tables, current($row)); |
512
|
|
|
} |
513
|
|
|
} else { |
514
|
|
|
// include only the tables mentioned in include-tables |
|
|
|
|
515
|
|
|
foreach ($this->dbHandler->query($this->typeAdapter->show_tables($this->dbName)) as $row) { |
|
|
|
|
516
|
|
|
if (in_array(current($row), $this->dumpSettings['include-tables'], true)) { |
|
|
|
|
517
|
|
|
array_push($this->tables, current($row)); |
518
|
|
|
$elem = array_search( |
519
|
|
|
current($row), |
520
|
|
|
$this->dumpSettings['include-tables'] |
521
|
|
|
); |
522
|
|
|
unset($this->dumpSettings['include-tables'][$elem]); |
523
|
|
|
} |
524
|
|
|
} |
525
|
|
|
} |
|
|
|
|
526
|
|
|
return; |
|
|
|
|
527
|
|
|
} |
|
|
|
|
528
|
|
|
|
529
|
|
|
/** |
530
|
|
|
* Reads view names from database. |
531
|
|
|
* Fills $this->tables array so they will be dumped later. |
532
|
|
|
* |
533
|
|
|
* @return null |
534
|
|
|
*/ |
535
|
|
|
private function getDatabaseStructureViews() |
|
|
|
|
536
|
|
|
{ |
|
|
|
|
537
|
|
|
// Listing all views from database |
|
|
|
|
538
|
|
|
if (empty($this->dumpSettings['include-views'])) { |
539
|
|
|
// include all views for now, blacklisting happens later |
|
|
|
|
540
|
|
|
foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
|
|
|
|
541
|
|
|
array_push($this->views, current($row)); |
542
|
|
|
} |
543
|
|
|
} else { |
544
|
|
|
// include only the tables mentioned in include-tables |
|
|
|
|
545
|
|
|
foreach ($this->dbHandler->query($this->typeAdapter->show_views($this->dbName)) as $row) { |
|
|
|
|
546
|
|
|
if (in_array(current($row), $this->dumpSettings['include-views'], true)) { |
|
|
|
|
547
|
|
|
array_push($this->views, current($row)); |
548
|
|
|
$elem = array_search( |
549
|
|
|
current($row), |
550
|
|
|
$this->dumpSettings['include-views'] |
551
|
|
|
); |
552
|
|
|
unset($this->dumpSettings['include-views'][$elem]); |
553
|
|
|
} |
554
|
|
|
} |
555
|
|
|
} |
|
|
|
|
556
|
|
|
return; |
|
|
|
|
557
|
|
|
} |
|
|
|
|
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Reads trigger names from database. |
561
|
|
|
* Fills $this->tables array so they will be dumped later. |
562
|
|
|
* |
563
|
|
|
* @return null |
564
|
|
|
*/ |
565
|
|
|
private function getDatabaseStructureTriggers() |
|
|
|
|
566
|
|
|
{ |
|
|
|
|
567
|
|
|
// Listing all triggers from database |
|
|
|
|
568
|
|
|
if (false === $this->dumpSettings['skip-triggers']) { |
|
|
|
|
569
|
|
|
foreach ($this->dbHandler->query($this->typeAdapter->show_triggers($this->dbName)) as $row) { |
|
|
|
|
570
|
|
|
array_push($this->triggers, $row['Trigger']); |
571
|
|
|
} |
572
|
|
|
} |
|
|
|
|
573
|
|
|
return; |
|
|
|
|
574
|
|
|
} |
|
|
|
|
575
|
|
|
|
576
|
|
|
/** |
577
|
|
|
* Reads procedure names from database. |
578
|
|
|
* Fills $this->tables array so they will be dumped later. |
579
|
|
|
* |
580
|
|
|
* @return null |
581
|
|
|
*/ |
582
|
|
|
private function getDatabaseStructureProcedures() |
|
|
|
|
583
|
|
|
{ |
|
|
|
|
584
|
|
|
// Listing all procedures from database |
|
|
|
|
585
|
|
|
if ($this->dumpSettings['routines']) { |
586
|
|
|
foreach ($this->dbHandler->query($this->typeAdapter->show_procedures($this->dbName)) as $row) { |
|
|
|
|
587
|
|
|
array_push($this->procedures, $row['procedure_name']); |
588
|
|
|
} |
589
|
|
|
} |
|
|
|
|
590
|
|
|
return; |
|
|
|
|
591
|
|
|
} |
|
|
|
|
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Reads functions names from database. |
595
|
|
|
* Fills $this->tables array so they will be dumped later. |
596
|
|
|
* |
597
|
|
|
* @return null |
598
|
|
|
*/ |
599
|
|
|
private function getDatabaseStructureFunctions() |
|
|
|
|
600
|
|
|
{ |
|
|
|
|
601
|
|
|
// Listing all functions from database |
|
|
|
|
602
|
|
|
if ($this->dumpSettings['routines']) { |
603
|
|
|
foreach ($this->dbHandler->query($this->typeAdapter->show_functions($this->dbName)) as $row) { |
|
|
|
|
604
|
|
|
array_push($this->functions, $row['function_name']); |
605
|
|
|
} |
606
|
|
|
} |
|
|
|
|
607
|
|
|
return; |
|
|
|
|
608
|
|
|
} |
|
|
|
|
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Reads event names from database. |
612
|
|
|
* Fills $this->tables array so they will be dumped later. |
613
|
|
|
* |
614
|
|
|
* @return null |
615
|
|
|
*/ |
616
|
|
|
private function getDatabaseStructureEvents() |
|
|
|
|
617
|
|
|
{ |
|
|
|
|
618
|
|
|
// Listing all events from database |
|
|
|
|
619
|
|
|
if ($this->dumpSettings['events']) { |
620
|
|
|
foreach ($this->dbHandler->query($this->typeAdapter->show_events($this->dbName)) as $row) { |
|
|
|
|
621
|
|
|
array_push($this->events, $row['event_name']); |
622
|
|
|
} |
623
|
|
|
} |
|
|
|
|
624
|
|
|
return; |
|
|
|
|
625
|
|
|
} |
|
|
|
|
626
|
|
|
|
627
|
|
|
/** |
|
|
|
|
628
|
|
|
* Compare if $table name matches with a definition inside $arr |
629
|
|
|
* @param $table string |
|
|
|
|
630
|
|
|
* @param $arr array with strings or patterns |
|
|
|
|
631
|
|
|
* @return boolean |
|
|
|
|
632
|
|
|
*/ |
633
|
|
|
private function matches($table, $arr) |
|
|
|
|
634
|
|
|
{ |
|
|
|
|
635
|
|
|
$match = false; |
|
|
|
|
636
|
|
|
|
637
|
|
|
foreach ($arr as $pattern) { |
638
|
|
|
if ('/' != $pattern[0]) { |
|
|
|
|
639
|
|
|
continue; |
640
|
|
|
} |
|
|
|
|
641
|
|
|
if (1 == preg_match($pattern, $table)) { |
|
|
|
|
642
|
|
|
$match = true; |
|
|
|
|
643
|
|
|
} |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
return in_array($table, $arr) || $match; |
|
|
|
|
647
|
|
|
} |
|
|
|
|
648
|
|
|
|
649
|
|
|
/** |
650
|
|
|
* Exports all the tables selected from database |
651
|
|
|
* |
652
|
|
|
* @return null |
|
|
|
|
653
|
|
|
*/ |
654
|
|
|
private function exportTables() |
|
|
|
|
655
|
|
|
{ |
|
|
|
|
656
|
|
|
// Exporting tables one by one |
|
|
|
|
657
|
|
|
foreach ($this->tables as $table) { |
658
|
|
|
if ($this->matches($table, $this->dumpSettings['exclude-tables'])) { |
659
|
|
|
continue; |
660
|
|
|
} |
|
|
|
|
661
|
|
|
$this->getTableStructure($table); |
662
|
|
|
if (false === $this->dumpSettings['no-data']) { // don't break compatibility with old trigger |
|
|
|
|
663
|
|
|
$this->listValues($table); |
664
|
|
|
} elseif (true === $this->dumpSettings['no-data'] |
|
|
|
|
665
|
|
|
|| $this->matches($table, $this->dumpSettings['no-data'])) { |
|
|
|
|
666
|
|
|
continue; |
667
|
|
|
} else { |
668
|
|
|
$this->listValues($table); |
669
|
|
|
} |
670
|
|
|
} |
671
|
|
|
} |
|
|
|
|
672
|
|
|
|
673
|
|
|
/** |
674
|
|
|
* Exports all the views found in database |
675
|
|
|
* |
676
|
|
|
* @return null |
|
|
|
|
677
|
|
|
*/ |
678
|
|
|
private function exportViews() |
|
|
|
|
679
|
|
|
{ |
|
|
|
|
680
|
|
|
if (false === $this->dumpSettings['no-create-info']) { |
|
|
|
|
681
|
|
|
// Exporting views one by one |
|
|
|
|
682
|
|
|
foreach ($this->views as $view) { |
683
|
|
|
if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
684
|
|
|
continue; |
685
|
|
|
} |
|
|
|
|
686
|
|
|
$this->tableColumnTypes[$view] = $this->getTableColumnTypes($view); |
687
|
|
|
$this->getViewStructureTable($view); |
688
|
|
|
} |
|
|
|
|
689
|
|
|
foreach ($this->views as $view) { |
690
|
|
|
if ($this->matches($view, $this->dumpSettings['exclude-tables'])) { |
691
|
|
|
continue; |
692
|
|
|
} |
|
|
|
|
693
|
|
|
$this->getViewStructureView($view); |
694
|
|
|
} |
695
|
|
|
} |
696
|
|
|
} |
|
|
|
|
697
|
|
|
|
698
|
|
|
/** |
699
|
|
|
* Exports all the triggers found in database |
700
|
|
|
* |
701
|
|
|
* @return null |
|
|
|
|
702
|
|
|
*/ |
703
|
|
|
private function exportTriggers() |
|
|
|
|
704
|
|
|
{ |
|
|
|
|
705
|
|
|
// Exporting triggers one by one |
|
|
|
|
706
|
|
|
foreach ($this->triggers as $trigger) { |
707
|
|
|
$this->getTriggerStructure($trigger); |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
} |
|
|
|
|
711
|
|
|
|
712
|
|
|
/** |
713
|
|
|
* Exports all the procedures found in database |
714
|
|
|
* |
715
|
|
|
* @return null |
|
|
|
|
716
|
|
|
*/ |
717
|
|
|
private function exportProcedures() |
|
|
|
|
718
|
|
|
{ |
|
|
|
|
719
|
|
|
// Exporting triggers one by one |
|
|
|
|
720
|
|
|
foreach ($this->procedures as $procedure) { |
721
|
|
|
$this->getProcedureStructure($procedure); |
722
|
|
|
} |
723
|
|
|
} |
|
|
|
|
724
|
|
|
|
725
|
|
|
/** |
726
|
|
|
* Exports all the functions found in database |
727
|
|
|
* |
728
|
|
|
* @return null |
|
|
|
|
729
|
|
|
*/ |
730
|
|
|
private function exportFunctions() |
|
|
|
|
731
|
|
|
{ |
|
|
|
|
732
|
|
|
// Exporting triggers one by one |
|
|
|
|
733
|
|
|
foreach ($this->functions as $function) { |
734
|
|
|
$this->getFunctionStructure($function); |
735
|
|
|
} |
736
|
|
|
} |
|
|
|
|
737
|
|
|
|
738
|
|
|
/** |
739
|
|
|
* Exports all the events found in database |
740
|
|
|
* |
741
|
|
|
* @return null |
|
|
|
|
742
|
|
|
*/ |
743
|
|
|
private function exportEvents() |
|
|
|
|
744
|
|
|
{ |
|
|
|
|
745
|
|
|
// Exporting triggers one by one |
|
|
|
|
746
|
|
|
foreach ($this->events as $event) { |
747
|
|
|
$this->getEventStructure($event); |
748
|
|
|
} |
749
|
|
|
} |
|
|
|
|
750
|
|
|
|
751
|
|
|
/** |
752
|
|
|
* Table structure extractor |
753
|
|
|
* |
754
|
|
|
* @todo move specific mysql code to typeAdapter |
|
|
|
|
755
|
|
|
* @param string $tableName Name of table to export |
|
|
|
|
756
|
|
|
* @return null |
|
|
|
|
757
|
|
|
*/ |
758
|
|
|
private function getTableStructure($tableName) |
|
|
|
|
759
|
|
|
{ |
|
|
|
|
760
|
|
|
if (!$this->dumpSettings['no-create-info']) { |
|
|
|
|
761
|
|
|
$ret = ''; |
762
|
|
|
if (!$this->dumpSettings['skip-comments']) { |
|
|
|
|
763
|
|
|
$ret = "--".PHP_EOL. |
|
|
|
|
764
|
|
|
"-- Table structure for table `$tableName`".PHP_EOL. |
|
|
|
|
765
|
|
|
"--".PHP_EOL.PHP_EOL; |
|
|
|
|
766
|
|
|
} |
|
|
|
|
767
|
|
|
$stmt = $this->typeAdapter->show_create_table($tableName); |
768
|
|
|
foreach ($this->dbHandler->query($stmt) as $r) { |
769
|
|
|
$this->compressManager->write($ret); |
770
|
|
|
if ($this->dumpSettings['add-drop-table']) { |
771
|
|
|
$this->compressManager->write( |
772
|
|
|
$this->typeAdapter->drop_table($tableName) |
773
|
|
|
); |
774
|
|
|
} |
|
|
|
|
775
|
|
|
$this->compressManager->write( |
776
|
|
|
$this->typeAdapter->create_table($r) |
777
|
|
|
); |
778
|
|
|
break; |
779
|
|
|
} |
780
|
|
|
} |
|
|
|
|
781
|
|
|
$this->tableColumnTypes[$tableName] = $this->getTableColumnTypes($tableName); |
782
|
|
|
return; |
|
|
|
|
783
|
|
|
} |
|
|
|
|
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* Store column types to create data dumps and for Stand-In tables |
787
|
|
|
* |
788
|
|
|
* @param string $tableName Name of table to export |
|
|
|
|
789
|
|
|
* @return array type column types detailed |
|
|
|
|
790
|
|
|
*/ |
|
|
|
|
791
|
|
|
|
792
|
|
|
private function getTableColumnTypes($tableName) |
|
|
|
|
793
|
|
|
{ |
|
|
|
|
794
|
|
|
$columnTypes = array(); |
|
|
|
|
795
|
|
|
$columns = $this->dbHandler->query( |
|
|
|
|
796
|
|
|
$this->typeAdapter->show_columns($tableName) |
797
|
|
|
); |
798
|
|
|
$columns->setFetchMode(PDO::FETCH_ASSOC); |
799
|
|
|
|
800
|
|
|
foreach ($columns as $key => $col) { |
801
|
|
|
$types = $this->typeAdapter->parseColumnType($col); |
|
|
|
|
802
|
|
|
$columnTypes[$col['Field']] = array( |
|
|
|
|
803
|
|
|
'is_numeric'=> $types['is_numeric'], |
|
|
|
|
804
|
|
|
'is_blob' => $types['is_blob'], |
|
|
|
|
805
|
|
|
'type' => $types['type'], |
|
|
|
|
806
|
|
|
'type_sql' => $col['Type'], |
|
|
|
|
807
|
|
|
'is_virtual' => $types['is_virtual'] |
|
|
|
|
808
|
|
|
); |
|
|
|
|
809
|
|
|
} |
810
|
|
|
|
811
|
|
|
return $columnTypes; |
812
|
|
|
} |
|
|
|
|
813
|
|
|
|
814
|
|
|
/** |
815
|
|
|
* View structure extractor, create table (avoids cyclic references) |
816
|
|
|
* |
817
|
|
|
* @todo move mysql specific code to typeAdapter |
|
|
|
|
818
|
|
|
* @param string $viewName Name of view to export |
|
|
|
|
819
|
|
|
* @return null |
|
|
|
|
820
|
|
|
*/ |
821
|
|
|
private function getViewStructureTable($viewName) |
|
|
|
|
822
|
|
|
{ |
|
|
|
|
823
|
|
|
if (!$this->dumpSettings['skip-comments']) { |
|
|
|
|
824
|
|
|
$ret = "--".PHP_EOL. |
|
|
|
|
825
|
|
|
"-- Stand-In structure for view `${viewName}`".PHP_EOL. |
|
|
|
|
826
|
|
|
"--".PHP_EOL.PHP_EOL; |
|
|
|
|
827
|
|
|
$this->compressManager->write($ret); |
828
|
|
|
} |
|
|
|
|
829
|
|
|
$stmt = $this->typeAdapter->show_create_view($viewName); |
830
|
|
|
|
831
|
|
|
// create views as tables, to resolve dependencies |
|
|
|
|
832
|
|
|
foreach ($this->dbHandler->query($stmt) as $r) { |
833
|
|
|
if ($this->dumpSettings['add-drop-table']) { |
834
|
|
|
$this->compressManager->write( |
835
|
|
|
$this->typeAdapter->drop_view($viewName) |
836
|
|
|
); |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
$this->compressManager->write( |
840
|
|
|
$this->createStandInTable($viewName) |
841
|
|
|
); |
842
|
|
|
break; |
843
|
|
|
} |
844
|
|
|
} |
|
|
|
|
845
|
|
|
|
846
|
|
|
/** |
847
|
|
|
* Write a create table statement for the table Stand-In, show create |
848
|
|
|
* table would return a create algorithm when used on a view |
849
|
|
|
* |
850
|
|
|
* @param string $viewName Name of view to export |
|
|
|
|
851
|
|
|
* @return string create statement |
|
|
|
|
852
|
|
|
*/ |
853
|
|
|
public function createStandInTable($viewName) |
854
|
|
|
{ |
|
|
|
|
855
|
|
|
$ret = array(); |
|
|
|
|
856
|
|
|
foreach ($this->tableColumnTypes[$viewName] as $k => $v) { |
857
|
|
|
$ret[] = "`${k}` ${v['type_sql']}"; |
858
|
|
|
} |
|
|
|
|
859
|
|
|
$ret = implode(PHP_EOL.",", $ret); |
|
|
|
|
860
|
|
|
|
861
|
|
|
$ret = "CREATE TABLE IF NOT EXISTS `$viewName` (". |
|
|
|
|
862
|
|
|
PHP_EOL.$ret.PHP_EOL.");".PHP_EOL; |
|
|
|
|
863
|
|
|
|
864
|
|
|
return $ret; |
865
|
|
|
} |
|
|
|
|
866
|
|
|
|
867
|
|
|
/** |
868
|
|
|
* View structure extractor, create view |
869
|
|
|
* |
870
|
|
|
* @todo move mysql specific code to typeAdapter |
|
|
|
|
871
|
|
|
* @param string $viewName Name of view to export |
|
|
|
|
872
|
|
|
* @return null |
|
|
|
|
873
|
|
|
*/ |
874
|
|
|
private function getViewStructureView($viewName) |
|
|
|
|
875
|
|
|
{ |
|
|
|
|
876
|
|
|
if (!$this->dumpSettings['skip-comments']) { |
|
|
|
|
877
|
|
|
$ret = "--".PHP_EOL. |
|
|
|
|
878
|
|
|
"-- View structure for view `${viewName}`".PHP_EOL. |
|
|
|
|
879
|
|
|
"--".PHP_EOL.PHP_EOL; |
|
|
|
|
880
|
|
|
$this->compressManager->write($ret); |
881
|
|
|
} |
|
|
|
|
882
|
|
|
$stmt = $this->typeAdapter->show_create_view($viewName); |
883
|
|
|
|
884
|
|
|
// create views, to resolve dependencies |
|
|
|
|
885
|
|
|
// replacing tables with views |
|
|
|
|
886
|
|
|
foreach ($this->dbHandler->query($stmt) as $r) { |
887
|
|
|
// because we must replace table with view, we should delete it |
|
|
|
|
888
|
|
|
$this->compressManager->write( |
889
|
|
|
$this->typeAdapter->drop_view($viewName) |
890
|
|
|
); |
891
|
|
|
$this->compressManager->write( |
892
|
|
|
$this->typeAdapter->create_view($r) |
893
|
|
|
); |
894
|
|
|
break; |
895
|
|
|
} |
896
|
|
|
} |
|
|
|
|
897
|
|
|
|
898
|
|
|
/** |
899
|
|
|
* Trigger structure extractor |
900
|
|
|
* |
901
|
|
|
* @param string $triggerName Name of trigger to export |
|
|
|
|
902
|
|
|
* @return null |
|
|
|
|
903
|
|
|
*/ |
904
|
|
|
private function getTriggerStructure($triggerName) |
|
|
|
|
905
|
|
|
{ |
|
|
|
|
906
|
|
|
$stmt = $this->typeAdapter->show_create_trigger($triggerName); |
907
|
|
|
foreach ($this->dbHandler->query($stmt) as $r) { |
908
|
|
|
if ($this->dumpSettings['add-drop-trigger']) { |
909
|
|
|
$this->compressManager->write( |
910
|
|
|
$this->typeAdapter->add_drop_trigger($triggerName) |
911
|
|
|
); |
912
|
|
|
} |
|
|
|
|
913
|
|
|
$this->compressManager->write( |
914
|
|
|
$this->typeAdapter->create_trigger($r) |
915
|
|
|
); |
916
|
|
|
return; |
|
|
|
|
917
|
|
|
} |
918
|
|
|
} |
|
|
|
|
919
|
|
|
|
920
|
|
|
/** |
921
|
|
|
* Procedure structure extractor |
922
|
|
|
* |
923
|
|
|
* @param string $procedureName Name of procedure to export |
|
|
|
|
924
|
|
|
* @return null |
|
|
|
|
925
|
|
|
*/ |
926
|
|
|
private function getProcedureStructure($procedureName) |
|
|
|
|
927
|
|
|
{ |
|
|
|
|
928
|
|
|
if (!$this->dumpSettings['skip-comments']) { |
|
|
|
|
929
|
|
|
$ret = "--".PHP_EOL. |
|
|
|
|
930
|
|
|
"-- Dumping routines for database '".$this->dbName."'".PHP_EOL. |
|
|
|
|
931
|
|
|
"--".PHP_EOL.PHP_EOL; |
|
|
|
|
932
|
|
|
$this->compressManager->write($ret); |
933
|
|
|
} |
|
|
|
|
934
|
|
|
$stmt = $this->typeAdapter->show_create_procedure($procedureName); |
935
|
|
|
foreach ($this->dbHandler->query($stmt) as $r) { |
936
|
|
|
$this->compressManager->write( |
937
|
|
|
$this->typeAdapter->create_procedure($r) |
938
|
|
|
); |
939
|
|
|
return; |
|
|
|
|
940
|
|
|
} |
941
|
|
|
} |
|
|
|
|
942
|
|
|
|
943
|
|
|
/** |
944
|
|
|
* Function structure extractor |
945
|
|
|
* |
946
|
|
|
* @param string $functionName Name of function to export |
|
|
|
|
947
|
|
|
* @return null |
|
|
|
|
948
|
|
|
*/ |
949
|
|
|
private function getFunctionStructure($functionName) |
|
|
|
|
950
|
|
|
{ |
|
|
|
|
951
|
|
|
if (!$this->dumpSettings['skip-comments']) { |
|
|
|
|
952
|
|
|
$ret = "--".PHP_EOL. |
|
|
|
|
953
|
|
|
"-- Dumping routines for database '".$this->dbName."'".PHP_EOL. |
|
|
|
|
954
|
|
|
"--".PHP_EOL.PHP_EOL; |
|
|
|
|
955
|
|
|
$this->compressManager->write($ret); |
956
|
|
|
} |
|
|
|
|
957
|
|
|
$stmt = $this->typeAdapter->show_create_function($functionName); |
958
|
|
|
foreach ($this->dbHandler->query($stmt) as $r) { |
959
|
|
|
$this->compressManager->write( |
960
|
|
|
$this->typeAdapter->create_function($r) |
961
|
|
|
); |
962
|
|
|
return; |
|
|
|
|
963
|
|
|
} |
964
|
|
|
} |
|
|
|
|
965
|
|
|
|
966
|
|
|
/** |
967
|
|
|
* Event structure extractor |
968
|
|
|
* |
969
|
|
|
* @param string $eventName Name of event to export |
|
|
|
|
970
|
|
|
* @return null |
|
|
|
|
971
|
|
|
*/ |
972
|
|
|
private function getEventStructure($eventName) |
|
|
|
|
973
|
|
|
{ |
|
|
|
|
974
|
|
|
if (!$this->dumpSettings['skip-comments']) { |
|
|
|
|
975
|
|
|
$ret = "--".PHP_EOL. |
|
|
|
|
976
|
|
|
"-- Dumping events for database '".$this->dbName."'".PHP_EOL. |
|
|
|
|
977
|
|
|
"--".PHP_EOL.PHP_EOL; |
|
|
|
|
978
|
|
|
$this->compressManager->write($ret); |
979
|
|
|
} |
|
|
|
|
980
|
|
|
$stmt = $this->typeAdapter->show_create_event($eventName); |
981
|
|
|
foreach ($this->dbHandler->query($stmt) as $r) { |
982
|
|
|
$this->compressManager->write( |
983
|
|
|
$this->typeAdapter->create_event($r) |
984
|
|
|
); |
985
|
|
|
return; |
|
|
|
|
986
|
|
|
} |
987
|
|
|
} |
|
|
|
|
988
|
|
|
|
989
|
|
|
/** |
990
|
|
|
* Prepare values for output |
991
|
|
|
* |
992
|
|
|
* @param string $tableName Name of table which contains rows |
|
|
|
|
993
|
|
|
* @param array $row Associative array of column names and values to be |
|
|
|
|
994
|
|
|
* quoted |
|
|
|
|
995
|
|
|
* |
996
|
|
|
* @return array |
997
|
|
|
*/ |
998
|
|
|
private function prepareColumnValues($tableName, array $row) |
|
|
|
|
999
|
|
|
{ |
|
|
|
|
1000
|
|
|
$ret = array(); |
|
|
|
|
1001
|
|
|
$columnTypes = $this->tableColumnTypes[$tableName]; |
1002
|
|
|
|
1003
|
|
|
if ($this->transformTableRowCallable) { |
1004
|
|
|
$row = call_user_func($this->transformTableRowCallable, $tableName, $row); |
1005
|
|
|
} |
1006
|
|
|
|
1007
|
|
|
foreach ($row as $colName => $colValue) { |
1008
|
|
|
if ($this->transformColumnValueCallable) { |
1009
|
|
|
$colValue = call_user_func($this->transformColumnValueCallable, $tableName, $colName, $colValue, $row); |
|
|
|
|
1010
|
|
|
} |
1011
|
|
|
|
1012
|
|
|
$ret[] = $this->escape($colValue, $columnTypes[$colName]); |
1013
|
|
|
} |
1014
|
|
|
|
1015
|
|
|
return $ret; |
1016
|
|
|
} |
|
|
|
|
1017
|
|
|
|
1018
|
|
|
/** |
|
|
|
|
1019
|
|
|
* Escape values with quotes when needed |
1020
|
|
|
* |
1021
|
|
|
* @param string $tableName Name of table which contains rows |
|
|
|
|
1022
|
|
|
* @param array $row Associative array of column names and values to be quoted |
|
|
|
|
1023
|
|
|
* |
1024
|
|
|
* @return string |
1025
|
|
|
*/ |
1026
|
|
|
private function escape($colValue, $colType) |
|
|
|
|
1027
|
|
|
{ |
|
|
|
|
1028
|
|
|
if (is_null($colValue)) { |
1029
|
|
|
return "NULL"; |
|
|
|
|
1030
|
|
|
} elseif ($this->dumpSettings['hex-blob'] && $colType['is_blob']) { |
|
|
|
|
1031
|
|
|
if ($colType['type'] == 'bit' || !empty($colValue)) { |
|
|
|
|
1032
|
|
|
return "0x${colValue}"; |
1033
|
|
|
} else { |
1034
|
|
|
return "''"; |
1035
|
|
|
} |
1036
|
|
|
} elseif ($colType['is_numeric']) { |
|
|
|
|
1037
|
|
|
return $colValue; |
1038
|
|
|
} |
1039
|
|
|
|
1040
|
|
|
return $this->dbHandler->quote($colValue); |
1041
|
|
|
} |
|
|
|
|
1042
|
|
|
|
1043
|
|
|
/** |
1044
|
|
|
* Set a callable that will be used to transform table rows |
1045
|
|
|
* |
1046
|
|
|
* @param callable $callable |
|
|
|
|
1047
|
|
|
* |
1048
|
|
|
* @return void |
1049
|
|
|
*/ |
1050
|
|
|
public function setTransformTableRowHook($callable) |
|
|
|
|
1051
|
|
|
{ |
|
|
|
|
1052
|
|
|
$this->transformTableRowCallable = $callable; |
1053
|
|
|
} |
|
|
|
|
1054
|
|
|
|
1055
|
|
|
/** |
1056
|
|
|
* Set a callable that will be used to transform column values |
1057
|
|
|
* |
1058
|
|
|
* @param callable $callable |
|
|
|
|
1059
|
|
|
* |
1060
|
|
|
* @return void |
1061
|
|
|
* |
1062
|
|
|
* @deprecated Use setTransformTableRowHook instead for better performance |
1063
|
|
|
*/ |
1064
|
|
|
public function setTransformColumnValueHook($callable) |
|
|
|
|
1065
|
|
|
{ |
|
|
|
|
1066
|
|
|
$this->transformColumnValueCallable = $callable; |
1067
|
|
|
} |
|
|
|
|
1068
|
|
|
|
1069
|
|
|
/** |
1070
|
|
|
* Set a callable that will be used to report dump information |
1071
|
|
|
* |
1072
|
|
|
* @param callable $callable |
|
|
|
|
1073
|
|
|
* |
1074
|
|
|
* @return void |
1075
|
|
|
*/ |
1076
|
|
|
public function setInfoHook($callable) |
|
|
|
|
1077
|
|
|
{ |
|
|
|
|
1078
|
|
|
$this->infoCallable = $callable; |
1079
|
|
|
} |
|
|
|
|
1080
|
|
|
|
1081
|
|
|
/** |
1082
|
|
|
* Table rows extractor |
1083
|
|
|
* |
1084
|
|
|
* @param string $tableName Name of table to export |
|
|
|
|
1085
|
|
|
* |
1086
|
|
|
* @return null |
|
|
|
|
1087
|
|
|
*/ |
1088
|
|
|
private function listValues($tableName) |
|
|
|
|
1089
|
|
|
{ |
|
|
|
|
1090
|
|
|
$this->prepareListValues($tableName); |
1091
|
|
|
|
1092
|
|
|
$onlyOnce = true; |
|
|
|
|
1093
|
|
|
$lineSize = 0; |
1094
|
|
|
|
1095
|
|
|
// colStmt is used to form a query to obtain row values |
|
|
|
|
1096
|
|
|
$colStmt = $this->getColumnStmt($tableName); |
1097
|
|
|
// colNames is used to get the name of the columns when using complete-insert |
|
|
|
|
1098
|
|
|
if ($this->dumpSettings['complete-insert']) { |
1099
|
|
|
$colNames = $this->getColumnNames($tableName); |
1100
|
|
|
} |
1101
|
|
|
|
1102
|
|
|
$stmt = "SELECT ".implode(",", $colStmt)." FROM `$tableName`"; |
|
|
|
|
1103
|
|
|
|
1104
|
|
|
// Table specific conditions override the default 'where' |
|
|
|
|
1105
|
|
|
$condition = $this->getTableWhere($tableName); |
1106
|
|
|
|
1107
|
|
|
if ($condition) { |
1108
|
|
|
$stmt .= " WHERE {$condition}"; |
|
|
|
|
1109
|
|
|
} |
1110
|
|
|
|
1111
|
|
|
$limit = $this->getTableLimit($tableName); |
1112
|
|
|
|
1113
|
|
|
if ($limit !== false) { |
|
|
|
|
1114
|
|
|
$stmt .= " LIMIT {$limit}"; |
|
|
|
|
1115
|
|
|
} |
1116
|
|
|
|
1117
|
|
|
$resultSet = $this->dbHandler->query($stmt); |
1118
|
|
|
$resultSet->setFetchMode(PDO::FETCH_ASSOC); |
1119
|
|
|
|
1120
|
|
|
$ignore = $this->dumpSettings['insert-ignore'] ? ' IGNORE' : ''; |
|
|
|
|
1121
|
|
|
|
1122
|
|
|
$count = 0; |
1123
|
|
|
foreach ($resultSet as $row) { |
1124
|
|
|
$count++; |
1125
|
|
|
$vals = $this->prepareColumnValues($tableName, $row); |
1126
|
|
|
if ($onlyOnce || !$this->dumpSettings['extended-insert']) { |
|
|
|
|
1127
|
|
|
if ($this->dumpSettings['complete-insert']) { |
1128
|
|
|
$lineSize += $this->compressManager->write( |
1129
|
|
|
"INSERT$ignore INTO `$tableName` (". |
|
|
|
|
1130
|
|
|
implode(", ", $colNames). |
|
|
|
|
1131
|
|
|
") VALUES (".implode(",", $vals).")" |
|
|
|
|
1132
|
|
|
); |
1133
|
|
|
} else { |
1134
|
|
|
$lineSize += $this->compressManager->write( |
1135
|
|
|
"INSERT$ignore INTO `$tableName` VALUES (".implode(",", $vals).")" |
|
|
|
|
1136
|
|
|
); |
1137
|
|
|
} |
|
|
|
|
1138
|
|
|
$onlyOnce = false; |
|
|
|
|
1139
|
|
|
} else { |
1140
|
|
|
$lineSize += $this->compressManager->write(",(".implode(",", $vals).")"); |
|
|
|
|
1141
|
|
|
} |
|
|
|
|
1142
|
|
|
if (($lineSize > $this->dumpSettings['net_buffer_length']) || |
1143
|
|
|
!$this->dumpSettings['extended-insert']) { |
|
|
|
|
1144
|
|
|
$onlyOnce = true; |
|
|
|
|
1145
|
|
|
$lineSize = $this->compressManager->write(";".PHP_EOL); |
|
|
|
|
1146
|
|
|
} |
1147
|
|
|
} |
|
|
|
|
1148
|
|
|
$resultSet->closeCursor(); |
1149
|
|
|
|
1150
|
|
|
if (!$onlyOnce) { |
|
|
|
|
1151
|
|
|
$this->compressManager->write(";".PHP_EOL); |
|
|
|
|
1152
|
|
|
} |
1153
|
|
|
|
1154
|
|
|
$this->endListValues($tableName, $count); |
1155
|
|
|
|
1156
|
|
|
if ($this->infoCallable) { |
1157
|
|
|
call_user_func($this->infoCallable, 'table', array('name' => $tableName, 'rowCount' => $count)); |
|
|
|
|
1158
|
|
|
} |
1159
|
|
|
} |
|
|
|
|
1160
|
|
|
|
1161
|
|
|
/** |
1162
|
|
|
* Table rows extractor, append information prior to dump |
1163
|
|
|
* |
1164
|
|
|
* @param string $tableName Name of table to export |
|
|
|
|
1165
|
|
|
* |
1166
|
|
|
* @return null |
1167
|
|
|
*/ |
1168
|
|
|
public function prepareListValues($tableName) |
1169
|
|
|
{ |
|
|
|
|
1170
|
|
|
if (!$this->dumpSettings['skip-comments']) { |
|
|
|
|
1171
|
|
|
$this->compressManager->write( |
1172
|
|
|
"--".PHP_EOL. |
|
|
|
|
1173
|
|
|
"-- Dumping data for table `$tableName`".PHP_EOL. |
|
|
|
|
1174
|
|
|
"--".PHP_EOL.PHP_EOL |
|
|
|
|
1175
|
|
|
); |
1176
|
|
|
} |
1177
|
|
|
|
1178
|
|
|
if ($this->dumpSettings['single-transaction']) { |
1179
|
|
|
$this->dbHandler->exec($this->typeAdapter->setup_transaction()); |
1180
|
|
|
$this->dbHandler->exec($this->typeAdapter->start_transaction()); |
1181
|
|
|
} |
1182
|
|
|
|
1183
|
|
|
if ($this->dumpSettings['lock-tables'] && !$this->dumpSettings['single-transaction']) { |
|
|
|
|
1184
|
|
|
$this->typeAdapter->lock_table($tableName); |
1185
|
|
|
} |
1186
|
|
|
|
1187
|
|
|
if ($this->dumpSettings['add-locks']) { |
1188
|
|
|
$this->compressManager->write( |
1189
|
|
|
$this->typeAdapter->start_add_lock_table($tableName) |
1190
|
|
|
); |
1191
|
|
|
} |
1192
|
|
|
|
1193
|
|
|
if ($this->dumpSettings['disable-keys']) { |
1194
|
|
|
$this->compressManager->write( |
1195
|
|
|
$this->typeAdapter->start_add_disable_keys($tableName) |
1196
|
|
|
); |
1197
|
|
|
} |
1198
|
|
|
|
1199
|
|
|
// Disable autocommit for faster reload |
|
|
|
|
1200
|
|
|
if ($this->dumpSettings['no-autocommit']) { |
1201
|
|
|
$this->compressManager->write( |
1202
|
|
|
$this->typeAdapter->start_disable_autocommit() |
1203
|
|
|
); |
1204
|
|
|
} |
1205
|
|
|
|
1206
|
|
|
return; |
|
|
|
|
1207
|
|
|
} |
|
|
|
|
1208
|
|
|
|
1209
|
|
|
/** |
1210
|
|
|
* Table rows extractor, close locks and commits after dump |
1211
|
|
|
* |
1212
|
|
|
* @param string $tableName Name of table to export. |
|
|
|
|
1213
|
|
|
* @param integer $count Number of rows inserted. |
|
|
|
|
1214
|
|
|
* |
1215
|
|
|
* @return void |
1216
|
|
|
*/ |
1217
|
|
|
public function endListValues($tableName, $count = 0) |
1218
|
|
|
{ |
|
|
|
|
1219
|
|
|
if ($this->dumpSettings['disable-keys']) { |
1220
|
|
|
$this->compressManager->write( |
1221
|
|
|
$this->typeAdapter->end_add_disable_keys($tableName) |
1222
|
|
|
); |
1223
|
|
|
} |
1224
|
|
|
|
1225
|
|
|
if ($this->dumpSettings['add-locks']) { |
1226
|
|
|
$this->compressManager->write( |
1227
|
|
|
$this->typeAdapter->end_add_lock_table($tableName) |
1228
|
|
|
); |
1229
|
|
|
} |
1230
|
|
|
|
1231
|
|
|
if ($this->dumpSettings['single-transaction']) { |
1232
|
|
|
$this->dbHandler->exec($this->typeAdapter->commit_transaction()); |
1233
|
|
|
} |
1234
|
|
|
|
1235
|
|
|
if ($this->dumpSettings['lock-tables'] && !$this->dumpSettings['single-transaction']) { |
|
|
|
|
1236
|
|
|
$this->typeAdapter->unlock_table($tableName); |
1237
|
|
|
} |
1238
|
|
|
|
1239
|
|
|
// Commit to enable autocommit |
|
|
|
|
1240
|
|
|
if ($this->dumpSettings['no-autocommit']) { |
1241
|
|
|
$this->compressManager->write( |
1242
|
|
|
$this->typeAdapter->end_disable_autocommit() |
1243
|
|
|
); |
1244
|
|
|
} |
1245
|
|
|
|
1246
|
|
|
$this->compressManager->write(PHP_EOL); |
1247
|
|
|
|
1248
|
|
|
if (!$this->dumpSettings['skip-comments']) { |
|
|
|
|
1249
|
|
|
$this->compressManager->write( |
1250
|
|
|
"-- Dumped table `".$tableName."` with $count row(s)".PHP_EOL. |
|
|
|
|
1251
|
|
|
'--'.PHP_EOL.PHP_EOL |
1252
|
|
|
); |
1253
|
|
|
} |
1254
|
|
|
|
1255
|
|
|
return; |
|
|
|
|
1256
|
|
|
} |
|
|
|
|
1257
|
|
|
|
1258
|
|
|
/** |
1259
|
|
|
* Build SQL List of all columns on current table which will be used for selecting |
1260
|
|
|
* |
1261
|
|
|
* @param string $tableName Name of table to get columns |
|
|
|
|
1262
|
|
|
* |
1263
|
|
|
* @return array SQL sentence with columns for select |
1264
|
|
|
*/ |
1265
|
|
|
public function getColumnStmt($tableName) |
1266
|
|
|
{ |
|
|
|
|
1267
|
|
|
$colStmt = array(); |
|
|
|
|
1268
|
|
|
foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { |
1269
|
|
|
if ($colType['type'] == 'bit' && $this->dumpSettings['hex-blob']) { |
|
|
|
|
1270
|
|
|
$colStmt[] = "LPAD(HEX(`${colName}`),2,'0') AS `${colName}`"; |
1271
|
|
|
} elseif ($colType['is_blob'] && $this->dumpSettings['hex-blob']) { |
|
|
|
|
1272
|
|
|
$colStmt[] = "HEX(`${colName}`) AS `${colName}`"; |
1273
|
|
|
} elseif ($colType['is_virtual']) { |
|
|
|
|
1274
|
|
|
$this->dumpSettings['complete-insert'] = true; |
|
|
|
|
1275
|
|
|
continue; |
1276
|
|
|
} else { |
1277
|
|
|
$colStmt[] = "`${colName}`"; |
1278
|
|
|
} |
1279
|
|
|
} |
1280
|
|
|
|
1281
|
|
|
return $colStmt; |
1282
|
|
|
} |
|
|
|
|
1283
|
|
|
|
1284
|
|
|
/** |
1285
|
|
|
* Build SQL List of all columns on current table which will be used for inserting |
1286
|
|
|
* |
1287
|
|
|
* @param string $tableName Name of table to get columns |
|
|
|
|
1288
|
|
|
* |
1289
|
|
|
* @return array columns for sql sentence for insert |
1290
|
|
|
*/ |
1291
|
|
|
public function getColumnNames($tableName) |
1292
|
|
|
{ |
|
|
|
|
1293
|
|
|
$colNames = array(); |
|
|
|
|
1294
|
|
|
foreach ($this->tableColumnTypes[$tableName] as $colName => $colType) { |
1295
|
|
|
if ($colType['is_virtual']) { |
1296
|
|
|
$this->dumpSettings['complete-insert'] = true; |
|
|
|
|
1297
|
|
|
continue; |
1298
|
|
|
} else { |
1299
|
|
|
$colNames[] = "`${colName}`"; |
1300
|
|
|
} |
1301
|
|
|
} |
|
|
|
|
1302
|
|
|
return $colNames; |
1303
|
|
|
} |
|
|
|
|
1304
|
|
|
} |
|
|
|
|
1305
|
|
|
|
1306
|
|
|
/** |
1307
|
|
|
* Enum with all available compression methods |
1308
|
|
|
* |
1309
|
|
|
*/ |
|
|
|
|
1310
|
|
|
abstract class CompressMethod |
|
|
|
|
1311
|
|
|
{ |
|
|
|
|
1312
|
|
|
public static $enums = array( |
|
|
|
|
1313
|
|
|
Mysqldump::NONE, |
|
|
|
|
1314
|
|
|
Mysqldump::GZIP, |
|
|
|
|
1315
|
|
|
Mysqldump::BZIP2, |
|
|
|
|
1316
|
|
|
Mysqldump::GZIPSTREAM, |
|
|
|
|
1317
|
|
|
); |
|
|
|
|
1318
|
|
|
|
1319
|
|
|
/** |
|
|
|
|
1320
|
|
|
* @param string $c |
|
|
|
|
1321
|
|
|
* @return boolean |
1322
|
|
|
*/ |
1323
|
|
|
public static function isValid($c) |
|
|
|
|
1324
|
|
|
{ |
|
|
|
|
1325
|
|
|
return in_array($c, self::$enums); |
1326
|
|
|
} |
|
|
|
|
1327
|
|
|
} |
|
|
|
|
1328
|
|
|
|
1329
|
|
|
abstract class CompressManagerFactory |
|
|
|
|
1330
|
|
|
{ |
|
|
|
|
1331
|
|
|
/** |
|
|
|
|
1332
|
|
|
* @param string $c |
|
|
|
|
1333
|
|
|
* @return CompressBzip2|CompressGzip|CompressNone |
1334
|
|
|
*/ |
|
|
|
|
1335
|
|
|
public static function create($c) |
|
|
|
|
1336
|
|
|
{ |
|
|
|
|
1337
|
|
|
$c = ucfirst(strtolower($c)); |
1338
|
|
|
if (!CompressMethod::isValid($c)) { |
|
|
|
|
1339
|
|
|
throw new Exception("Compression method ($c) is not defined yet"); |
|
|
|
|
1340
|
|
|
} |
1341
|
|
|
|
1342
|
|
|
$method = __NAMESPACE__."\\"."Compress".$c; |
|
|
|
|
1343
|
|
|
|
1344
|
|
|
return new $method; |
1345
|
|
|
} |
|
|
|
|
1346
|
|
|
} |
|
|
|
|
1347
|
|
|
|
1348
|
|
|
class CompressBzip2 extends CompressManagerFactory |
|
|
|
|
1349
|
|
|
{ |
|
|
|
|
1350
|
|
|
private $fileHandler = null; |
|
|
|
|
1351
|
|
|
|
1352
|
|
|
public function __construct() |
|
|
|
|
1353
|
|
|
{ |
|
|
|
|
1354
|
|
|
if (!function_exists("bzopen")) { |
|
|
|
|
1355
|
|
|
throw new Exception("Compression is enabled, but bzip2 lib is not installed or configured properly"); |
|
|
|
|
1356
|
|
|
} |
1357
|
|
|
} |
|
|
|
|
1358
|
|
|
|
1359
|
|
|
/** |
|
|
|
|
1360
|
|
|
* @param string $filename |
|
|
|
|
1361
|
|
|
*/ |
|
|
|
|
1362
|
|
|
public function open($filename) |
1363
|
|
|
{ |
|
|
|
|
1364
|
|
|
$this->fileHandler = bzopen($filename, "w"); |
|
|
|
|
1365
|
|
|
if (false === $this->fileHandler) { |
|
|
|
|
1366
|
|
|
throw new Exception("Output file is not writable"); |
|
|
|
|
1367
|
|
|
} |
1368
|
|
|
|
1369
|
|
|
return true; |
|
|
|
|
1370
|
|
|
} |
|
|
|
|
1371
|
|
|
|
1372
|
|
|
public function write($str) |
|
|
|
|
1373
|
|
|
{ |
|
|
|
|
1374
|
|
|
$bytesWritten = bzwrite($this->fileHandler, $str); |
1375
|
|
|
if (false === $bytesWritten) { |
|
|
|
|
1376
|
|
|
throw new Exception("Writting to file failed! Probably, there is no more free space left?"); |
|
|
|
|
1377
|
|
|
} |
|
|
|
|
1378
|
|
|
return $bytesWritten; |
1379
|
|
|
} |
|
|
|
|
1380
|
|
|
|
1381
|
|
|
public function close() |
|
|
|
|
1382
|
|
|
{ |
|
|
|
|
1383
|
|
|
return bzclose($this->fileHandler); |
1384
|
|
|
} |
|
|
|
|
1385
|
|
|
} |
|
|
|
|
1386
|
|
|
|
1387
|
|
|
class CompressGzip extends CompressManagerFactory |
|
|
|
|
1388
|
|
|
{ |
|
|
|
|
1389
|
|
|
private $fileHandler = null; |
|
|
|
|
1390
|
|
|
|
1391
|
|
|
public function __construct() |
|
|
|
|
1392
|
|
|
{ |
|
|
|
|
1393
|
|
|
if (!function_exists("gzopen")) { |
|
|
|
|
1394
|
|
|
throw new Exception("Compression is enabled, but gzip lib is not installed or configured properly"); |
|
|
|
|
1395
|
|
|
} |
1396
|
|
|
} |
|
|
|
|
1397
|
|
|
|
1398
|
|
|
/** |
|
|
|
|
1399
|
|
|
* @param string $filename |
|
|
|
|
1400
|
|
|
*/ |
|
|
|
|
1401
|
|
|
public function open($filename) |
1402
|
|
|
{ |
|
|
|
|
1403
|
|
|
$this->fileHandler = gzopen($filename, "wb"); |
|
|
|
|
1404
|
|
|
if (false === $this->fileHandler) { |
|
|
|
|
1405
|
|
|
throw new Exception("Output file is not writable"); |
|
|
|
|
1406
|
|
|
} |
1407
|
|
|
|
1408
|
|
|
return true; |
|
|
|
|
1409
|
|
|
} |
|
|
|
|
1410
|
|
|
|
1411
|
|
|
public function write($str) |
|
|
|
|
1412
|
|
|
{ |
|
|
|
|
1413
|
|
|
$bytesWritten = gzwrite($this->fileHandler, $str); |
1414
|
|
|
if (false === $bytesWritten) { |
|
|
|
|
1415
|
|
|
throw new Exception("Writting to file failed! Probably, there is no more free space left?"); |
|
|
|
|
1416
|
|
|
} |
|
|
|
|
1417
|
|
|
return $bytesWritten; |
1418
|
|
|
} |
|
|
|
|
1419
|
|
|
|
1420
|
|
|
public function close() |
|
|
|
|
1421
|
|
|
{ |
|
|
|
|
1422
|
|
|
return gzclose($this->fileHandler); |
1423
|
|
|
} |
|
|
|
|
1424
|
|
|
} |
|
|
|
|
1425
|
|
|
|
1426
|
|
|
class CompressNone extends CompressManagerFactory |
|
|
|
|
1427
|
|
|
{ |
|
|
|
|
1428
|
|
|
private $fileHandler = null; |
|
|
|
|
1429
|
|
|
|
1430
|
|
|
/** |
|
|
|
|
1431
|
|
|
* @param string $filename |
|
|
|
|
1432
|
|
|
*/ |
|
|
|
|
1433
|
|
|
public function open($filename) |
|
|
|
|
1434
|
|
|
{ |
|
|
|
|
1435
|
|
|
$this->fileHandler = fopen($filename, "wb"); |
|
|
|
|
1436
|
|
|
if (false === $this->fileHandler) { |
|
|
|
|
1437
|
|
|
throw new Exception("Output file is not writable"); |
|
|
|
|
1438
|
|
|
} |
1439
|
|
|
|
1440
|
|
|
return true; |
|
|
|
|
1441
|
|
|
} |
|
|
|
|
1442
|
|
|
|
1443
|
|
|
public function write($str) |
|
|
|
|
1444
|
|
|
{ |
|
|
|
|
1445
|
|
|
$bytesWritten = fwrite($this->fileHandler, $str); |
1446
|
|
|
if (false === $bytesWritten) { |
|
|
|
|
1447
|
|
|
throw new Exception("Writting to file failed! Probably, there is no more free space left?"); |
|
|
|
|
1448
|
|
|
} |
|
|
|
|
1449
|
|
|
return $bytesWritten; |
1450
|
|
|
} |
|
|
|
|
1451
|
|
|
|
1452
|
|
|
public function close() |
|
|
|
|
1453
|
|
|
{ |
|
|
|
|
1454
|
|
|
return fclose($this->fileHandler); |
1455
|
|
|
} |
|
|
|
|
1456
|
|
|
} |
|
|
|
|
1457
|
|
|
|
1458
|
|
|
class CompressGzipstream extends CompressManagerFactory |
|
|
|
|
1459
|
|
|
{ |
|
|
|
|
1460
|
|
|
private $fileHandler = null; |
|
|
|
|
1461
|
|
|
|
1462
|
|
|
private $compressContext; |
|
|
|
|
1463
|
|
|
|
1464
|
|
|
/** |
|
|
|
|
1465
|
|
|
* @param string $filename |
|
|
|
|
1466
|
|
|
*/ |
|
|
|
|
1467
|
|
|
public function open($filename) |
|
|
|
|
1468
|
|
|
{ |
|
|
|
|
1469
|
|
|
$this->fileHandler = fopen($filename, "wb"); |
|
|
|
|
1470
|
|
|
if (false === $this->fileHandler) { |
|
|
|
|
1471
|
|
|
throw new Exception("Output file is not writable"); |
|
|
|
|
1472
|
|
|
} |
|
|
|
|
1473
|
|
|
|
1474
|
|
|
$this->compressContext = deflate_init(ZLIB_ENCODING_GZIP, array('level' => 9)); |
|
|
|
|
1475
|
|
|
return true; |
|
|
|
|
1476
|
|
|
} |
|
|
|
|
1477
|
|
|
|
1478
|
|
|
public function write($str) |
|
|
|
|
1479
|
|
|
{ |
|
|
|
|
1480
|
|
|
|
1481
|
|
|
$bytesWritten = fwrite($this->fileHandler, deflate_add($this->compressContext, $str, ZLIB_NO_FLUSH)); |
|
|
|
|
1482
|
|
|
if (false === $bytesWritten) { |
|
|
|
|
1483
|
|
|
throw new Exception("Writting to file failed! Probably, there is no more free space left?"); |
|
|
|
|
1484
|
|
|
} |
|
|
|
|
1485
|
|
|
return $bytesWritten; |
|
|
|
|
1486
|
|
|
} |
|
|
|
|
1487
|
|
|
|
1488
|
|
|
public function close() |
|
|
|
|
1489
|
|
|
{ |
|
|
|
|
1490
|
|
|
fwrite($this->fileHandler, deflate_add($this->compressContext, '', ZLIB_FINISH)); |
|
|
|
|
1491
|
|
|
return fclose($this->fileHandler); |
|
|
|
|
1492
|
|
|
} |
|
|
|
|
1493
|
|
|
} |
|
|
|
|
1494
|
|
|
|
1495
|
|
|
/** |
1496
|
|
|
* Enum with all available TypeAdapter implementations |
1497
|
|
|
* |
1498
|
|
|
*/ |
|
|
|
|
1499
|
|
|
abstract class TypeAdapter |
|
|
|
|
1500
|
|
|
{ |
|
|
|
|
1501
|
|
|
public static $enums = array( |
|
|
|
|
1502
|
|
|
"Sqlite", |
|
|
|
|
1503
|
|
|
"Mysql" |
|
|
|
|
1504
|
|
|
); |
|
|
|
|
1505
|
|
|
|
1506
|
|
|
/** |
|
|
|
|
1507
|
|
|
* @param string $c |
|
|
|
|
1508
|
|
|
* @return boolean |
1509
|
|
|
*/ |
1510
|
|
|
public static function isValid($c) |
|
|
|
|
1511
|
|
|
{ |
|
|
|
|
1512
|
|
|
return in_array($c, self::$enums); |
1513
|
|
|
} |
|
|
|
|
1514
|
|
|
} |
|
|
|
|
1515
|
|
|
|
1516
|
|
|
/** |
1517
|
|
|
* TypeAdapter Factory |
1518
|
|
|
* |
1519
|
|
|
*/ |
|
|
|
|
1520
|
|
|
abstract class TypeAdapterFactory |
|
|
|
|
1521
|
|
|
{ |
|
|
|
|
1522
|
|
|
protected $dbHandler = null; |
|
|
|
|
1523
|
|
|
protected $dumpSettings = array(); |
|
|
|
|
1524
|
|
|
|
1525
|
|
|
/** |
|
|
|
|
1526
|
|
|
* @param string $c Type of database factory to create (Mysql, Sqlite,...) |
|
|
|
|
1527
|
|
|
* @param PDO $dbHandler |
|
|
|
|
1528
|
|
|
*/ |
|
|
|
|
1529
|
|
|
public static function create($c, $dbHandler = null, $dumpSettings = array()) |
|
|
|
|
1530
|
|
|
{ |
|
|
|
|
1531
|
|
|
$c = ucfirst(strtolower($c)); |
1532
|
|
|
if (!TypeAdapter::isValid($c)) { |
|
|
|
|
1533
|
|
|
throw new Exception("Database type support for ($c) not yet available"); |
|
|
|
|
1534
|
|
|
} |
|
|
|
|
1535
|
|
|
$method = __NAMESPACE__."\\"."TypeAdapter".$c; |
|
|
|
|
1536
|
|
|
return new $method($dbHandler, $dumpSettings); |
1537
|
|
|
} |
|
|
|
|
1538
|
|
|
|
1539
|
|
|
public function __construct($dbHandler = null, $dumpSettings = array()) |
|
|
|
|
1540
|
|
|
{ |
|
|
|
|
1541
|
|
|
$this->dbHandler = $dbHandler; |
|
|
|
|
1542
|
|
|
$this->dumpSettings = $dumpSettings; |
1543
|
|
|
} |
|
|
|
|
1544
|
|
|
|
1545
|
|
|
/** |
1546
|
|
|
* function databases Add sql to create and use database |
|
|
|
|
1547
|
|
|
* @todo make it do something with sqlite |
|
|
|
|
1548
|
|
|
*/ |
|
|
|
|
1549
|
|
|
public function databases() |
1550
|
|
|
{ |
|
|
|
|
1551
|
|
|
return ""; |
|
|
|
|
1552
|
|
|
} |
|
|
|
|
1553
|
|
|
|
1554
|
|
|
public function show_create_table($tableName) |
|
|
|
|
1555
|
|
|
{ |
|
|
|
|
1556
|
|
|
return "SELECT tbl_name as 'Table', sql as 'Create Table' ". |
|
|
|
|
1557
|
|
|
"FROM sqlite_master ". |
|
|
|
|
1558
|
|
|
"WHERE type='table' AND tbl_name='$tableName'"; |
|
|
|
|
1559
|
|
|
} |
|
|
|
|
1560
|
|
|
|
1561
|
|
|
/** |
|
|
|
|
1562
|
|
|
* function create_table Get table creation code from database |
|
|
|
|
1563
|
|
|
* @todo make it do something with sqlite |
|
|
|
|
1564
|
|
|
*/ |
|
|
|
|
1565
|
|
|
public function create_table($row) |
|
|
|
|
1566
|
|
|
{ |
|
|
|
|
1567
|
|
|
return ""; |
|
|
|
|
1568
|
|
|
} |
|
|
|
|
1569
|
|
|
|
1570
|
|
|
public function show_create_view($viewName) |
|
|
|
|
1571
|
|
|
{ |
|
|
|
|
1572
|
|
|
return "SELECT tbl_name as 'View', sql as 'Create View' ". |
|
|
|
|
1573
|
|
|
"FROM sqlite_master ". |
|
|
|
|
1574
|
|
|
"WHERE type='view' AND tbl_name='$viewName'"; |
|
|
|
|
1575
|
|
|
} |
|
|
|
|
1576
|
|
|
|
1577
|
|
|
/** |
|
|
|
|
1578
|
|
|
* function create_view Get view creation code from database |
|
|
|
|
1579
|
|
|
* @todo make it do something with sqlite |
|
|
|
|
1580
|
|
|
*/ |
|
|
|
|
1581
|
|
|
public function create_view($row) |
|
|
|
|
1582
|
|
|
{ |
|
|
|
|
1583
|
|
|
return ""; |
|
|
|
|
1584
|
|
|
} |
|
|
|
|
1585
|
|
|
|
1586
|
|
|
/** |
|
|
|
|
1587
|
|
|
* function show_create_trigger Get trigger creation code from database |
|
|
|
|
1588
|
|
|
* @todo make it do something with sqlite |
|
|
|
|
1589
|
|
|
*/ |
|
|
|
|
1590
|
|
|
public function show_create_trigger($triggerName) |
|
|
|
|
1591
|
|
|
{ |
|
|
|
|
1592
|
|
|
return ""; |
|
|
|
|
1593
|
|
|
} |
|
|
|
|
1594
|
|
|
|
1595
|
|
|
/** |
|
|
|
|
1596
|
|
|
* function create_trigger Modify trigger code, add delimiters, etc |
|
|
|
|
1597
|
|
|
* @todo make it do something with sqlite |
|
|
|
|
1598
|
|
|
*/ |
|
|
|
|
1599
|
|
|
public function create_trigger($triggerName) |
|
|
|
|
1600
|
|
|
{ |
|
|
|
|
1601
|
|
|
return ""; |
|
|
|
|
1602
|
|
|
} |
|
|
|
|
1603
|
|
|
|
1604
|
|
|
/** |
|
|
|
|
1605
|
|
|
* function create_procedure Modify procedure code, add delimiters, etc |
|
|
|
|
1606
|
|
|
* @todo make it do something with sqlite |
|
|
|
|
1607
|
|
|
*/ |
|
|
|
|
1608
|
|
|
public function create_procedure($procedureName) |
|
|
|
|
1609
|
|
|
{ |
|
|
|
|
1610
|
|
|
return ""; |
|
|
|
|
1611
|
|
|
} |
|
|
|
|
1612
|
|
|
|
1613
|
|
|
/** |
|
|
|
|
1614
|
|
|
* function create_function Modify function code, add delimiters, etc |
|
|
|
|
1615
|
|
|
* @todo make it do something with sqlite |
|
|
|
|
1616
|
|
|
*/ |
|
|
|
|
1617
|
|
|
public function create_function($functionName) |
|
|
|
|
1618
|
|
|
{ |
|
|
|
|
1619
|
|
|
return ""; |
|
|
|
|
1620
|
|
|
} |
|
|
|
|
1621
|
|
|
|
1622
|
|
|
public function show_tables() |
|
|
|
|
1623
|
|
|
{ |
|
|
|
|
1624
|
|
|
return "SELECT tbl_name FROM sqlite_master WHERE type='table'"; |
1625
|
|
|
} |
|
|
|
|
1626
|
|
|
|
1627
|
|
|
public function show_views() |
|
|
|
|
1628
|
|
|
{ |
|
|
|
|
1629
|
|
|
return "SELECT tbl_name FROM sqlite_master WHERE type='view'"; |
1630
|
|
|
} |
|
|
|
|
1631
|
|
|
|
1632
|
|
|
public function show_triggers() |
|
|
|
|
1633
|
|
|
{ |
|
|
|
|
1634
|
|
|
return "SELECT name FROM sqlite_master WHERE type='trigger'"; |
1635
|
|
|
} |
|
|
|
|
1636
|
|
|
|
1637
|
|
|
public function show_columns() |
|
|
|
|
1638
|
|
|
{ |
|
|
|
|
1639
|
|
|
if (func_num_args() != 1) { |
|
|
|
|
1640
|
|
|
return ""; |
|
|
|
|
1641
|
|
|
} |
1642
|
|
|
|
1643
|
|
|
$args = func_get_args(); |
1644
|
|
|
|
1645
|
|
|
return "pragma table_info(${args[0]})"; |
1646
|
|
|
} |
|
|
|
|
1647
|
|
|
|
1648
|
|
|
public function show_procedures() |
|
|
|
|
1649
|
|
|
{ |
|
|
|
|
1650
|
|
|
return ""; |
|
|
|
|
1651
|
|
|
} |
|
|
|
|
1652
|
|
|
|
1653
|
|
|
public function show_functions() |
|
|
|
|
1654
|
|
|
{ |
|
|
|
|
1655
|
|
|
return ""; |
|
|
|
|
1656
|
|
|
} |
|
|
|
|
1657
|
|
|
|
1658
|
|
|
public function show_events() |
|
|
|
|
1659
|
|
|
{ |
|
|
|
|
1660
|
|
|
return ""; |
|
|
|
|
1661
|
|
|
} |
|
|
|
|
1662
|
|
|
|
1663
|
|
|
public function setup_transaction() |
|
|
|
|
1664
|
|
|
{ |
|
|
|
|
1665
|
|
|
return ""; |
|
|
|
|
1666
|
|
|
} |
|
|
|
|
1667
|
|
|
|
1668
|
|
|
public function start_transaction() |
|
|
|
|
1669
|
|
|
{ |
|
|
|
|
1670
|
|
|
return "BEGIN EXCLUSIVE"; |
|
|
|
|
1671
|
|
|
} |
|
|
|
|
1672
|
|
|
|
1673
|
|
|
public function commit_transaction() |
|
|
|
|
1674
|
|
|
{ |
|
|
|
|
1675
|
|
|
return "COMMIT"; |
|
|
|
|
1676
|
|
|
} |
|
|
|
|
1677
|
|
|
|
1678
|
|
|
public function lock_table() |
|
|
|
|
1679
|
|
|
{ |
|
|
|
|
1680
|
|
|
return ""; |
|
|
|
|
1681
|
|
|
} |
|
|
|
|
1682
|
|
|
|
1683
|
|
|
public function unlock_table() |
|
|
|
|
1684
|
|
|
{ |
|
|
|
|
1685
|
|
|
return ""; |
|
|
|
|
1686
|
|
|
} |
|
|
|
|
1687
|
|
|
|
1688
|
|
|
public function start_add_lock_table() |
|
|
|
|
1689
|
|
|
{ |
|
|
|
|
1690
|
|
|
return PHP_EOL; |
1691
|
|
|
} |
|
|
|
|
1692
|
|
|
|
1693
|
|
|
public function end_add_lock_table() |
|
|
|
|
1694
|
|
|
{ |
|
|
|
|
1695
|
|
|
return PHP_EOL; |
1696
|
|
|
} |
|
|
|
|
1697
|
|
|
|
1698
|
|
|
public function start_add_disable_keys() |
|
|
|
|
1699
|
|
|
{ |
|
|
|
|
1700
|
|
|
return PHP_EOL; |
1701
|
|
|
} |
|
|
|
|
1702
|
|
|
|
1703
|
|
|
public function end_add_disable_keys() |
|
|
|
|
1704
|
|
|
{ |
|
|
|
|
1705
|
|
|
return PHP_EOL; |
1706
|
|
|
} |
|
|
|
|
1707
|
|
|
|
1708
|
|
|
public function start_disable_foreign_keys_check() |
|
|
|
|
1709
|
|
|
{ |
|
|
|
|
1710
|
|
|
return PHP_EOL; |
1711
|
|
|
} |
|
|
|
|
1712
|
|
|
|
1713
|
|
|
public function end_disable_foreign_keys_check() |
|
|
|
|
1714
|
|
|
{ |
|
|
|
|
1715
|
|
|
return PHP_EOL; |
1716
|
|
|
} |
|
|
|
|
1717
|
|
|
|
1718
|
|
|
public function add_drop_database() |
|
|
|
|
1719
|
|
|
{ |
|
|
|
|
1720
|
|
|
return PHP_EOL; |
1721
|
|
|
} |
|
|
|
|
1722
|
|
|
|
1723
|
|
|
public function add_drop_trigger() |
|
|
|
|
1724
|
|
|
{ |
|
|
|
|
1725
|
|
|
return PHP_EOL; |
1726
|
|
|
} |
|
|
|
|
1727
|
|
|
|
1728
|
|
|
public function drop_table() |
|
|
|
|
1729
|
|
|
{ |
|
|
|
|
1730
|
|
|
return PHP_EOL; |
1731
|
|
|
} |
|
|
|
|
1732
|
|
|
|
1733
|
|
|
public function drop_view() |
|
|
|
|
1734
|
|
|
{ |
|
|
|
|
1735
|
|
|
return PHP_EOL; |
1736
|
|
|
} |
|
|
|
|
1737
|
|
|
|
1738
|
|
|
/** |
1739
|
|
|
* Decode column metadata and fill info structure. |
1740
|
|
|
* type, is_numeric and is_blob will always be available. |
1741
|
|
|
* |
1742
|
|
|
* @param array $colType Array returned from "SHOW COLUMNS FROM tableName" |
|
|
|
|
1743
|
|
|
* @return array |
|
|
|
|
1744
|
|
|
*/ |
1745
|
|
|
public function parseColumnType($colType) |
|
|
|
|
1746
|
|
|
{ |
|
|
|
|
1747
|
|
|
return array(); |
|
|
|
|
1748
|
|
|
} |
|
|
|
|
1749
|
|
|
|
1750
|
|
|
public function backup_parameters() |
|
|
|
|
1751
|
|
|
{ |
|
|
|
|
1752
|
|
|
return PHP_EOL; |
1753
|
|
|
} |
|
|
|
|
1754
|
|
|
|
1755
|
|
|
public function restore_parameters() |
|
|
|
|
1756
|
|
|
{ |
|
|
|
|
1757
|
|
|
return PHP_EOL; |
1758
|
|
|
} |
|
|
|
|
1759
|
|
|
} |
|
|
|
|
1760
|
|
|
|
1761
|
|
|
class TypeAdapterPgsql extends TypeAdapterFactory |
|
|
|
|
1762
|
|
|
{ |
|
|
|
|
1763
|
|
|
} |
|
|
|
|
1764
|
|
|
|
1765
|
|
|
class TypeAdapterDblib extends TypeAdapterFactory |
|
|
|
|
1766
|
|
|
{ |
|
|
|
|
1767
|
|
|
} |
|
|
|
|
1768
|
|
|
|
1769
|
|
|
class TypeAdapterSqlite extends TypeAdapterFactory |
|
|
|
|
1770
|
|
|
{ |
|
|
|
|
1771
|
|
|
} |
|
|
|
|
1772
|
|
|
|
1773
|
|
|
class TypeAdapterMysql extends TypeAdapterFactory |
|
|
|
|
1774
|
|
|
{ |
|
|
|
|
1775
|
|
|
const DEFINER_RE = 'DEFINER=`(?:[^`]|``)*`@`(?:[^`]|``)*`'; |
1776
|
|
|
|
1777
|
|
|
|
1778
|
|
|
// Numerical Mysql types |
|
|
|
|
1779
|
|
|
public $mysqlTypes = array( |
|
|
|
|
1780
|
|
|
'numerical' => array( |
|
|
|
|
1781
|
|
|
'bit', |
|
|
|
|
1782
|
|
|
'tinyint', |
|
|
|
|
1783
|
|
|
'smallint', |
|
|
|
|
1784
|
|
|
'mediumint', |
|
|
|
|
1785
|
|
|
'int', |
|
|
|
|
1786
|
|
|
'integer', |
|
|
|
|
1787
|
|
|
'bigint', |
|
|
|
|
1788
|
|
|
'real', |
|
|
|
|
1789
|
|
|
'double', |
|
|
|
|
1790
|
|
|
'float', |
|
|
|
|
1791
|
|
|
'decimal', |
|
|
|
|
1792
|
|
|
'numeric' |
|
|
|
|
1793
|
|
|
), |
|
|
|
|
1794
|
|
|
'blob' => array( |
|
|
|
|
1795
|
|
|
'tinyblob', |
|
|
|
|
1796
|
|
|
'blob', |
|
|
|
|
1797
|
|
|
'mediumblob', |
|
|
|
|
1798
|
|
|
'longblob', |
|
|
|
|
1799
|
|
|
'binary', |
|
|
|
|
1800
|
|
|
'varbinary', |
|
|
|
|
1801
|
|
|
'bit', |
|
|
|
|
1802
|
|
|
'geometry', /* http://bugs.mysql.com/bug.php?id=43544 */ |
|
|
|
|
1803
|
|
|
'point', |
|
|
|
|
1804
|
|
|
'linestring', |
|
|
|
|
1805
|
|
|
'polygon', |
|
|
|
|
1806
|
|
|
'multipoint', |
|
|
|
|
1807
|
|
|
'multilinestring', |
|
|
|
|
1808
|
|
|
'multipolygon', |
|
|
|
|
1809
|
|
|
'geometrycollection', |
|
|
|
|
1810
|
|
|
) |
|
|
|
|
1811
|
|
|
); |
|
|
|
|
1812
|
|
|
|
1813
|
|
|
public function databases() |
|
|
|
|
1814
|
|
|
{ |
|
|
|
|
1815
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
1816
|
|
|
$args = func_get_args(); |
|
|
|
|
1817
|
|
|
$databaseName = $args[0]; |
|
|
|
|
1818
|
|
|
|
1819
|
|
|
$resultSet = $this->dbHandler->query("SHOW VARIABLES LIKE 'character_set_database';"); |
|
|
|
|
1820
|
|
|
$characterSet = $resultSet->fetchColumn(1); |
1821
|
|
|
$resultSet->closeCursor(); |
1822
|
|
|
|
1823
|
|
|
$resultSet = $this->dbHandler->query("SHOW VARIABLES LIKE 'collation_database';"); |
|
|
|
|
1824
|
|
|
$collationDb = $resultSet->fetchColumn(1); |
1825
|
|
|
$resultSet->closeCursor(); |
1826
|
|
|
$ret = ""; |
|
|
|
|
1827
|
|
|
|
1828
|
|
|
$ret .= "CREATE DATABASE /*!32312 IF NOT EXISTS*/ `${databaseName}`". |
|
|
|
|
1829
|
|
|
" /*!40100 DEFAULT CHARACTER SET ${characterSet} ". |
|
|
|
|
1830
|
|
|
" COLLATE ${collationDb} */;".PHP_EOL.PHP_EOL. |
|
|
|
|
1831
|
|
|
"USE `${databaseName}`;".PHP_EOL.PHP_EOL; |
1832
|
|
|
|
1833
|
|
|
return $ret; |
1834
|
|
|
} |
|
|
|
|
1835
|
|
|
|
1836
|
|
|
public function show_create_table($tableName) |
|
|
|
|
1837
|
|
|
{ |
|
|
|
|
1838
|
|
|
return "SHOW CREATE TABLE `$tableName`"; |
|
|
|
|
1839
|
|
|
} |
|
|
|
|
1840
|
|
|
|
1841
|
|
|
public function show_create_view($viewName) |
|
|
|
|
1842
|
|
|
{ |
|
|
|
|
1843
|
|
|
return "SHOW CREATE VIEW `$viewName`"; |
|
|
|
|
1844
|
|
|
} |
|
|
|
|
1845
|
|
|
|
1846
|
|
|
public function show_create_trigger($triggerName) |
|
|
|
|
1847
|
|
|
{ |
|
|
|
|
1848
|
|
|
return "SHOW CREATE TRIGGER `$triggerName`"; |
|
|
|
|
1849
|
|
|
} |
|
|
|
|
1850
|
|
|
|
1851
|
|
|
public function show_create_procedure($procedureName) |
|
|
|
|
1852
|
|
|
{ |
|
|
|
|
1853
|
|
|
return "SHOW CREATE PROCEDURE `$procedureName`"; |
|
|
|
|
1854
|
|
|
} |
|
|
|
|
1855
|
|
|
|
1856
|
|
|
public function show_create_function($functionName) |
|
|
|
|
1857
|
|
|
{ |
|
|
|
|
1858
|
|
|
return "SHOW CREATE FUNCTION `$functionName`"; |
|
|
|
|
1859
|
|
|
} |
|
|
|
|
1860
|
|
|
|
1861
|
|
|
public function show_create_event($eventName) |
|
|
|
|
1862
|
|
|
{ |
|
|
|
|
1863
|
|
|
return "SHOW CREATE EVENT `$eventName`"; |
|
|
|
|
1864
|
|
|
} |
|
|
|
|
1865
|
|
|
|
1866
|
|
|
public function create_table($row) |
|
|
|
|
1867
|
|
|
{ |
|
|
|
|
1868
|
|
|
if (!isset($row['Create Table'])) { |
|
|
|
|
1869
|
|
|
throw new Exception("Error getting table code, unknown output"); |
|
|
|
|
1870
|
|
|
} |
1871
|
|
|
|
1872
|
|
|
$createTable = $row['Create Table']; |
1873
|
|
|
if ($this->dumpSettings['reset-auto-increment']) { |
1874
|
|
|
$match = "/AUTO_INCREMENT=[0-9]+/s"; |
|
|
|
|
1875
|
|
|
$replace = ""; |
|
|
|
|
1876
|
|
|
$createTable = preg_replace($match, $replace, $createTable); |
1877
|
|
|
} |
1878
|
|
|
|
1879
|
|
|
if ($this->dumpSettings['if-not-exists'] ) { |
|
|
|
|
1880
|
|
|
$createTable = preg_replace('/^CREATE TABLE/', 'CREATE TABLE IF NOT EXISTS', $createTable); |
|
|
|
|
1881
|
|
|
} |
|
|
|
|
1882
|
|
|
|
1883
|
|
|
$ret = "/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL. |
|
|
|
|
1884
|
|
|
"/*!40101 SET character_set_client = ".$this->dumpSettings['default-character-set']." */;".PHP_EOL. |
|
|
|
|
1885
|
|
|
$createTable.";".PHP_EOL. |
|
|
|
|
1886
|
|
|
"/*!40101 SET character_set_client = @saved_cs_client */;".PHP_EOL. |
|
|
|
|
1887
|
|
|
PHP_EOL; |
1888
|
|
|
return $ret; |
1889
|
|
|
} |
|
|
|
|
1890
|
|
|
|
1891
|
|
|
public function create_view($row) |
|
|
|
|
1892
|
|
|
{ |
|
|
|
|
1893
|
|
|
$ret = ""; |
|
|
|
|
1894
|
|
|
if (!isset($row['Create View'])) { |
|
|
|
|
1895
|
|
|
throw new Exception("Error getting view structure, unknown output"); |
|
|
|
|
1896
|
|
|
} |
1897
|
|
|
|
1898
|
|
|
$viewStmt = $row['Create View']; |
1899
|
|
|
|
1900
|
|
|
$definerStr = $this->dumpSettings['skip-definer'] ? '' : '/*!50013 \2 */'.PHP_EOL; |
|
|
|
|
1901
|
|
|
|
1902
|
|
|
if ($viewStmtReplaced = preg_replace( |
|
|
|
|
1903
|
|
|
'/^(CREATE(?:\s+ALGORITHM=(?:UNDEFINED|MERGE|TEMPTABLE))?)\s+(' |
1904
|
|
|
.self::DEFINER_RE.'(?:\s+SQL SECURITY DEFINER|INVOKER)?)?\s+(VIEW .+)$/', |
|
|
|
|
1905
|
|
|
'/*!50001 \1 */'.PHP_EOL.$definerStr.'/*!50001 \3 */', |
1906
|
|
|
$viewStmt, |
1907
|
|
|
1 |
1908
|
|
|
)) { |
|
|
|
|
1909
|
|
|
$viewStmt = $viewStmtReplaced; |
1910
|
|
|
}; |
1911
|
|
|
|
1912
|
|
|
$ret .= $viewStmt.';'.PHP_EOL.PHP_EOL; |
1913
|
|
|
return $ret; |
1914
|
|
|
} |
|
|
|
|
1915
|
|
|
|
1916
|
|
|
public function create_trigger($row) |
|
|
|
|
1917
|
|
|
{ |
|
|
|
|
1918
|
|
|
$ret = ""; |
|
|
|
|
1919
|
|
|
if (!isset($row['SQL Original Statement'])) { |
|
|
|
|
1920
|
|
|
throw new Exception("Error getting trigger code, unknown output"); |
|
|
|
|
1921
|
|
|
} |
1922
|
|
|
|
1923
|
|
|
$triggerStmt = $row['SQL Original Statement']; |
1924
|
|
|
$definerStr = $this->dumpSettings['skip-definer'] ? '' : '/*!50017 \2*/ '; |
|
|
|
|
1925
|
|
|
if ($triggerStmtReplaced = preg_replace( |
|
|
|
|
1926
|
|
|
'/^(CREATE)\s+('.self::DEFINER_RE.')?\s+(TRIGGER\s.*)$/s', |
1927
|
|
|
'/*!50003 \1*/ '.$definerStr.'/*!50003 \3 */', |
1928
|
|
|
$triggerStmt, |
1929
|
|
|
1 |
1930
|
|
|
)) { |
|
|
|
|
1931
|
|
|
$triggerStmt = $triggerStmtReplaced; |
1932
|
|
|
} |
1933
|
|
|
|
1934
|
|
|
$ret .= "DELIMITER ;;".PHP_EOL. |
|
|
|
|
1935
|
|
|
$triggerStmt.";;".PHP_EOL. |
|
|
|
|
1936
|
|
|
"DELIMITER ;".PHP_EOL.PHP_EOL; |
|
|
|
|
1937
|
|
|
return $ret; |
1938
|
|
|
} |
|
|
|
|
1939
|
|
|
|
1940
|
|
|
public function create_procedure($row) |
|
|
|
|
1941
|
|
|
{ |
|
|
|
|
1942
|
|
|
$ret = ""; |
|
|
|
|
1943
|
|
|
if (!isset($row['Create Procedure'])) { |
|
|
|
|
1944
|
|
|
throw new Exception("Error getting procedure code, unknown output. ". |
|
|
|
|
1945
|
|
|
"Please check 'https://bugs.mysql.com/bug.php?id=14564'"); |
|
|
|
|
1946
|
|
|
} |
|
|
|
|
1947
|
|
|
$procedureStmt = $row['Create Procedure']; |
1948
|
|
|
if ($this->dumpSettings['skip-definer']) { |
1949
|
|
|
if ($procedureStmtReplaced = preg_replace( |
|
|
|
|
1950
|
|
|
'/^(CREATE)\s+('.self::DEFINER_RE.')?\s+(PROCEDURE\s.*)$/s', |
1951
|
|
|
'\1 \3', |
1952
|
|
|
$procedureStmt, |
1953
|
|
|
1 |
1954
|
|
|
)) { |
|
|
|
|
1955
|
|
|
$procedureStmt = $procedureStmtReplaced; |
1956
|
|
|
} |
1957
|
|
|
} |
1958
|
|
|
|
1959
|
|
|
$ret .= "/*!50003 DROP PROCEDURE IF EXISTS `". |
|
|
|
|
1960
|
|
|
$row['Procedure']."` */;".PHP_EOL. |
|
|
|
|
1961
|
|
|
"/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL. |
|
|
|
|
1962
|
|
|
"/*!40101 SET character_set_client = ".$this->dumpSettings['default-character-set']." */;".PHP_EOL. |
|
|
|
|
1963
|
|
|
"DELIMITER ;;".PHP_EOL. |
|
|
|
|
1964
|
|
|
$procedureStmt." ;;".PHP_EOL. |
|
|
|
|
1965
|
|
|
"DELIMITER ;".PHP_EOL. |
|
|
|
|
1966
|
|
|
"/*!40101 SET character_set_client = @saved_cs_client */;".PHP_EOL.PHP_EOL; |
|
|
|
|
1967
|
|
|
|
1968
|
|
|
return $ret; |
1969
|
|
|
} |
|
|
|
|
1970
|
|
|
|
1971
|
|
|
public function create_function($row) |
|
|
|
|
1972
|
|
|
{ |
|
|
|
|
1973
|
|
|
$ret = ""; |
|
|
|
|
1974
|
|
|
if (!isset($row['Create Function'])) { |
|
|
|
|
1975
|
|
|
throw new Exception("Error getting function code, unknown output. ". |
|
|
|
|
1976
|
|
|
"Please check 'https://bugs.mysql.com/bug.php?id=14564'"); |
|
|
|
|
1977
|
|
|
} |
|
|
|
|
1978
|
|
|
$functionStmt = $row['Create Function']; |
|
|
|
|
1979
|
|
|
$characterSetClient = $row['character_set_client']; |
|
|
|
|
1980
|
|
|
$collationConnection = $row['collation_connection']; |
1981
|
|
|
$sqlMode = $row['sql_mode']; |
|
|
|
|
1982
|
|
|
if ( $this->dumpSettings['skip-definer'] ) { |
|
|
|
|
1983
|
|
|
if ($functionStmtReplaced = preg_replace( |
|
|
|
|
1984
|
|
|
'/^(CREATE)\s+('.self::DEFINER_RE.')?\s+(FUNCTION\s.*)$/s', |
1985
|
|
|
'\1 \3', |
1986
|
|
|
$functionStmt, |
1987
|
|
|
1 |
1988
|
|
|
)) { |
|
|
|
|
1989
|
|
|
$functionStmt = $functionStmtReplaced; |
1990
|
|
|
} |
1991
|
|
|
} |
1992
|
|
|
|
1993
|
|
|
$ret .= "/*!50003 DROP FUNCTION IF EXISTS `". |
|
|
|
|
1994
|
|
|
$row['Function']."` */;".PHP_EOL. |
|
|
|
|
1995
|
|
|
"/*!40101 SET @saved_cs_client = @@character_set_client */;".PHP_EOL. |
|
|
|
|
1996
|
|
|
"/*!50003 SET @saved_cs_results = @@character_set_results */ ;".PHP_EOL. |
|
|
|
|
1997
|
|
|
"/*!50003 SET @saved_col_connection = @@collation_connection */ ;".PHP_EOL. |
|
|
|
|
1998
|
|
|
"/*!40101 SET character_set_client = ".$characterSetClient." */;".PHP_EOL. |
|
|
|
|
1999
|
|
|
"/*!40101 SET character_set_results = ".$characterSetClient." */;".PHP_EOL. |
|
|
|
|
2000
|
|
|
"/*!50003 SET collation_connection = ".$collationConnection." */ ;".PHP_EOL. |
|
|
|
|
2001
|
|
|
"/*!50003 SET @saved_sql_mode = @@sql_mode */ ;;".PHP_EOL. |
|
|
|
|
2002
|
|
|
"/*!50003 SET sql_mode = '".$sqlMode."' */ ;;".PHP_EOL. |
|
|
|
|
2003
|
|
|
"/*!50003 SET @saved_time_zone = @@time_zone */ ;;".PHP_EOL. |
|
|
|
|
2004
|
|
|
"/*!50003 SET time_zone = 'SYSTEM' */ ;;".PHP_EOL. |
|
|
|
|
2005
|
|
|
"DELIMITER ;;".PHP_EOL. |
|
|
|
|
2006
|
|
|
$functionStmt." ;;".PHP_EOL. |
|
|
|
|
2007
|
|
|
"DELIMITER ;".PHP_EOL. |
|
|
|
|
2008
|
|
|
"/*!50003 SET sql_mode = @saved_sql_mode */ ;".PHP_EOL. |
|
|
|
|
2009
|
|
|
"/*!50003 SET character_set_client = @saved_cs_client */ ;".PHP_EOL. |
|
|
|
|
2010
|
|
|
"/*!50003 SET character_set_results = @saved_cs_results */ ;".PHP_EOL. |
|
|
|
|
2011
|
|
|
"/*!50003 SET collation_connection = @saved_col_connection */ ;".PHP_EOL. |
|
|
|
|
2012
|
|
|
"/*!50106 SET TIME_ZONE= @saved_time_zone */ ;".PHP_EOL.PHP_EOL; |
|
|
|
|
2013
|
|
|
|
|
|
|
|
2014
|
|
|
|
2015
|
|
|
return $ret; |
2016
|
|
|
} |
|
|
|
|
2017
|
|
|
|
2018
|
|
|
public function create_event($row) |
|
|
|
|
2019
|
|
|
{ |
|
|
|
|
2020
|
|
|
$ret = ""; |
|
|
|
|
2021
|
|
|
if (!isset($row['Create Event'])) { |
|
|
|
|
2022
|
|
|
throw new Exception("Error getting event code, unknown output. ". |
|
|
|
|
2023
|
|
|
"Please check 'http://stackoverflow.com/questions/10853826/mysql-5-5-create-event-gives-syntax-error'"); |
|
|
|
|
2024
|
|
|
} |
|
|
|
|
2025
|
|
|
$eventName = $row['Event']; |
|
|
|
|
2026
|
|
|
$eventStmt = $row['Create Event']; |
|
|
|
|
2027
|
|
|
$sqlMode = $row['sql_mode']; |
|
|
|
|
2028
|
|
|
$definerStr = $this->dumpSettings['skip-definer'] ? '' : '/*!50117 \2*/ '; |
|
|
|
|
2029
|
|
|
|
2030
|
|
|
if ($eventStmtReplaced = preg_replace( |
|
|
|
|
2031
|
|
|
'/^(CREATE)\s+('.self::DEFINER_RE.')?\s+(EVENT .*)$/', |
2032
|
|
|
'/*!50106 \1*/ '.$definerStr.'/*!50106 \3 */', |
2033
|
|
|
$eventStmt, |
2034
|
|
|
1 |
2035
|
|
|
)) { |
|
|
|
|
2036
|
|
|
$eventStmt = $eventStmtReplaced; |
2037
|
|
|
} |
2038
|
|
|
|
2039
|
|
|
$ret .= "/*!50106 SET @save_time_zone= @@TIME_ZONE */ ;".PHP_EOL. |
|
|
|
|
2040
|
|
|
"/*!50106 DROP EVENT IF EXISTS `".$eventName."` */;".PHP_EOL. |
|
|
|
|
2041
|
|
|
"DELIMITER ;;".PHP_EOL. |
|
|
|
|
2042
|
|
|
"/*!50003 SET @saved_cs_client = @@character_set_client */ ;;".PHP_EOL. |
|
|
|
|
2043
|
|
|
"/*!50003 SET @saved_cs_results = @@character_set_results */ ;;".PHP_EOL. |
|
|
|
|
2044
|
|
|
"/*!50003 SET @saved_col_connection = @@collation_connection */ ;;".PHP_EOL. |
|
|
|
|
2045
|
|
|
"/*!50003 SET character_set_client = utf8 */ ;;".PHP_EOL. |
|
|
|
|
2046
|
|
|
"/*!50003 SET character_set_results = utf8 */ ;;".PHP_EOL. |
|
|
|
|
2047
|
|
|
"/*!50003 SET collation_connection = utf8_general_ci */ ;;".PHP_EOL. |
|
|
|
|
2048
|
|
|
"/*!50003 SET @saved_sql_mode = @@sql_mode */ ;;".PHP_EOL. |
|
|
|
|
2049
|
|
|
"/*!50003 SET sql_mode = '".$sqlMode."' */ ;;".PHP_EOL. |
|
|
|
|
2050
|
|
|
"/*!50003 SET @saved_time_zone = @@time_zone */ ;;".PHP_EOL. |
|
|
|
|
2051
|
|
|
"/*!50003 SET time_zone = 'SYSTEM' */ ;;".PHP_EOL. |
|
|
|
|
2052
|
|
|
$eventStmt." ;;".PHP_EOL. |
|
|
|
|
2053
|
|
|
"/*!50003 SET time_zone = @saved_time_zone */ ;;".PHP_EOL. |
|
|
|
|
2054
|
|
|
"/*!50003 SET sql_mode = @saved_sql_mode */ ;;".PHP_EOL. |
|
|
|
|
2055
|
|
|
"/*!50003 SET character_set_client = @saved_cs_client */ ;;".PHP_EOL. |
|
|
|
|
2056
|
|
|
"/*!50003 SET character_set_results = @saved_cs_results */ ;;".PHP_EOL. |
|
|
|
|
2057
|
|
|
"/*!50003 SET collation_connection = @saved_col_connection */ ;;".PHP_EOL. |
|
|
|
|
2058
|
|
|
"DELIMITER ;".PHP_EOL. |
|
|
|
|
2059
|
|
|
"/*!50106 SET TIME_ZONE= @save_time_zone */ ;".PHP_EOL.PHP_EOL; |
|
|
|
|
2060
|
|
|
// Commented because we are doing this in restore_parameters() |
2061
|
|
|
// "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;" . PHP_EOL . PHP_EOL; |
|
|
|
|
2062
|
|
|
|
2063
|
|
|
return $ret; |
2064
|
|
|
} |
|
|
|
|
2065
|
|
|
|
2066
|
|
|
public function show_tables() |
|
|
|
|
2067
|
|
|
{ |
|
|
|
|
2068
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2069
|
|
|
$args = func_get_args(); |
|
|
|
|
2070
|
|
|
return "SELECT TABLE_NAME AS tbl_name ". |
|
|
|
|
2071
|
|
|
"FROM INFORMATION_SCHEMA.TABLES ". |
|
|
|
|
2072
|
|
|
"WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='${args[0]}'"; |
2073
|
|
|
} |
|
|
|
|
2074
|
|
|
|
2075
|
|
|
public function show_views() |
|
|
|
|
2076
|
|
|
{ |
|
|
|
|
2077
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2078
|
|
|
$args = func_get_args(); |
|
|
|
|
2079
|
|
|
return "SELECT TABLE_NAME AS tbl_name ". |
|
|
|
|
2080
|
|
|
"FROM INFORMATION_SCHEMA.TABLES ". |
|
|
|
|
2081
|
|
|
"WHERE TABLE_TYPE='VIEW' AND TABLE_SCHEMA='${args[0]}'"; |
2082
|
|
|
} |
|
|
|
|
2083
|
|
|
|
2084
|
|
|
public function show_triggers() |
|
|
|
|
2085
|
|
|
{ |
|
|
|
|
2086
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2087
|
|
|
$args = func_get_args(); |
|
|
|
|
2088
|
|
|
return "SHOW TRIGGERS FROM `${args[0]}`;"; |
2089
|
|
|
} |
|
|
|
|
2090
|
|
|
|
2091
|
|
|
public function show_columns() |
|
|
|
|
2092
|
|
|
{ |
|
|
|
|
2093
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2094
|
|
|
$args = func_get_args(); |
|
|
|
|
2095
|
|
|
return "SHOW COLUMNS FROM `${args[0]}`;"; |
2096
|
|
|
} |
|
|
|
|
2097
|
|
|
|
2098
|
|
|
public function show_procedures() |
|
|
|
|
2099
|
|
|
{ |
|
|
|
|
2100
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2101
|
|
|
$args = func_get_args(); |
|
|
|
|
2102
|
|
|
return "SELECT SPECIFIC_NAME AS procedure_name ". |
|
|
|
|
2103
|
|
|
"FROM INFORMATION_SCHEMA.ROUTINES ". |
|
|
|
|
2104
|
|
|
"WHERE ROUTINE_TYPE='PROCEDURE' AND ROUTINE_SCHEMA='${args[0]}'"; |
2105
|
|
|
} |
|
|
|
|
2106
|
|
|
|
2107
|
|
|
public function show_functions() |
|
|
|
|
2108
|
|
|
{ |
|
|
|
|
2109
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2110
|
|
|
$args = func_get_args(); |
|
|
|
|
2111
|
|
|
return "SELECT SPECIFIC_NAME AS function_name ". |
|
|
|
|
2112
|
|
|
"FROM INFORMATION_SCHEMA.ROUTINES ". |
|
|
|
|
2113
|
|
|
"WHERE ROUTINE_TYPE='FUNCTION' AND ROUTINE_SCHEMA='${args[0]}'"; |
2114
|
|
|
} |
|
|
|
|
2115
|
|
|
|
2116
|
|
|
/** |
2117
|
|
|
* Get query string to ask for names of events from current database. |
2118
|
|
|
* |
2119
|
|
|
* @param string Name of database |
|
|
|
|
2120
|
|
|
* @return string |
|
|
|
|
2121
|
|
|
*/ |
2122
|
|
|
public function show_events() |
|
|
|
|
2123
|
|
|
{ |
|
|
|
|
2124
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2125
|
|
|
$args = func_get_args(); |
|
|
|
|
2126
|
|
|
return "SELECT EVENT_NAME AS event_name ". |
|
|
|
|
2127
|
|
|
"FROM INFORMATION_SCHEMA.EVENTS ". |
|
|
|
|
2128
|
|
|
"WHERE EVENT_SCHEMA='${args[0]}'"; |
2129
|
|
|
} |
|
|
|
|
2130
|
|
|
|
2131
|
|
|
public function setup_transaction() |
|
|
|
|
2132
|
|
|
{ |
|
|
|
|
2133
|
|
|
return "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ"; |
|
|
|
|
2134
|
|
|
} |
|
|
|
|
2135
|
|
|
|
2136
|
|
|
public function start_transaction() |
|
|
|
|
2137
|
|
|
{ |
|
|
|
|
2138
|
|
|
return "START TRANSACTION ". |
|
|
|
|
2139
|
|
|
"/*!40100 WITH CONSISTENT SNAPSHOT */"; |
|
|
|
|
2140
|
|
|
} |
|
|
|
|
2141
|
|
|
|
2142
|
|
|
|
2143
|
|
|
public function commit_transaction() |
|
|
|
|
2144
|
|
|
{ |
|
|
|
|
2145
|
|
|
return "COMMIT"; |
|
|
|
|
2146
|
|
|
} |
|
|
|
|
2147
|
|
|
|
2148
|
|
|
public function lock_table() |
|
|
|
|
2149
|
|
|
{ |
|
|
|
|
2150
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2151
|
|
|
$args = func_get_args(); |
|
|
|
|
2152
|
|
|
return $this->dbHandler->exec("LOCK TABLES `${args[0]}` READ LOCAL"); |
2153
|
|
|
} |
|
|
|
|
2154
|
|
|
|
2155
|
|
|
public function unlock_table() |
|
|
|
|
2156
|
|
|
{ |
|
|
|
|
2157
|
|
|
return $this->dbHandler->exec("UNLOCK TABLES"); |
|
|
|
|
2158
|
|
|
} |
|
|
|
|
2159
|
|
|
|
2160
|
|
|
public function start_add_lock_table() |
|
|
|
|
2161
|
|
|
{ |
|
|
|
|
2162
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2163
|
|
|
$args = func_get_args(); |
|
|
|
|
2164
|
|
|
return "LOCK TABLES `${args[0]}` WRITE;".PHP_EOL; |
2165
|
|
|
} |
|
|
|
|
2166
|
|
|
|
2167
|
|
|
public function end_add_lock_table() |
|
|
|
|
2168
|
|
|
{ |
|
|
|
|
2169
|
|
|
return "UNLOCK TABLES;".PHP_EOL; |
|
|
|
|
2170
|
|
|
} |
|
|
|
|
2171
|
|
|
|
2172
|
|
|
public function start_add_disable_keys() |
|
|
|
|
2173
|
|
|
{ |
|
|
|
|
2174
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2175
|
|
|
$args = func_get_args(); |
|
|
|
|
2176
|
|
|
return "/*!40000 ALTER TABLE `${args[0]}` DISABLE KEYS */;". |
|
|
|
|
2177
|
|
|
PHP_EOL; |
2178
|
|
|
} |
|
|
|
|
2179
|
|
|
|
2180
|
|
|
public function end_add_disable_keys() |
|
|
|
|
2181
|
|
|
{ |
|
|
|
|
2182
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2183
|
|
|
$args = func_get_args(); |
|
|
|
|
2184
|
|
|
return "/*!40000 ALTER TABLE `${args[0]}` ENABLE KEYS */;". |
|
|
|
|
2185
|
|
|
PHP_EOL; |
2186
|
|
|
} |
|
|
|
|
2187
|
|
|
|
2188
|
|
|
public function start_disable_autocommit() |
|
|
|
|
2189
|
|
|
{ |
|
|
|
|
2190
|
|
|
return "SET autocommit=0;".PHP_EOL; |
|
|
|
|
2191
|
|
|
} |
|
|
|
|
2192
|
|
|
|
2193
|
|
|
public function end_disable_autocommit() |
|
|
|
|
2194
|
|
|
{ |
|
|
|
|
2195
|
|
|
return "COMMIT;".PHP_EOL; |
|
|
|
|
2196
|
|
|
} |
|
|
|
|
2197
|
|
|
|
2198
|
|
|
public function add_drop_database() |
|
|
|
|
2199
|
|
|
{ |
|
|
|
|
2200
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2201
|
|
|
$args = func_get_args(); |
|
|
|
|
2202
|
|
|
return "/*!40000 DROP DATABASE IF EXISTS `${args[0]}`*/;". |
|
|
|
|
2203
|
|
|
PHP_EOL.PHP_EOL; |
2204
|
|
|
} |
|
|
|
|
2205
|
|
|
|
2206
|
|
|
public function add_drop_trigger() |
|
|
|
|
2207
|
|
|
{ |
|
|
|
|
2208
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2209
|
|
|
$args = func_get_args(); |
|
|
|
|
2210
|
|
|
return "DROP TRIGGER IF EXISTS `${args[0]}`;".PHP_EOL; |
2211
|
|
|
} |
|
|
|
|
2212
|
|
|
|
2213
|
|
|
public function drop_table() |
|
|
|
|
2214
|
|
|
{ |
|
|
|
|
2215
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2216
|
|
|
$args = func_get_args(); |
|
|
|
|
2217
|
|
|
return "DROP TABLE IF EXISTS `${args[0]}`;".PHP_EOL; |
2218
|
|
|
} |
|
|
|
|
2219
|
|
|
|
2220
|
|
|
public function drop_view() |
|
|
|
|
2221
|
|
|
{ |
|
|
|
|
2222
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2223
|
|
|
$args = func_get_args(); |
|
|
|
|
2224
|
|
|
return "DROP TABLE IF EXISTS `${args[0]}`;".PHP_EOL. |
|
|
|
|
2225
|
|
|
"/*!50001 DROP VIEW IF EXISTS `${args[0]}`*/;".PHP_EOL; |
2226
|
|
|
} |
|
|
|
|
2227
|
|
|
|
2228
|
|
|
public function getDatabaseHeader() |
|
|
|
|
2229
|
|
|
{ |
|
|
|
|
2230
|
|
|
$this->check_parameters(func_num_args(), $expected_num_args = 1, __METHOD__); |
|
|
|
|
2231
|
|
|
$args = func_get_args(); |
|
|
|
|
2232
|
|
|
return "--".PHP_EOL. |
|
|
|
|
2233
|
|
|
"-- Current Database: `${args[0]}`".PHP_EOL. |
|
|
|
|
2234
|
|
|
"--".PHP_EOL.PHP_EOL; |
|
|
|
|
2235
|
|
|
} |
|
|
|
|
2236
|
|
|
|
2237
|
|
|
/** |
2238
|
|
|
* Decode column metadata and fill info structure. |
2239
|
|
|
* type, is_numeric and is_blob will always be available. |
2240
|
|
|
* |
2241
|
|
|
* @param array $colType Array returned from "SHOW COLUMNS FROM tableName" |
|
|
|
|
2242
|
|
|
* @return array |
|
|
|
|
2243
|
|
|
*/ |
2244
|
|
|
public function parseColumnType($colType) |
|
|
|
|
2245
|
|
|
{ |
|
|
|
|
2246
|
|
|
$colInfo = array(); |
|
|
|
|
2247
|
|
|
$colParts = explode(" ", $colType['Type']); |
|
|
|
|
2248
|
|
|
|
2249
|
|
|
if ($fparen = strpos($colParts[0], "(")) { |
|
|
|
|
2250
|
|
|
$colInfo['type'] = substr($colParts[0], 0, $fparen); |
|
|
|
|
2251
|
|
|
$colInfo['length'] = str_replace(")", "", substr($colParts[0], $fparen + 1)); |
|
|
|
|
2252
|
|
|
$colInfo['attributes'] = isset($colParts[1]) ? $colParts[1] : null; |
|
|
|
|
2253
|
|
|
} else { |
2254
|
|
|
$colInfo['type'] = $colParts[0]; |
2255
|
|
|
} |
|
|
|
|
2256
|
|
|
$colInfo['is_numeric'] = in_array($colInfo['type'], $this->mysqlTypes['numerical']); |
2257
|
|
|
$colInfo['is_blob'] = in_array($colInfo['type'], $this->mysqlTypes['blob']); |
|
|
|
|
2258
|
|
|
// for virtual columns that are of type 'Extra', column type |
|
|
|
|
2259
|
|
|
// could by "STORED GENERATED" or "VIRTUAL GENERATED" |
2260
|
|
|
// MySQL reference: https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html |
|
|
|
|
2261
|
|
|
$colInfo['is_virtual'] = strpos($colType['Extra'], "VIRTUAL GENERATED") !== false || strpos($colType['Extra'], "STORED GENERATED") !== false; |
|
|
|
|
2262
|
|
|
|
2263
|
|
|
return $colInfo; |
2264
|
|
|
} |
|
|
|
|
2265
|
|
|
|
2266
|
|
|
public function backup_parameters() |
|
|
|
|
2267
|
|
|
{ |
|
|
|
|
2268
|
|
|
$ret = "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;".PHP_EOL. |
|
|
|
|
2269
|
|
|
"/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;".PHP_EOL. |
|
|
|
|
2270
|
|
|
"/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;".PHP_EOL. |
|
|
|
|
2271
|
|
|
"/*!40101 SET NAMES ".$this->dumpSettings['default-character-set']." */;".PHP_EOL; |
|
|
|
|
2272
|
|
|
|
2273
|
|
|
if (false === $this->dumpSettings['skip-tz-utc']) { |
|
|
|
|
2274
|
|
|
$ret .= "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;".PHP_EOL. |
|
|
|
|
2275
|
|
|
"/*!40103 SET TIME_ZONE='+00:00' */;".PHP_EOL; |
2276
|
|
|
} |
2277
|
|
|
|
2278
|
|
|
if ($this->dumpSettings['no-autocommit']) { |
2279
|
|
|
$ret .= "/*!40101 SET @OLD_AUTOCOMMIT=@@AUTOCOMMIT */;".PHP_EOL; |
|
|
|
|
2280
|
|
|
} |
2281
|
|
|
|
2282
|
|
|
$ret .= "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;".PHP_EOL. |
|
|
|
|
2283
|
|
|
"/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;".PHP_EOL. |
|
|
|
|
2284
|
|
|
"/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;".PHP_EOL. |
|
|
|
|
2285
|
|
|
"/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;".PHP_EOL.PHP_EOL; |
|
|
|
|
2286
|
|
|
|
2287
|
|
|
return $ret; |
2288
|
|
|
} |
|
|
|
|
2289
|
|
|
|
2290
|
|
|
public function restore_parameters() |
|
|
|
|
2291
|
|
|
{ |
|
|
|
|
2292
|
|
|
$ret = ""; |
|
|
|
|
2293
|
|
|
|
2294
|
|
|
if (false === $this->dumpSettings['skip-tz-utc']) { |
|
|
|
|
2295
|
|
|
$ret .= "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;".PHP_EOL; |
|
|
|
|
2296
|
|
|
} |
2297
|
|
|
|
2298
|
|
|
if ($this->dumpSettings['no-autocommit']) { |
2299
|
|
|
$ret .= "/*!40101 SET AUTOCOMMIT=@OLD_AUTOCOMMIT */;".PHP_EOL; |
|
|
|
|
2300
|
|
|
} |
2301
|
|
|
|
2302
|
|
|
$ret .= "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;".PHP_EOL. |
|
|
|
|
2303
|
|
|
"/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;".PHP_EOL. |
|
|
|
|
2304
|
|
|
"/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;".PHP_EOL. |
|
|
|
|
2305
|
|
|
"/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;".PHP_EOL. |
|
|
|
|
2306
|
|
|
"/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;".PHP_EOL. |
|
|
|
|
2307
|
|
|
"/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;".PHP_EOL. |
|
|
|
|
2308
|
|
|
"/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;".PHP_EOL.PHP_EOL; |
|
|
|
|
2309
|
|
|
|
2310
|
|
|
return $ret; |
2311
|
|
|
} |
|
|
|
|
2312
|
|
|
|
2313
|
|
|
/** |
2314
|
|
|
* Check number of parameters passed to function, useful when inheriting. |
2315
|
|
|
* Raise exception if unexpected. |
2316
|
|
|
* |
2317
|
|
|
* @param integer $num_args |
|
|
|
|
2318
|
|
|
* @param integer $expected_num_args |
|
|
|
|
2319
|
|
|
* @param string $method_name |
|
|
|
|
2320
|
|
|
*/ |
|
|
|
|
2321
|
|
|
private function check_parameters($num_args, $expected_num_args, $method_name) |
|
|
|
|
2322
|
|
|
{ |
|
|
|
|
2323
|
|
|
if ($num_args != $expected_num_args) { |
|
|
|
|
2324
|
|
|
throw new Exception("Unexpected parameter passed to $method_name"); |
|
|
|
|
2325
|
|
|
} |
|
|
|
|
2326
|
|
|
return; |
|
|
|
|
2327
|
|
|
} |
|
|
|
|
2328
|
|
|
} |
|
|
|
|
2329
|
|
|
|