Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/book/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 5 additions & 7 deletions src/Sql/AbstractSql.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<?php
/**
* 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)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see http://github.com/zendframework/zend-db for the canonical source repository
* @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://github.com/zendframework/zend-db/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Db\Sql;

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
{
Expand Down Expand Up @@ -435,7 +433,7 @@ protected function resolveTable(
}

if ($schema && $table) {
$table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table;
$table = $platform->quoteIdentifierChain($schema) . $platform->getIdentifierSeparator() . $table;
}
return $table;
}
Expand Down
23 changes: 12 additions & 11 deletions src/Sql/TableIdentifier.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php
/**
* 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)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see http://github.com/zendframework/zend-db for the canonical source repository
* @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://github.com/zendframework/zend-db/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Db\Sql;
Expand All @@ -19,13 +17,13 @@ class TableIdentifier
protected $table;

/**
* @var null|string
* @var null|string|array
*/
protected $schema;

/**
* @param string $table
* @param null|string $schema
* @param string $table
* @param null|string|array $schema
*/
public function __construct($table, $schema = null)
{
Expand All @@ -45,14 +43,17 @@ public function __construct($table, $schema = null)
if (null === $schema) {
$this->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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably check for callable [$schema, '__toString'] only to avoid re-cast $schema that already string ?

if (is_callable([$schema, '__toString'])) {

$schema = (string) $schema;
}
$this->schema = $schema;

if ('' === $this->schema) {
throw new Exception\InvalidArgumentException(
Expand Down
21 changes: 15 additions & 6 deletions test/Sql/TableIdentifierTest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php
/**
* 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)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @see http://github.com/zendframework/zend-db for the canonical source repository
* @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://github.com/zendframework/zend-db/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\Db\Sql;

use stdClass;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\TableIdentifier;
use ZendTest\Db\TestAsset\TrustingSql92Platform;

/**
* Tests for {@see \Zend\Db\Sql\TableIdentifier}
Expand Down Expand Up @@ -88,6 +88,16 @@ public function testRejectsInvalidSchema($invalidSchema)
new TableIdentifier('foo', $invalidSchema);
}

public function testSchemaPath()
{
$select = new Select(new TableIdentifier('table', ['db', 'schema']));

$this->assertEquals(
'SELECT "db"."schema"."table".* FROM "db"."schema"."table"',
$select->getSqlString(new TrustingSql92Platform())
);
}

/**
* Data provider
*
Expand All @@ -111,7 +121,6 @@ public function invalidSchemaProvider()
return [
[''],
[new stdClass()],
[[]],
];
}
}