3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Db\Sql
;
12 use Zend\Db\Adapter\AdapterInterface
;
13 use Zend\Db\Adapter\Driver\StatementInterface
;
14 use Zend\Db\Adapter\Platform\PlatformInterface
;
18 /** @var AdapterInterface */
19 protected $adapter = null;
21 /** @var string|array|TableIdentifier */
22 protected $table = null;
24 /** @var Platform\Platform */
25 protected $sqlPlatform = null;
28 * @param AdapterInterface $adapter
29 * @param null|string|array|TableIdentifier $table
30 * @param null|Platform\AbstractPlatform $sqlPlatform @deprecated since version 3.0
32 public function __construct(AdapterInterface
$adapter, $table = null, Platform\AbstractPlatform
$sqlPlatform = null)
34 $this->adapter
= $adapter;
36 $this->setTable($table);
38 $this->sqlPlatform
= $sqlPlatform ?
: new Platform\
Platform($adapter);
42 * @return null|\Zend\Db\Adapter\AdapterInterface
44 public function getAdapter()
46 return $this->adapter
;
49 public function hasTable()
51 return ($this->table
!== null);
54 public function setTable($table)
56 if (is_string($table) ||
is_array($table) ||
$table instanceof TableIdentifier
) {
57 $this->table
= $table;
59 throw new Exception\
InvalidArgumentException('Table must be a string, array or instance of TableIdentifier.');
64 public function getTable()
69 public function getSqlPlatform()
71 return $this->sqlPlatform
;
74 public function select($table = null)
76 if ($this->table
!== null && $table !== null) {
77 throw new Exception\
InvalidArgumentException(sprintf(
78 'This Sql object is intended to work with only the table "%s" provided at construction time.',
82 return new Select(($table) ?
: $this->table
);
85 public function insert($table = null)
87 if ($this->table
!== null && $table !== null) {
88 throw new Exception\
InvalidArgumentException(sprintf(
89 'This Sql object is intended to work with only the table "%s" provided at construction time.',
93 return new Insert(($table) ?
: $this->table
);
96 public function update($table = null)
98 if ($this->table
!== null && $table !== null) {
99 throw new Exception\
InvalidArgumentException(sprintf(
100 'This Sql object is intended to work with only the table "%s" provided at construction time.',
104 return new Update(($table) ?
: $this->table
);
107 public function delete($table = null)
109 if ($this->table
!== null && $table !== null) {
110 throw new Exception\
InvalidArgumentException(sprintf(
111 'This Sql object is intended to work with only the table "%s" provided at construction time.',
115 return new Delete(($table) ?
: $this->table
);
119 * @param PreparableSqlInterface $sqlObject
120 * @param StatementInterface $statement
121 * @param AdapterInterface $adapter
123 * @return StatementInterface
125 public function prepareStatementForSqlObject(PreparableSqlInterface
$sqlObject, StatementInterface
$statement = null, AdapterInterface
$adapter = null)
127 $adapter = $adapter ?
: $this->adapter
;
128 $statement = $statement ?
: $adapter->getDriver()->createStatement();
130 return $this->sqlPlatform
->setSubject($sqlObject)->prepareStatement($adapter, $statement);
134 * Get sql string using platform or sql object
136 * @param SqlInterface $sqlObject
137 * @param PlatformInterface|null $platform
141 * @deprecated Deprecated in 2.4. Use buildSqlString() instead
143 public function getSqlStringForSqlObject(SqlInterface
$sqlObject, PlatformInterface
$platform = null)
145 $platform = ($platform) ?
: $this->adapter
->getPlatform();
146 return $this->sqlPlatform
->setSubject($sqlObject)->getSqlString($platform);
150 * @param SqlInterface $sqlObject
151 * @param AdapterInterface $adapter
155 * @throws Exception\InvalidArgumentException
157 public function buildSqlString(SqlInterface
$sqlObject, AdapterInterface
$adapter = null)
161 ->setSubject($sqlObject)
162 ->getSqlString($adapter ?
$adapter->getPlatform() : $this->adapter
->getPlatform());