Commit 4f77e750 authored by Benjamin Eberlei's avatar Benjamin Eberlei

DDC-1360 - Add support for quoting dot chained identifiers.

parent 5df86161
...@@ -1276,7 +1276,8 @@ abstract class AbstractPlatform ...@@ -1276,7 +1276,8 @@ abstract class AbstractPlatform
/** /**
* Quotes a string so that it can be safely used as a table or column name, * 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 * NOTE: Just because you CAN use quoted identifiers doesn't mean
* you SHOULD use them. In general, they end up causing way more * you SHOULD use them. In general, they end up causing way more
...@@ -1286,6 +1287,22 @@ abstract class AbstractPlatform ...@@ -1286,6 +1287,22 @@ abstract class AbstractPlatform
* @return string quoted identifier string * @return string quoted identifier string
*/ */
public function quoteIdentifier($str) 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(); $c = $this->getIdentifierQuoteCharacter();
......
...@@ -831,17 +831,9 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -831,17 +831,9 @@ class MsSqlPlatform extends AbstractPlatform
} }
/** /**
* Quotes a string so that it can be safely used as a table or column name, * {@inheritDoc}
* 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
*/ */
public function quoteIdentifier($str) public function quoteSingleIdentifier($str)
{ {
return "[" . str_replace("]", "][", $str) . "]"; return "[" . str_replace("]", "][", $str) . "]";
} }
......
...@@ -19,6 +19,9 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -19,6 +19,9 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
$this->_platform = $this->createPlatform(); $this->_platform = $this->createPlatform();
} }
/**
* @group DDC-1360
*/
public function testQuoteIdentifier() public function testQuoteIdentifier()
{ {
if ($this->_platform->getName() == "mssql") { if ($this->_platform->getName() == "mssql") {
...@@ -26,9 +29,26 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -26,9 +29,26 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
} }
$c = $this->_platform->getIdentifierQuoteCharacter(); $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)); $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() public function testGetInvalidtForeignKeyReferentialActionSQL()
{ {
$this->setExpectedException('InvalidArgumentException'); $this->setExpectedException('InvalidArgumentException');
......
...@@ -171,8 +171,23 @@ class MsSqlPlatformTest extends AbstractPlatformTestCase ...@@ -171,8 +171,23 @@ class MsSqlPlatformTest extends AbstractPlatformTestCase
$this->assertEquals('SELECT TOP 10 * FROM user ORDER BY username DESC', $sql); $this->assertEquals('SELECT TOP 10 * FROM user ORDER BY username DESC', $sql);
} }
/**
* @group DDC-1360
*/
public function testQuoteIdentifier() public function testQuoteIdentifier()
{ {
$this->assertEquals('[fo][o]', $this->_platform->quoteIdentifier('fo]o')); $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'));
} }
} }
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