Skip to content
Snippets Groups Projects
Commit 7adc8e07 authored by Christian Eßl's avatar Christian Eßl Committed by Daniel Goerz
Browse files

[BUGFIX] Allow multiline comments in SqlReader

When using c-style multiline comments in the ext_tables.sql of an
extension, the SchemaMigrator would then ignore the subsequent
"CREATE TABLE" statement, because the SqlReader also read the multiline
comments into the statement array.
This patch fixes this behaviour.
Now the following comments are possible inside ext_tables.sql:

/*
 Some comment text
 more text
*/

/* Same line c-style comment */

/* More multiline
 comments */

CREATE TABLE table1 (
    header varchar(255) DEFAULT '' NOT NULL,
    bodytext text
);

Resolves: #89714
Releases: master, 9.5
Change-Id: I49e687215d0b42d6ca5416c687fdbc19b44d237d
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63951


Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: default avatarSusanne Moog <look@susi.dev>
Tested-by: default avatarDaniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: default avatarSusanne Moog <look@susi.dev>
Reviewed-by: default avatarDaniel Goerz <daniel.goerz@posteo.de>
parent f880e966
Branches
Tags
No related merge requests found
...@@ -89,11 +89,21 @@ class SqlReader ...@@ -89,11 +89,21 @@ class SqlReader
{ {
$statementArray = []; $statementArray = [];
$statementArrayPointer = 0; $statementArrayPointer = 0;
$isInMultilineComment = false;
foreach (explode(LF, $dumpContent) as $lineContent) { foreach (explode(LF, $dumpContent) as $lineContent) {
$lineContent = trim($lineContent); $lineContent = trim($lineContent);
// Skip empty lines and comments // Skip empty lines and comments
if ($lineContent === '' || $lineContent[0] === '#' || strpos($lineContent, '--') === 0) { if ($lineContent === '' || $lineContent[0] === '#' || strpos($lineContent, '--') === 0 ||
strpos($lineContent, '/*') === 0 || substr($lineContent, -2) === '*/' || $isInMultilineComment
) {
// skip c style multiline comments
if (strpos($lineContent, '/*') === 0 && substr($lineContent, -2) !== '*/') {
$isInMultilineComment = true;
}
if (substr($lineContent, -2) === '*/') {
$isInMultilineComment = false;
}
continue; continue;
} }
......
...@@ -111,4 +111,42 @@ class SqlReaderTest extends UnitTestCase ...@@ -111,4 +111,42 @@ class SqlReaderTest extends UnitTestCase
self::assertCount(1, $result); self::assertCount(1, $result);
self::assertStringStartsWith('CREATE TABLE', array_pop($result)); self::assertStringStartsWith('CREATE TABLE', array_pop($result));
} }
/**
* @param string $comment
* @dataProvider commentProvider
* @test
*/
public function getCreateTableStatementArrayResultWithComment(string $comment)
{
$subject = new SqlReader($this->prophesize(EventDispatcherInterface::class)->reveal(), $this->prophesize(PackageManager::class)->reveal());
$result = $subject->getCreateTableStatementArray(
$comment . LF . 'CREATE TABLE aTestTable(' . LF . ' aTestField INT(11)' . LF . ');' .
LF .
'INSERT INTO aTestTable(`aTestField`) VALUES(1);'
);
self::assertCount(1, $result);
self::assertStringStartsWith('CREATE TABLE', array_pop($result));
}
public function commentProvider(): array
{
return [
'Single line comment starting with "#"' => [
'# Comment'
],
'Single line comment starting with "--"' => [
'-- Comment'
],
'Single line c-style comment' => [
'/* Same line c-style comment */'
],
'Multiline comment variant 1' => [
'/*' . LF . 'Some comment text' . LF . 'more text' . LF . '*/'
],
'Multiline comment variant 2' => [
'/* More' . LF . ' comments */'
]
];
}
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment