* $q = ezcDbInstance::get()->createDeleteQuery(); * $q->deleteFrom( 'MyTable' )->where( $q->expr->eq( 'id', 1 ) ); * $stmt = $q->prepare(); * $stmt->execute(); * * * @package Database * @version //autogentag// * @mainclass */ class ezcQueryDelete extends ezcQuery { /** * The target table for the delete query. * * @var string */ private $table = null; /** * Stores the WHERE part of the SQL. * * @var string */ protected $whereString = null; /** * Constructs a new ezcQueryDelete that works on the database $db and with the aliases $aliases. * * The paramters are passed directly to ezcQuery. * @param PDO $db * @param array(string=>string) $aliases */ public function __construct( PDO $db, array $aliases = array() ) { parent::__construct( $db, $aliases ); } /** * Opens the query and sets the target table to $table. * * deleteFrom() returns a pointer to $this. * * @param string $table * @return ezcQueryDelete */ public function deleteFrom( $table ) { $this->table = $table; return $this; } /** * Adds a where clause with logical expressions to the query. * * where() accepts an arbitrary number of parameters. Each parameter * must contain a logical expression or an array with logical expressions. * where() could be invoked several times. All provided arguments * added to the end of $whereString and form final WHERE clause of the query. * If you specify multiple logical expression they are connected using * a logical and. * * Example: * * $q->deleteFrom( 'MyTable' )->where( $q->eq( 'id', 1 ) ); * * * @throws ezcQueryVaraibleParameterException if called with no parameters. * @param string|array(string) $... Either a string with a logical expression name * or an array with logical expressions. * @return ezcQueryDelete */ public function where() { if ( $this->whereString == null ) { $this->whereString = 'WHERE '; } $args = func_get_args(); $expressions = self::arrayFlatten( $args ); if ( count( $expressions ) < 1 ) { throw new ezcQueryVariableParameterException( 'where', count( $args ), 1 ); } // glue string should be inserted each time but not before first entry if ( $this->whereString != 'WHERE ' ) { $this->whereString .= ' AND '; } $this->whereString .= join( ' AND ', $expressions ); return $this; } /** * Returns the query string for this query object. * * @todo wrong exception * @throws ezcQueryInvalidException if no table or no values have been set. * @return string */ public function getQuery() { if ( $this->table == null ) { throw new ezcQueryInvalidException( "DELETE", "deleteFrom() was not called before getQuery()." ); } $query = "DELETE FROM {$this->table}"; // append where part. if ( $this->whereString !== null ) { $query .= " {$this->whereString}"; } return $query; } } ?>