| Total Complexity | 63 |
| Total Lines | 440 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SqlMySql 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 SqlMySql, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class SqlMySql extends SqlHelper |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Retorna las comillas que encierran al nombre de la tabla en una consulta SQL. |
||
| 31 | * |
||
| 32 | * @author Rafael San José Tovar <[email protected]> |
||
| 33 | * @version 2023.0108 |
||
| 34 | * |
||
| 35 | * @return string |
||
| 36 | */ |
||
| 37 | public static function getTableQuote(): string |
||
| 38 | { |
||
| 39 | return '`'; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Retorna las comillas que encierran al nombre de un campo en una consulta SQL |
||
| 44 | * |
||
| 45 | * @author Rafael San José Tovar <[email protected]> |
||
| 46 | * @version 2023.0108 |
||
| 47 | * |
||
| 48 | * @return string |
||
| 49 | */ |
||
| 50 | public static function getFieldQuote(): string |
||
| 51 | { |
||
| 52 | return '"'; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Retorna true si la tabla existe en la base de datos. |
||
| 57 | * |
||
| 58 | * @author Rafael San José Tovar <[email protected]> |
||
| 59 | * @version 2023.0106 |
||
| 60 | * |
||
| 61 | * @param string $tableName |
||
| 62 | * |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | public static function tableExists(string $tableName): bool |
||
| 66 | { |
||
| 67 | $dbName = DB::$dbName; |
||
| 68 | $sql = "SELECT COUNT(*) AS Total FROM information_schema.tables WHERE table_schema = '{$dbName}' AND table_name='{$tableName}'"; |
||
| 69 | |||
| 70 | $data = DB::select($sql); |
||
| 71 | $result = reset($data); |
||
| 72 | |||
| 73 | return $result['Total'] === '1'; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Retorna un array con la asociación de tipos del motor SQL para cada tipo definido |
||
| 78 | * en el Schema. |
||
| 79 | * |
||
| 80 | * @author Rafael San José Tovar <[email protected]> |
||
| 81 | * @version 2023.0108 |
||
| 82 | * |
||
| 83 | * @return array[] |
||
| 84 | */ |
||
| 85 | public static function getDataTypes(): array |
||
| 97 | ]; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Retorna un array con el nombre de todas las tablas de la base de datos. |
||
| 102 | * |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | public static function getTables(): array |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Retorna el tipo de dato que se utiliza para los índices autoincrementados |
||
| 113 | * |
||
| 114 | * @author Rafael San José Tovar <[email protected]> |
||
| 115 | * @version 2023.0108 |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public static function getIndexType(): string |
||
| 122 | } |
||
| 123 | |||
| 124 | public static function getIntegerMinMax(int $size, bool $unsigned): array |
||
| 125 | { |
||
| 126 | switch ($size) { |
||
| 127 | case 1: |
||
| 128 | $type = 'tinyint'; |
||
| 129 | break; |
||
| 130 | case 2: |
||
| 131 | $type = 'smallint'; |
||
| 132 | break; |
||
| 133 | case 3: |
||
| 134 | $type = 'mediumint'; |
||
| 135 | break; |
||
| 136 | case 4: |
||
| 137 | $type = 'int'; |
||
| 138 | break; |
||
| 139 | default: |
||
| 140 | $type = 'bigint'; |
||
| 141 | $size = 8; |
||
| 142 | break; |
||
| 143 | } |
||
| 144 | |||
| 145 | $bits = 8 * (int) $size; |
||
| 146 | $physicalMaxLength = 2 ** $bits; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * $minDataLength y $maxDataLength contendrán el mínimo y máximo valor que puede contener el campo. |
||
| 150 | */ |
||
| 151 | $minDataLength = $unsigned ? 0 : -$physicalMaxLength / 2; |
||
| 152 | $maxDataLength = ($unsigned ? $physicalMaxLength : $physicalMaxLength / 2) - 1; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * De momento, se asignan los límites máximos por el tipo de dato. |
||
| 156 | * En $min y $max, iremos arrastrando los límites conforme se vayan comprobando. |
||
| 157 | * $min nunca podrá ser menor que $minDataLength. |
||
| 158 | * $max nunca podrá ser mayor que $maxDataLength. |
||
| 159 | */ |
||
| 160 | $min = $minDataLength; |
||
| 161 | $max = $maxDataLength; |
||
| 162 | |||
| 163 | return [ |
||
| 164 | 'dbtype' => $type, |
||
| 165 | 'min' => $min, |
||
| 166 | 'max' => $max, |
||
| 167 | 'size' => $size, |
||
| 168 | 'unsigned' => $unsigned, |
||
| 169 | ]; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Retorna un array asociativo con la información de cada columna de la tabla. |
||
| 174 | * El resultado será dependiente del motor de base de datos. |
||
| 175 | * |
||
| 176 | * @author Rafael San José Tovar <[email protected]> |
||
| 177 | * @version 2023.0108 |
||
| 178 | * |
||
| 179 | * @param string $tableName |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | public static function getColumns(string $tableName): array |
||
| 192 | } |
||
| 193 | |||
| 194 | public static function yamlFieldIntegerToDb(array $data): string |
||
| 195 | { |
||
| 196 | $type = $data['dbtype']; |
||
| 197 | // TODO: Aunque lo que está comentado va, igual no hace falta si al comparar |
||
| 198 | // ignoramos el tamaño a mostrar para los integer |
||
| 199 | |||
| 200 | /* |
||
| 201 | $unsigned = $data['unsigned'] ?? false; |
||
| 202 | switch ($type) { |
||
| 203 | case 'tinyint': |
||
| 204 | return $type . ($unsigned ? '(3) unsigned' : '(4)'); |
||
| 205 | case 'smallint': |
||
| 206 | break; |
||
| 207 | case 'mediumint': |
||
| 208 | break; |
||
| 209 | case 'int': |
||
| 210 | return $type . ($unsigned ? '(10) unsigned' : '(11)'); |
||
| 211 | case 'bigint': |
||
| 212 | $type .= '(20)'; |
||
| 213 | } |
||
| 214 | */ |
||
| 215 | return $type . ($unsigned ? ' unsigned' : ''); |
||
|
|
|||
| 216 | } |
||
| 217 | |||
| 218 | public static function yamlFieldToDb(array $data): array |
||
| 219 | { |
||
| 220 | $unsigned = $data['unsigned'] ?? false; |
||
| 221 | |||
| 222 | $result = []; |
||
| 223 | $result['Field'] = $data['name']; |
||
| 224 | |||
| 225 | $type = $data['dbtype']; |
||
| 226 | switch ($data['generictype']) { |
||
| 227 | case Schema::TYPE_INTEGER: |
||
| 228 | $type = self::yamlFieldIntegerToDb($data); |
||
| 229 | break; |
||
| 230 | case Schema::TYPE_FLOAT: |
||
| 231 | case Schema::TYPE_DECIMAL: |
||
| 232 | case Schema::TYPE_STRING: |
||
| 233 | case Schema::TYPE_TEXT: |
||
| 234 | case Schema::TYPE_DATE: |
||
| 235 | case Schema::TYPE_TIME: |
||
| 236 | case Schema::TYPE_DATETIME: |
||
| 237 | case Schema::TYPE_BOOLEAN: |
||
| 238 | $type = 'tinyint(1)'; |
||
| 239 | break; |
||
| 240 | } |
||
| 241 | $result['Type'] = $type; |
||
| 242 | $result['Null'] = !isset($data['nullable']) || $data['nullable'] ? 'YES' : 'NO'; |
||
| 243 | $result['Key'] = $data['type'] === 'autoincrement' ? 'PRI' : ''; |
||
| 244 | $result['Default'] = $data['default'] ?? null; |
||
| 245 | $result['Extra'] = $data['type'] === 'autoincrement' ? 'auto_increment' : ''; |
||
| 246 | return $result; |
||
| 247 | } |
||
| 248 | |||
| 249 | public static function getSqlField(array $column): string |
||
| 250 | { |
||
| 251 | $field = $column['Field']; |
||
| 252 | $type = $column['Type']; |
||
| 253 | $null = $column['Null']; |
||
| 254 | $key = $column['Key']; |
||
| 255 | $default = $column['Default']; |
||
| 256 | $extra = $column['Extra']; |
||
| 257 | |||
| 258 | $sql = self::quoteTableName($field) . ' ' . $type; |
||
| 259 | $nulo = ($null === 'YES'); |
||
| 260 | if ($extra === 'auto_increment') { |
||
| 261 | $nulo = false; |
||
| 262 | $sql .= ' PRIMARY KEY AUTO_INCREMENT'; |
||
| 263 | } |
||
| 264 | |||
| 265 | $sql .= ($nulo ? '' : ' NOT') . ' NULL'; |
||
| 266 | |||
| 267 | $defecto = ''; |
||
| 268 | if (isset($default)) { |
||
| 269 | if ($default === 'CURRENT_TIMESTAMP') { |
||
| 270 | $defecto = $default; |
||
| 271 | } elseif (is_bool($default)) { |
||
| 272 | $defecto = $default ? 1 : 0; |
||
| 273 | } else { |
||
| 274 | $defecto = "'$defecto'"; |
||
| 275 | } |
||
| 276 | } else { |
||
| 277 | if ($nulo) { |
||
| 278 | $defecto = 'NULL'; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | if (!empty($defecto)) { |
||
| 283 | $sql .= ' DEFAULT ' . $defecto; |
||
| 284 | } |
||
| 285 | return $sql; |
||
| 286 | } |
||
| 287 | |||
| 288 | public static function _dbFieldToSchema(array $data): array |
||
| 289 | { |
||
| 290 | return $data; |
||
| 291 | } |
||
| 292 | |||
| 293 | public static function _dbFieldToYaml(array $data): array |
||
| 294 | { |
||
| 295 | return $data; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Recibiendo un array con los datos de un campo tal y como lo retorna la base de |
||
| 300 | * datos, devuelve la información normalizada para ser utilizada por Schema. |
||
| 301 | * |
||
| 302 | * @author Rafael San José Tovar <[email protected]> |
||
| 303 | * @version 2023.0108 |
||
| 304 | * |
||
| 305 | * @param array $row |
||
| 306 | * |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | public static function _normalizeDbField(array $row): array |
||
| 310 | { |
||
| 311 | $result = []; |
||
| 312 | $result['Field'] = $row['key']; |
||
| 313 | $result['Type'] = $row['type']; |
||
| 314 | $result['Null'] = $row['nullable'] ? 'YES' : 'NO'; |
||
| 315 | $result['Key'] = $row['type'] === 'autoincrement' ? 'PRI' : ''; |
||
| 316 | $result['Default'] = $row['default'] ?? null; |
||
| 317 | $result['Extra'] = $row['type'] === 'autoincrement' ? 'auto_increment' : ''; |
||
| 318 | return $result; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Divide the data type of a MySQL field into its various components: type, |
||
| 323 | * length, unsigned or zerofill, if applicable. |
||
| 324 | * |
||
| 325 | * @param string $originalType |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | private static function _splitType(string $originalType): array |
||
| 330 | { |
||
| 331 | $explode = explode(' ', strtolower($originalType)); |
||
| 332 | |||
| 333 | $pos = strpos($explode[0], '('); |
||
| 334 | |||
| 335 | $type = $pos ? substr($explode[0], 0, $pos) : $explode[0]; |
||
| 336 | $length = $pos ? intval(substr($explode[0], $pos + 1)) : null; |
||
| 337 | |||
| 338 | $pos = array_search('unsigned', $explode); |
||
| 339 | $unsigned = $pos ? 'unsigned' : null; |
||
| 340 | |||
| 341 | $pos = array_search('zerofill', $explode); |
||
| 342 | $zerofill = $pos ? 'zerofill' : null; |
||
| 343 | |||
| 344 | return ['type' => $type, 'length' => $length, 'unsigned' => $unsigned, 'zerofill' => $zerofill]; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns an array with the index information, and if there are, also constraints. |
||
| 349 | * |
||
| 350 | * @param array $row |
||
| 351 | * |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | public function _normalizeIndexes(array $row): array |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * The data about the constraint that is found in the KEY_COLUMN_USAGE table |
||
| 378 | * is returned. |
||
| 379 | * Attempting to return the consolidated data generates an extremely slow query |
||
| 380 | * in some MySQL installations, so 2 additional simple queries are made. |
||
| 381 | * |
||
| 382 | * @param string $tableName |
||
| 383 | * @param string $constraintName |
||
| 384 | * |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | private function _getConstraintData(string $tableName, string $constraintName): array |
||
| 388 | { |
||
| 389 | $dbName = Config::getVar('dbName') ?? 'Unknown'; |
||
| 390 | |||
| 391 | return DB::select(' |
||
| 392 | SELECT |
||
| 393 | TABLE_NAME, |
||
| 394 | COLUMN_NAME, |
||
| 395 | CONSTRAINT_NAME, |
||
| 396 | REFERENCED_TABLE_NAME, |
||
| 397 | REFERENCED_COLUMN_NAME |
||
| 398 | FROM |
||
| 399 | INFORMATION_SCHEMA.KEY_COLUMN_USAGE |
||
| 400 | WHERE |
||
| 401 | TABLE_SCHEMA = ' . $this->quoteFieldName($dbName) . ' AND |
||
| 402 | TABLE_NAME = ' . $this->quoteFieldName($tableName) . ' AND |
||
| 403 | constraint_name = ' . $this->quoteFieldName($constraintName) . ' AND |
||
| 404 | REFERENCED_COLUMN_NAME IS NOT NULL; |
||
| 405 | '); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * The rules for updating and deleting data with constraint (table |
||
| 410 | * REFERENTIAL_CONSTRAINTS) are returned. |
||
| 411 | * Attempting to return the consolidated data generates an extremely slow query |
||
| 412 | * in some MySQL installations, so 2 additional simple queries are made. |
||
| 413 | * |
||
| 414 | * @param string $tableName |
||
| 415 | * @param string $constraintName |
||
| 416 | * |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | private function _getConstraintRules(string $tableName, string $constraintName): array |
||
| 420 | { |
||
| 421 | $dbName = Config::getVar('dbName') ?? 'Unknown'; |
||
| 422 | |||
| 423 | return DB::selectselect(' |
||
| 424 | SELECT |
||
| 425 | MATCH_OPTION, |
||
| 426 | UPDATE_RULE, |
||
| 427 | DELETE_RULE |
||
| 428 | FROM information_schema.REFERENTIAL_CONSTRAINTS |
||
| 429 | WHERE |
||
| 430 | constraint_schema = ' . $this->quoteFieldName($dbName) . ' AND |
||
| 431 | table_name = ' . $this->quoteFieldName($tableName) . ' AND |
||
| 432 | constraint_name = ' . $this->quoteFieldName($constraintName) . '; |
||
| 433 | '); |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Obtain an array with the basic information about the indexes of the table, |
||
| 438 | * which will be supplemented with the restrictions later. |
||
| 439 | * |
||
| 440 | * @param string $tableName |
||
| 441 | * |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | public function _getIndexesSql(string $tableName): string |
||
| 449 | } |
||
| 450 | |||
| 451 | public static function _modify(string $tableName, array $oldField, array $newField): string |
||
| 467 | } |
||
| 468 | } |
||
| 469 |