2023-03-11 12:04:29 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Zend Framework (http://framework.zend.com/)
|
|
|
|
*
|
|
|
|
* @link http://github.com/zendframework/zf2 for the canonical source repository
|
2023-04-01 09:03:34 +03:00
|
|
|
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
|
2023-03-11 12:04:29 +03:00
|
|
|
* @license http://framework.zend.com/license/new-bsd New BSD License
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Zend\Db\Sql;
|
|
|
|
|
|
|
|
use Zend\Db\Adapter\ParameterContainer;
|
|
|
|
use Zend\Db\Adapter\Platform\PlatformInterface;
|
2023-04-01 09:03:34 +03:00
|
|
|
use Zend\Db\Adapter\Driver\DriverInterface;
|
2023-03-11 12:04:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @property Where $where
|
|
|
|
*/
|
2023-04-01 09:03:34 +03:00
|
|
|
class Delete extends AbstractPreparableSql
|
2023-03-11 12:04:29 +03:00
|
|
|
{
|
|
|
|
/**@#+
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
const SPECIFICATION_DELETE = 'delete';
|
|
|
|
const SPECIFICATION_WHERE = 'where';
|
|
|
|
/**@#-*/
|
|
|
|
|
|
|
|
/**
|
2023-04-01 09:03:34 +03:00
|
|
|
* {@inheritDoc}
|
2023-03-11 12:04:29 +03:00
|
|
|
*/
|
|
|
|
protected $specifications = array(
|
|
|
|
self::SPECIFICATION_DELETE => 'DELETE FROM %1$s',
|
|
|
|
self::SPECIFICATION_WHERE => 'WHERE %1$s'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string|TableIdentifier
|
|
|
|
*/
|
|
|
|
protected $table = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $emptyWhereProtection = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $set = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var null|string|Where
|
|
|
|
*/
|
|
|
|
protected $where = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param null|string|TableIdentifier $table
|
|
|
|
*/
|
|
|
|
public function __construct($table = null)
|
|
|
|
{
|
|
|
|
if ($table) {
|
|
|
|
$this->from($table);
|
|
|
|
}
|
|
|
|
$this->where = new Where();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create from statement
|
|
|
|
*
|
|
|
|
* @param string|TableIdentifier $table
|
|
|
|
* @return Delete
|
|
|
|
*/
|
|
|
|
public function from($table)
|
|
|
|
{
|
|
|
|
$this->table = $table;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-04-01 09:03:34 +03:00
|
|
|
/**
|
|
|
|
* @param null $key
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2023-03-11 12:04:29 +03:00
|
|
|
public function getRawState($key = null)
|
|
|
|
{
|
|
|
|
$rawState = array(
|
|
|
|
'emptyWhereProtection' => $this->emptyWhereProtection,
|
|
|
|
'table' => $this->table,
|
|
|
|
'set' => $this->set,
|
|
|
|
'where' => $this->where
|
|
|
|
);
|
|
|
|
return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create where clause
|
|
|
|
*
|
|
|
|
* @param Where|\Closure|string|array $predicate
|
|
|
|
* @param string $combination One of the OP_* constants from Predicate\PredicateSet
|
2023-04-01 09:03:34 +03:00
|
|
|
*
|
2023-03-11 12:04:29 +03:00
|
|
|
* @return Delete
|
|
|
|
*/
|
|
|
|
public function where($predicate, $combination = Predicate\PredicateSet::OP_AND)
|
|
|
|
{
|
|
|
|
if ($predicate instanceof Where) {
|
|
|
|
$this->where = $predicate;
|
|
|
|
} else {
|
|
|
|
$this->where->addPredicates($predicate, $combination);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-01 09:03:34 +03:00
|
|
|
* @param PlatformInterface $platform
|
|
|
|
* @param DriverInterface|null $driver
|
|
|
|
* @param ParameterContainer|null $parameterContainer
|
2023-03-11 12:04:29 +03:00
|
|
|
*
|
2023-04-01 09:03:34 +03:00
|
|
|
* @return string
|
2023-03-11 12:04:29 +03:00
|
|
|
*/
|
2023-04-01 09:03:34 +03:00
|
|
|
protected function processDelete(PlatformInterface $platform, DriverInterface $driver = null, ParameterContainer $parameterContainer = null)
|
2023-03-11 12:04:29 +03:00
|
|
|
{
|
2023-04-01 09:03:34 +03:00
|
|
|
return sprintf(
|
|
|
|
$this->specifications[static::SPECIFICATION_DELETE],
|
|
|
|
$this->resolveTable($this->table, $platform, $driver, $parameterContainer)
|
|
|
|
);
|
2023-03-11 12:04:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-01 09:03:34 +03:00
|
|
|
* @param PlatformInterface $platform
|
|
|
|
* @param DriverInterface|null $driver
|
|
|
|
* @param ParameterContainer|null $parameterContainer
|
2023-03-11 12:04:29 +03:00
|
|
|
*
|
2023-04-01 09:03:34 +03:00
|
|
|
* @return null|string
|
2023-03-11 12:04:29 +03:00
|
|
|
*/
|
2023-04-01 09:03:34 +03:00
|
|
|
protected function processWhere(PlatformInterface $platform, DriverInterface $driver = null, ParameterContainer $parameterContainer = null)
|
2023-03-11 12:04:29 +03:00
|
|
|
{
|
2023-04-01 09:03:34 +03:00
|
|
|
if ($this->where->count() == 0) {
|
|
|
|
return;
|
2023-03-11 12:04:29 +03:00
|
|
|
}
|
|
|
|
|
2023-04-01 09:03:34 +03:00
|
|
|
return sprintf(
|
|
|
|
$this->specifications[static::SPECIFICATION_WHERE],
|
|
|
|
$this->processExpression($this->where, $platform, $driver, $parameterContainer, 'where')
|
|
|
|
);
|
2023-03-11 12:04:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Property overloading
|
|
|
|
*
|
|
|
|
* Overloads "where" only.
|
|
|
|
*
|
|
|
|
* @param string $name
|
2023-04-01 09:03:34 +03:00
|
|
|
*
|
|
|
|
* @return Where|null
|
2023-03-11 12:04:29 +03:00
|
|
|
*/
|
|
|
|
public function __get($name)
|
|
|
|
{
|
|
|
|
switch (strtolower($name)) {
|
|
|
|
case 'where':
|
|
|
|
return $this->where;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|