|
5 | 5 | namespace PhpMyAdmin\SqlParser\Tests\Utils; |
6 | 6 |
|
7 | 7 | use PhpMyAdmin\SqlParser\Parser; |
| 8 | +use PhpMyAdmin\SqlParser\Statement; |
8 | 9 | use PhpMyAdmin\SqlParser\Tests\TestCase; |
9 | 10 | use PhpMyAdmin\SqlParser\Utils\Query; |
10 | 11 |
|
@@ -416,6 +417,38 @@ public function testGetAll(): void |
416 | 417 | ); |
417 | 418 | } |
418 | 419 |
|
| 420 | + public function testGetAllTableWithDotsAndReplaceClause(): void |
| 421 | + { |
| 422 | + $query = 'SELECT * FROM `test.2024-11-01` ORDER BY `test.2024-11-01`.`id` ASC;'; |
| 423 | + |
| 424 | + $statements = Query::getAll($query); |
| 425 | + |
| 426 | + $this->assertInstanceOf(Statement::class, $statements['statement']); |
| 427 | + |
| 428 | + $fromClause = Query::replaceClause($statements['statement'], $statements['parser']->list, 'ORDER BY', ''); |
| 429 | + $this->assertEquals('SELECT * FROM `test.2024-11-01` ', $fromClause); |
| 430 | + |
| 431 | + $fromClause = Query::replaceClause($statements['statement'], $statements['parser']->list, 'ORDER BY'); |
| 432 | + $this->assertEquals('SELECT * FROM `test.2024-11-01` ORDER BY ', $fromClause); |
| 433 | + |
| 434 | + // With spaces |
| 435 | + $fromClause = Query::replaceClause($statements['statement'], $statements['parser']->list, 'ORDER BY '); |
| 436 | + $this->assertEquals('SELECT * FROM `test.2024-11-01` ORDER BY ', $fromClause); |
| 437 | + } |
| 438 | + |
| 439 | + public function testGetAllTableWithDotsAndReplaceClauseEmptyName(): void |
| 440 | + { |
| 441 | + $query = 'SELECT * FROM `test.2024-11-01` ORDER BY `test.2024-11-01`.`id` ASC;'; |
| 442 | + |
| 443 | + $statements = Query::getAll($query); |
| 444 | + |
| 445 | + $this->assertInstanceOf(Statement::class, $statements['statement']); |
| 446 | + |
| 447 | + // No clause name |
| 448 | + $result = Query::replaceClause($statements['statement'], $statements['parser']->list, ''); |
| 449 | + $this->assertEquals(' SELECT * FROM `test.2024-11-01` ORDER BY `test.2024-11-01`.`id` ASC', $result); |
| 450 | + } |
| 451 | + |
419 | 452 | /** |
420 | 453 | * @param string[] $expected |
421 | 454 | * |
@@ -659,6 +692,23 @@ public function testReplaceNonExistingPart(): void |
659 | 692 | ); |
660 | 693 | } |
661 | 694 |
|
| 695 | + public function testReplaceClauseTableWithDots(): void |
| 696 | + { |
| 697 | + $query = 'SELECT * FROM `test.2024-11-01` ORDER BY `test.2024-11-01`.`id` ASC;'; |
| 698 | + |
| 699 | + $parser = new Parser($query); |
| 700 | + |
| 701 | + $fromClause = Query::replaceClause($parser->statements[0], $parser->list, 'ORDER BY', ''); |
| 702 | + $this->assertEquals('SELECT * FROM `test.2024-11-01` ', $fromClause); |
| 703 | + |
| 704 | + $fromClause = Query::replaceClause($parser->statements[0], $parser->list, 'ORDER BY'); |
| 705 | + $this->assertEquals('SELECT * FROM `test.2024-11-01` ORDER BY ', $fromClause); |
| 706 | + |
| 707 | + // With spaces |
| 708 | + $fromClause = Query::replaceClause($parser->statements[0], $parser->list, 'ORDER BY ', ''); |
| 709 | + $this->assertEquals('SELECT * FROM `test.2024-11-01` ', $fromClause); |
| 710 | + } |
| 711 | + |
662 | 712 | public function testReplaceClauses(): void |
663 | 713 | { |
664 | 714 | $parser = new Parser('SELECT *, (SELECT 1) FROM film LIMIT 0, 10;'); |
|
0 commit comments