Location: PHPKode > scripts > tgcSqlBuilder > include/tgcSqlBuilder/Delete.php
<?php
/**
 * $Id: Delete.php,v 1.1.1.1 2004/02/14 01:43:23 luckec Exp $
 *
 * Subclass of tgcSqlBuilder that helps to create DELETE sql-statements.
 *
 * @package     tgcSqlBuilder
 * @author      Carsten Lucke <hide@address.com>
 * @copyright   Carsten Lucke <http://www.tool-garage.de>
 */

 

/**
 * Subclass of tgcSqlBuilder that helps to create DELETE sql-statements.
 *
 * @package     tgcSqlBuilder
 * @access      public
 * @version     1.0.0
 * @author      Carsten Lucke <hide@address.com>
 */
class tgcSqlBuilder_Delete extends tgcSqlBuilder 
{
   
   /**
    * Keeps the ORDER BY information.
    *
    * The array's structure looks like that:
    * <pre>
    * array(
    *       array(
    *               'table'     =>      $tableName,
    *               'column'    =>      $columnName,
    *               'direction' =>      $direction
    *       ),
    *       array( ... ),
    *       ...
    * )
    * </pre>
    *
    * @access       private
    * @var          array           ORDER BY information
    *
    *
    */
    var $_orderBy = array();
    
   /**
    * Keeps the WHERE information.
    *
    * Structure:
    * <pre>
    * array(
    *       'OR'    =>  array(
    *                           array(
    *                                   'table'     =>  $tableName,
    *                                   'column'    =>  $columnName,
    *                                   'value'     =>  $value,
    *                                   'compOp'    =>  $comparisonOperator,
    *                           },
    *                           ...
    *                   ),
    *       'AND'   =>  array(
    *                           array(
    *                                   'table'     =>  $tableName,
    *                                   'column'    =>  $columnName,
    *                                   'value'     =>  $value,
    *                                   'compOp'    =>  $comparisonOperator,
    *                           },
    *                           ...
    *                   )
    * </pre>
    *
    * @access       private
    * @var          array           WHERE information
    */
    var $_where =   array(
                            SQLBUILDER_LOGICAL_AND  =>  array(),
                            SQLBUILDER_LOGICAL_OR   =>  array()
                    );
                    
   /**
    * Keeps the raw WHERE information.
    *
    * Structure:
    * <pre>
    * array(
    *       'OR'    =>  array(
    *                           $statement1,
    *                           $statement2,
    *                           ...
    *                   ),
    *       'AND'   =>  array(
    *                           $statement1,
    *                           $statement2,
    *                           ...
    *                   )
    * </pre>
    *
    * @access       private
    * @var          array           WHERE information
    */
    var $_rawWhere =   array(
                            SQLBUILDER_LOGICAL_AND  =>  array(),
                            SQLBUILDER_LOGICAL_OR   =>  array()
                    );
                    
   /**
    * Keeps the LIMIT information.
    *
    * @access       private
    * @var          int             limit information
    */
    var $_limit;
    
   /**
    * Tablename on which a sql-statement concerns.
    *
    * It's a numeric array, that contains the tablenames, that shall be used in an sql-statement.
    *
    * @access       private
    * @var          array           tablename
    */
    var $_tables;
    
    
    
   /**
    * Constructor
    *
    * @access       public
    * @param        object          $dbc            PEAR::DB connection object
    */
    function tgcSqlBuilder_Delete(&$dbc)
    {
        parent::tgcSqlBuilder($dbc);
    }
    
   /**
    * Generate the sql-statement.
    *
    * This method generates a query based on the object-information and returns it as a string.
    *
    * <code>
    * $sql      =   new tgcSqlBuilder_Delete($dbc);
    * $query    =   $sql->generateQuery();
    * </code>
    *
    * @access       public
    * @return       string          sql-statement
    */
    function generateQuery()
    {
        // check if a table has been specified
        if (empty($this->_tables))
        {
            return PEAR::raiseError (
                                        'You have to specifiy a tablename first',
                                        SQLBUILDER_ERROR_NO_TABLE_FOUND
                                    );
        }
        
        $query       =      'DELETE FROM ' . $this->_tables;
        
        if (count($this->_where[SQLBUILDER_LOGICAL_AND]) || count($this->_where[SQLBUILDER_LOGICAL_OR])) {
            $query  .=      ' WHERE ' . $this->_generateWhereInformation($this->_where, $this->_rawWhere);
        }
        
        if (count($this->_orderBy)) {
            $query  .=      ' ORDER BY ' . $this->_generateOrderByInformation($this->_orderBy);
        }
        
        if (! is_null($this->_limit)) {
            $query  .=      ' LIMIT ' . $this->_limit;
        }

        $this->reset();
        return $query;
    }
    
   /**
    * Add the statements table.
    *
    * If you call this method twice, the tablename that was set in first call will be overwritten.
    *
    * <code>
    * $sql      =   new tgcSqlBuilder_Delete($dbc);
    * $sql->addTable('users');
    * // now the generated sql-statement would look like: DELETE FROM users ...
    * </code>
    *
    * @access       public
    * @param        string          $tableName      tablename
    */
    function addTable($tableName)
    {
        $this->_tables      =   $tableName;
    }
    
   /**
    * Remove the tablename.
    *
    * @access       public
    */
    function removeTable()
    {
        $this->_tables      =   null;
    }
    
   /**
    * Add an ORDER BY setting.
    *
    * The parameter $direction can be either SQLBUILDER_ORDER_ASC or SQLBUILDER_ORDER_DESC.
    * If none is specified, then SQLBUILDER_ORDER_ASC will be used.
    * 
    * You can also leave $column null, if you want to order by an alias.
    * <code>
    * $sql      =       new tgcSqlBuilder_Delete($dbc);
    *
    * // ... ORDER BY alias1 ASC ...
    * $sql->addOrderBy('alias1');
    *
    * // ... ORDER BY alias2 DESC ...
    * $sql->addOrderBy('alias2', null, SQLBUILDER_ORDER_DESC);
    * </code>
    *
    * If a setting for this table/column exists, it will be overwritten.
    *
    * @access       public
    * @param        string      $table      tablename
    * @param        string      $column     columnname
    * @param        string      $direction  oder direction
    */
    function addOrderBy($table, $column = null, $direction = null)
    {
        $direction  =   is_null($direction) ? SQLBUILDER_ORDER_ASC : $direction;
        
        if (! is_null($column)) {
            $orderBy    =   array(
                                    'table'     =>  $table,
                                    'column'    =>  $column,
                                    'direction' =>  $direction
                            );
        } else {
            $orderBy    =   array(
                                    'table'     =>  $table,
                                    'column'    =>  null,
                                    'direction' =>  $direction
                            );
        }
        
        $exists     =   false;
        foreach ($this->_orderBy as $index => $orderByData) {
            if ($orderByData['table'] == $table && $orderByData['column'] == $column) {
                $exists     =   true;
                break;
            }
        }
        if ($exists) {
            $this->_orderBy[$index]     =   $orderBy;
            return;
        }
        
        array_push($this->_orderBy, $orderBy);
    }
    
   /**
    * Remove an ORDER BY setting.
    *
    * If you don't specify any parameter, then all ORDER BY information will be removed.
    * If you specify a tablename and a columnname, then this specific ORDER BY setting will be removed.
    *
    * @access       public
    * @param        string      $table      tablename
    * @param        string      $column     columnname
    * @return       mixed       true on success or PEAR_Error (possible error(s): SQLBUILDER_ERROR_INVALID_PARAM_COMBO)
    */
    function removeOrderBy($table = null, $column = null)
    {
        // delete all
        if (is_null($table) && is_null($column)) {
            $this->_orderBy     =       array();
            return true;
        }
        
        // delete table.column
        if (! is_null($table) && ! is_null($column)) {
            $newOrderBy         =       array();
            foreach ($this->_orderBy as $orderByData) {
                if (! ($orderByData['table'] == $table && $orderByData['column'] == $column)) {
                    array_push($newOrderBy, $orderByData);
                }
            }
            $this->_orderBy     =       $newOrderBy;
            return true;
        }
        
        // delete alias
        if (! is_null($table) && is_null($column)) {
            $newOrderBy         =       array();
            foreach ($this->_orderBy as $orderByData) {
                if (! ($orderByData['table'] == $table && $orderByData['column'] == null)) {
                    array_push($newOrderBy, $orderByData);
                }
            }
            $this->_orderBy     =       $newOrderBy;
            return true;
        }
        
        return PEAR::raiseError (
                                    'The combination of parameters is invalid',
                                    SQLBUILDER_ERROR_INVALID_PARAM_COMBO,
                                    null,
                                    null,
                                    'You have to call the method with none or two parameters'
                                );
    }
    
   /**
    * Add a WHERE statement.
    *
    * Possible comparison operators are:
    * SQLBUILDER_COMP_EQUAL, SQLBUILDER_COMP_NOT_EQUAL, SQLBUILDER_COMP_LESSER_THAN, SQLBUILDER_COMP_LESSER_EQUAL,
    * SQLBUILDER_COMP_GREATER_EQUAL, SQLBUILDER_COMP_GREATER_THAN, SQLBUILDER_COMP_STARTSWITH, SQLBUILDER_COMP_CONTAINS, 
    * SQLBUILDER_COMP_ENDSWITH, SQLBUILDER_COMP_BETWEEN
    *
    * If none is specified then SQLBUILDER_COMP_EQUAL will be used.
    *
    * Possible logical expressions are:
    * SQLBUILDER_LOGICAL_AND, SQLBUILDER_LOGICAL_OR
    *
    * If none is specified, then SQLBUILDER_LOGICAL_AND will be used.
    *
    * When you are using SQLBUILDER_COMP_BETWEEN, then specify  $values as a numerical array with two values 
    * in correct order.
    *
    * @access       public
    * @param        string      $table      tablename
    * @param        string      $column     columnname
    * @param        mixed       $value      value(s)
    * @param        string      $compOp     comparison operator
    * @param        string      $logic      logical linkup
    */
    function addWhere($table, $column, $value, $compOp = null, $logic = null)
    {
        $this->_addWhereHaving($this->_where, $table, $column, $value, $compOp, $logic);
    }
    
   /**
    * Remove a WHERE statement.
    *
    * If you don't specify any parameter, then all WHERE information will be removed.
    * If you specify a tablename and a columnname, then this specific ORDER BY setting will be removed.
    *
    * @access       public
    * @param        string      $table      tablename
    * @param        string      $column     columnname
    * @param        string      $logic      logical operation (possible values: SQLBUILDER_LOGICAL_AND, SQLBUILDER_LOGICAL_OR)
    * @return       mixed       true on success or PEAR_Error (possible error(s): SQLBUILDER_ERROR_INVALID_PARAM_COMBO)
    */
    function removeWhere($table = null, $column = null, $logic = null)
    {
        return $this->_removeWhereHaving($this->_where, $table, $column, $logic);
    }
    
   /**
    * Set the LIMIT for the sql-statement.
    *
    * @access       public
    * @param        int         $offset         offset
    * @param        int         $rows           rows
    */
    function setLimit($rows)
    {
        $this->_limit       =       $rows;
    }
    
   /**
    * Remove the LIMIT for the sql-statement.
    *
    * @access       public
    */
    function unsetLimit()
    {
        $this->_limit       =       null;
    }
    
   /**
    * Reset the object's whole information.
    *
    * @access       public
    */
    function reset()
    {
        $this->removeTable();
        $this->removeWhere();
        $this->removeRawWhere();
        $this->removeOrderBy();
        $this->unsetLimit();
    }
    
   /**
    * Add a raw WHERE statement.
    *
    * You can add a raw WHERE statement and define a logical operator. As default this is the logical AND.
    *
    * @access       public
    * @param        string      $statement      raw WHERE statement
    * @param       string      $logic          SQLBUILDER_LOGICAL_AND or SQLBUILDER_LOGICAL_OR
    */
    function addRawWhere($statement, $logic = SQLBUILDER_LOGICAL_AND)
    {
        $this->_addRawWhereHaving($this->_rawWhere, $statement, $logic);
    }
    
   /**
    * Remove the raw WHERE statements, that have been stored so far.
    *
    * @access       public
    * @param       string      $logic          SQLBUILDER_LOGICAL_AND or SQLBUILDER_LOGICAL_OR
    */
    function removeRawWhere($logic = null)
    {
        return $this->_removeRawWhereHaving($this->_rawWhere, $logic);
    }
    
}
?>
Return current item: tgcSqlBuilder