Index: MDB2/Extended.php
===================================================================
RCS file: /repository/pear/MDB2/MDB2/Extended.php,v
retrieving revision 1.28
diff -u -r1.28 Extended.php
--- MDB2/Extended.php	19 Aug 2005 06:42:16 -0000	1.28
+++ MDB2/Extended.php	9 Sep 2005 21:07:21 -0000
@@ -55,6 +55,7 @@
  */
 define('MDB2_AUTOQUERY_INSERT', 1);
 define('MDB2_AUTOQUERY_UPDATE', 2);
+define('MDB2_AUTOQUERY_DELETE', 3);
 
 /**
  * MDB2_Extended: class which adds several high level methods to MDB2
@@ -69,12 +70,13 @@
     // {{{ autoPrepare()
 
     /**
-     * Make automaticaly an insert or update query and call prepare() with it
+     * Automatically builds an insert, update or delete query and calls
+     * prepare() with it
      *
      * @param string $table name of the table
      * @param array $table_fields ordered array containing the fields names
-     * @param int $mode type of query to make (MDB2_AUTOQUERY_INSERT or MDB2_AUTOQUERY_UPDATE)
-     * @param string $where in case of update queries, this string will be put after the sql WHERE statement
+     * @param int $mode type of query to make (MDB2_AUTOQUERY_INSERT, MDB2_AUTOQUERY_UPDATE or MDB2_AUTOQUERY_DELETE)
+     * @param string $where in case of update and delete queries, this string will be put after the sql WHERE statement
      * @return resource handle for the query
      * @param mixed   $types  array that contains the types of the placeholders
      * @param mixed   $result_types  array that contains the types of the columns in
@@ -101,12 +103,13 @@
     // }}} autoExecute()
 
     /**
-     * Make automaticaly an insert or update query and call prepare() and execute() with it
+     * Automatically builds an insert, update or delete query and calls
+     * prepare() and execute() with it
      *
      * @param string $table name of the table
-     * @param array $fields_values assoc ($key=>$value) where $key is a field name and $value its value
-     * @param int $mode type of query to make (MDB2_AUTOQUERY_INSERT or MDB2_AUTOQUERY_UPDATE)
-     * @param string $where in case of update queries, this string will be put after the sql WHERE statement
+     * @param array $fields_values assoc ($key=>$value) where $key is a field name and $value its value. For delete queries, these are used for the sql WHERE statement. In that case, value cannot be NULL, use the $where parameter instead.
+     * @param int $mode type of query to make (MDB2_AUTOQUERY_INSERT, MDB2_AUTOQUERY_UPDATE or MDB2_AUTOQUERY_DELETE)
+     * @param string $where in case of update and delete queries, this string will be put after the sql WHERE statement
      * @param mixed   $types  array that contains the types of the placeholders
      * @param mixed   $result_types  array that contains the types of the columns in
      *                        the result set
@@ -134,18 +137,26 @@
     // }}} buildManipSQL()
 
     /**
-     * Make automaticaly an sql query for prepare()
+     * Automatically builds a sql query for prepare()
      *
-     * Example : buildManipSQL('table_sql', array('field1', 'field2', 'field3'), MDB2_AUTOQUERY_INSERT)
+     * Insert example : buildManipSQL('table_sql', array('field1', 'field2', 'field3'), MDB2_AUTOQUERY_INSERT)
      *           will return the string : INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?)
+     *
+     * Update example : buildManipSQL('table_sql', array('field1', 'field2', 'field3'), MDB2_AUTOQUERY_UPDATE, 'field4 = 1')
+     *           will return the string : UPDATE table_sql SET field1 = ?, field2 = ?, field3 = ? WHERE field4 = 1
+     *
+     * Delete example : buildManipSQL('table_sql', array('field1', 'field2'), MDB2_AUTOQUERY_DELETE, 'field3 IS NULL')
+     *           will return the string : DELETE FROM table_sql WHERE field1 = ? AND field2 = ? AND (field3 IS NULL)
+     *
      * NB : - This belongs more to a SQL Builder class, but this is a simple facility
-     *      - Be carefull ! If you don't give a $where param with an UPDATE query, all
-     *        the records of the table will be updated !
+     *      - Be carefull! If you don't give a $where param with an UPDATE
+     *        (or DELETE) query, all the records of the table will be updated
+     *        (or could be deleted)!
      *
      * @param string $table name of the table
      * @param array $table_fields ordered array containing the fields names
-     * @param int $mode type of query to make (MDB2_AUTOQUERY_INSERT or MDB2_AUTOQUERY_UPDATE)
-     * @param string $where in case of update queries, this string will be put after the sql WHERE statement
+     * @param int $mode type of query to make (MDB2_AUTOQUERY_INSERT, MDB2_AUTOQUERY_UPDATE or MDB2_AUTOQUERY_DELETE)
+     * @param string $where in case of update and delete queries, this string will be put after the sql WHERE statement
      * @return string sql query for prepare()
      * @access public
      */
@@ -173,6 +184,21 @@
             }
             return $sql;
             break;
+        case MDB2_AUTOQUERY_DELETE:
+            $where_fields = implode(' = ? AND ', $table_fields);
+            $sql = 'DELETE FROM '.$table;
+            if ($where_fields) {
+                $sql .= ' WHERE '.$where_fields.' = ?';
+            }
+            if ($where !== false) {
+                if ($where_fields) {
+                    $sql .= ' AND ('.$where.')';
+                } else {
+                    $sql .= ' WHERE '.$where;
+                }
+            }
+            return $sql;
+            break;
         }
         return $db->raiseError(MDB2_ERROR_SYNTAX);
     }
