Total Complexity | 113 |
Total Lines | 738 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Schema often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Schema, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class Schema |
||
48 | { |
||
49 | public const TYPE_INTEGER = 'integer'; |
||
50 | public const TYPE_FLOAT = 'float'; |
||
51 | public const TYPE_DECIMAL = 'decimal'; |
||
52 | public const TYPE_STRING = 'string'; |
||
53 | public const TYPE_TEXT = 'text'; |
||
54 | public const TYPE_DATE = 'date'; |
||
55 | public const TYPE_TIME = 'time'; |
||
56 | public const TYPE_DATETIME = 'datetime'; |
||
57 | public const TYPE_BOOLEAN = 'bool'; |
||
58 | |||
59 | public const YAML_CACHE_TABLES_DIR = 'models'; |
||
60 | |||
61 | /** |
||
62 | * Carriage Return and Line Feed |
||
63 | */ |
||
64 | const CRLF = "\r\n"; |
||
65 | const DB_INDEX_TYPE = 'bigint (20) unsigned'; |
||
66 | |||
67 | public static array $tables = []; |
||
68 | |||
69 | /** |
||
70 | * Contains the database structure data. |
||
71 | * Each table is an index of the associative array. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | public static array $bbddStructure; |
||
76 | |||
77 | public static function checkDatabaseStructure() |
||
78 | { |
||
79 | // Se obtiene la relación de tablas definidas para la base de datos |
||
80 | if (empty(self::$tables)) { |
||
81 | Schema::getTables(); |
||
82 | } |
||
83 | |||
84 | foreach (self::$tables as $key=>$table) { |
||
85 | dump("Verificando la tabla $key, definida en $table."); |
||
86 | static::checkTable($key); |
||
87 | } |
||
88 | die(); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Return true if $tableName exists in database |
||
93 | * |
||
94 | * @param string $tableName |
||
95 | * |
||
96 | * @return bool |
||
97 | * @throws DebugBarException |
||
98 | */ |
||
99 | public static function tableExists($tableName): bool |
||
100 | { |
||
101 | $tableNameWithPrefix = Config::$dbPrefix . $tableName; |
||
102 | $dbName = Config::$dbName; |
||
103 | $sql = "SELECT COUNT(*) AS Total FROM information_schema.tables WHERE table_schema = '{$dbName}' AND table_name='{$tableNameWithPrefix}'"; |
||
104 | |||
105 | $data = Engine::select($sql); |
||
106 | $result = reset($data); |
||
107 | |||
108 | return $result['Total'] === '1'; |
||
109 | } |
||
110 | |||
111 | private static function getFieldsAndIndexes($tableName):array |
||
112 | { |
||
113 | $yamlSourceFilename = self::$tables[$tableName]; |
||
114 | if (!file_exists($yamlSourceFilename)) { |
||
115 | dump('No existe el archivo ' . $yamlSourceFilename); |
||
116 | } |
||
117 | |||
118 | $data = Yaml::parseFile($yamlSourceFilename); |
||
119 | |||
120 | $result = []; |
||
121 | foreach ($data['fields']??[] as $key => $datum) { |
||
122 | $datum['key'] = $key; |
||
123 | $result['fields'][$key] = Schema::normalize($datum); |
||
124 | if ($result['fields'][$key]['type']==='autoincrement') { |
||
125 | // TODO: Ver cómo tendría que ser la primary key |
||
126 | $result['indexes']['primary'] = $key; |
||
127 | } |
||
128 | } |
||
129 | foreach ($data['indexes']??[] as $key=>$datum) { |
||
130 | $datum['key'] = $key; |
||
131 | $result['indexes'][$key] = $datum; |
||
132 | } |
||
133 | |||
134 | /* |
||
135 | Igual conviene crear una clase: |
||
136 | - DBSchema (con los datos de la base de datos real) |
||
137 | - DefinedSchema (con los datos definidos) |
||
138 | y que Schema cree o adapte según los datos de ambas. Que cada una lleve lo suyo |
||
139 | |||
140 | Que el resultado se guarde en el yaml y que se encargue de realizar las conversines |
||
141 | oportunas siempre que no suponga una pérdida de datos. |
||
142 | */ |
||
143 | |||
144 | return $result; |
||
145 | } |
||
146 | |||
147 | private static function getFields($tableName): array |
||
148 | { |
||
149 | $yamlSourceFilename = self::$tables[$tableName]; |
||
150 | if (!file_exists($yamlSourceFilename)) { |
||
151 | dump('No existe el archivo ' . $yamlSourceFilename); |
||
152 | } |
||
153 | |||
154 | $data = Yaml::parseFile($yamlSourceFilename); |
||
155 | |||
156 | $result = []; |
||
157 | foreach ($data as $key => $datum) { |
||
158 | $datum['key'] = $key; |
||
159 | $result[$key] = Schema::normalize($datum); |
||
160 | } |
||
161 | |||
162 | /* |
||
163 | Igual conviene crear una clase: |
||
164 | - DBSchema (con los datos de la base de datos real) |
||
165 | - DefinedSchema (con los datos definidos) |
||
166 | y que Schema cree o adapte según los datos de ambas. Que cada una lleve lo suyo |
||
167 | |||
168 | Que el resultado se guarde en el yaml y que se encargue de realizar las conversines |
||
169 | oportunas siempre que no suponga una pérdida de datos. |
||
170 | */ |
||
171 | |||
172 | return $result; |
||
173 | } |
||
174 | |||
175 | private static function getIndexes($tableName): array |
||
176 | { |
||
177 | $result = []; |
||
178 | return $result; |
||
179 | } |
||
180 | |||
181 | private static function getRelated($tableName): array |
||
182 | { |
||
183 | $result = []; |
||
184 | return $result; |
||
185 | } |
||
186 | |||
187 | private static function getSeed($tableName): array |
||
188 | { |
||
189 | $result = []; |
||
190 | return $result; |
||
191 | } |
||
192 | |||
193 | private static function checkTable(string $tableName, bool $create=true): array |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Comprueba la estructura de la tabla y la crea si no existe y así se solicita. |
||
207 | * Si los datos de la estructura no están en la caché, los regenera y almacena. |
||
208 | * Al regenerar los datos para la caché, también realiza una verificación de |
||
209 | * la estructura por si hay cambios que aplicar en la misma. |
||
210 | * |
||
211 | * TODO: Es mejor que haya un checkStructure que genere TODAS las tablas e índices |
||
212 | * Ese checkstructure se debe de generar tras limpiar caché. |
||
213 | * La caché deberá de limpiarse cada vez que se active o desactive un módulo. |
||
214 | * El último paso de la generación de tablas, sería comprobar las dependencias |
||
215 | * de tablas para saber cuántas tablas usan una constraint de cada tabla para poder |
||
216 | * realizar cambios en la base de datos y tener una visión más nítida de la misma en |
||
217 | * cualquier momento, si bien, esa estructura no será clara hasta que no se hayan leído |
||
218 | * todas, y si hay un cambio entre medias, pues igual la única solución viable es |
||
219 | * determinarlo por la propia base de datos. |
||
220 | * |
||
221 | * @author Rafael San José Tovar <[email protected]> |
||
222 | * @version 2023.0105 |
||
223 | * |
||
224 | * @param string $tableName |
||
225 | * @param bool $create |
||
226 | * |
||
227 | * @return bool |
||
228 | */ |
||
229 | public static function checkStructure(string $tableName, bool $create = true): bool |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Obtiene el tipo genérico del tipo de dato que se le ha pasado. |
||
259 | * |
||
260 | * @author Rafael San José Tovar <[email protected]> |
||
261 | * @version 2023.0101 |
||
262 | * |
||
263 | * @param string $type |
||
264 | * |
||
265 | * @return string |
||
266 | */ |
||
267 | public static function getTypeOf(string $type): string |
||
268 | { |
||
269 | foreach (DB::getDataTypes() as $index => $types) { |
||
270 | if (in_array(strtolower($type), $types)) { |
||
271 | return $index; |
||
272 | } |
||
273 | } |
||
274 | Debug::addMessage('messages', $type . ' not found in DBSchema::getTypeOf()'); |
||
275 | return 'text'; |
||
276 | } |
||
277 | |||
278 | private static function splitType(string $originalType): array |
||
279 | { |
||
280 | $replacesSources = [ |
||
281 | 'character varying', |
||
282 | // 'timestamp without time zone', |
||
283 | 'double precision', |
||
284 | ]; |
||
285 | $replacesDestination = [ |
||
286 | 'varchar', |
||
287 | // 'timestamp', |
||
288 | 'double', |
||
289 | ]; |
||
290 | $modifiedType = (str_replace($replacesSources, $replacesDestination, $originalType)); |
||
291 | |||
292 | if ($originalType !== $modifiedType) { |
||
293 | Debug::addMessage('messages', "XML: Uso de '{$originalType}' en lugar de '{$modifiedType}'."); |
||
294 | } |
||
295 | $explode = explode(' ', strtolower($modifiedType)); |
||
296 | |||
297 | $pos = strpos($explode[0], '('); |
||
298 | if ($pos > 0) { |
||
299 | $begin = $pos + 1; |
||
300 | $end = strpos($explode[0], ')'); |
||
301 | $type = substr($explode[0], 0, $pos); |
||
302 | $length = substr($explode[0], $begin, $end - $begin); |
||
303 | } else { |
||
304 | $type = $explode[0]; |
||
305 | $length = null; |
||
306 | } |
||
307 | |||
308 | $pos = array_search('unsigned', $explode, true); |
||
309 | $unsigned = $pos ? 'yes' : 'no'; |
||
310 | |||
311 | $pos = array_search('zerofill', $explode, true); |
||
312 | $zerofill = $pos ? 'yes' : 'no'; |
||
313 | |||
314 | return ['type' => $type, 'length' => $length, 'unsigned' => $unsigned, 'zerofill' => $zerofill]; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Toma los datos del fichero de definición de una tabla y genera el definitivo. |
||
319 | * |
||
320 | * @author Rafael San José Tovar <[email protected]> |
||
321 | * @version 2022.1224 |
||
322 | * |
||
323 | * @param array $structure |
||
324 | * |
||
325 | * @return array |
||
326 | */ |
||
327 | protected static function normalize(array $structure): array |
||
328 | { |
||
329 | $column = []; |
||
330 | $key = (string) $structure['key']; |
||
331 | $type = (string) $structure['type']; |
||
332 | $column['key'] = $key; |
||
333 | |||
334 | /** |
||
335 | * Entrada: |
||
336 | * - type es el tipo lógico del campo y tiene que estar definido como índice en |
||
337 | * TYPES, o ser uno de los predefinidos como 'autoincrement', 'relationship', etc. |
||
338 | * |
||
339 | * Salida: |
||
340 | * - type queda intacto. |
||
341 | * - dbtype es como queda definido en la tabla, por ejemplo, varchar(20) |
||
342 | * - realtype es el tipo resultado, por ejemplo varchar (sin el tamaño) |
||
343 | * - generictype es uno de los índices de TYPE. P.E. autoincrement se cambiará por integer |
||
344 | * |
||
345 | */ |
||
346 | |||
347 | $column['type'] = $type; |
||
348 | switch ($type) { |
||
349 | case 'autoincrement': |
||
350 | case 'relationship': |
||
351 | $colType = self::DB_INDEX_TYPE; |
||
352 | break; |
||
353 | case 'boolean': |
||
354 | $colType = 'tinyint(1) unsigned'; |
||
355 | break; |
||
356 | default: |
||
357 | $colType = $type; |
||
358 | } |
||
359 | |||
360 | $typeArray = static::splitType($colType); |
||
361 | /** |
||
362 | * ^ array:4 [▼ |
||
363 | * "type" => "bigint" |
||
364 | * "length" => null |
||
365 | * "unsigned" => "yes" |
||
366 | * "zerofill" => "no" |
||
367 | * ] |
||
368 | */ |
||
369 | $type = $typeArray['type']; |
||
370 | $length = $typeArray['length'] ?? $structure['length']; |
||
371 | $unsigned = $typeArray['unsigned'] === 'yes'; |
||
372 | $zerofill = $typeArray['zerofill'] === 'yes'; |
||
373 | $genericType = static::getTypeOf($type); |
||
374 | |||
375 | $column['dbtype'] = $colType; |
||
376 | $column['realtype'] = $type; |
||
377 | $column['generictype'] = $genericType; |
||
378 | |||
379 | $column['null'] = 'YES'; |
||
380 | if ($structure['null'] && mb_strtolower($structure['null']) == 'no') { |
||
381 | $column['null'] = 'NO'; |
||
382 | } |
||
383 | |||
384 | if (empty($structure['default'])) { |
||
385 | $column['default'] = null; |
||
386 | } else { |
||
387 | $column['default'] = (string) $structure['default']; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Pueden existir otras definiciones de limitaciones físicas como min y max |
||
392 | * De existir, tienen que ser contempladas en el método test y tener mayor peso que |
||
393 | * la limitación en plantilla. |
||
394 | */ |
||
395 | foreach (['min', 'max'] as $field) { |
||
396 | if (isset($structure[$field])) { |
||
397 | $column[$field] = (string) $structure[$field]; |
||
398 | } |
||
399 | } |
||
400 | |||
401 | if (isset($structure['comment'])) { |
||
402 | $column['comentario'] = (string) $structure['comment']; |
||
403 | } |
||
404 | |||
405 | if (isset($structure['default'])) { |
||
406 | $column['default'] = trim($structure['default'], " \"'`"); |
||
407 | } |
||
408 | |||
409 | switch ($genericType) { |
||
410 | case 'text': |
||
411 | $column['dbtype'] = 'varchar(' . $length . ')'; |
||
412 | $column['maxlength'] = $length; |
||
413 | break; |
||
414 | case 'integer': |
||
415 | /** |
||
416 | * Lo primero es ver la capacidad física máxima según el tipo de dato. |
||
417 | */ |
||
418 | $bytes = 4; |
||
419 | switch ($type) { |
||
420 | case 'tinyint': |
||
421 | $bytes = 1; |
||
422 | break; |
||
423 | case 'smallint': |
||
424 | $bytes = 2; |
||
425 | break; |
||
426 | case 'mediumint': |
||
427 | $bytes = 3; |
||
428 | break; |
||
429 | case 'int': |
||
430 | $bytes = 4; |
||
431 | break; |
||
432 | case 'bigint': |
||
433 | $bytes = 8; |
||
434 | break; |
||
435 | } |
||
436 | $bits = 8 * (int) $bytes; |
||
437 | $physicalMaxLength = 2 ** $bits; |
||
438 | |||
439 | /** |
||
440 | * $minDataLength y $maxDataLength contendrán el mínimo y máximo valor que puede contener el campo. |
||
441 | */ |
||
442 | $minDataLength = $unsigned ? 0 : -$physicalMaxLength / 2; |
||
443 | $maxDataLength = ($unsigned ? $physicalMaxLength : $physicalMaxLength / 2) - 1; |
||
444 | |||
445 | /** |
||
446 | * De momento, se asignan los límites máximos por el tipo de dato. |
||
447 | * En $min y $max, iremos arrastrando los límites conforme se vayan comprobando. |
||
448 | * $min nunca podrá ser menor que $minDataLength. |
||
449 | * $max nunca podrá ser mayor que $maxDataLength. |
||
450 | */ |
||
451 | $min = $minDataLength; |
||
452 | $max = $maxDataLength; |
||
453 | |||
454 | /** |
||
455 | * Se puede hacer una limitación física Se puede haber definido en el xml un min y un max. |
||
456 | * A todos los efectos, lo definido en el XML como min o max se toma como limitación |
||
457 | * física del campo. |
||
458 | */ |
||
459 | if (isset($structure['min'])) { |
||
460 | $minXmlLength = $structure['min']; |
||
461 | if ($minXmlLength > $minDataLength) { |
||
462 | $min = $minXmlLength; |
||
463 | } else { |
||
464 | Debug::addMessage('messages', "({$key}): Se ha especificado un min {$minXmlLength} en el XML, pero por el tipo de datos, el mínimo es {$minDataLength}."); |
||
465 | } |
||
466 | } |
||
467 | if (isset($structure['max'])) { |
||
468 | $maxXmlLength = $structure['max']; |
||
469 | if ($maxXmlLength < $maxDataLength) { |
||
470 | $max = $maxXmlLength; |
||
471 | } else { |
||
472 | Debug::addMessage('messages', "({$key}): Se ha especificado un min {$maxXmlLength} en el XML, pero por el tipo de datos, el máximo es {$maxDataLength}."); |
||
473 | } |
||
474 | } |
||
475 | |||
476 | $column['min'] = $min; |
||
477 | $column['max'] = $max; |
||
478 | break; |
||
479 | default: |
||
480 | // ??? |
||
481 | } |
||
482 | |||
483 | return $column; |
||
484 | } |
||
485 | |||
486 | private static function getTables() |
||
489 | } |
||
490 | |||
491 | public function compare_columns($table_name, $xml_cols, $db_cols) |
||
583 | } |
||
584 | |||
585 | /** |
||
586 | * Create a table in the database. |
||
587 | * Build the default fields, indexes and values defined in the model. |
||
588 | * |
||
589 | * @param string $tableName |
||
590 | * |
||
591 | * @return bool |
||
592 | * @throws DebugBarException |
||
593 | */ |
||
594 | |||
595 | public static function createTable(string $tableName): bool |
||
596 | { |
||
597 | $tabla = self::$bbddStructure[$tableName]; |
||
598 | $sql = self::createFields($tableName, $tabla['fields']); |
||
599 | |||
600 | foreach ($tabla['keys'] as $name => $index) { |
||
601 | $sql .= self::createIndex($tableName, $name, $index); |
||
602 | } |
||
603 | if (isset($tabla['values'])) { |
||
604 | $sql .= self::setValues($tableName, $tabla['values']); |
||
605 | } |
||
606 | |||
607 | dump($sql); |
||
608 | die('?'); |
||
609 | |||
610 | return Engine::exec($sql); |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * Build the SQL statement to create the fields in the table. |
||
615 | * It can also create the primary key if the auto_increment attribute is defined. |
||
616 | * |
||
617 | * @param string $tablename |
||
618 | * @param array $fieldList |
||
619 | * |
||
620 | * @return string |
||
621 | */ |
||
622 | protected static function createFields(string $tablename, array $fieldList): string |
||
623 | { |
||
624 | $tablenameWithPrefix = Config::$dbPrefix . $tablename; |
||
625 | |||
626 | $sql = "CREATE TABLE $tablenameWithPrefix ( "; |
||
627 | foreach ($fieldList as $index => $col) { |
||
628 | if (!isset($col['dbtype'])) { |
||
629 | die('Tipo no especificado en createTable ' . $index); |
||
630 | } |
||
631 | |||
632 | $sql .= '`' . $index . '` ' . $col['dbtype']; |
||
633 | $nulo = isset($col['null']) && $col['null']; |
||
634 | |||
635 | $sql .= ($nulo ? '' : ' NOT') . ' NULL'; |
||
636 | |||
637 | if (isset($col['extra']) && (strtolower($col['extra']) == 'auto_increment')) { |
||
638 | $sql .= ' PRIMARY KEY AUTO_INCREMENT'; |
||
639 | } |
||
640 | |||
641 | $tmpDefecto = $col['default'] ?? null; |
||
642 | $defecto = ''; |
||
643 | if (isset($tmpDefecto)) { |
||
644 | if ($tmpDefecto == 'CURRENT_TIMESTAMP') { |
||
645 | $defecto = "$tmpDefecto"; |
||
646 | } else { |
||
647 | $defecto = "'$tmpDefecto'"; |
||
648 | } |
||
649 | } else { |
||
650 | if ($nulo) { |
||
651 | $defecto = 'NULL'; |
||
652 | } |
||
653 | } |
||
654 | |||
655 | if ($defecto != '') { |
||
656 | $sql .= ' DEFAULT ' . $defecto; |
||
657 | } |
||
658 | |||
659 | $sql .= ', '; |
||
660 | } |
||
661 | $sql = substr($sql, 0, -2); // Quitamos la coma y el espacio del final |
||
662 | $sql .= ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;' . self::CRLF; |
||
663 | |||
664 | return $sql; |
||
665 | } |
||
666 | |||
667 | /** |
||
668 | * Create the SQL statements for the construction of one index. |
||
669 | * In the case of the primary index, it is not necessary if it is auto_increment. |
||
670 | * |
||
671 | * TODO: |
||
672 | * |
||
673 | * Moreover, it should not be defined if it is auto_increment because it would |
||
674 | * generate an error when it already exists. |
||
675 | * |
||
676 | * @param string $tableName |
||
677 | * @param string $indexname |
||
678 | * @param array $indexData |
||
679 | * |
||
680 | * @return string |
||
681 | */ |
||
682 | protected static function createIndex($tableName, $indexname, $indexData) |
||
683 | { |
||
684 | $sql = "ALTER TABLE $tableName ADD CONSTRAINT $indexname "; |
||
685 | |||
686 | $command = ''; |
||
687 | // https://www.w3schools.com/sql/sql_primarykey.asp |
||
688 | // ALTER TABLE Persons ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName); |
||
689 | if (isset($indexData['PRIMARY'])) { |
||
690 | $command = 'PRIMARY KEY '; |
||
691 | $fields = $indexData['PRIMARY']; |
||
692 | } |
||
693 | |||
694 | // https://www.w3schools.com/sql/sql_create_index.asp |
||
695 | // CREATE INDEX idx_pname ON Persons (LastName, FirstName); |
||
696 | if (isset($indexData['INDEX'])) { |
||
697 | $command = 'INDEX '; |
||
698 | $fields = $indexData['INDEX']; |
||
699 | } |
||
700 | |||
701 | // https://www.w3schools.com/sql/sql_unique.asp |
||
702 | // ALTER TABLE Persons ADD CONSTRAINT UC_Person UNIQUE (ID,LastName); |
||
703 | if (isset($indexData['UNIQUE'])) { |
||
704 | $command = 'UNIQUE INDEX '; |
||
705 | $fields = $indexData['UNIQUE']; |
||
706 | } |
||
707 | |||
708 | if ($command == '') { |
||
709 | // https://www.w3schools.com/sql/sql_foreignkey.asp |
||
710 | // ALTER TABLE Orders ADD CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID) REFERENCES Persons(PersonID); |
||
711 | if (isset($indexData['FOREIGN'])) { |
||
712 | $command = 'FOREIGN KEY '; |
||
713 | $foreignField = $indexData['FOREIGN']; |
||
714 | if (isset($indexData['REFERENCES'])) { |
||
715 | $references = $indexData['REFERENCES']; |
||
716 | if (!is_array($references)) { |
||
717 | die('Esperaba un array en REFERENCES: ' . $tableName . '/' . $indexname); |
||
718 | } |
||
719 | if (count($references) != 1) { |
||
720 | die('Esperaba un array de 1 elemento en REFERENCES: ' . $tableName . '/' . $indexname); |
||
721 | } |
||
722 | $refTable = key($references); |
||
723 | $fields = '(' . implode(',', $references) . ')'; |
||
724 | } else { |
||
725 | die('FOREIGN necesita REFERENCES en ' . $tableName . '/' . $indexname); |
||
726 | } |
||
727 | |||
728 | $sql .= $command . ' ' . $foreignField . ' REFERENCES ' . $refTable . $fields; |
||
729 | |||
730 | if (isset($indexData['ON']) && is_array($indexData['ON'])) { |
||
731 | foreach ($indexData['ON'] as $key => $value) { |
||
732 | $sql .= ' ON ' . $key . ' ' . $value . ', '; |
||
733 | } |
||
734 | $sql = substr($sql, 0, -2); // Quitamos el ', ' de detrás |
||
735 | } |
||
736 | } |
||
737 | } else { |
||
738 | if (is_array($fields)) { |
||
739 | $fields = '(' . implode(',', $fields) . ')'; |
||
740 | } else { |
||
741 | $fields = "($fields)"; |
||
742 | } |
||
743 | |||
744 | if ($command == 'INDEX ') { |
||
745 | $sql = "CREATE INDEX {$indexname} ON {$tableName}" . $fields; |
||
746 | } else { |
||
747 | $sql .= $command . ' ' . $fields; |
||
748 | } |
||
749 | } |
||
750 | |||
751 | return $sql . ';' . self::CRLF; |
||
752 | } |
||
753 | |||
754 | /** |
||
755 | * Create the SQL statements to fill the table with default data. |
||
756 | * |
||
757 | * @param string $tableName |
||
758 | * @param array $values |
||
759 | * |
||
760 | * @return string |
||
761 | */ |
||
762 | protected static function setValues(string $tableName, array $values): string |
||
785 | } |
||
786 | } |
||
787 |