3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 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;
27 public function __construct(AdapterInterface
$adapter, $table = null, Platform\AbstractPlatform
$sqlPlatform = null)
29 $this->adapter
= $adapter;
31 $this->setTable($table);
33 $this->sqlPlatform
= ($sqlPlatform) ?
: new Platform\
Platform($adapter);
37 * @return null|\Zend\Db\Adapter\AdapterInterface
39 public function getAdapter()
41 return $this->adapter
;
44 public function hasTable()
46 return ($this->table
!= null);
49 public function setTable($table)
51 if (is_string($table) ||
is_array($table) ||
$table instanceof TableIdentifier
) {
52 $this->table
= $table;
54 throw new Exception\
InvalidArgumentException('Table must be a string, array or instance of TableIdentifier.');
59 public function getTable()
64 public function getSqlPlatform()
66 return $this->sqlPlatform
;
69 public function select($table = null)
71 if ($this->table
!== null && $table !== null) {
72 throw new Exception\
InvalidArgumentException(sprintf(
73 'This Sql object is intended to work with only the table "%s" provided at construction time.',
77 return new Select(($table) ?
: $this->table
);
80 public function insert($table = null)
82 if ($this->table
!== null && $table !== null) {
83 throw new Exception\
InvalidArgumentException(sprintf(
84 'This Sql object is intended to work with only the table "%s" provided at construction time.',
88 return new Insert(($table) ?
: $this->table
);
91 public function update($table = null)
93 if ($this->table
!== null && $table !== null) {
94 throw new Exception\
InvalidArgumentException(sprintf(
95 'This Sql object is intended to work with only the table "%s" provided at construction time.',
99 return new Update(($table) ?
: $this->table
);
102 public function delete($table = null)
104 if ($this->table
!== null && $table !== null) {
105 throw new Exception\
InvalidArgumentException(sprintf(
106 'This Sql object is intended to work with only the table "%s" provided at construction time.',
110 return new Delete(($table) ?
: $this->table
);
114 * @param PreparableSqlInterface $sqlObject
115 * @param StatementInterface|null $statement
116 * @return StatementInterface
118 public function prepareStatementForSqlObject(PreparableSqlInterface
$sqlObject, StatementInterface
$statement = null)
120 $statement = ($statement) ?
: $this->adapter
->getDriver()->createStatement();
122 if ($this->sqlPlatform
) {
123 $this->sqlPlatform
->setSubject($sqlObject);
124 $this->sqlPlatform
->prepareStatement($this->adapter
, $statement);
126 $sqlObject->prepareStatement($this->adapter
, $statement);
132 public function getSqlStringForSqlObject(SqlInterface
$sqlObject, PlatformInterface
$platform = null)
134 $platform = ($platform) ?
: $this->adapter
->getPlatform();
136 if ($this->sqlPlatform
) {
137 $this->sqlPlatform
->setSubject($sqlObject);
138 $sqlString = $this->sqlPlatform
->getSqlString($platform);
140 $sqlString = $sqlObject->getSqlString($platform);