From ebc996c8206c5de5f556fe19ffab24ba16f2d25f Mon Sep 17 00:00:00 2001 From: Mike Meier Date: Fri, 15 Mar 2013 13:53:22 +0100 Subject: [PATCH 01/11] Update SqlWalker.php Always be sure that only a-z characters are used for table alias, otherwise use generic "t" for "table" --- lib/Doctrine/ORM/Query/SqlWalker.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 35ac87c38..e85d9caec 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -291,7 +291,11 @@ class SqlWalker implements TreeWalker $tableName .= ($dqlAlias) ? '@[' . $dqlAlias . ']' : ''; if ( ! isset($this->tableAliasMap[$tableName])) { - $this->tableAliasMap[$tableName] = strtolower(substr($tableName, 0, 1)) . $this->tableAliasCounter++ . '_'; + $tablePrefixAlias = strtolower(substr($tableName, 0, 1)); + if( ! preg_match("/[a-z]/", $tablePrefixAlias)) { + $tablePrefixAlias = 't'; + } + $this->tableAliasMap[$tableName] = $tablePrefixAlias . $this->tableAliasCounter++ . '_'; } return $this->tableAliasMap[$tableName]; From fb5f76f0256a926a09113213993ae74a9aa25078 Mon Sep 17 00:00:00 2001 From: Mike Meier Date: Fri, 15 Mar 2013 14:19:41 +0100 Subject: [PATCH 02/11] rewrite tablePrefixAlias --- lib/Doctrine/ORM/Query/SqlWalker.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index e85d9caec..31151debe 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -291,10 +291,7 @@ class SqlWalker implements TreeWalker $tableName .= ($dqlAlias) ? '@[' . $dqlAlias . ']' : ''; if ( ! isset($this->tableAliasMap[$tableName])) { - $tablePrefixAlias = strtolower(substr($tableName, 0, 1)); - if( ! preg_match("/[a-z]/", $tablePrefixAlias)) { - $tablePrefixAlias = 't'; - } + $tablePrefixAlias = preg_match('~[a-z]~i', $tableName[0]) ? strtolower($tableName[0]) : 't'; $this->tableAliasMap[$tableName] = $tablePrefixAlias . $this->tableAliasCounter++ . '_'; } From 109b0444f5da780dfbf58bc5d840e8df0953753a Mon Sep 17 00:00:00 2001 From: Mike Meier Date: Mon, 18 Mar 2013 11:10:14 +0100 Subject: [PATCH 03/11] add SqlWalkerTest for getSQLTableAlias method --- .../Tests/ORM/Query/SqlWalkerTest.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php diff --git a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php new file mode 100644 index 000000000..14be24073 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php @@ -0,0 +1,20 @@ +_getTestEntityManager()); + $sqlWalker = new SqlWalker($query, new ParserResult(), array()); + + $this->assertSame('t0_', $sqlWalker->getSQLTableAlias('table')); + $this->assertSame('t1_', $sqlWalker->getSQLTableAlias('çtable')); + } +} \ No newline at end of file From a4e9c235f8a3e38f2ad220b988cfcb3e8d07a645 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 19 Oct 2014 17:08:33 +0200 Subject: [PATCH 04/11] #615 - assignment alignment --- lib/Doctrine/ORM/Query/SqlWalker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 31151debe..9adcd1a08 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -291,7 +291,7 @@ class SqlWalker implements TreeWalker $tableName .= ($dqlAlias) ? '@[' . $dqlAlias . ']' : ''; if ( ! isset($this->tableAliasMap[$tableName])) { - $tablePrefixAlias = preg_match('~[a-z]~i', $tableName[0]) ? strtolower($tableName[0]) : 't'; + $tablePrefixAlias = preg_match('/[a-z]/i', $tableName[0]) ? strtolower($tableName[0]) : 't'; $this->tableAliasMap[$tableName] = $tablePrefixAlias . $this->tableAliasCounter++ . '_'; } From d557a0e01bd3a0ad273eab9f193f29547172f78f Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 19 Oct 2014 17:09:27 +0200 Subject: [PATCH 05/11] #615 - assignment alignment --- tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php index 14be24073..31abc1a7f 100644 --- a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php @@ -11,7 +11,7 @@ class SqlWalkerTest extends OrmTestCase { public function testGetSQLTableAlias() { - $query = new Query($this->_getTestEntityManager()); + $query = new Query($this->_getTestEntityManager()); $sqlWalker = new SqlWalker($query, new ParserResult(), array()); $this->assertSame('t0_', $sqlWalker->getSQLTableAlias('table')); From 435befd25cac8ff8705ff4b8d2c35c236dfcd72b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 19 Oct 2014 17:09:41 +0200 Subject: [PATCH 06/11] #615 - EOF EOL fixes --- tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php index 31abc1a7f..de5a1b0cc 100644 --- a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php @@ -17,4 +17,4 @@ class SqlWalkerTest extends OrmTestCase $this->assertSame('t0_', $sqlWalker->getSQLTableAlias('table')); $this->assertSame('t1_', $sqlWalker->getSQLTableAlias('çtable')); } -} \ No newline at end of file +} From 17c93884730b159cfcf2b4bffdd5baf84734e557 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 19 Oct 2014 17:10:39 +0200 Subject: [PATCH 07/11] #615 - Test coverage annotations --- tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php index de5a1b0cc..6d9d3e257 100644 --- a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php @@ -7,6 +7,11 @@ use Doctrine\ORM\Query; use Doctrine\Tests\OrmTestCase; use Doctrine\ORM\Query\ParserResult; +/** + * Tests for {@see \Doctrine\ORM\Query\SqlWalker} + * + * @covers \Doctrine\ORM\Query\SqlWalker + */ class SqlWalkerTest extends OrmTestCase { public function testGetSQLTableAlias() From 10da2eb85b874fec7b6aa6a3ac11350a63db5103 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 19 Oct 2014 17:16:07 +0200 Subject: [PATCH 08/11] #615 - Using a data provider to build aliases for the SqlWalkerTest --- .../Tests/ORM/Query/SqlWalkerTest.php | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php index 6d9d3e257..d057fc8e0 100644 --- a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php @@ -14,12 +14,28 @@ use Doctrine\ORM\Query\ParserResult; */ class SqlWalkerTest extends OrmTestCase { - public function testGetSQLTableAlias() + /** + * @dataProvider getColumnNamesAndSqlAliases + */ + public function testGetSQLTableAlias($tableName, $expectedAlias) { $query = new Query($this->_getTestEntityManager()); $sqlWalker = new SqlWalker($query, new ParserResult(), array()); - $this->assertSame('t0_', $sqlWalker->getSQLTableAlias('table')); - $this->assertSame('t1_', $sqlWalker->getSQLTableAlias('çtable')); + $this->assertSame($expectedAlias, $sqlWalker->getSQLTableAlias($tableName)); + } + + /** + * @private data provider + * + * @return string[][] + */ + public function getColumnNamesAndSqlAliases() + { + return array( + array('aaaaa', 'a0_'), + array('table', 't0_'), + array('çtable', 't0_'), + ); } } From 69494dc624c5bb4122256c48578cd017e01f0056 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 19 Oct 2014 17:18:30 +0200 Subject: [PATCH 09/11] #615 - sql table aliases should stay constant over time --- tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php index d057fc8e0..1ea0808b1 100644 --- a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php @@ -25,6 +25,17 @@ class SqlWalkerTest extends OrmTestCase $this->assertSame($expectedAlias, $sqlWalker->getSQLTableAlias($tableName)); } + /** + * @dataProvider getColumnNamesAndSqlAliases + */ + public function testGetSQLTableAliasIsSameForMultipleCalls($tableName) + { + $query = new Query($this->_getTestEntityManager()); + $sqlWalker = new SqlWalker($query, new ParserResult(), array()); + + $this->assertSame($sqlWalker->getSQLTableAlias($tableName), $sqlWalker->getSQLTableAlias($tableName)); + } + /** * @private data provider * From 264cd8ee11bbf3a956fa9c6d63272a4e2a243330 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 19 Oct 2014 17:21:13 +0200 Subject: [PATCH 10/11] #615 - Refactoring test case to remove duplication --- .../Tests/ORM/Query/SqlWalkerTest.php | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php index 1ea0808b1..263cffc56 100644 --- a/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SqlWalkerTest.php @@ -14,15 +14,25 @@ use Doctrine\ORM\Query\ParserResult; */ class SqlWalkerTest extends OrmTestCase { + /** + * @var SqlWalker + */ + private $sqlWalker; + + /** + * {@inheritDoc} + */ + protected function setUp() + { + $this->sqlWalker = new SqlWalker(new Query($this->_getTestEntityManager()), new ParserResult(), array()); + } + /** * @dataProvider getColumnNamesAndSqlAliases */ public function testGetSQLTableAlias($tableName, $expectedAlias) { - $query = new Query($this->_getTestEntityManager()); - $sqlWalker = new SqlWalker($query, new ParserResult(), array()); - - $this->assertSame($expectedAlias, $sqlWalker->getSQLTableAlias($tableName)); + $this->assertSame($expectedAlias, $this->sqlWalker->getSQLTableAlias($tableName)); } /** @@ -30,10 +40,10 @@ class SqlWalkerTest extends OrmTestCase */ public function testGetSQLTableAliasIsSameForMultipleCalls($tableName) { - $query = new Query($this->_getTestEntityManager()); - $sqlWalker = new SqlWalker($query, new ParserResult(), array()); - - $this->assertSame($sqlWalker->getSQLTableAlias($tableName), $sqlWalker->getSQLTableAlias($tableName)); + $this->assertSame( + $this->sqlWalker->getSQLTableAlias($tableName), + $this->sqlWalker->getSQLTableAlias($tableName) + ); } /** From b9891757540f5ecc6c6ef21e8115ef2c691dfcab Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 19 Oct 2014 17:24:07 +0200 Subject: [PATCH 11/11] #615 - Removing unused assignment --- lib/Doctrine/ORM/Query/SqlWalker.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 9adcd1a08..c24fa40cc 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -291,8 +291,8 @@ class SqlWalker implements TreeWalker $tableName .= ($dqlAlias) ? '@[' . $dqlAlias . ']' : ''; if ( ! isset($this->tableAliasMap[$tableName])) { - $tablePrefixAlias = preg_match('/[a-z]/i', $tableName[0]) ? strtolower($tableName[0]) : 't'; - $this->tableAliasMap[$tableName] = $tablePrefixAlias . $this->tableAliasCounter++ . '_'; + $this->tableAliasMap[$tableName] = (preg_match('/[a-z]/i', $tableName[0]) ? strtolower($tableName[0]) : 't') + . $this->tableAliasCounter++ . '_'; } return $this->tableAliasMap[$tableName];