From da1385ef5261f9ca0d6cd148f26ccb3770566cd1 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Thu, 28 Mar 2024 13:21:01 +0200 Subject: [PATCH 1/9] Added test case --- .../Generator/Platform/Util/AlterTableStatementMerger.php | 5 +++++ .../Generator/Platform/PlatformMigrationTestProvider.php | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Propel/Generator/Platform/Util/AlterTableStatementMerger.php b/src/Propel/Generator/Platform/Util/AlterTableStatementMerger.php index 1abca18b2..0916faa22 100644 --- a/src/Propel/Generator/Platform/Util/AlterTableStatementMerger.php +++ b/src/Propel/Generator/Platform/Util/AlterTableStatementMerger.php @@ -9,6 +9,7 @@ namespace Propel\Generator\Platform\Util; use Propel\Generator\Model\Table; +use Propel\Generator\Util\SqlParser; /** * Merges several ALTER TABLE statements when creating migrations. @@ -67,6 +68,10 @@ public function __construct(Table $table) public function mergeStatements(string $sql): string { $statements = explode(';', $sql); +// $sqlParser = new SqlParser(); +// $sqlParser->setSQL($sql); +// $statements = $sqlParser->explodeIntoStatements(); + $blocks = []; $currentBlock = []; diff --git a/tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php b/tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php index 8c048786e..6fa891bf2 100644 --- a/tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php +++ b/tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php @@ -107,7 +107,7 @@ public function providerForTestGetModifyTableDDL() - + From 15f951f8f7559f0a99df29f25442064c9ecb18f6 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Thu, 28 Mar 2024 13:25:22 +0200 Subject: [PATCH 2/9] Uses Sql parser for string --- .../Generator/Platform/Util/AlterTableStatementMerger.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Propel/Generator/Platform/Util/AlterTableStatementMerger.php b/src/Propel/Generator/Platform/Util/AlterTableStatementMerger.php index 0916faa22..e7020a325 100644 --- a/src/Propel/Generator/Platform/Util/AlterTableStatementMerger.php +++ b/src/Propel/Generator/Platform/Util/AlterTableStatementMerger.php @@ -67,10 +67,9 @@ public function __construct(Table $table) */ public function mergeStatements(string $sql): string { - $statements = explode(';', $sql); -// $sqlParser = new SqlParser(); -// $sqlParser->setSQL($sql); -// $statements = $sqlParser->explodeIntoStatements(); + $sqlParser = new SqlParser(); + $sqlParser->setSQL($sql); + $statements = $sqlParser->explodeIntoStatements(); $blocks = []; $currentBlock = []; From 4b57173bfb1bfdbcbb5f83a2f08d2f22b5918f44 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Thu, 28 Mar 2024 13:31:40 +0200 Subject: [PATCH 3/9] Adjusted tests --- .../Tests/Generator/Platform/MysqlPlatformMigrationTest.php | 2 +- .../Tests/Generator/Platform/OraclePlatformMigrationTest.php | 2 +- .../Tests/Generator/Platform/PgsqlPlatformMigrationTest.php | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php index 34f3f5a6b..8f54b376f 100644 --- a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php +++ b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php @@ -86,7 +86,7 @@ public function testRenameTableDDL($databaseDiff) CHANGE `bar` `bar1` INTEGER, - CHANGE `baz` `baz` VARCHAR(12), + CHANGE `baz` `baz` VARCHAR(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt', ADD `baz3` TEXT AFTER `baz`; diff --git a/tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php b/tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php index 841a321ef..b6ca8135d 100644 --- a/tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php +++ b/tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php @@ -105,7 +105,7 @@ public function testGetModifyTableDDL($tableDiff) MODIFY ( - baz NVARCHAR2(12) + baz NVARCHAR2(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt' ), ADD diff --git a/tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php b/tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php index 3a2838be9..13aa5e3c7 100755 --- a/tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php +++ b/tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php @@ -99,6 +99,8 @@ public function testGetModifyTableDDL($tableDiff) ALTER TABLE "foo" + ALTER COLUMN "baz" SET DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt', + ALTER COLUMN "baz" DROP NOT NULL, ADD "baz3" TEXT; From 444084c1326b4b538118b938d2f9d6e022110347 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Thu, 28 Mar 2024 13:34:24 +0200 Subject: [PATCH 4/9] Adjusted tests --- .../Generator/Platform/MysqlPlatformMigrationMyISAMTest.php | 2 +- .../Tests/Generator/Platform/MysqlPlatformMigrationTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php index 4bf6badd1..845c87ba0 100644 --- a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php +++ b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php @@ -131,7 +131,7 @@ public function testGetModifyTableDDL($tableDiff) CHANGE `bar` `bar1` INTEGER, - CHANGE `baz` `baz` VARCHAR(12), + CHANGE `baz` `baz` VARCHAR(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt', ADD `baz3` TEXT AFTER `baz`; diff --git a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php index 8f54b376f..8cefaf3e7 100644 --- a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php +++ b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php @@ -86,7 +86,7 @@ public function testRenameTableDDL($databaseDiff) CHANGE `bar` `bar1` INTEGER, - CHANGE `baz` `baz` VARCHAR(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt', + CHANGE `baz` `baz` VARCHAR(12), ADD `baz3` TEXT AFTER `baz`; @@ -139,7 +139,7 @@ public function testGetModifyTableDDL($tableDiff) CHANGE `bar` `bar1` INTEGER, - CHANGE `baz` `baz` VARCHAR(12), + CHANGE `baz` `baz` VARCHAR(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt', ADD `baz3` TEXT AFTER `baz`; From 518f28dab80a38350407c0762ce4bb5abe421116 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Thu, 28 Mar 2024 14:39:33 +0200 Subject: [PATCH 5/9] Adjusted tests --- .../Generator/Platform/MysqlPlatformMigrationMyISAMTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php index 845c87ba0..4bf6badd1 100644 --- a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php +++ b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php @@ -131,7 +131,7 @@ public function testGetModifyTableDDL($tableDiff) CHANGE `bar` `bar1` INTEGER, - CHANGE `baz` `baz` VARCHAR(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt', + CHANGE `baz` `baz` VARCHAR(12), ADD `baz3` TEXT AFTER `baz`; From 63bc05d2a4c2b9cbda97d2e6e69eb9f0a4463701 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Thu, 28 Mar 2024 14:42:20 +0200 Subject: [PATCH 6/9] Adjusted tests --- .../Generator/Platform/MysqlPlatformMigrationMyISAMTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php index 4bf6badd1..845c87ba0 100644 --- a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php +++ b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php @@ -131,7 +131,7 @@ public function testGetModifyTableDDL($tableDiff) CHANGE `bar` `bar1` INTEGER, - CHANGE `baz` `baz` VARCHAR(12), + CHANGE `baz` `baz` VARCHAR(12) DEFAULT 'pdf;jpg;png;doc;docx;xls;xlsx;txt', ADD `baz3` TEXT AFTER `baz`; From 1a726f588ab5150bfa25173de3b8ce27da592c41 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Fri, 29 Mar 2024 08:37:24 +0200 Subject: [PATCH 7/9] Adjusted SQL parser to skip comments --- src/Propel/Generator/Util/SqlParser.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Propel/Generator/Util/SqlParser.php b/src/Propel/Generator/Util/SqlParser.php index b22d8dcc8..38be757c2 100644 --- a/src/Propel/Generator/Util/SqlParser.php +++ b/src/Propel/Generator/Util/SqlParser.php @@ -232,14 +232,30 @@ public function explodeIntoStatements(): array public function getNextStatement(): string { $isAfterBackslash = false; + $isCommentLine = false; $isInString = false; $stringQuotes = ''; $parsedString = ''; $lowercaseString = ''; // helper variable for performance sake while ($this->pos <= $this->len) { $char = $this->sql[$this->pos] ?? ''; + if ($isCommentLine === true && $char !== "\n") { + $this->pos++; + + continue; + } // check flags for strings or escaper switch ($char) { + case '#': + $isCommentLine = true; + + continue 2; + case "\n": + if ($isCommentLine === true) { + $isCommentLine = false; + } + + break; case '\\': $isAfterBackslash = true; @@ -295,8 +311,9 @@ public function getNextStatement(): string // check for end of statement if ($char . $nextChars == $this->delimiter) { $this->pos += $i; // increase position + $parsedTrimmedString = trim($parsedString); - return trim($parsedString); + return $parsedTrimmedString ? $parsedTrimmedString : $parsedString; // empty line } // avoid using strtolower on the whole parsed string every time new character is added // there is also no point in adding characters which are in the string From 94561f5c2c4ab793b21f57c44e1ac696a292cbbb Mon Sep 17 00:00:00 2001 From: vol4onok Date: Fri, 29 Mar 2024 15:20:06 +0200 Subject: [PATCH 8/9] Adjusted code --- src/Propel/Generator/Util/SqlParser.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Propel/Generator/Util/SqlParser.php b/src/Propel/Generator/Util/SqlParser.php index 38be757c2..5439c3704 100644 --- a/src/Propel/Generator/Util/SqlParser.php +++ b/src/Propel/Generator/Util/SqlParser.php @@ -239,7 +239,8 @@ public function getNextStatement(): string $lowercaseString = ''; // helper variable for performance sake while ($this->pos <= $this->len) { $char = $this->sql[$this->pos] ?? ''; - if ($isCommentLine === true && $char !== "\n") { + // Skip comments + if ($isCommentLine === true && $char !== PHP_EOL) { $this->pos++; continue; @@ -247,9 +248,12 @@ public function getNextStatement(): string // check flags for strings or escaper switch ($char) { case '#': - $isCommentLine = true; + // detect comment line + if ($this->sql[$this->pos--] === PHP_EOL) { + $isCommentLine = true; - continue 2; + continue 2; + } case "\n": if ($isCommentLine === true) { $isCommentLine = false; @@ -313,7 +317,7 @@ public function getNextStatement(): string $this->pos += $i; // increase position $parsedTrimmedString = trim($parsedString); - return $parsedTrimmedString ? $parsedTrimmedString : $parsedString; // empty line + return $parsedTrimmedString ?: $parsedString; // to check empty line to avoid stop parsing } // avoid using strtolower on the whole parsed string every time new character is added // there is also no point in adding characters which are in the string From d7b87fd40656d49dc382456ee2f5c7044e2669a9 Mon Sep 17 00:00:00 2001 From: vol4onok Date: Fri, 29 Mar 2024 16:27:11 +0200 Subject: [PATCH 9/9] Adjusted code --- src/Propel/Generator/Util/SqlParser.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Propel/Generator/Util/SqlParser.php b/src/Propel/Generator/Util/SqlParser.php index 5439c3704..175cc86ff 100644 --- a/src/Propel/Generator/Util/SqlParser.php +++ b/src/Propel/Generator/Util/SqlParser.php @@ -239,9 +239,15 @@ public function getNextStatement(): string $lowercaseString = ''; // helper variable for performance sake while ($this->pos <= $this->len) { $char = $this->sql[$this->pos] ?? ''; + + $newLine = !isset($this->sql[$this->pos - 1]) || $this->sql[$this->pos - 1] === PHP_EOL; + // Skip comments - if ($isCommentLine === true && $char !== PHP_EOL) { + if ($isCommentLine === true) { $this->pos++; + if ($char === PHP_EOL) { + $isCommentLine = false; // end of comments line + } continue; } @@ -249,15 +255,12 @@ public function getNextStatement(): string switch ($char) { case '#': // detect comment line - if ($this->sql[$this->pos--] === PHP_EOL) { + if ($newLine === true && $isCommentLine === false) { + $this->pos++; $isCommentLine = true; continue 2; } - case "\n": - if ($isCommentLine === true) { - $isCommentLine = false; - } break; case '\\':