Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Adding a fix for #267. #268

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions src/Adapter/Platform/Sql92.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

class Sql92 extends AbstractPlatform
{
/**
* Overrides value from AbstractPlatform to use proper escaping for SQL92
*
* @var string
*/
protected $quoteIdentifierTo = '""';

/**
* {@inheritDoc}
*/
Expand Down
11 changes: 4 additions & 7 deletions src/Adapter/Platform/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@
class Sqlite extends AbstractPlatform
{
/**
* {@inheritDoc}
*/
protected $quoteIdentifier = ['"','"'];

/**
* {@inheritDoc}
* Overrides value from AbstractPlatform to use proper escaping for SQLite
*
* @var string
*/
protected $quoteIdentifierTo = '\'';
protected $quoteIdentifierTo = '""';

/**
* @var \PDO
Expand Down
2 changes: 2 additions & 0 deletions src/Sql/AbstractSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ protected function processExpression(
);
} elseif ($type == ExpressionInterface::TYPE_IDENTIFIER) {
$values[$vIndex] = $platform->quoteIdentifierInFragment($value);
} elseif ($type == ExpressionInterface::TYPE_IDENTIFIER_ATOMIC) {
$values[$vIndex] = $platform->quoteIdentifier($value);
} elseif ($type == ExpressionInterface::TYPE_VALUE) {
// if prepareType is set, it means that this particular value must be
// passed back to the statement in a way it can be used as a placeholder value
Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Ddl/Column/AbstractTimestampColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getExpressionData()
$params[] = $this->name;
$params[] = $this->type;

$types = [self::TYPE_IDENTIFIER, self::TYPE_LITERAL];
$types = [self::TYPE_IDENTIFIER_ATOMIC, self::TYPE_LITERAL];

if (! $this->isNullable) {
$spec .= ' NOT NULL';
Expand Down
2 changes: 1 addition & 1 deletion src/Sql/Ddl/Column/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function getExpressionData()
$params[] = $this->name;
$params[] = $this->type;

$types = [self::TYPE_IDENTIFIER, self::TYPE_LITERAL];
$types = [self::TYPE_IDENTIFIER_ATOMIC, self::TYPE_LITERAL];

if (! $this->isNullable) {
$spec .= ' NOT NULL';
Expand Down
4 changes: 2 additions & 2 deletions src/Sql/Ddl/Constraint/AbstractConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ public function getExpressionData()
if ($this->name) {
$newSpec .= $this->namedSpecification;
$values[] = $this->name;
$newSpecTypes[] = self::TYPE_IDENTIFIER;
$newSpecTypes[] = self::TYPE_IDENTIFIER_ATOMIC;
}

$newSpec .= $this->specification;

if ($colCount) {
$values = array_merge($values, $this->columns);
$newSpecParts = array_fill(0, $colCount, '%s');
$newSpecTypes = array_merge($newSpecTypes, array_fill(0, $colCount, self::TYPE_IDENTIFIER));
$newSpecTypes = array_merge($newSpecTypes, array_fill(0, $colCount, self::TYPE_IDENTIFIER_ATOMIC));
$newSpec .= sprintf($this->columnSpecification, implode(', ', $newSpecParts));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Sql/Ddl/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getExpressionData()
$colCount = count($this->columns);
$values = [];
$values[] = $this->name ?: '';
$newSpecTypes = [self::TYPE_IDENTIFIER];
$newSpecTypes = [self::TYPE_IDENTIFIER_ATOMIC];
$newSpecParts = [];

for ($i = 0; $i < $colCount; $i++) {
Expand All @@ -66,7 +66,7 @@ public function getExpressionData()
}

$newSpecParts[] = $specPart;
$newSpecTypes[] = self::TYPE_IDENTIFIER;
$newSpecTypes[] = self::TYPE_IDENTIFIER_ATOMIC;
}

$newSpec = str_replace('...', implode(', ', $newSpecParts), $this->specification);
Expand Down
1 change: 1 addition & 0 deletions src/Sql/ExpressionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
interface ExpressionInterface
{
const TYPE_IDENTIFIER = 'identifier';
const TYPE_IDENTIFIER_ATOMIC = 'identifier_atomic';
const TYPE_VALUE = 'value';
const TYPE_LITERAL = 'literal';
const TYPE_SELECT = 'select';
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/AbstractLengthColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testGetExpressionData()
$column = $this->getMockForAbstractClass('Zend\Db\Sql\Ddl\Column\AbstractLengthColumn', ['foo', 4]);

self::assertEquals(
[['%s %s NOT NULL', ['foo', 'INTEGER(4)'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'INTEGER(4)'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/AbstractPrecisionColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testGetExpressionData()
$column = $this->getMockForAbstractClass('Zend\Db\Sql\Ddl\Column\AbstractPrecisionColumn', ['foo', 10, 5]);

self::assertEquals(
[['%s %s NOT NULL', ['foo', 'INTEGER(10,5)'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'INTEGER(10,5)'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/BigIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testGetExpressionData()
{
$column = new BigInteger('foo');
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'BIGINT'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'BIGINT'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/BinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetExpressionData()
{
$column = new Binary('foo', 10000000);
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'BINARY(10000000)'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'BINARY(10000000)'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/BlobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetExpressionData()
{
$column = new Blob('foo');
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'BLOB'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'BLOB'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/BooleanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetExpressionData()
{
$column = new Boolean('foo');
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'BOOLEAN'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'BOOLEAN'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/CharTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetExpressionData()
{
$column = new Char('foo', 20);
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'CHAR(20)'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'CHAR(20)'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
6 changes: 3 additions & 3 deletions test/unit/Sql/Ddl/Column/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ public function testGetExpressionData()
$column = new Column;
$column->setName('foo');
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'INTEGER'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'INTEGER'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);

$column->setNullable(true);
self::assertEquals(
[['%s %s', ['foo', 'INTEGER'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s', ['foo', 'INTEGER'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);

Expand All @@ -124,7 +124,7 @@ public function testGetExpressionData()
[[
'%s %s DEFAULT %s',
['foo', 'INTEGER', 'bar'],
[$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL, $column::TYPE_VALUE],
[$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL, $column::TYPE_VALUE],
]],
$column->getExpressionData()
);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetExpressionData()
{
$column = new Date('foo');
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'DATE'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'DATE'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/DatetimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetExpressionData()
{
$column = new Datetime('foo');
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'DATETIME'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'DATETIME'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/DecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetExpressionData()
{
$column = new Decimal('foo', 10, 5);
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'DECIMAL(10,5)'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'DECIMAL(10,5)'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/FloatingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testGetExpressionData()
[[
'%s %s NOT NULL',
['foo', 'FLOAT(10,5)'],
[$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL],
[$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL],
]],
$column->getExpressionData()
);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/Sql/Ddl/Column/IntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public function testGetExpressionData()
{
$column = new Integer('foo');
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'INTEGER'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'INTEGER'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);

$column = new Integer('foo');
$column->addConstraint(new PrimaryKey());
self::assertEquals(
[
['%s %s NOT NULL', ['foo', 'INTEGER'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]],
['%s %s NOT NULL', ['foo', 'INTEGER'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]],
' ',
['PRIMARY KEY', [], []],
],
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetExpressionData()
{
$column = new Text('foo');
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'TEXT'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'TEXT'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetExpressionData()
{
$column = new Time('foo');
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'TIME'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'TIME'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/TimestampTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetExpressionData()
{
$column = new Timestamp('foo');
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'TIMESTAMP'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'TIMESTAMP'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Column/VarbinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetExpressionData()
{
$column = new Varbinary('foo', 20);
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'VARBINARY(20)'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'VARBINARY(20)'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/Sql/Ddl/Column/VarcharTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testGetExpressionData()
{
$column = new Varchar('foo', 20);
self::assertEquals(
[['%s %s NOT NULL', ['foo', 'VARCHAR(20)'], [$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL]]],
[['%s %s NOT NULL', ['foo', 'VARCHAR(20)'], [$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL]]],
$column->getExpressionData()
);

Expand All @@ -30,7 +30,7 @@ public function testGetExpressionData()
[[
'%s %s NOT NULL DEFAULT %s',
['foo', 'VARCHAR(20)', 'bar'],
[$column::TYPE_IDENTIFIER, $column::TYPE_LITERAL, $column::TYPE_VALUE],
[$column::TYPE_IDENTIFIER_ATOMIC, $column::TYPE_LITERAL, $column::TYPE_VALUE],
]],
$column->getExpressionData()
);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/Sql/Ddl/Constraint/ForeignKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public function testGetExpressionData()
'CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s) ON DELETE %s ON UPDATE %s',
['foo', 'bar', 'baz', 'bam', 'CASCADE', 'SET NULL'],
[
$fk::TYPE_IDENTIFIER,
$fk::TYPE_IDENTIFIER,
$fk::TYPE_IDENTIFIER_ATOMIC,
$fk::TYPE_IDENTIFIER_ATOMIC,
$fk::TYPE_IDENTIFIER,
$fk::TYPE_IDENTIFIER,
$fk::TYPE_LITERAL,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Constraint/PrimaryKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testGetExpressionData()
[[
'PRIMARY KEY (%s)',
['foo'],
[$pk::TYPE_IDENTIFIER],
[$pk::TYPE_IDENTIFIER_ATOMIC],
]],
$pk->getExpressionData()
);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/Ddl/Constraint/UniqueKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testGetExpressionData()
[[
'CONSTRAINT %s UNIQUE (%s)',
['my_uk', 'foo'],
[$uk::TYPE_IDENTIFIER, $uk::TYPE_IDENTIFIER],
[$uk::TYPE_IDENTIFIER_ATOMIC, $uk::TYPE_IDENTIFIER_ATOMIC],
]],
$uk->getExpressionData()
);
Expand Down
35 changes: 35 additions & 0 deletions test/unit/Sql/Ddl/CreateTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace ZendTest\Db\Sql\Ddl;

use PHPUnit\Framework\TestCase;
use Zend\Db\Adapter\Platform;
use Zend\Db\Sql\Ddl\Column\Column;
use Zend\Db\Sql\Ddl\Index\Index;
use Zend\Db\Sql\Ddl\Constraint;
use Zend\Db\Sql\Ddl\CreateTable;

Expand Down Expand Up @@ -162,5 +164,38 @@ public function testGetSqlString()
"CREATE TABLE \"foo\" ( \n PRIMARY KEY (\"bar\"),\n PRIMARY KEY (\"bat\") \n)",
$ct->getSqlString()
);

/**
* @link https://github.com/zendframework/zend-db/issues/266
*/
$ct = new CreateTable('t\'e"s`t');
$ct->addColumn(new Column('t\'e"s`tCol'));
$ct->addColumn(new Column('t\'e"s`tCol2'));
$ct->addConstraint(new Constraint\PrimaryKey('t\'e"s`tCol'));
$ct->addConstraint(new Index('t\'e"s`tCol2', 't\'e"s`tIndex'));
self::assertEquals(
"CREATE TABLE \"t'e\"\"s`t\" ( \n \"t'e\"\"s`tCol\" INTEGER NOT NULL,\n" .
" \"t'e\"\"s`tCol2\" INTEGER NOT NULL , \n PRIMARY KEY (\"t'e\"\"s`tCol\"),\n" .
" INDEX \"t'e\"\"s`tIndex\"(\"t'e\"\"s`tCol2\") \n)",
$ct->getSqlString()
);
self::assertEquals(
"CREATE TABLE `t'e\"s``t` ( \n `t'e\"s``tCol` INTEGER NOT NULL,\n" .
" `t'e\"s``tCol2` INTEGER NOT NULL , \n PRIMARY KEY (`t'e\"s``tCol`),\n" .
" INDEX `t'e\"s``tIndex`(`t'e\"s``tCol2`) \n)",
$ct->getSqlString(new Platform\Mysql())
);
self::assertEquals(
"CREATE TABLE \"t'e\"\"s`t\" ( \n \"t'e\"\"s`tCol\" INTEGER NOT NULL,\n" .
" \"t'e\"\"s`tCol2\" INTEGER NOT NULL , \n PRIMARY KEY (\"t'e\"\"s`tCol\"),\n" .
" INDEX \"t'e\"\"s`tIndex\"(\"t'e\"\"s`tCol2\") \n)",
$ct->getSqlString(new Platform\Postgresql())
);
self::assertEquals(
"CREATE TABLE \"t'e\"\"s`t\" ( \n \"t'e\"\"s`tCol\" INTEGER NOT NULL,\n" .
" \"t'e\"\"s`tCol2\" INTEGER NOT NULL , \n PRIMARY KEY (\"t'e\"\"s`tCol\"),\n" .
" INDEX \"t'e\"\"s`tIndex\"(\"t'e\"\"s`tCol2\") \n)",
$ct->getSqlString(new Platform\Sqlite())
);
}
}
Loading