Skip to content

Commit

Permalink
DDC-1360 - Add support for quoting dot chained identifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Dec 31, 2011
1 parent c811c2a commit 4cb8a99
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
21 changes: 19 additions & 2 deletions lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
Expand Up @@ -1276,7 +1276,8 @@ public function getCreatePrimaryKeySQL(Index $index, $table)

/**
* Quotes a string so that it can be safely used as a table or column name,
* even if it is a reserved word of the platform.
* even if it is a reserved word of the platform. This also detects identifier
* chains seperated by dot and quotes them independently.
*
* NOTE: Just because you CAN use quoted identifiers doesn't mean
* you SHOULD use them. In general, they end up causing way more
Expand All @@ -1286,6 +1287,22 @@ public function getCreatePrimaryKeySQL(Index $index, $table)
* @return string quoted identifier string
*/
public function quoteIdentifier($str)
{
if (strpos($str, ".") !== false) {
$parts = array_map(array($this, "quoteIdentifier"), explode(".", $str));
return implode(".", $parts);
}

return $this->quoteSingleIdentifier($str);
}

/**
* Quote a single identifier (no dot chain seperation)
*
* @param string $str
* @return string
*/
public function quoteSingleIdentifier($str)
{
$c = $this->getIdentifierQuoteCharacter();

Expand Down Expand Up @@ -2591,4 +2608,4 @@ protected function getReservedKeywordsClass()
{
throw DBALException::notSupported(__METHOD__);
}
}
}
12 changes: 2 additions & 10 deletions lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php
Expand Up @@ -831,17 +831,9 @@ protected function getReservedKeywordsClass()
}

/**
* Quotes a string so that it can be safely used as a table or column name,
* even if it is a reserved word of the platform.
*
* NOTE: Just because you CAN use quoted identifiers doesn't mean
* you SHOULD use them. In general, they end up causing way more
* problems than they solve.
*
* @param string $str identifier name to be quoted
* @return string quoted identifier string
* {@inheritDoc}
*/
public function quoteIdentifier($str)
public function quoteSingleIdentifier($str)
{
return "[" . str_replace("]", "][", $str) . "]";
}
Expand Down
20 changes: 20 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
Expand Up @@ -19,16 +19,36 @@ public function setUp()
$this->_platform = $this->createPlatform();
}

/**
* @group DDC-1360
*/
public function testQuoteIdentifier()
{
if ($this->_platform->getName() == "mssql") {
$this->markTestSkipped('Not working this way on mssql.');
}

$c = $this->_platform->getIdentifierQuoteCharacter();
$this->assertEquals($c."test".$c, $this->_platform->quoteIdentifier("test"));
$this->assertEquals($c."test".$c.".".$c."test".$c, $this->_platform->quoteIdentifier("test.test"));
$this->assertEquals(str_repeat($c, 4), $this->_platform->quoteIdentifier($c));
}

/**
* @group DDC-1360
*/
public function testQuoteSingleIdentifier()
{
if ($this->_platform->getName() == "mssql") {
$this->markTestSkipped('Not working this way on mssql.');
}

$c = $this->_platform->getIdentifierQuoteCharacter();
$this->assertEquals($c."test".$c, $this->_platform->quoteSingleIdentifier("test"));
$this->assertEquals($c."test.test".$c, $this->_platform->quoteSingleIdentifier("test.test"));
$this->assertEquals(str_repeat($c, 4), $this->_platform->quoteSingleIdentifier($c));
}

public function testGetInvalidtForeignKeyReferentialActionSQL()
{
$this->setExpectedException('InvalidArgumentException');
Expand Down
17 changes: 16 additions & 1 deletion tests/Doctrine/Tests/DBAL/Platforms/MsSqlPlatformTest.php
Expand Up @@ -171,8 +171,23 @@ public function testModifyLimitQueryWithDescOrderBy()
$this->assertEquals('SELECT TOP 10 * FROM user ORDER BY username DESC', $sql);
}

/**
* @group DDC-1360
*/
public function testQuoteIdentifier()
{
$this->assertEquals('[fo][o]', $this->_platform->quoteIdentifier('fo]o'));
$this->assertEquals('[test]', $this->_platform->quoteIdentifier('test'));
$this->assertEquals('[test].[test]', $this->_platform->quoteIdentifier('test.test'));
}
}

/**
* @group DDC-1360
*/
public function testQuoteSingleIdentifier()
{
$this->assertEquals('[fo][o]', $this->_platform->quoteSingleIdentifier('fo]o'));
$this->assertEquals('[test]', $this->_platform->quoteSingleIdentifier('test'));
$this->assertEquals('[test.test]', $this->_platform->quoteSingleIdentifier('test.test'));

This comment has been minimized.

Copy link
@guilhermeblanco

guilhermeblanco Dec 31, 2011

Member

This can never happen.
It must detect for schema separator (which I suggest to be a platform constant) and call quote multiple or single accordingly.

This comment has been minimized.

Copy link
@beberlei

beberlei Dec 31, 2011

Author Member

This is a test for the "wrong" quoting, i.e. using the function wrong.

}
}

0 comments on commit 4cb8a99

Please sign in to comment.