Use NULL instead of false when parsing SQL queries

The reasoning behind this is to essentially rely on a single type (or
the absence of it), therefore add type declarations.

Since these are private methods we can do that without breaking BC.
parent 9dfaed2a
......@@ -264,7 +264,7 @@ class SqliteSchemaManager extends AbstractSchemaManager
$comment = $this->parseColumnCommentFromSQL($columnName, $createSql);
if (false !== $comment) {
if ($comment !== null) {
$type = $this->extractDoctrineTypeFromComment($comment, null);
if (null !== $type) {
......@@ -435,38 +435,29 @@ class SqliteSchemaManager extends AbstractSchemaManager
return $tableDiff;
}
/**
* @param string $column
* @param string $sql
*
* @return string|false
*/
private function parseColumnCollationFromSQL($column, $sql)
private function parseColumnCollationFromSQL(string $column, string $sql) : ?string
{
$pattern = '{(?:\W' . preg_quote($column) . '\W|\W' . preg_quote($this->_platform->quoteSingleIdentifier($column))
. '\W)[^,(]+(?:\([^()]+\)[^,]*)?(?:(?:DEFAULT|CHECK)\s*(?:\(.*?\))?[^,]*)*COLLATE\s+["\']?([^\s,"\')]+)}isx';
if (preg_match($pattern, $sql, $match) === 1) {
return $match[1];
if (preg_match($pattern, $sql, $match) !== 1) {
return null;
}
return false;
return $match[1];
}
/**
* @return string|false
*/
private function parseColumnCommentFromSQL($column, $sql)
private function parseColumnCommentFromSQL(string $column, string $sql) : ?string
{
$pattern = '{[\s(,](?:\W' . preg_quote($this->_platform->quoteSingleIdentifier($column)) . '\W|\W' . preg_quote($column)
. '\W)(?:\(.*?\)|[^,(])*?,?((?:(?!\n))(?:\s*--[^\n]*\n?)+)}ix';
if (preg_match($pattern, $sql, $match) === 1) {
$comment = preg_replace('{^\s*--}m', '', rtrim($match[1], "\n"));
return '' === $comment ? false : $comment;
if (preg_match($pattern, $sql, $match) !== 1) {
return null;
}
return false;
$comment = preg_replace('{^\s*--}m', '', rtrim($match[1], "\n"));
return '' === $comment ? null : $comment;
}
}
......@@ -13,7 +13,7 @@ class SqliteSchemaManagerTest extends \PHPUnit_Framework_TestCase
*
* @group 2865
*/
public function testParseColumnCollation($collation, string $column, string $sql) : void
public function testParseColumnCollation(?string $collation, string $column, string $sql) : void
{
$conn = $this->createMock(Connection::class);
$conn->method('getDatabasePlatform')->willReturn(new SqlitePlatform());
......@@ -31,7 +31,7 @@ class SqliteSchemaManagerTest extends \PHPUnit_Framework_TestCase
['RTRIM', 'a', 'CREATE TABLE "a" ("a" text DEFAULT "aa" COLLATE "RTRIM" NOT NULL)'],
['utf-8', 'a', 'CREATE TABLE "a" ("b" text UNIQUE NOT NULL COLLATE NOCASE, "a" text DEFAULT "aa" COLLATE "utf-8" NOT NULL)'],
['NOCASE', 'a', 'CREATE TABLE "a" ("a" text DEFAULT (lower(ltrim(" a") || rtrim("a "))) CHECK ("a") NOT NULL COLLATE NOCASE UNIQUE, "b" text COLLATE RTRIM)'],
[false, 'a', 'CREATE TABLE "a" ("a" text CHECK ("a") NOT NULL, "b" text COLLATE RTRIM)'],
[null, 'a', 'CREATE TABLE "a" ("a" text CHECK ("a") NOT NULL, "b" text COLLATE RTRIM)'],
['RTRIM', 'a"b', 'CREATE TABLE "a" ("a""b" text COLLATE RTRIM)'],
['BINARY', 'b', 'CREATE TABLE "a" (bb TEXT COLLATE RTRIM, b VARCHAR(42) NOT NULL COLLATE BINARY)'],
['BINARY', 'b', 'CREATE TABLE "a" (bbb TEXT COLLATE NOCASE, bb TEXT COLLATE RTRIM, b VARCHAR(42) NOT NULL COLLATE BINARY)'],
......@@ -44,7 +44,7 @@ class SqliteSchemaManagerTest extends \PHPUnit_Framework_TestCase
*
* @group 2865
*/
public function testParseColumnCommentFromSQL($comment, string $column, string $sql) : void
public function testParseColumnCommentFromSQL(?string $comment, string $column, string $sql) : void
{
$conn = $this->createMock(Connection::class);
$conn->method('getDatabasePlatform')->willReturn(new SqlitePlatform());
......@@ -60,14 +60,14 @@ class SqliteSchemaManagerTest extends \PHPUnit_Framework_TestCase
{
return [
'Single column with no comment' => [
false, 'a', 'CREATE TABLE "a" ("a" TEXT DEFAULT "a" COLLATE RTRIM)',
null, 'a', 'CREATE TABLE "a" ("a" TEXT DEFAULT "a" COLLATE RTRIM)',
],
'Single column with type comment' => [
'(DC2Type:x)', 'a', 'CREATE TABLE "a" ("a" CLOB DEFAULT NULL COLLATE BINARY --(DC2Type:x)
)',
],
'Multiple similar columns with type comment 1' => [
false, 'b', 'CREATE TABLE "a" (a TEXT COLLATE RTRIM, "b" TEXT DEFAULT "a" COLLATE RTRIM, "bb" CLOB DEFAULT NULL COLLATE BINARY --(DC2Type:x)
null, 'b', 'CREATE TABLE "a" (a TEXT COLLATE RTRIM, "b" TEXT DEFAULT "a" COLLATE RTRIM, "bb" CLOB DEFAULT NULL COLLATE BINARY --(DC2Type:x)
)',
],
'Multiple similar columns with type comment 2' => [
......@@ -75,7 +75,7 @@ class SqliteSchemaManagerTest extends \PHPUnit_Framework_TestCase
)',
],
'Multiple similar columns on different lines, with type comment 1' => [
false, 'bb', 'CREATE TABLE "a" (a TEXT COLLATE RTRIM, "b" CLOB DEFAULT NULL COLLATE BINARY --(DC2Type:x)
null, 'bb', 'CREATE TABLE "a" (a TEXT COLLATE RTRIM, "b" CLOB DEFAULT NULL COLLATE BINARY --(DC2Type:x)
, "bb" TEXT DEFAULT "a" COLLATE RTRIM',
],
'Multiple similar columns on different lines, with type comment 2' => [
......@@ -83,12 +83,12 @@ class SqliteSchemaManagerTest extends \PHPUnit_Framework_TestCase
, "b" TEXT DEFAULT "a" COLLATE RTRIM',
],
'Column with numeric but no comment 1' => [
false, 'a', 'CREATE TABLE "a" ("a" NUMERIC(10, 0) NOT NULL, "b" CLOB NOT NULL --(DC2Type:array)
null, 'a', 'CREATE TABLE "a" ("a" NUMERIC(10, 0) NOT NULL, "b" CLOB NOT NULL --(DC2Type:array)
, "c" CHAR(36) NOT NULL --(DC2Type:guid)
)',
],
'Column with numeric but no comment 2' => [
false, 'a', 'CREATE TABLE "b" ("a" NUMERIC(10, 0) NOT NULL, "b" CLOB NOT NULL --(DC2Type:array)
null, 'a', 'CREATE TABLE "b" ("a" NUMERIC(10, 0) NOT NULL, "b" CLOB NOT NULL --(DC2Type:array)
, "c" CHAR(36) NOT NULL --(DC2Type:guid)
)',
],
......
Markdown is supported
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