| Total Complexity | 193 |
| Total Lines | 1700 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Driver 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 Driver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 65 | class Driver |
||
| 66 | { |
||
| 67 | /** |
||
| 68 | * The driver default date format |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected string $dateFormat = 'Y-m-d H:i:s'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The quote identifier for a table and columns |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected string $identifier = '"%s"'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Each query separator |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected string $separator = ';'; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The columns modifiers |
||
| 87 | * @var array<string> |
||
| 88 | */ |
||
| 89 | protected array $modifiers = [ |
||
| 90 | 'unsigned', |
||
| 91 | 'nullable', |
||
| 92 | 'default', |
||
| 93 | 'autoincrement', |
||
| 94 | 'description', |
||
| 95 | 'after', |
||
| 96 | ]; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Columns serial |
||
| 100 | * @var array<string> |
||
| 101 | */ |
||
| 102 | protected array $serials = [ |
||
| 103 | 'tiny', |
||
| 104 | 'small', |
||
| 105 | 'normal', |
||
| 106 | 'medium', |
||
| 107 | 'big' |
||
| 108 | ]; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Auto increment value modifier |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | protected string $autoincrement = 'AUTO_INCREMENT'; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * The query parameters |
||
| 118 | * @var array<mixed> |
||
| 119 | */ |
||
| 120 | protected array $params = []; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @class constructor |
||
| 124 | * @param Connection $connection |
||
| 125 | */ |
||
| 126 | public function __construct(protected Connection $connection) |
||
| 127 | { |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns the SQL for SELECT statement |
||
| 132 | * @param QueryStatement $select |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | public function select(QueryStatement $select): string |
||
| 137 | { |
||
| 138 | $sql = $select->hasDistinct() ? 'SELECT DISTINCT ' : 'SELECT '; |
||
| 139 | $sql .= $this->getColumnList($select->getColumns()); |
||
| 140 | $sql .= $this->getInto($select->getIntoTable()); |
||
| 141 | $sql .= ' FROM '; |
||
| 142 | $sql .= $this->getTableList($select->getTables()); |
||
| 143 | $sql .= $this->getJoins($select->getJoins()); |
||
| 144 | $sql .= $this->getWheres($select->getWheres()); |
||
| 145 | $sql .= $this->getGroupBy($select->getGroupBy()); |
||
| 146 | $sql .= $this->getHaving($select->getHaving()); |
||
| 147 | $sql .= $this->getOrders($select->getOrder()); |
||
| 148 | $sql .= $this->getLimit($select->getLimit()); |
||
| 149 | $sql .= $this->getOffset($select->getOffset()); |
||
| 150 | |||
| 151 | return $sql; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Return SQL for INSERT statement |
||
| 156 | * @param QueryStatement $insert |
||
| 157 | * |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function insert(QueryStatement $insert): string |
||
| 161 | { |
||
| 162 | $columns = $this->getColumnList($insert->getColumns()); |
||
| 163 | |||
| 164 | $sql = 'INSERT INTO '; |
||
| 165 | $sql .= $this->getTableList($insert->getTables()); |
||
| 166 | $sql .= ($columns == '*') ? '' : '(' . $columns . ')'; |
||
| 167 | $sql .= $this->getInsertValues($insert->getValues()); |
||
| 168 | |||
| 169 | return $sql; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Return the SQL for UPDATE statement |
||
| 174 | * @param QueryStatement $update |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | public function update(QueryStatement $update): string |
||
| 179 | { |
||
| 180 | $sql = 'UPDATE '; |
||
| 181 | $sql .= $this->getTableList($update->getTables()); |
||
| 182 | $sql .= $this->getJoins($update->getJoins()); |
||
| 183 | $sql .= $this->getSetColumns($update->getColumns()); |
||
| 184 | $sql .= $this->getWheres($update->getWheres()); |
||
| 185 | |||
| 186 | return $sql; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Return the SQL for DELETE statement |
||
| 191 | * @param QueryStatement $delete |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function delete(QueryStatement $delete): string |
||
| 196 | { |
||
| 197 | $sql = 'DELETE ' . $this->getTableList($delete->getTables()); |
||
| 198 | $sql .= $sql === 'DELETE ' ? 'FROM ' : ' FROM '; |
||
| 199 | $sql .= $this->getTableList($delete->getFrom()); |
||
| 200 | $sql .= $this->getJoins($delete->getJoins()); |
||
| 201 | $sql .= $this->getWheres($delete->getWheres()); |
||
| 202 | |||
| 203 | return $sql; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Return the date format |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getDateFormat(): string |
||
| 211 | { |
||
| 212 | return $this->dateFormat; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * |
||
| 217 | * @param string $format |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | public function setDateFormat(string $format): self |
||
| 221 | { |
||
| 222 | $this->dateFormat = $format; |
||
| 223 | |||
| 224 | return $this; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set the drive options |
||
| 229 | * @param array<string, mixed> $options |
||
| 230 | */ |
||
| 231 | public function setOptions(array $options): void |
||
| 232 | { |
||
| 233 | foreach ($options as $name => $value) { |
||
| 234 | $this->{$name} = $value; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param array<mixed> $params |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function params(array $params): string |
||
| 244 | { |
||
| 245 | return implode(', ', array_map([$this, 'param'], $params)); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return array<mixed> |
||
| 250 | */ |
||
| 251 | public function getParams(): array |
||
| 252 | { |
||
| 253 | $params = $this->params; |
||
| 254 | $this->params = []; |
||
| 255 | |||
| 256 | return $params; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param Expression[]|string[] $columns |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | public function columns(array $columns): string |
||
| 265 | { |
||
| 266 | return implode(', ', array_map([$this, 'quoteIdentifier'], $columns)); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $value |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function quote(string $value): string |
||
| 275 | { |
||
| 276 | return "'" . str_replace("'", "''", $value) . "'"; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return the SQL for the current database |
||
| 281 | * @return array<string, mixed> |
||
| 282 | */ |
||
| 283 | public function getDatabaseName(): array |
||
| 284 | { |
||
| 285 | return [ |
||
| 286 | 'sql' => 'SELECT database()', |
||
| 287 | 'params' => [] |
||
| 288 | ]; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * |
||
| 293 | * @param string $current |
||
| 294 | * @param string $new |
||
| 295 | * @return array<string, mixed> |
||
| 296 | */ |
||
| 297 | public function renameTable(string $current, string $new): array |
||
| 298 | { |
||
| 299 | return [ |
||
| 300 | 'sql' => 'RENAME TABLE ' . $this->quoteIdentifier($current) |
||
| 301 | . ' TO ' . $this->quoteIdentifier($new), |
||
| 302 | 'params' => [] |
||
| 303 | ]; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * |
||
| 308 | * @param string $database |
||
| 309 | * @return array<string, mixed> |
||
| 310 | */ |
||
| 311 | public function getTables(string $database): array |
||
| 312 | { |
||
| 313 | $sql = sprintf( |
||
| 314 | 'SELECT %s FROM %s.%s WHERE table_type = ? ' |
||
| 315 | . 'AND table_schema = ? ORDER BY %s ASC', |
||
| 316 | $this->quoteIdentifier('table_name'), |
||
| 317 | $this->quoteIdentifier('information_schema'), |
||
| 318 | $this->quoteIdentifier('tables'), |
||
| 319 | $this->quoteIdentifier('table_name'), |
||
| 320 | ); |
||
| 321 | |||
| 322 | return [ |
||
| 323 | 'sql' => $sql, |
||
| 324 | 'params' => ['BASE TABLE', $database] |
||
| 325 | ]; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * |
||
| 330 | * @param string $database |
||
| 331 | * @return array<string, mixed> |
||
| 332 | */ |
||
| 333 | public function getViews(string $database): array |
||
| 334 | { |
||
| 335 | $sql = sprintf( |
||
| 336 | 'SELECT %s FROM %s.%s WHERE table_type = ? ' |
||
| 337 | . 'AND table_schema = ? ORDER BY %s ASC', |
||
| 338 | $this->quoteIdentifier('table_name'), |
||
| 339 | $this->quoteIdentifier('information_schema'), |
||
| 340 | $this->quoteIdentifier('tables'), |
||
| 341 | $this->quoteIdentifier('table_name'), |
||
| 342 | ); |
||
| 343 | |||
| 344 | return [ |
||
| 345 | 'sql' => $sql, |
||
| 346 | 'params' => ['VIEW', $database] |
||
| 347 | ]; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * |
||
| 352 | * @param string $database |
||
| 353 | * @param string $table |
||
| 354 | * @return array<string, mixed> |
||
| 355 | */ |
||
| 356 | public function getColumns(string $database, string $table): array |
||
| 357 | { |
||
| 358 | $sql = sprintf( |
||
| 359 | 'SELECT %s AS %s, %s AS %s FROM %s.%s WHERE %s = ? ' |
||
| 360 | . 'AND %s = ? ORDER BY %s ASC', |
||
| 361 | $this->quoteIdentifier('column_name'), |
||
| 362 | $this->quoteIdentifier('name'), |
||
| 363 | $this->quoteIdentifier('column_type'), |
||
| 364 | $this->quoteIdentifier('type'), |
||
| 365 | $this->quoteIdentifier('information_schema'), |
||
| 366 | $this->quoteIdentifier('columns'), |
||
| 367 | $this->quoteIdentifier('table_schema'), |
||
| 368 | $this->quoteIdentifier('table_name'), |
||
| 369 | $this->quoteIdentifier('ordinal_position'), |
||
| 370 | ); |
||
| 371 | |||
| 372 | return [ |
||
| 373 | 'sql' => $sql, |
||
| 374 | 'params' => [$database, $table] |
||
| 375 | ]; |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * |
||
| 380 | * @param string $database |
||
| 381 | * @param string $view |
||
| 382 | * @return array<string, mixed> |
||
| 383 | */ |
||
| 384 | public function getViewColumns(string $database, string $view): array |
||
| 385 | { |
||
| 386 | $sql = sprintf( |
||
| 387 | 'SELECT %s AS %s, %s AS %s FROM %s.%s WHERE %s = ? ' |
||
| 388 | . 'AND %s = ? ORDER BY %s ASC', |
||
| 389 | $this->quoteIdentifier('column_name'), |
||
| 390 | $this->quoteIdentifier('name'), |
||
| 391 | $this->quoteIdentifier('column_type'), |
||
| 392 | $this->quoteIdentifier('type'), |
||
| 393 | $this->quoteIdentifier('information_schema'), |
||
| 394 | $this->quoteIdentifier('columns'), |
||
| 395 | $this->quoteIdentifier('table_schema'), |
||
| 396 | $this->quoteIdentifier('table_name'), |
||
| 397 | $this->quoteIdentifier('ordinal_position'), |
||
| 398 | ); |
||
| 399 | |||
| 400 | return [ |
||
| 401 | 'sql' => $sql, |
||
| 402 | 'params' => [$database, $view] |
||
| 403 | ]; |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * |
||
| 408 | * @param CreateTable $schema |
||
| 409 | * @return array<int, array<string, mixed>> |
||
| 410 | */ |
||
| 411 | public function create(CreateTable $schema): array |
||
| 412 | { |
||
| 413 | $sql = 'CREATE TABLE ' . $this->quoteIdentifier($schema->getTableName()); |
||
| 414 | $sql .= "(\n"; |
||
| 415 | $sql .= $this->getSchemaColumns($schema->getColumns()); |
||
| 416 | $sql .= $this->getPrimaryKey($schema); |
||
| 417 | $sql .= $this->getUniqueKeys($schema); |
||
| 418 | $sql .= $this->getForeignKeys($schema); |
||
| 419 | $sql .= ")\n"; |
||
| 420 | $sql .= $this->getEngine($schema); |
||
| 421 | |||
| 422 | $commands = []; |
||
| 423 | |||
| 424 | $commands[] = [ |
||
| 425 | 'sql' => $sql, |
||
| 426 | 'params' => [] |
||
| 427 | ]; |
||
| 428 | |||
| 429 | foreach ($this->getIndexKeys($schema) as $index) { |
||
| 430 | $commands[] = [ |
||
| 431 | 'sql' => $index, |
||
| 432 | 'params' => [] |
||
| 433 | ]; |
||
| 434 | } |
||
| 435 | |||
| 436 | return $commands; |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * |
||
| 441 | * @param AlterTable $schema |
||
| 442 | * @return array<int, array<string, mixed>> |
||
| 443 | */ |
||
| 444 | public function alter(AlterTable $schema): array |
||
| 445 | { |
||
| 446 | $commands = []; |
||
| 447 | |||
| 448 | foreach ($schema->getCommands() as $command) { |
||
| 449 | $callback = 'get' . ucfirst($command['type']); |
||
| 450 | $sql = $this->{$callback}($schema, $command['data']); |
||
| 451 | |||
| 452 | if ($sql === '') { |
||
| 453 | continue; |
||
| 454 | } |
||
| 455 | |||
| 456 | $commands[] = [ |
||
| 457 | 'sql' => $sql, |
||
| 458 | 'params' => $this->getParams() |
||
| 459 | ]; |
||
| 460 | } |
||
| 461 | |||
| 462 | return $commands; |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * |
||
| 467 | * @param string $table |
||
| 468 | * @return array<string, mixed> |
||
| 469 | */ |
||
| 470 | public function drop(string $table): array |
||
| 471 | { |
||
| 472 | return [ |
||
| 473 | 'sql' => 'DROP TABLE ' . $this->quoteIdentifier($table), |
||
| 474 | 'params' => [] |
||
| 475 | ]; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * |
||
| 480 | * @param string $table |
||
| 481 | * @return array<string, mixed> |
||
| 482 | */ |
||
| 483 | public function truncate(string $table): array |
||
| 484 | { |
||
| 485 | return [ |
||
| 486 | 'sql' => 'TRUNCATE TABLE ' . $this->quoteIdentifier($table), |
||
| 487 | 'params' => [] |
||
| 488 | ]; |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Add quote identifier like "", `` |
||
| 493 | * @param string|Expression $value |
||
| 494 | * |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | public function quoteIdentifier(string|Expression $value): string |
||
| 498 | { |
||
| 499 | if ($value instanceof Expression) { |
||
| 500 | return $this->getExpressions($value->getExpressions()); |
||
| 501 | } |
||
| 502 | |||
| 503 | $identifiers = []; |
||
| 504 | |||
| 505 | foreach (explode('.', $value) as $segment) { |
||
| 506 | if ($segment === '*') { |
||
| 507 | $identifiers[] = $segment; |
||
| 508 | } else { |
||
| 509 | $identifiers[] = sprintf($this->identifier, $segment); |
||
| 510 | } |
||
| 511 | } |
||
| 512 | |||
| 513 | return implode('.', $identifiers); |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * |
||
| 518 | * @param array<mixed> $values |
||
| 519 | * @param string $separator |
||
| 520 | * @return string |
||
| 521 | */ |
||
| 522 | public function quoteIdentifiers(array $values, string $separator = ', '): string |
||
| 523 | { |
||
| 524 | return implode($separator, array_map([$this, 'quoteIdentifier'], $values)); |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @param mixed $value |
||
| 529 | * |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | protected function param(mixed $value): string |
||
| 533 | { |
||
| 534 | if ($value instanceof Expression) { |
||
| 535 | return $this->getExpressions($value->getExpressions()); |
||
| 536 | } elseif ($value instanceof DateTime) { |
||
| 537 | $this->params[] = $value->format($this->dateFormat); |
||
| 538 | } else { |
||
| 539 | $this->params[] = $value; |
||
| 540 | } |
||
| 541 | |||
| 542 | return '?'; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get the value by convert it to the type |
||
| 547 | * @param mixed $value |
||
| 548 | * @return mixed |
||
| 549 | * |
||
| 550 | */ |
||
| 551 | protected function value(mixed $value): mixed |
||
| 552 | { |
||
| 553 | if (is_numeric($value)) { |
||
| 554 | return $value; |
||
| 555 | } |
||
| 556 | |||
| 557 | if (is_bool($value)) { |
||
| 558 | return $value ? 1 : 0; |
||
| 559 | } |
||
| 560 | |||
| 561 | if (is_string($value)) { |
||
| 562 | return "'" . str_replace("'", "''", $value) . "'"; |
||
| 563 | } |
||
| 564 | |||
| 565 | return 'NULL'; |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Handle expressions |
||
| 570 | * @param array<int, array<string, mixed>> $expressions |
||
| 571 | * |
||
| 572 | * @return string |
||
| 573 | */ |
||
| 574 | protected function getExpressions(array $expressions): string |
||
| 575 | { |
||
| 576 | $sql = []; |
||
| 577 | |||
| 578 | foreach ($expressions as $expression) { |
||
| 579 | switch ($expression['type']) { |
||
| 580 | case 'column': |
||
| 581 | $sql[] = $this->quoteIdentifier($expression['value']); |
||
| 582 | break; |
||
| 583 | case 'op': |
||
| 584 | $sql[] = $expression['value']; |
||
| 585 | break; |
||
| 586 | case 'value': |
||
| 587 | $sql[] = $this->param($expression['value']); |
||
| 588 | break; |
||
| 589 | case 'group': |
||
| 590 | $expr = $expression['value']; |
||
| 591 | $sql[] = '(' . $this->getExpressions($expr->getExpressions()) . ')'; |
||
| 592 | break; |
||
| 593 | case 'function': |
||
| 594 | $sql[] = $this->getSqlFunction($expression['value']); |
||
| 595 | break; |
||
| 596 | case 'subquery': |
||
| 597 | $subQuery = $expression['value']; |
||
| 598 | $sql[] = '(' . $this->select($subQuery->getQueryStatement()) . ')'; |
||
| 599 | break; |
||
| 600 | } |
||
| 601 | } |
||
| 602 | |||
| 603 | return implode(' ', $sql); |
||
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Handle SQL function |
||
| 608 | * @param array<string, mixed> $functions |
||
| 609 | * |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | protected function getSqlFunction(array $functions): string |
||
| 613 | { |
||
| 614 | $method = $functions['type'] . $functions['name']; |
||
| 615 | |||
| 616 | return $this->{$method}($functions); |
||
| 617 | } |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Handle columns |
||
| 621 | * @param array<int, array<string, mixed>> $columns |
||
| 622 | * |
||
| 623 | * @return string |
||
| 624 | */ |
||
| 625 | protected function getColumnList(array $columns): string |
||
| 626 | { |
||
| 627 | if (count($columns) === 0) { |
||
| 628 | return '*'; |
||
| 629 | } |
||
| 630 | $sql = []; |
||
| 631 | |||
| 632 | foreach ($columns as $column) { |
||
| 633 | if (isset($column['alias'])) { |
||
| 634 | $sql[] = $this->quoteIdentifier($column['name']) |
||
| 635 | . ' AS ' . $this->quoteIdentifier($column['alias']); |
||
| 636 | } else { |
||
| 637 | $sql[] = $this->quoteIdentifier($column['name']); |
||
| 638 | } |
||
| 639 | } |
||
| 640 | |||
| 641 | return implode(', ', $sql); |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Handle schema columns |
||
| 646 | * @param array<int|string, BaseColumn> $columns list of BaseColumn |
||
| 647 | * @return string |
||
| 648 | */ |
||
| 649 | protected function getSchemaColumns(array $columns): string |
||
| 650 | { |
||
| 651 | $sql = []; |
||
| 652 | |||
| 653 | foreach ($columns as $column) { |
||
| 654 | $line = $this->quoteIdentifier($column->getName()); |
||
| 655 | $line .= $this->getColumnType($column); |
||
| 656 | $line .= $this->getColumnModifiers($column); |
||
| 657 | |||
| 658 | $sql[] = $line; |
||
| 659 | } |
||
| 660 | |||
| 661 | return implode(",\n", $sql); |
||
| 662 | } |
||
| 663 | |||
| 664 | /** |
||
| 665 | * |
||
| 666 | * @param BaseColumn $column |
||
| 667 | * @return string |
||
| 668 | */ |
||
| 669 | protected function getColumnType(BaseColumn $column): string |
||
| 670 | { |
||
| 671 | $type = $column->getType(); |
||
| 672 | $result = ''; |
||
| 673 | if (is_string($type)) { |
||
| 674 | $callback = 'getType' . ucfirst($type); |
||
| 675 | $result = trim($this->{$callback}($column)); |
||
| 676 | |||
| 677 | if ($result !== '') { |
||
| 678 | $result = ' ' . $result; |
||
| 679 | } |
||
| 680 | } |
||
| 681 | |||
| 682 | return $result; |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * |
||
| 687 | * @param BaseColumn $column |
||
| 688 | * @return string |
||
| 689 | */ |
||
| 690 | protected function getColumnModifiers(BaseColumn $column): string |
||
| 691 | { |
||
| 692 | $line = ''; |
||
| 693 | foreach ($this->modifiers as $modifier) { |
||
| 694 | $callback = 'getModifier' . ucfirst($modifier); |
||
| 695 | $result = trim($this->{$callback}($column)); |
||
| 696 | |||
| 697 | |||
| 698 | if ($result !== '') { |
||
| 699 | $result = ' ' . $result; |
||
| 700 | } |
||
| 701 | |||
| 702 | $line .= $result; |
||
| 703 | } |
||
| 704 | |||
| 705 | return $line; |
||
| 706 | } |
||
| 707 | |||
| 708 | /** |
||
| 709 | * |
||
| 710 | * @param BaseColumn $column |
||
| 711 | * @return string |
||
| 712 | */ |
||
| 713 | protected function getTypeInteger(BaseColumn $column): string |
||
| 714 | { |
||
| 715 | return 'INT'; |
||
| 716 | } |
||
| 717 | |||
| 718 | /** |
||
| 719 | * |
||
| 720 | * @param BaseColumn $column |
||
| 721 | * @return string |
||
| 722 | */ |
||
| 723 | protected function getTypeFloat(BaseColumn $column): string |
||
| 724 | { |
||
| 725 | return 'FLOAT'; |
||
| 726 | } |
||
| 727 | |||
| 728 | /** |
||
| 729 | * |
||
| 730 | * @param BaseColumn $column |
||
| 731 | * @return string |
||
| 732 | */ |
||
| 733 | protected function getTypeDouble(BaseColumn $column): string |
||
| 734 | { |
||
| 735 | return 'DOUBLE'; |
||
| 736 | } |
||
| 737 | |||
| 738 | /** |
||
| 739 | * |
||
| 740 | * @param BaseColumn $column |
||
| 741 | * @return string |
||
| 742 | */ |
||
| 743 | protected function getTypeDecimal(BaseColumn $column): string |
||
| 744 | { |
||
| 745 | return 'DECIMAL'; |
||
| 746 | } |
||
| 747 | |||
| 748 | /** |
||
| 749 | * |
||
| 750 | * @param BaseColumn $column |
||
| 751 | * @return string |
||
| 752 | */ |
||
| 753 | protected function getTypeEnum(BaseColumn $column): string |
||
| 754 | { |
||
| 755 | return 'ENUM'; |
||
| 756 | } |
||
| 757 | |||
| 758 | /** |
||
| 759 | * |
||
| 760 | * @param BaseColumn $column |
||
| 761 | * @return string |
||
| 762 | */ |
||
| 763 | protected function getTypeBoolean(BaseColumn $column): string |
||
| 764 | { |
||
| 765 | return 'BOOLEAN'; |
||
| 766 | } |
||
| 767 | |||
| 768 | /** |
||
| 769 | * |
||
| 770 | * @param BaseColumn $column |
||
| 771 | * @return string |
||
| 772 | */ |
||
| 773 | protected function getTypeBinary(BaseColumn $column): string |
||
| 774 | { |
||
| 775 | return 'BLOB'; |
||
| 776 | } |
||
| 777 | |||
| 778 | /** |
||
| 779 | * |
||
| 780 | * @param BaseColumn $column |
||
| 781 | * @return string |
||
| 782 | */ |
||
| 783 | protected function getTypeText(BaseColumn $column): string |
||
| 784 | { |
||
| 785 | return 'TEXT'; |
||
| 786 | } |
||
| 787 | |||
| 788 | /** |
||
| 789 | * |
||
| 790 | * @param BaseColumn $column |
||
| 791 | * @return string |
||
| 792 | */ |
||
| 793 | protected function getTypeString(BaseColumn $column): string |
||
| 794 | { |
||
| 795 | return 'VARCHAR(' . $this->value($column->get('length', 255)) . ')'; |
||
| 796 | } |
||
| 797 | |||
| 798 | /** |
||
| 799 | * |
||
| 800 | * @param BaseColumn $column |
||
| 801 | * @return string |
||
| 802 | */ |
||
| 803 | protected function getTypeFixed(BaseColumn $column): string |
||
| 804 | { |
||
| 805 | return 'CHAR(' . $this->value($column->get('length', 255)) . ')'; |
||
| 806 | } |
||
| 807 | |||
| 808 | /** |
||
| 809 | * |
||
| 810 | * @param BaseColumn $column |
||
| 811 | * @return string |
||
| 812 | */ |
||
| 813 | protected function getTypeTime(BaseColumn $column): string |
||
| 814 | { |
||
| 815 | return 'TIME'; |
||
| 816 | } |
||
| 817 | |||
| 818 | /** |
||
| 819 | * |
||
| 820 | * @param BaseColumn $column |
||
| 821 | * @return string |
||
| 822 | */ |
||
| 823 | protected function getTypeTimestamp(BaseColumn $column): string |
||
| 824 | { |
||
| 825 | return 'TIMESTAMP'; |
||
| 826 | } |
||
| 827 | |||
| 828 | /** |
||
| 829 | * |
||
| 830 | * @param BaseColumn $column |
||
| 831 | * @return string |
||
| 832 | */ |
||
| 833 | protected function getTypeDate(BaseColumn $column): string |
||
| 834 | { |
||
| 835 | return 'DATE'; |
||
| 836 | } |
||
| 837 | |||
| 838 | /** |
||
| 839 | * |
||
| 840 | * @param BaseColumn $column |
||
| 841 | * @return string |
||
| 842 | */ |
||
| 843 | protected function getTypeDatetime(BaseColumn $column): string |
||
| 844 | { |
||
| 845 | return 'DATETIME'; |
||
| 846 | } |
||
| 847 | |||
| 848 | /** |
||
| 849 | * |
||
| 850 | * @param BaseColumn $column |
||
| 851 | * @return string |
||
| 852 | */ |
||
| 853 | protected function getModifierUnsigned(BaseColumn $column): string |
||
| 854 | { |
||
| 855 | return $column->get('unsigned', false) ? 'UNSIGNED' : ''; |
||
| 856 | } |
||
| 857 | |||
| 858 | /** |
||
| 859 | * |
||
| 860 | * @param BaseColumn $column |
||
| 861 | * @return string |
||
| 862 | */ |
||
| 863 | protected function getModifierNullable(BaseColumn $column): string |
||
| 864 | { |
||
| 865 | return $column->get('nullable', true) ? '' : 'NOT NULL'; |
||
| 866 | } |
||
| 867 | |||
| 868 | /** |
||
| 869 | * |
||
| 870 | * @param BaseColumn $column |
||
| 871 | * @return string |
||
| 872 | */ |
||
| 873 | protected function getModifierDefault(BaseColumn $column): string |
||
| 878 | } |
||
| 879 | |||
| 880 | /** |
||
| 881 | * |
||
| 882 | * @param BaseColumn $column |
||
| 883 | * @return string |
||
| 884 | */ |
||
| 885 | protected function getModifierDescription(BaseColumn $column): string |
||
| 886 | { |
||
| 887 | return $column->get('description', null) === null ? '' : 'COMMENT ' |
||
| 888 | . $this->value($column->get('description')); |
||
| 889 | } |
||
| 890 | |||
| 891 | /** |
||
| 892 | * |
||
| 893 | * @param BaseColumn $column |
||
| 894 | * @return string |
||
| 895 | */ |
||
| 896 | protected function getModifierAfter(BaseColumn $column): string |
||
| 897 | { |
||
| 898 | return $column->get('after', null) === null ? '' : 'AFTER ' |
||
| 899 | . $this->quoteIdentifier($column->get('after')); |
||
| 900 | } |
||
| 901 | |||
| 902 | /** |
||
| 903 | * |
||
| 904 | * @param BaseColumn $column |
||
| 905 | * @return string |
||
| 906 | */ |
||
| 907 | protected function getModifierAutoincrement(BaseColumn $column): string |
||
| 908 | { |
||
| 909 | if ( |
||
| 910 | $column->getType() !== 'integer' |
||
| 911 | || !in_array($column->get('size', 'normal'), $this->serials) |
||
| 912 | ) { |
||
| 913 | return ''; |
||
| 914 | } |
||
| 915 | return $column->get('autoincrement', false) ? $this->autoincrement : ''; |
||
| 916 | } |
||
| 917 | |||
| 918 | /** |
||
| 919 | * |
||
| 920 | * @param CreateTable $schema |
||
| 921 | * @return string |
||
| 922 | */ |
||
| 923 | protected function getPrimaryKey(CreateTable $schema): string |
||
| 924 | { |
||
| 925 | $primaryKey = $schema->getPrimaryKey(); |
||
| 926 | if (count($primaryKey) === 0) { |
||
| 927 | return ''; |
||
| 928 | } |
||
| 929 | |||
| 930 | return ",\n" . 'CONSTRAINT ' . $this->quoteIdentifier($primaryKey['name']) |
||
| 931 | . ' PRIMARY KEY (' . $this->quoteIdentifiers($primaryKey['columns']) . ')'; |
||
| 932 | } |
||
| 933 | |||
| 934 | /** |
||
| 935 | * |
||
| 936 | * @param CreateTable $schema |
||
| 937 | * @return string |
||
| 938 | */ |
||
| 939 | protected function getUniqueKeys(CreateTable $schema): string |
||
| 940 | { |
||
| 941 | $indexes = $schema->getUniqueKeys(); |
||
| 942 | |||
| 943 | if (count($indexes) === 0) { |
||
| 944 | return ''; |
||
| 945 | } |
||
| 946 | |||
| 947 | $sql = []; |
||
| 948 | |||
| 949 | foreach ($indexes as $name => $columns) { |
||
| 950 | $sql[] = 'CONSTRAINT ' . $this->quoteIdentifier($name) |
||
| 951 | . ' UNIQUE (' . $this->quoteIdentifiers($columns) . ')'; |
||
| 952 | } |
||
| 953 | |||
| 954 | return ",\n" . implode(",\n", $sql); |
||
| 955 | } |
||
| 956 | |||
| 957 | /** |
||
| 958 | * |
||
| 959 | * @param CreateTable $schema |
||
| 960 | * @return array<int, string> |
||
| 961 | */ |
||
| 962 | protected function getIndexKeys(CreateTable $schema): array |
||
| 963 | { |
||
| 964 | $indexes = $schema->getIndexes(); |
||
| 965 | |||
| 966 | if (count($indexes) === 0) { |
||
| 967 | return []; |
||
| 968 | } |
||
| 969 | |||
| 970 | $sql = []; |
||
| 971 | $table = $this->quoteIdentifier($schema->getTableName()); |
||
| 972 | |||
| 973 | foreach ($indexes as $name => $columns) { |
||
| 974 | $sql[] = 'CREATE INDEX ' . $this->quoteIdentifier($name) |
||
| 975 | . ' ON ' . $table . '(' . $this->quoteIdentifiers($columns) . ')'; |
||
| 976 | } |
||
| 977 | |||
| 978 | return $sql; |
||
| 979 | } |
||
| 980 | |||
| 981 | /** |
||
| 982 | * |
||
| 983 | * @param CreateTable $schema |
||
| 984 | * @return string |
||
| 985 | */ |
||
| 986 | protected function getForeignKeys(CreateTable $schema): string |
||
| 987 | { |
||
| 988 | $keys = $schema->getForeignKeys(); |
||
| 989 | |||
| 990 | if (count($keys) === 0) { |
||
| 991 | return ''; |
||
| 992 | } |
||
| 993 | |||
| 994 | $sql = []; |
||
| 995 | |||
| 996 | foreach ($keys as $name => $key) { |
||
| 997 | $cmd = 'CONSTRAINT ' . $this->quoteIdentifier($name) |
||
| 998 | . ' FOREIGN KEY (' . $this->quoteIdentifiers($key->getColumns()) . ') '; |
||
| 999 | $cmd .= 'REFERENCES ' . $this->quoteIdentifier($key->getReferenceTable()) |
||
| 1000 | . ' (' . $this->quoteIdentifiers($key->getReferenceColumns()) . ')'; |
||
| 1001 | |||
| 1002 | foreach ($key->getActions() as $actionName => $action) { |
||
| 1003 | $cmd .= ' ' . $actionName . ' ' . $action; |
||
| 1004 | } |
||
| 1005 | $sql[] = $cmd; |
||
| 1006 | } |
||
| 1007 | |||
| 1008 | return ",\n" . implode(",\n", $sql); |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * |
||
| 1013 | * @param CreateTable $schema |
||
| 1014 | * @return string |
||
| 1015 | */ |
||
| 1016 | protected function getEngine(CreateTable $schema): string |
||
| 1017 | { |
||
| 1018 | $engine = $schema->getEngine(); |
||
| 1019 | if ($engine === null) { |
||
| 1020 | return ''; |
||
| 1021 | } |
||
| 1022 | |||
| 1023 | return ' ENGINE = ' . strtoupper($engine); |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | /** |
||
| 1027 | * |
||
| 1028 | * @param AlterTable $schema |
||
| 1029 | * @param mixed $data |
||
| 1030 | * @return string |
||
| 1031 | */ |
||
| 1032 | protected function getDropPrimaryKey(AlterTable $schema, mixed $data): string |
||
| 1033 | { |
||
| 1034 | return sprintf( |
||
| 1035 | 'ALTER TABLE %s DROP CONSTRAINT %s', |
||
| 1036 | $this->quoteIdentifier($schema->getTableName()), |
||
| 1037 | $this->quoteIdentifier($data) |
||
| 1038 | ); |
||
| 1039 | } |
||
| 1040 | |||
| 1041 | /** |
||
| 1042 | * |
||
| 1043 | * @param AlterTable $schema |
||
| 1044 | * @param mixed $data |
||
| 1045 | * @return string |
||
| 1046 | */ |
||
| 1047 | protected function getDropUniqueKey(AlterTable $schema, mixed $data): string |
||
| 1048 | { |
||
| 1049 | return sprintf( |
||
| 1050 | 'ALTER TABLE %s DROP CONSTRAINT %s', |
||
| 1051 | $this->quoteIdentifier($schema->getTableName()), |
||
| 1052 | $this->quoteIdentifier($data) |
||
| 1053 | ); |
||
| 1054 | } |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * |
||
| 1058 | * @param AlterTable $schema |
||
| 1059 | * @param mixed $data |
||
| 1060 | * @return string |
||
| 1061 | */ |
||
| 1062 | protected function getDropIndex(AlterTable $schema, mixed $data): string |
||
| 1063 | { |
||
| 1064 | return sprintf( |
||
| 1065 | 'DROP INDEX %s.%s', |
||
| 1066 | $this->quoteIdentifier($schema->getTableName()), |
||
| 1067 | $this->quoteIdentifier($data) |
||
| 1068 | ); |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | /** |
||
| 1072 | * |
||
| 1073 | * @param AlterTable $schema |
||
| 1074 | * @param mixed $data |
||
| 1075 | * @return string |
||
| 1076 | */ |
||
| 1077 | protected function getDropForeignKey(AlterTable $schema, mixed $data): string |
||
| 1078 | { |
||
| 1079 | return sprintf( |
||
| 1080 | 'ALTER TABLE %s DROP CONSTRAINT %s', |
||
| 1081 | $this->quoteIdentifier($schema->getTableName()), |
||
| 1082 | $this->quoteIdentifier($data) |
||
| 1083 | ); |
||
| 1084 | } |
||
| 1085 | |||
| 1086 | /** |
||
| 1087 | * |
||
| 1088 | * @param AlterTable $schema |
||
| 1089 | * @param mixed $data |
||
| 1090 | * @return string |
||
| 1091 | */ |
||
| 1092 | protected function getDropColumn(AlterTable $schema, mixed $data): string |
||
| 1093 | { |
||
| 1094 | return sprintf( |
||
| 1095 | 'ALTER TABLE %s DROP COLUMN %s', |
||
| 1096 | $this->quoteIdentifier($schema->getTableName()), |
||
| 1097 | $this->quoteIdentifier($data) |
||
| 1098 | ); |
||
| 1099 | } |
||
| 1100 | |||
| 1101 | /** |
||
| 1102 | * |
||
| 1103 | * @param AlterTable $schema |
||
| 1104 | * @param mixed $data |
||
| 1105 | * @return string |
||
| 1106 | */ |
||
| 1107 | protected function getRenameColumn(AlterTable $schema, mixed $data): string |
||
| 1108 | { |
||
| 1109 | //TODO: please implement it in subclass |
||
| 1110 | return ''; |
||
| 1111 | } |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * |
||
| 1115 | * @param AlterTable $schema |
||
| 1116 | * @param mixed $data |
||
| 1117 | * @return string |
||
| 1118 | */ |
||
| 1119 | protected function getModifyColumn(AlterTable $schema, mixed $data): string |
||
| 1120 | { |
||
| 1121 | return sprintf( |
||
| 1122 | 'ALTER TABLE %s MODIFY COLUMN %s', |
||
| 1123 | $this->quoteIdentifier($schema->getTableName()), |
||
| 1124 | $this->getSchemaColumns([$data]) |
||
| 1125 | ); |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | /** |
||
| 1129 | * |
||
| 1130 | * @param AlterTable $schema |
||
| 1131 | * @param mixed $data |
||
| 1132 | * @return string |
||
| 1133 | */ |
||
| 1134 | protected function getAddColumn(AlterTable $schema, mixed $data): string |
||
| 1135 | { |
||
| 1136 | return sprintf( |
||
| 1137 | 'ALTER TABLE %s ADD COLUMN %s', |
||
| 1138 | $this->quoteIdentifier($schema->getTableName()), |
||
| 1139 | $this->getSchemaColumns([$data]) |
||
| 1140 | ); |
||
| 1141 | } |
||
| 1142 | |||
| 1143 | /** |
||
| 1144 | * |
||
| 1145 | * @param AlterTable $schema |
||
| 1146 | * @param mixed $data |
||
| 1147 | * @return string |
||
| 1148 | */ |
||
| 1149 | protected function getAddPrimary(AlterTable $schema, mixed $data): string |
||
| 1150 | { |
||
| 1151 | return sprintf( |
||
| 1152 | 'ALTER TABLE %s ADD CONSTRAINT %s PRIMARY KEY (%s)', |
||
| 1153 | $this->quoteIdentifier($schema->getTableName()), |
||
| 1154 | $this->quoteIdentifier($data['name']), |
||
| 1155 | $this->quoteIdentifiers($data['columns']) |
||
| 1156 | ); |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | /** |
||
| 1160 | * |
||
| 1161 | * @param AlterTable $schema |
||
| 1162 | * @param mixed $data |
||
| 1163 | * @return string |
||
| 1164 | */ |
||
| 1165 | protected function getAddUnique(AlterTable $schema, mixed $data): string |
||
| 1172 | ); |
||
| 1173 | } |
||
| 1174 | |||
| 1175 | /** |
||
| 1176 | * |
||
| 1177 | * @param AlterTable $schema |
||
| 1178 | * @param mixed $data |
||
| 1179 | * @return string |
||
| 1180 | */ |
||
| 1181 | protected function getAddIndex(AlterTable $schema, mixed $data): string |
||
| 1182 | { |
||
| 1183 | return sprintf( |
||
| 1184 | 'CREATE INDEX %s ON %s (%s)', |
||
| 1185 | $this->quoteIdentifier($data['name']), |
||
| 1186 | $this->quoteIdentifier($schema->getTableName()), |
||
| 1187 | $this->quoteIdentifiers($data['columns']) |
||
| 1188 | ); |
||
| 1189 | } |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * |
||
| 1193 | * @param AlterTable $schema |
||
| 1194 | * @param mixed $data |
||
| 1195 | * @return string |
||
| 1196 | */ |
||
| 1197 | protected function getAddForeign(AlterTable $schema, mixed $data): string |
||
| 1198 | { |
||
| 1199 | /** @var ForeignKey $key */ |
||
| 1200 | $key = $data['foreign']; |
||
| 1201 | $actions = ''; |
||
| 1202 | foreach ($key->getActions() as $actionName => $action) { |
||
| 1203 | $actions .= ' ' . $actionName . ' ' . $action; |
||
| 1204 | } |
||
| 1205 | |||
| 1206 | return sprintf( |
||
| 1207 | 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s', |
||
| 1208 | $this->quoteIdentifier($schema->getTableName()), |
||
| 1209 | $this->quoteIdentifier($data['name']), |
||
| 1210 | $this->quoteIdentifiers($key->getColumns()), |
||
| 1211 | $this->quoteIdentifier($key->getReferenceTable()), |
||
| 1212 | $this->quoteIdentifiers($key->getReferenceColumns()), |
||
| 1213 | $actions |
||
| 1214 | ); |
||
| 1215 | } |
||
| 1216 | |||
| 1217 | /** |
||
| 1218 | * |
||
| 1219 | * @param AlterTable $schema |
||
| 1220 | * @param mixed $data |
||
| 1221 | * @return string |
||
| 1222 | */ |
||
| 1223 | protected function getSetDefaultValue(AlterTable $schema, mixed $data): string |
||
| 1224 | { |
||
| 1225 | return sprintf( |
||
| 1226 | 'ALTER TABLE %s ALTER COLUMN %s SET DEFAULT (%s)', |
||
| 1227 | $this->quoteIdentifier($schema->getTableName()), |
||
| 1228 | $this->quoteIdentifier($data['column']), |
||
| 1229 | $this->value($data['value']) |
||
| 1230 | ); |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | /** |
||
| 1234 | * |
||
| 1235 | * @param AlterTable $schema |
||
| 1236 | * @param mixed $data |
||
| 1237 | * @return string |
||
| 1238 | */ |
||
| 1239 | protected function getDropDefaultValue(AlterTable $schema, mixed $data): string |
||
| 1240 | { |
||
| 1241 | return sprintf( |
||
| 1242 | 'ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT', |
||
| 1243 | $this->quoteIdentifier($schema->getTableName()), |
||
| 1244 | $this->quoteIdentifier($data) |
||
| 1245 | ); |
||
| 1246 | } |
||
| 1247 | |||
| 1248 | /** |
||
| 1249 | * Handle into the table |
||
| 1250 | * |
||
| 1251 | * @param string|null $table |
||
| 1252 | * |
||
| 1253 | * @return string |
||
| 1254 | */ |
||
| 1255 | protected function getInto(?string $table): string |
||
| 1256 | { |
||
| 1257 | if ($table === null) { |
||
| 1258 | return ''; |
||
| 1259 | } |
||
| 1260 | return ' INTO ' . $this->quoteIdentifier($table); |
||
| 1261 | } |
||
| 1262 | |||
| 1263 | /** |
||
| 1264 | * Handle tables |
||
| 1265 | * @param array<mixed, string> $tables |
||
| 1266 | * |
||
| 1267 | * @return string |
||
| 1268 | */ |
||
| 1269 | protected function getTableList(array $tables): string |
||
| 1270 | { |
||
| 1271 | if (count($tables) === 0) { |
||
| 1272 | return ''; |
||
| 1273 | } |
||
| 1274 | $sql = []; |
||
| 1275 | foreach ($tables as $name => $alias) { |
||
| 1276 | if (is_string($name)) { |
||
| 1277 | $sql[] = $this->quoteIdentifier($name) . ' AS ' . $this->quoteIdentifier($alias); |
||
| 1278 | } else { |
||
| 1279 | $sql[] = $this->quoteIdentifier($alias); |
||
| 1280 | } |
||
| 1281 | } |
||
| 1282 | |||
| 1283 | return implode(', ', $sql); |
||
| 1284 | } |
||
| 1285 | |||
| 1286 | /** |
||
| 1287 | * Handle for joins |
||
| 1288 | * @param array<int, mixed> $joins |
||
| 1289 | * |
||
| 1290 | * @return string |
||
| 1291 | */ |
||
| 1292 | protected function getJoins(array $joins): string |
||
| 1293 | { |
||
| 1294 | if (count($joins) === 0) { |
||
| 1295 | return ''; |
||
| 1296 | } |
||
| 1297 | $sql = []; |
||
| 1298 | |||
| 1299 | foreach ($joins as $join) { |
||
| 1300 | $joinObject = $join['join']; |
||
| 1301 | |||
| 1302 | $on = ''; |
||
| 1303 | if ($joinObject) { |
||
| 1304 | $on = $this->getJoinConditions($joinObject->getJoinConditions()); |
||
| 1305 | } |
||
| 1306 | |||
| 1307 | if ($on !== '') { |
||
| 1308 | $on = ' ON ' . $on; |
||
| 1309 | } |
||
| 1310 | |||
| 1311 | $sql[] = $join['type'] . ' JOIN ' . $this->getTableList($join['table']) . $on; |
||
| 1312 | } |
||
| 1313 | |||
| 1314 | return ' ' . implode(' ', $sql); |
||
| 1315 | } |
||
| 1316 | |||
| 1317 | /** |
||
| 1318 | * Handle for the join conditions |
||
| 1319 | * @param array<int, mixed> $conditions |
||
| 1320 | * @return string |
||
| 1321 | */ |
||
| 1322 | protected function getJoinConditions(array $conditions): string |
||
| 1323 | { |
||
| 1324 | if (count($conditions) === 0) { |
||
| 1325 | return ''; |
||
| 1326 | } |
||
| 1327 | |||
| 1328 | $sql = []; |
||
| 1329 | |||
| 1330 | $sql[] = $this->{$conditions[0]['type']}($conditions[0]); |
||
| 1331 | |||
| 1332 | $count = count($conditions); |
||
| 1333 | for ($i = 1; $i < $count; $i++) { |
||
| 1334 | $sql[] = $conditions[$i]['separator'] . ' ' |
||
| 1335 | . $this->{$conditions[$i]['type']}($conditions[$i]); |
||
| 1336 | } |
||
| 1337 | |||
| 1338 | return implode(' ', $sql); |
||
| 1339 | } |
||
| 1340 | |||
| 1341 | /** |
||
| 1342 | * Handle group by |
||
| 1343 | * @param Expression[]|string[] $groupBy |
||
| 1344 | * |
||
| 1345 | * @return string |
||
| 1346 | */ |
||
| 1347 | protected function getGroupBy(array $groupBy): string |
||
| 1348 | { |
||
| 1349 | return count($groupBy) === 0 ? '' : ' GROUP BY ' . $this->columns($groupBy); |
||
| 1350 | } |
||
| 1351 | |||
| 1352 | /** |
||
| 1353 | * Handle for Order |
||
| 1354 | * @param array<int, array<string, mixed>> $orders |
||
| 1355 | * @return string |
||
| 1356 | */ |
||
| 1357 | protected function getOrders(array $orders): string |
||
| 1358 | { |
||
| 1359 | if (count($orders) === 0) { |
||
| 1360 | return ''; |
||
| 1361 | } |
||
| 1362 | $sql = []; |
||
| 1363 | foreach ($orders as $order) { |
||
| 1364 | $sql[] = $this->columns($order['columns']) . ' ' . $order['order']; |
||
| 1365 | } |
||
| 1366 | |||
| 1367 | return ' ORDER BY ' . implode(', ', $sql); |
||
| 1368 | } |
||
| 1369 | |||
| 1370 | /** |
||
| 1371 | * Handle columns for set (UPDATE) |
||
| 1372 | * @param array<int, array<string, mixed>> $columns |
||
| 1373 | * @return string |
||
| 1374 | */ |
||
| 1375 | protected function getSetColumns(array $columns): string |
||
| 1376 | { |
||
| 1377 | if (count($columns) === 0) { |
||
| 1378 | return ''; |
||
| 1379 | } |
||
| 1380 | $sql = []; |
||
| 1381 | |||
| 1382 | foreach ($columns as $column) { |
||
| 1383 | $sql[] = $this->quoteIdentifier($column['column']) . ' = ' . $this->param($column['value']); |
||
| 1384 | } |
||
| 1385 | |||
| 1386 | return ' SET ' . implode(', ', $sql); |
||
| 1387 | } |
||
| 1388 | |||
| 1389 | /** |
||
| 1390 | * Handler where |
||
| 1391 | * @param array<int, mixed> $wheres |
||
| 1392 | * @param bool $prefix |
||
| 1393 | * |
||
| 1394 | * @return string |
||
| 1395 | */ |
||
| 1396 | protected function getWheres(array $wheres, bool $prefix = true): string |
||
| 1397 | { |
||
| 1398 | $sql = $this->getWheresHaving($wheres); |
||
| 1399 | if (empty($sql)) { |
||
| 1400 | return ''; |
||
| 1401 | } |
||
| 1402 | return ($prefix ? ' WHERE ' : '') . $sql; |
||
| 1403 | } |
||
| 1404 | |||
| 1405 | /** |
||
| 1406 | * Handle for having |
||
| 1407 | * @param array<int, mixed> $having |
||
| 1408 | * @param bool $prefix |
||
| 1409 | * @return string |
||
| 1410 | */ |
||
| 1411 | protected function getHaving(array $having, bool $prefix = true): string |
||
| 1412 | { |
||
| 1413 | $sql = $this->getWheresHaving($having); |
||
| 1414 | if (empty($sql)) { |
||
| 1415 | return ''; |
||
| 1416 | } |
||
| 1417 | return ($prefix ? ' HAVING ' : '') . $sql; |
||
| 1418 | } |
||
| 1419 | |||
| 1420 | /** |
||
| 1421 | * Return the build part for where or having |
||
| 1422 | * @param array<int, mixed> $values |
||
| 1423 | * |
||
| 1424 | * @return string |
||
| 1425 | */ |
||
| 1426 | protected function getWheresHaving(array $values): string |
||
| 1427 | { |
||
| 1428 | if (count($values) === 0) { |
||
| 1429 | return ''; |
||
| 1430 | } |
||
| 1431 | $sql = []; |
||
| 1432 | $sql[] = $this->{$values[0]['type']}($values[0]); |
||
| 1433 | $count = count($values); |
||
| 1434 | |||
| 1435 | for ($i = 1; $i < $count; $i++) { |
||
| 1436 | $sql[] = $values[$i]['separator'] . ' ' . $this->{$values[$i]['type']}($values[$i]); |
||
| 1437 | } |
||
| 1438 | return implode(' ', $sql); |
||
| 1439 | } |
||
| 1440 | |||
| 1441 | /** |
||
| 1442 | * Handle for insert values |
||
| 1443 | * @param array<int, mixed> $values |
||
| 1444 | * @return string |
||
| 1445 | */ |
||
| 1446 | protected function getInsertValues(array $values): string |
||
| 1447 | { |
||
| 1448 | return ' VALUES (' . $this->params($values) . ')'; |
||
| 1449 | } |
||
| 1450 | |||
| 1451 | /** |
||
| 1452 | * Handle for limit |
||
| 1453 | * @param int $limit |
||
| 1454 | * @return string |
||
| 1455 | */ |
||
| 1456 | protected function getLimit(int $limit): string |
||
| 1457 | { |
||
| 1458 | return ($limit === 0) ? '' : ' LIMIT ' . $this->param($limit); |
||
| 1459 | } |
||
| 1460 | |||
| 1461 | /** |
||
| 1462 | * Handle for offset |
||
| 1463 | * @param int $offset |
||
| 1464 | * @return string |
||
| 1465 | */ |
||
| 1466 | protected function getOffset(int $offset): string |
||
| 1467 | { |
||
| 1468 | return ($offset < 0) ? '' : ' OFFSET ' . $this->param($offset); |
||
| 1469 | } |
||
| 1470 | |||
| 1471 | /** |
||
| 1472 | * @param array<string, mixed> $join |
||
| 1473 | * @return string |
||
| 1474 | */ |
||
| 1475 | protected function joinColumn(array $join): string |
||
| 1476 | { |
||
| 1477 | return sprintf( |
||
| 1478 | '%s %s %s', |
||
| 1479 | $this->quoteIdentifier($join['column1']), |
||
| 1480 | $join['operator'], |
||
| 1481 | $this->quoteIdentifier($join['column2']) |
||
| 1482 | ); |
||
| 1483 | } |
||
| 1484 | |||
| 1485 | /** |
||
| 1486 | * @param array<string, mixed> $join |
||
| 1487 | * @return string |
||
| 1488 | */ |
||
| 1489 | protected function joinNested(array $join): string |
||
| 1490 | { |
||
| 1491 | return '(' . $this->getJoinConditions($join['join']->getJoinConditions()) . ')'; |
||
| 1492 | } |
||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * @param array<string, mixed> $join |
||
| 1496 | * @return string |
||
| 1497 | */ |
||
| 1498 | protected function joinExpression(array $join): string |
||
| 1499 | { |
||
| 1500 | return $this->quoteIdentifier($join['expression']); |
||
| 1501 | } |
||
| 1502 | |||
| 1503 | /** |
||
| 1504 | * @param array<string, mixed> $where |
||
| 1505 | * @return string |
||
| 1506 | */ |
||
| 1507 | protected function whereColumn(array $where): string |
||
| 1508 | { |
||
| 1509 | return sprintf( |
||
| 1510 | '%s %s %s', |
||
| 1511 | $this->quoteIdentifier($where['column']), |
||
| 1512 | $where['operator'], |
||
| 1513 | $this->param($where['value']) |
||
| 1514 | ); |
||
| 1515 | } |
||
| 1516 | |||
| 1517 | /** |
||
| 1518 | * @param array<string, mixed> $where |
||
| 1519 | * @return string |
||
| 1520 | */ |
||
| 1521 | protected function whereIn(array $where): string |
||
| 1522 | { |
||
| 1523 | return sprintf( |
||
| 1524 | '%s %s (%s)', |
||
| 1525 | $this->quoteIdentifier($where['column']), |
||
| 1526 | $where['not'] ? 'NOT IN' : 'IN', |
||
| 1527 | $this->params($where['value']) |
||
| 1528 | ); |
||
| 1529 | } |
||
| 1530 | |||
| 1531 | /** |
||
| 1532 | * @param array<string, mixed> $where |
||
| 1533 | * @return string |
||
| 1534 | */ |
||
| 1535 | protected function whereInSelect(array $where): string |
||
| 1536 | { |
||
| 1537 | return sprintf( |
||
| 1538 | '%s %s (%s)', |
||
| 1539 | $this->quoteIdentifier($where['column']), |
||
| 1540 | $where['not'] ? 'NOT IN' : 'IN', |
||
| 1541 | $this->select($where['subquery']->getQueryStatement()) |
||
| 1542 | ); |
||
| 1543 | } |
||
| 1544 | |||
| 1545 | /** |
||
| 1546 | * @param array<string, mixed> $where |
||
| 1547 | * @return string |
||
| 1548 | */ |
||
| 1549 | protected function whereNested(array $where): string |
||
| 1550 | { |
||
| 1551 | return '(' . $this->getWheres($where['clause'], false) . ')'; |
||
| 1552 | } |
||
| 1553 | |||
| 1554 | /** |
||
| 1555 | * @param array<string, mixed> $where |
||
| 1556 | * @return string |
||
| 1557 | */ |
||
| 1558 | public function whereExists(array $where): string |
||
| 1559 | { |
||
| 1560 | return sprintf( |
||
| 1561 | '%s (%s)', |
||
| 1562 | $where['not'] ? 'NOT EXISTS' : 'EXISTS', |
||
| 1563 | $this->select($where['subquery']->getQueryStatement()) |
||
| 1564 | ); |
||
| 1565 | } |
||
| 1566 | |||
| 1567 | /** |
||
| 1568 | * @param array<string, mixed> $where |
||
| 1569 | * @return string |
||
| 1570 | */ |
||
| 1571 | protected function whereNull(array $where): string |
||
| 1572 | { |
||
| 1573 | return sprintf( |
||
| 1574 | '%s %s', |
||
| 1575 | $this->quoteIdentifier($where['column']), |
||
| 1576 | $where['not'] ? 'IS NOT NULL' : 'IS NULL', |
||
| 1577 | ); |
||
| 1578 | } |
||
| 1579 | |||
| 1580 | /** |
||
| 1581 | * @param array<string, mixed> $where |
||
| 1582 | * @return string |
||
| 1583 | */ |
||
| 1584 | protected function whereBetween(array $where): string |
||
| 1585 | { |
||
| 1586 | return sprintf( |
||
| 1587 | '%s %s %s AND %s', |
||
| 1588 | $this->quoteIdentifier($where['column']), |
||
| 1589 | $where['not'] ? 'NOT BETWEEN' : 'BETWEEN', |
||
| 1590 | $this->param($where['value1']), |
||
| 1591 | $this->param($where['value2']), |
||
| 1592 | ); |
||
| 1593 | } |
||
| 1594 | |||
| 1595 | /** |
||
| 1596 | * @param array<string, mixed> $where |
||
| 1597 | * @return string |
||
| 1598 | */ |
||
| 1599 | protected function whereLike(array $where): string |
||
| 1600 | { |
||
| 1601 | return sprintf( |
||
| 1602 | '%s %s %s', |
||
| 1603 | $this->quoteIdentifier($where['column']), |
||
| 1604 | $where['not'] ? 'NOT LIKE' : 'LIKE', |
||
| 1605 | $this->param($where['pattern']), |
||
| 1606 | ); |
||
| 1607 | } |
||
| 1608 | |||
| 1609 | /** |
||
| 1610 | * @param array<string, mixed> $where |
||
| 1611 | * @return string |
||
| 1612 | */ |
||
| 1613 | protected function whereSubQuery(array $where): string |
||
| 1614 | { |
||
| 1615 | return sprintf( |
||
| 1616 | '%s %s (%s)', |
||
| 1617 | $this->quoteIdentifier($where['column']), |
||
| 1618 | $where['operator'], |
||
| 1619 | $this->select($where['subquery']->getQueryStatement()) |
||
| 1620 | ); |
||
| 1621 | } |
||
| 1622 | |||
| 1623 | /** |
||
| 1624 | * @param array<string, mixed> $where |
||
| 1625 | * @return string |
||
| 1626 | */ |
||
| 1627 | protected function whereNop(array $where): string |
||
| 1628 | { |
||
| 1629 | return $this->quoteIdentifier($where['column']); |
||
| 1630 | } |
||
| 1631 | |||
| 1632 | /** |
||
| 1633 | * @param array<string, mixed> $having |
||
| 1634 | * @return string |
||
| 1635 | */ |
||
| 1636 | protected function havingCondition(array $having): string |
||
| 1637 | { |
||
| 1638 | return sprintf( |
||
| 1639 | '%s %s %s', |
||
| 1640 | $this->quoteIdentifier($having['aggregate']), |
||
| 1641 | $having['operator'], |
||
| 1642 | $having['value'] |
||
| 1643 | ); |
||
| 1644 | } |
||
| 1645 | |||
| 1646 | /** |
||
| 1647 | * @param array<string, mixed> $having |
||
| 1648 | * @return string |
||
| 1649 | */ |
||
| 1650 | protected function havingNested(array $having): string |
||
| 1653 | } |
||
| 1654 | |||
| 1655 | /** |
||
| 1656 | * @param array<string, mixed> $having |
||
| 1657 | * @return string |
||
| 1658 | */ |
||
| 1659 | protected function havingBetween(array $having): string |
||
| 1660 | { |
||
| 1661 | return sprintf( |
||
| 1662 | '%s %s %s AND %s', |
||
| 1663 | $this->quoteIdentifier($having['aggregate']), |
||
| 1664 | $having['not'] ? 'NOT BETWEEN' : 'BETWEEN', |
||
| 1665 | $this->param($having['value1']), |
||
| 1666 | $this->param($having['value2']), |
||
| 1667 | ); |
||
| 1668 | } |
||
| 1669 | |||
| 1670 | /** |
||
| 1671 | * @param array<string, mixed> $having |
||
| 1672 | * @return string |
||
| 1673 | */ |
||
| 1674 | protected function havingInSelect(array $having): string |
||
| 1681 | ); |
||
| 1682 | } |
||
| 1683 | |||
| 1684 | /** |
||
| 1685 | * @param array<string, mixed> $having |
||
| 1686 | * @return string |
||
| 1687 | */ |
||
| 1688 | protected function havingIn(array $having): string |
||
| 1689 | { |
||
| 1690 | return sprintf( |
||
| 1691 | '%s %s (%s)', |
||
| 1692 | $this->quoteIdentifier($having['aggregate']), |
||
| 1693 | $having['not'] ? 'NOT IN' : 'IN', |
||
| 1694 | $this->params($having['value']) |
||
| 1695 | ); |
||
| 1696 | } |
||
| 1697 | |||
| 1698 | /** |
||
| 1699 | * Return aggregate function COUNT |
||
| 1700 | * @param array<string, mixed> $function |
||
| 1701 | * @return string |
||
| 1702 | */ |
||
| 1703 | protected function aggregateFunctionCOUNT(array $function): string |
||
| 1709 | ); |
||
| 1710 | } |
||
| 1711 | |||
| 1712 | /** |
||
| 1713 | * Return aggregate function AVG |
||
| 1714 | * @param array<string, mixed> $function |
||
| 1715 | * @return string |
||
| 1716 | */ |
||
| 1717 | protected function aggregateFunctionAVG(array $function): string |
||
| 1718 | { |
||
| 1719 | return sprintf( |
||
| 1720 | 'AVG(%s%s)', |
||
| 1721 | $function['distinct'] ? 'DISTINCT ' : '', |
||
| 1722 | $this->quoteIdentifier($function['column']) |
||
| 1723 | ); |
||
| 1724 | } |
||
| 1725 | |||
| 1726 | /** |
||
| 1727 | * Return aggregate function SUM |
||
| 1728 | * @param array<string, mixed> $function |
||
| 1729 | * @return string |
||
| 1730 | */ |
||
| 1731 | protected function aggregateFunctionSUM(array $function): string |
||
| 1732 | { |
||
| 1733 | return sprintf( |
||
| 1734 | 'SUM(%s%s)', |
||
| 1735 | $function['distinct'] ? 'DISTINCT ' : '', |
||
| 1736 | $this->quoteIdentifier($function['column']) |
||
| 1737 | ); |
||
| 1738 | } |
||
| 1739 | |||
| 1740 | /** |
||
| 1741 | * Return aggregate function MIN |
||
| 1742 | * @param array<string, mixed> $function |
||
| 1743 | * @return string |
||
| 1744 | */ |
||
| 1745 | protected function aggregateFunctionMIN(array $function): string |
||
| 1746 | { |
||
| 1747 | return sprintf( |
||
| 1748 | 'MIN(%s%s)', |
||
| 1749 | $function['distinct'] ? 'DISTINCT ' : '', |
||
| 1750 | $this->quoteIdentifier($function['column']) |
||
| 1751 | ); |
||
| 1752 | } |
||
| 1753 | |||
| 1754 | /** |
||
| 1755 | * Return aggregate function MAX |
||
| 1756 | * @param array<string, mixed> $function |
||
| 1757 | * @return string |
||
| 1758 | */ |
||
| 1759 | protected function aggregateFunctionMAX(array $function): string |
||
| 1765 | ); |
||
| 1766 | } |
||
| 1767 | } |
||
| 1768 |