@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | |
29 | 29 | private Schema $schema; |
30 | 30 | |
31 | - public function __construct (array $argv, array $opt) { |
|
31 | + public function __construct(array $argv, array $opt) { |
|
32 | 32 | $this->argv = $argv; |
33 | 33 | $opt['connection'] ??= 'default'; |
34 | 34 | $opt['config'] ??= 'db.config.php'; |
@@ -39,15 +39,15 @@ discard block |
||
39 | 39 | $this->schema = $this->db->getSchema(); |
40 | 40 | } |
41 | 41 | |
42 | - private function _stderr (string $text): void { |
|
42 | + private function _stderr(string $text): void { |
|
43 | 43 | fputs(STDERR, "{$text}\n\n"); |
44 | 44 | } |
45 | 45 | |
46 | - private function _stdout (string $text): void { |
|
46 | + private function _stdout(string $text): void { |
|
47 | 47 | echo "{$text}\n\n"; |
48 | 48 | } |
49 | 49 | |
50 | - private function _usage_exit (): void { |
|
50 | + private function _usage_exit(): void { |
|
51 | 51 | $this->_stderr(<<< USAGE |
52 | 52 | |
53 | 53 | $ php {$this->argv[0]} [OPTIONS] ACTION |
@@ -337,13 +337,4 @@ discard block |
||
337 | 337 | */ |
338 | 338 | public function down (\$schema) |
339 | 339 | { |
340 | - {$down} |
|
341 | - } |
|
342 | - |
|
343 | - }; |
|
344 | - |
|
345 | - MIGRATION |
|
346 | - ); |
|
347 | - } |
|
348 | - |
|
349 | -})->_exec(); |
|
340 | + {$down} |
|
350 | 341 | \ No newline at end of file |
@@ -116,13 +116,13 @@ discard block |
||
116 | 116 | */ |
117 | 117 | protected const PHP_TYPES = [ |
118 | 118 | 'bool' => self::T_BOOL, |
119 | - 'boolean' => self::T_BOOL, // gettype() |
|
120 | - 'double' => self::T_FLOAT, // gettype() |
|
119 | + 'boolean' => self::T_BOOL, // gettype() |
|
120 | + 'double' => self::T_FLOAT, // gettype() |
|
121 | 121 | 'float' => self::T_FLOAT, |
122 | 122 | 'int' => self::T_INT, |
123 | - 'integer' => self::T_INT, // gettype() |
|
123 | + 'integer' => self::T_INT, // gettype() |
|
124 | 124 | 'string' => self::T_STRING, |
125 | - 'String' => self::T_TEXT, // @var String |
|
125 | + 'String' => self::T_TEXT, // @var String |
|
126 | 126 | 'STRING' => self::T_BLOB // @var STRING |
127 | 127 | ]; |
128 | 128 | |
@@ -132,14 +132,14 @@ discard block |
||
132 | 132 | */ |
133 | 133 | const PHP_TYPE_NAMES = [ |
134 | 134 | 'bool' => 'T_BOOL', |
135 | - 'boolean' => 'T_BOOL', // gettype() |
|
136 | - 'double' => 'T_BLOB', // gettype() |
|
135 | + 'boolean' => 'T_BOOL', // gettype() |
|
136 | + 'double' => 'T_BLOB', // gettype() |
|
137 | 137 | 'float' => 'T_FLOAT', |
138 | 138 | 'int' => 'T_INT', |
139 | - 'integer' => 'T_INT', // gettype() |
|
139 | + 'integer' => 'T_INT', // gettype() |
|
140 | 140 | 'string' => 'T_STRING', |
141 | - 'String' => 'T_TEXT', // @var String |
|
142 | - 'STRING' => 'T_BLOB', // @var STRING |
|
141 | + 'String' => 'T_TEXT', // @var String |
|
142 | + 'STRING' => 'T_BLOB', // @var STRING |
|
143 | 143 | ]; |
144 | 144 | |
145 | 145 | /** |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | // bool |
150 | 150 | 'BOOLEAN' => 'bool', |
151 | 151 | // int |
152 | - 'BIGINT' => 'int', // mysql |
|
152 | + 'BIGINT' => 'int', // mysql |
|
153 | 153 | 'INTEGER' => 'int', // sqlite (must be this type to allow AUTOINCREMENT) |
154 | 154 | // float |
155 | 155 | 'DOUBLE PRECISION' => 'float', |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | // string <= 64k |
159 | 159 | 'TEXT' => 'String', |
160 | 160 | // string > 64k |
161 | - 'BLOB' => 'STRING', // sqlite |
|
161 | + 'BLOB' => 'STRING', // sqlite |
|
162 | 162 | 'LONGBLOB' => 'STRING', // mysql |
163 | 163 | ]; |
164 | 164 | |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | /** |
221 | 221 | * @param DB $db |
222 | 222 | */ |
223 | - public function __construct (DB $db) { |
|
223 | + public function __construct(DB $db) { |
|
224 | 224 | $this->db = $db; |
225 | 225 | $this->colDefs ??= self::COLUMN_DEFINITIONS[$db->getDriver()]; |
226 | 226 | } |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | * @param int $type |
234 | 234 | * @return $this |
235 | 235 | */ |
236 | - public function addColumn (string $table, string $column, int $type = self::T_STRING) { |
|
236 | + public function addColumn(string $table, string $column, int $type = self::T_STRING) { |
|
237 | 237 | $type = $this->colDefs[$type & self::T_MASK]; |
238 | 238 | $this->db->exec("ALTER TABLE {$table} ADD COLUMN {$column} {$type}"); |
239 | 239 | unset($this->tables[$table]); |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | * @param array[] $constraints `[ <TABLE_CONST> => spec ]` |
260 | 260 | * @return $this |
261 | 261 | */ |
262 | - public function createTable (string $table, array $columns, array $constraints = []) { |
|
262 | + public function createTable(string $table, array $columns, array $constraints = []) { |
|
263 | 263 | $defs = $this->toColumnDefinitions($columns); |
264 | 264 | |
265 | 265 | /** @var string[] $pk */ |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | * @param string $column |
296 | 296 | * @return $this |
297 | 297 | */ |
298 | - public function dropColumn (string $table, string $column) { |
|
298 | + public function dropColumn(string $table, string $column) { |
|
299 | 299 | $this->db->exec("ALTER TABLE {$table} DROP COLUMN {$column}"); |
300 | 300 | unset($this->tables[$table]); |
301 | 301 | return $this; |
@@ -306,7 +306,7 @@ discard block |
||
306 | 306 | * |
307 | 307 | * @param string $table |
308 | 308 | */ |
309 | - public function dropTable (string $table): void { |
|
309 | + public function dropTable(string $table): void { |
|
310 | 310 | $this->db->exec("DROP TABLE IF EXISTS {$table}"); |
311 | 311 | unset($this->tables[$table]); |
312 | 312 | } |
@@ -323,7 +323,7 @@ discard block |
||
323 | 323 | * @param string $column |
324 | 324 | * @return array[] Keyed by name. |
325 | 325 | */ |
326 | - public function getColumnInfo (string $table): array { |
|
326 | + public function getColumnInfo(string $table): array { |
|
327 | 327 | if ($this->db->isSQLite()) { |
328 | 328 | $info = $this->db->query("PRAGMA table_info({$table})")->fetchAll(); |
329 | 329 | return array_combine(array_column($info, 'name'), array_map(fn(array $each) => [ |
@@ -343,7 +343,7 @@ discard block |
||
343 | 343 | /** |
344 | 344 | * @return DB |
345 | 345 | */ |
346 | - public function getDb () { |
|
346 | + public function getDb() { |
|
347 | 347 | return $this->db; |
348 | 348 | } |
349 | 349 | |
@@ -351,7 +351,7 @@ discard block |
||
351 | 351 | * @param string $name |
352 | 352 | * @return null|Table |
353 | 353 | */ |
354 | - public function getTable (string $name) { |
|
354 | + public function getTable(string $name) { |
|
355 | 355 | if (!isset($this->tables[$name])) { |
356 | 356 | if ($this->db->isSQLite()) { |
357 | 357 | $info = $this->db->query("PRAGMA table_info({$name})")->fetchAll(); |
@@ -374,7 +374,7 @@ discard block |
||
374 | 374 | * @param string $table |
375 | 375 | * @return bool |
376 | 376 | */ |
377 | - final public function offsetExists ($table): bool { |
|
377 | + final public function offsetExists($table): bool { |
|
378 | 378 | return (bool)$this->offsetGet($table); |
379 | 379 | } |
380 | 380 | |
@@ -384,7 +384,7 @@ discard block |
||
384 | 384 | * @param string $table |
385 | 385 | * @return null|Table |
386 | 386 | */ |
387 | - public function offsetGet ($table) { |
|
387 | + public function offsetGet($table) { |
|
388 | 388 | return $this->getTable($table); |
389 | 389 | } |
390 | 390 | |
@@ -393,7 +393,7 @@ discard block |
||
393 | 393 | * @param $value |
394 | 394 | * @throws LogicException |
395 | 395 | */ |
396 | - final public function offsetSet ($offset, $value) { |
|
396 | + final public function offsetSet($offset, $value) { |
|
397 | 397 | throw new LogicException('The schema cannot be altered this way.'); |
398 | 398 | } |
399 | 399 | |
@@ -401,7 +401,7 @@ discard block |
||
401 | 401 | * @param $offset |
402 | 402 | * @throws LogicException |
403 | 403 | */ |
404 | - final public function offsetUnset ($offset) { |
|
404 | + final public function offsetUnset($offset) { |
|
405 | 405 | throw new LogicException('The schema cannot be altered this way.'); |
406 | 406 | } |
407 | 407 | |
@@ -413,7 +413,7 @@ discard block |
||
413 | 413 | * @param string $newName |
414 | 414 | * @return $this |
415 | 415 | */ |
416 | - public function renameColumn (string $table, string $oldName, string $newName) { |
|
416 | + public function renameColumn(string $table, string $oldName, string $newName) { |
|
417 | 417 | $this->db->exec("ALTER TABLE {$table} RENAME COLUMN {$oldName} TO {$newName}"); |
418 | 418 | unset($this->tables[$table]); |
419 | 419 | return $this; |
@@ -426,7 +426,7 @@ discard block |
||
426 | 426 | * @param string $newName |
427 | 427 | * @return $this |
428 | 428 | */ |
429 | - public function renameTable (string $oldName, string $newName) { |
|
429 | + public function renameTable(string $oldName, string $newName) { |
|
430 | 430 | $this->db->exec("ALTER TABLE {$oldName} RENAME TO {$newName}"); |
431 | 431 | unset($this->tables[$oldName]); |
432 | 432 | return $this; |
@@ -438,7 +438,7 @@ discard block |
||
438 | 438 | * @param int[] $types |
439 | 439 | * @return int[] |
440 | 440 | */ |
441 | - protected function sortColumns (array $types): array { |
|
441 | + protected function sortColumns(array $types): array { |
|
442 | 442 | uksort($types, function(string $a, string $b) use ($types) { |
443 | 443 | // descending index priority, increasing size, name |
444 | 444 | return $types[$b] <=> $types[$a] ?: $a <=> $b; |
@@ -450,7 +450,7 @@ discard block |
||
450 | 450 | * @param int[] $columns `[ name => <I_CONST> | <T_CONST> ]` |
451 | 451 | * @return string[] |
452 | 452 | */ |
453 | - protected function toColumnDefinitions (array $columns): array { |
|
453 | + protected function toColumnDefinitions(array $columns): array { |
|
454 | 454 | assert(count($columns) > 0); |
455 | 455 | $columns = $this->sortColumns($columns); |
456 | 456 | $defs = []; |
@@ -476,7 +476,7 @@ discard block |
||
476 | 476 | * @param Column $foreign |
477 | 477 | * @return string |
478 | 478 | */ |
479 | - protected function toForeignKeyConstraint (string $table, string $local, int $type, Column $foreign): string { |
|
479 | + protected function toForeignKeyConstraint(string $table, string $local, int $type, Column $foreign): string { |
|
480 | 480 | return sprintf( |
481 | 481 | 'CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s) ON UPDATE CASCADE ON DELETE %s', |
482 | 482 | $this->toForeignKeyConstraint_name($table, $local), |
@@ -494,7 +494,7 @@ discard block |
||
494 | 494 | * @param string $column |
495 | 495 | * @return string |
496 | 496 | */ |
497 | - protected function toForeignKeyConstraint_name (string $table, string $column): string { |
|
497 | + protected function toForeignKeyConstraint_name(string $table, string $column): string { |
|
498 | 498 | return 'FK_' . $table . '__' . $column; |
499 | 499 | } |
500 | 500 | |
@@ -503,7 +503,7 @@ discard block |
||
503 | 503 | * @param string[] $columns |
504 | 504 | * @return string |
505 | 505 | */ |
506 | - protected function toPrimaryKeyConstraint (string $table, array $columns): string { |
|
506 | + protected function toPrimaryKeyConstraint(string $table, array $columns): string { |
|
507 | 507 | return sprintf( |
508 | 508 | 'CONSTRAINT %s PRIMARY KEY (%s)', |
509 | 509 | $this->toPrimaryKeyConstraint_name($table, $columns), |
@@ -518,7 +518,7 @@ discard block |
||
518 | 518 | * @param string[] $columns |
519 | 519 | * @return string |
520 | 520 | */ |
521 | - protected function toPrimaryKeyConstraint_name (string $table, array $columns): string { |
|
521 | + protected function toPrimaryKeyConstraint_name(string $table, array $columns): string { |
|
522 | 522 | sort($columns, SORT_STRING); |
523 | 523 | return 'PK_' . $table . '__' . implode('__', $columns); |
524 | 524 | } |
@@ -528,7 +528,7 @@ discard block |
||
528 | 528 | * @param string[] $columns |
529 | 529 | * @return string |
530 | 530 | */ |
531 | - protected function toUniqueKeyConstraint (string $table, array $columns): string { |
|
531 | + protected function toUniqueKeyConstraint(string $table, array $columns): string { |
|
532 | 532 | return sprintf( |
533 | 533 | 'CONSTRAINT %s UNIQUE (%s)', |
534 | 534 | $this->toUniqueKeyConstraint_name($table, $columns), |
@@ -543,7 +543,7 @@ discard block |
||
543 | 543 | * @param string[] $columns |
544 | 544 | * @return string |
545 | 545 | */ |
546 | - protected function toUniqueKeyConstraint_name (string $table, array $columns): string { |
|
546 | + protected function toUniqueKeyConstraint_name(string $table, array $columns): string { |
|
547 | 547 | sort($columns, SORT_STRING); |
548 | 548 | return 'UQ_' . $table . '__' . implode('__', $columns); |
549 | 549 | } |
@@ -356,8 +356,7 @@ |
||
356 | 356 | if ($this->db->isSQLite()) { |
357 | 357 | $info = $this->db->query("PRAGMA table_info({$name})")->fetchAll(); |
358 | 358 | $cols = array_column($info, 'name'); |
359 | - } |
|
360 | - else { |
|
359 | + } else { |
|
361 | 360 | $cols = $this->db->query("SELECT column_name FROM information_schema.tables WHERE table_name = \"{$name}\"")->fetchAll(DB::FETCH_COLUMN); |
362 | 361 | } |
363 | 362 | if (!$cols) { |