Source of file Delete.php
Size: 5,737 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Db/Sql/Delete.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Db\Sql; use Zend\Db\Adapter\AdapterInterface; use Zend\Db\Adapter\ParameterContainer; use Zend\Db\Adapter\Platform\PlatformInterface; use Zend\Db\Adapter\Platform\Sql92; use Zend\Db\Adapter\StatementContainerInterface; /** * * @property Where $where */ class Delete extends AbstractSql implements SqlInterface, PreparableSqlInterface { /**@#+ * @const */ const SPECIFICATION_DELETE = 'delete'; const SPECIFICATION_WHERE = 'where'; /**@#-*/ /** * @var array Specifications */ 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; } 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 * @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; } /** * Prepare the delete statement * * @param AdapterInterface $adapter * @param StatementContainerInterface $statementContainer * @return void */ public function prepareStatement(AdapterInterface $adapter, StatementContainerInterface $statementContainer) { $driver = $adapter->getDriver(); $platform = $adapter->getPlatform(); $parameterContainer = $statementContainer->getParameterContainer(); if (!$parameterContainer instanceof ParameterContainer) { $parameterContainer = new ParameterContainer(); $statementContainer->setParameterContainer($parameterContainer); } $table = $this->table; $schema = null; // create quoted table name to use in delete processing if ($table instanceof TableIdentifier) { list($table, $schema) = $table->getTableAndSchema(); } $table = $platform->quoteIdentifier($table); if ($schema) { $table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table; } $sql = sprintf($this->specifications[static::SPECIFICATION_DELETE], $table); // process where if ($this->where->count() > 0) { $whereParts = $this->processExpression($this->where, $platform, $driver, 'where'); $parameterContainer->merge($whereParts->getParameterContainer()); $sql .= ' ' . sprintf($this->specifications[static::SPECIFICATION_WHERE], $whereParts->getSql()); } $statementContainer->setSql($sql); } /** * Get the SQL string, based on the platform * * Platform defaults to Sql92 if none provided * * @param null|PlatformInterface $adapterPlatform * @return string */ public function getSqlString(PlatformInterface $adapterPlatform = null) { $adapterPlatform = ($adapterPlatform) ?: new Sql92; $table = $this->table; $schema = null; // create quoted table name to use in delete processing if ($table instanceof TableIdentifier) { list($table, $schema) = $table->getTableAndSchema(); } $table = $adapterPlatform->quoteIdentifier($table); if ($schema) { $table = $adapterPlatform->quoteIdentifier($schema) . $adapterPlatform->getIdentifierSeparator() . $table; } $sql = sprintf($this->specifications[static::SPECIFICATION_DELETE], $table); if ($this->where->count() > 0) { $whereParts = $this->processExpression($this->where, $adapterPlatform, null, 'where'); $sql .= ' ' . sprintf($this->specifications[static::SPECIFICATION_WHERE], $whereParts->getSql()); } return $sql; } /** * Property overloading * * Overloads "where" only. * * @param string $name * @return mixed */ public function __get($name) { switch (strtolower($name)) { case 'where': return $this->where; } } } |