Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test case #1997

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -66,7 +67,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 = [];

Expand Down
26 changes: 25 additions & 1 deletion src/Propel/Generator/Util/SqlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,37 @@ 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] ?? '';

$newLine = !isset($this->sql[$this->pos - 1]) || $this->sql[$this->pos - 1] === PHP_EOL;

// Skip comments
if ($isCommentLine === true) {
$this->pos++;
if ($char === PHP_EOL) {
$isCommentLine = false; // end of comments line
}

continue;
}
// check flags for strings or escaper
switch ($char) {
case '#':
// detect comment line
if ($newLine === true && $isCommentLine === false) {
$this->pos++;
$isCommentLine = true;

continue 2;
}

break;
case '\\':
$isAfterBackslash = true;

Expand Down Expand Up @@ -295,8 +318,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 ?: $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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function providerForTestGetModifyTableDDL()
<table name="foo">
<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true"/>
<column name="bar1" type="INTEGER"/>
<column name="baz" type="VARCHAR" size="12" required="false"/>
<column name="baz" type="VARCHAR" size="12" required="false" defaultValue="pdf;jpg;png;doc;docx;xls;xlsx;txt"/>
<column name="baz3" type="LONGVARCHAR"/>
<foreign-key name="foo1_fk_1" foreignTable="foo2">
<reference local="bar1" foreign="bar"/>
Expand Down
Loading