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\ParameterContainer
;
14 use Zend\Db\Adapter\Platform\PlatformInterface
;
15 use Zend\Db\Adapter\Platform\Sql92
;
16 use Zend\Db\Adapter\StatementContainerInterface
;
20 * @property Where $where
22 class Delete
extends AbstractSql
implements SqlInterface
, PreparableSqlInterface
27 const SPECIFICATION_DELETE
= 'delete';
28 const SPECIFICATION_WHERE
= 'where';
32 * @var array Specifications
34 protected $specifications = array(
35 self
::SPECIFICATION_DELETE
=> 'DELETE FROM %1$s',
36 self
::SPECIFICATION_WHERE
=> 'WHERE %1$s'
40 * @var string|TableIdentifier
42 protected $table = '';
47 protected $emptyWhereProtection = true;
52 protected $set = array();
55 * @var null|string|Where
57 protected $where = null;
62 * @param null|string|TableIdentifier $table
64 public function __construct($table = null)
69 $this->where
= new Where();
73 * Create from statement
75 * @param string|TableIdentifier $table
78 public function from($table)
80 $this->table
= $table;
84 public function getRawState($key = null)
87 'emptyWhereProtection' => $this->emptyWhereProtection
,
88 'table' => $this->table
,
90 'where' => $this->where
92 return (isset($key) && array_key_exists($key, $rawState)) ?
$rawState[$key] : $rawState;
98 * @param Where|\Closure|string|array $predicate
99 * @param string $combination One of the OP_* constants from Predicate\PredicateSet
102 public function where($predicate, $combination = Predicate\PredicateSet
::OP_AND
)
104 if ($predicate instanceof Where
) {
105 $this->where
= $predicate;
106 } elseif ($predicate instanceof \Closure
) {
107 $predicate($this->where
);
110 if (is_string($predicate)) {
111 // String $predicate should be passed as an expression
112 $predicate = new Predicate\
Expression($predicate);
113 $this->where
->addPredicate($predicate, $combination);
114 } elseif (is_array($predicate)) {
116 foreach ($predicate as $pkey => $pvalue) {
117 // loop through predicates
119 if (is_string($pkey) && strpos($pkey, '?') !== false) {
120 // First, process strings that the abstraction replacement character ?
121 // as an Expression predicate
122 $predicate = new Predicate\
Expression($pkey, $pvalue);
124 } elseif (is_string($pkey)) {
125 // Otherwise, if still a string, do something intelligent with the PHP type provided
127 if ($pvalue === null) {
128 // map PHP null to SQL IS NULL expression
129 $predicate = new Predicate\
IsNull($pkey, $pvalue);
130 } elseif (is_array($pvalue)) {
131 // if the value is an array, assume IN() is desired
132 $predicate = new Predicate\
In($pkey, $pvalue);
134 // otherwise assume that array('foo' => 'bar') means "foo" = 'bar'
135 $predicate = new Predicate\
Operator($pkey, Predicate\Operator
::OP_EQ
, $pvalue);
137 } elseif ($pvalue instanceof Predicate\PredicateInterface
) {
138 // Predicate type is ok
139 $predicate = $pvalue;
141 // must be an array of expressions (with int-indexed array)
142 $predicate = new Predicate\
Expression($pvalue);
144 $this->where
->addPredicate($predicate, $combination);
152 * Prepare the delete statement
154 * @param AdapterInterface $adapter
155 * @param StatementContainerInterface $statementContainer
158 public function prepareStatement(AdapterInterface
$adapter, StatementContainerInterface
$statementContainer)
160 $driver = $adapter->getDriver();
161 $platform = $adapter->getPlatform();
162 $parameterContainer = $statementContainer->getParameterContainer();
164 if (!$parameterContainer instanceof ParameterContainer
) {
165 $parameterContainer = new ParameterContainer();
166 $statementContainer->setParameterContainer($parameterContainer);
169 $table = $this->table
;
172 // create quoted table name to use in delete processing
173 if ($table instanceof TableIdentifier
) {
174 list($table, $schema) = $table->getTableAndSchema();
177 $table = $platform->quoteIdentifier($table);
180 $table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table;
183 $sql = sprintf($this->specifications
[self
::SPECIFICATION_DELETE
], $table);
186 if ($this->where
->count() > 0) {
187 $whereParts = $this->processExpression($this->where
, $platform, $driver, 'where');
188 $parameterContainer->merge($whereParts->getParameterContainer());
189 $sql .= ' ' . sprintf($this->specifications
[self
::SPECIFICATION_WHERE
], $whereParts->getSql());
191 $statementContainer->setSql($sql);
195 * Get the SQL string, based on the platform
197 * Platform defaults to Sql92 if none provided
199 * @param null|PlatformInterface $adapterPlatform
202 public function getSqlString(PlatformInterface
$adapterPlatform = null)
204 $adapterPlatform = ($adapterPlatform) ?
: new Sql92
;
205 $table = $this->table
;
208 // create quoted table name to use in delete processing
209 if ($table instanceof TableIdentifier
) {
210 list($table, $schema) = $table->getTableAndSchema();
213 $table = $adapterPlatform->quoteIdentifier($table);
216 $table = $adapterPlatform->quoteIdentifier($schema) . $adapterPlatform->getIdentifierSeparator() . $table;
219 $sql = sprintf($this->specifications
[self
::SPECIFICATION_DELETE
], $table);
221 if ($this->where
->count() > 0) {
222 $whereParts = $this->processExpression($this->where
, $adapterPlatform, null, 'where');
223 $sql .= ' ' . sprintf($this->specifications
[self
::SPECIFICATION_WHERE
], $whereParts->getSql());
230 * Property overloading
232 * Overloads "where" only.
234 * @param string $name
237 public function __get($name)
239 switch (strtolower($name)) {