diff --git a/doc/book/sql.md b/doc/book/sql.md index bfc8d2e875..96e8ad2fab 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(['t' => new TableIdentifier('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..b945a1fb9e 100644 --- a/src/Sql/AbstractSql.php +++ b/src/Sql/AbstractSql.php @@ -1,10 +1,8 @@ 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..24e9fdcd80 100644 --- a/src/Sql/TableIdentifier.php +++ b/src/Sql/TableIdentifier.php @@ -1,10 +1,8 @@ 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..e1201c48d2 100644 --- a/test/Sql/TableIdentifierTest.php +++ b/test/Sql/TableIdentifierTest.php @@ -1,16 +1,16 @@ assertEquals( + 'SELECT "db"."schema"."table".* FROM "db"."schema"."table"', + $select->getSqlString(new TrustingSql92Platform()) + ); + } + /** * Data provider * @@ -111,7 +121,6 @@ public function invalidSchemaProvider() return [ [''], [new stdClass()], - [[]], ]; } }