From 296ab127a114eb86dcb7ea98b611ae33dc136147 Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Sat, 11 Mar 2017 00:50:47 -0500 Subject: [PATCH 1/3] #206 Allow expression of full path towards table by allowing array as parameter for $schema in TableIdentifier --- doc/book/sql.md | 5 +++++ src/Sql/AbstractSql.php | 6 +++--- src/Sql/TableIdentifier.php | 23 ++++++++++++++--------- test/Sql/TableIdentifierTest.php | 25 +++++++++++++++++++------ 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/doc/book/sql.md b/doc/book/sql.md index 9b6392b252..b29cd3889e 100644 --- a/doc/book/sql.md +++ b/doc/book/sql.md @@ -151,6 +151,11 @@ $select->from(['t' => 'table']); // Using a Sql\TableIdentifier: // (same output as above) $select->from(new TableIdentifier(['t' => 'table'])); + +// If your database engine requires full path to get to the table object, +// for example SQL Server uses database name as part of the table name, +// pass the schema path to the second parameter +$select->from(new TableIdentifier('table', ['dbo', 'schema'])); ``` ### columns() diff --git a/src/Sql/AbstractSql.php b/src/Sql/AbstractSql.php index f7d1d42e82..29047aae36 100644 --- a/src/Sql/AbstractSql.php +++ b/src/Sql/AbstractSql.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -12,8 +12,8 @@ use Zend\Db\Adapter\Driver\DriverInterface; use Zend\Db\Adapter\ParameterContainer; use Zend\Db\Adapter\Platform\PlatformInterface; -use Zend\Db\Sql\Platform\PlatformDecoratorInterface; use Zend\Db\Adapter\Platform\Sql92 as DefaultAdapterPlatform; +use Zend\Db\Sql\Platform\PlatformDecoratorInterface; abstract class AbstractSql implements SqlInterface { @@ -435,7 +435,7 @@ protected function resolveTable( } if ($schema && $table) { - $table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table; + $table = $platform->quoteIdentifierChain($schema).$platform->getIdentifierSeparator() . $table; } return $table; } diff --git a/src/Sql/TableIdentifier.php b/src/Sql/TableIdentifier.php index 85cb10e7f0..4d4526da15 100644 --- a/src/Sql/TableIdentifier.php +++ b/src/Sql/TableIdentifier.php @@ -1,9 +1,11 @@ schema = null; } else { - if (! (is_string($schema) || is_callable([$schema, '__toString']))) { + if (!(is_string($schema) || is_array($schema) || is_callable([$schema, '__toString']))) { throw new Exception\InvalidArgumentException(sprintf( - '$schema must be a valid schema name, parameter of type %s given', + '$schema must be a valid schema name or path in form of array, parameter of type %s given', is_object($schema) ? get_class($schema) : gettype($schema) )); } - $this->schema = (string) $schema; + if (!is_array($schema)) { + $schema = (string) $schema; + } + $this->schema = $schema; if ('' === $this->schema) { throw new Exception\InvalidArgumentException( diff --git a/test/Sql/TableIdentifierTest.php b/test/Sql/TableIdentifierTest.php index feb5c22cc6..1bbf19508b 100644 --- a/test/Sql/TableIdentifierTest.php +++ b/test/Sql/TableIdentifierTest.php @@ -1,19 +1,23 @@ assertEquals( + 'SELECT "db"."schema"."table".* FROM "db"."schema"."table"', + $select->getSqlString(new TrustingSql92Platform()) + ); + } + /** - * Data provider + * Data provider. * * @return mixed[][] */ @@ -102,7 +116,7 @@ public function invalidTableProvider() } /** - * Data provider + * Data provider. * * @return mixed[][] */ @@ -111,7 +125,6 @@ public function invalidSchemaProvider() return [ [''], [new stdClass()], - [[]], ]; } } From 2bb67762a175497267cd1dcbd8bdec5229e24d0c Mon Sep 17 00:00:00 2001 From: Sasha Alex Romanenko Date: Sat, 11 Mar 2017 02:28:21 -0500 Subject: [PATCH 2/3] revert php-cs-fixer "fixes". --- src/Sql/AbstractSql.php | 10 ++++------ src/Sql/TableIdentifier.php | 16 ++++++---------- test/Sql/TableIdentifierTest.php | 16 ++++++---------- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/Sql/AbstractSql.php b/src/Sql/AbstractSql.php index 29047aae36..773452481b 100644 --- a/src/Sql/AbstractSql.php +++ b/src/Sql/AbstractSql.php @@ -1,10 +1,8 @@ quoteIdentifierChain($schema).$platform->getIdentifierSeparator() . $table; + $table = $platform->quoteIdentifierChain($schema) . $platform->getIdentifierSeparator() . $table; } return $table; } diff --git a/src/Sql/TableIdentifier.php b/src/Sql/TableIdentifier.php index 4d4526da15..d4e745b282 100644 --- a/src/Sql/TableIdentifier.php +++ b/src/Sql/TableIdentifier.php @@ -1,12 +1,8 @@ schema = null; } else { - if (!(is_string($schema) || is_array($schema) || is_callable([$schema, '__toString']))) { + if (! (is_string($schema) || is_array($schema) || is_callable([$schema, '__toString']))) { throw new Exception\InvalidArgumentException(sprintf( '$schema must be a valid schema name or path in form of array, parameter of type %s given', is_object($schema) ? get_class($schema) : gettype($schema) )); } - if (!is_array($schema)) { + if (! is_array($schema)) { $schema = (string) $schema; } $this->schema = $schema; diff --git a/test/Sql/TableIdentifierTest.php b/test/Sql/TableIdentifierTest.php index 1bbf19508b..ccea5edc7b 100644 --- a/test/Sql/TableIdentifierTest.php +++ b/test/Sql/TableIdentifierTest.php @@ -1,12 +1,8 @@ Date: Sat, 11 Mar 2017 02:38:17 -0500 Subject: [PATCH 3/3] revert php-cs-fixer "fixes". --- src/Sql/AbstractSql.php | 4 ++-- src/Sql/TableIdentifier.php | 4 ++-- test/Sql/TableIdentifierTest.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Sql/AbstractSql.php b/src/Sql/AbstractSql.php index 773452481b..b945a1fb9e 100644 --- a/src/Sql/AbstractSql.php +++ b/src/Sql/AbstractSql.php @@ -1,8 +1,8 @@