Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Db / Sql / Delete.php
blobd7452f9688da0801b7ce391b95e9325372e8df53
1 <?php
2 /**
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
8 */
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;
18 /**
20 * @property Where $where
22 class Delete extends AbstractSql implements SqlInterface, PreparableSqlInterface
24 /**@#+
25 * @const
27 const SPECIFICATION_DELETE = 'delete';
28 const SPECIFICATION_WHERE = 'where';
29 /**@#-*/
31 /**
32 * @var array Specifications
34 protected $specifications = array(
35 self::SPECIFICATION_DELETE => 'DELETE FROM %1$s',
36 self::SPECIFICATION_WHERE => 'WHERE %1$s'
39 /**
40 * @var string|TableIdentifier
42 protected $table = '';
44 /**
45 * @var bool
47 protected $emptyWhereProtection = true;
49 /**
50 * @var array
52 protected $set = array();
54 /**
55 * @var null|string|Where
57 protected $where = null;
59 /**
60 * Constructor
62 * @param null|string|TableIdentifier $table
64 public function __construct($table = null)
66 if ($table) {
67 $this->from($table);
69 $this->where = new Where();
72 /**
73 * Create from statement
75 * @param string|TableIdentifier $table
76 * @return Delete
78 public function from($table)
80 $this->table = $table;
81 return $this;
84 public function getRawState($key = null)
86 $rawState = array(
87 'emptyWhereProtection' => $this->emptyWhereProtection,
88 'table' => $this->table,
89 'set' => $this->set,
90 'where' => $this->where
92 return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState;
95 /**
96 * Create where clause
98 * @param Where|\Closure|string|array $predicate
99 * @param string $combination One of the OP_* constants from Predicate\PredicateSet
100 * @return Delete
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);
108 } else {
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);
133 } else {
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;
140 } else {
141 // must be an array of expressions (with int-indexed array)
142 $predicate = new Predicate\Expression($pvalue);
144 $this->where->addPredicate($predicate, $combination);
148 return $this;
152 * Prepare the delete statement
154 * @param AdapterInterface $adapter
155 * @param StatementContainerInterface $statementContainer
156 * @return void
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;
170 $schema = null;
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);
179 if ($schema) {
180 $table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table;
183 $sql = sprintf($this->specifications[self::SPECIFICATION_DELETE], $table);
185 // process where
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
200 * @return string
202 public function getSqlString(PlatformInterface $adapterPlatform = null)
204 $adapterPlatform = ($adapterPlatform) ?: new Sql92;
205 $table = $this->table;
206 $schema = null;
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);
215 if ($schema) {
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());
226 return $sql;
230 * Property overloading
232 * Overloads "where" only.
234 * @param string $name
235 * @return mixed
237 public function __get($name)
239 switch (strtolower($name)) {
240 case 'where':
241 return $this->where;