cvs: pear /MDB2/MDB2 Extended.php /MDB2/tests MDB2_extended_testcase.php
| From: | Lukas Smith | Date: | Sat, 20 May 2006 10:57:20 +0000 |
| Subject: | cvs: pear /MDB2/MDB2 Extended.php /MDB2/tests MDB2_extended_testcase.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-40226@lists.php.net to get a copy of this message | ||
lsmith Sat May 20 10:57:20 2006 UTC
Modified files:
/pear/MDB2/MDB2 Extended.php
/pear/MDB2/tests MDB2_extended_testcase.php
Log:
- added support for DELETE in auto*() methods (Request #5345)
http://cvs.php.net/viewcvs.cgi/pear/MDB2/MDB2/Extended.php?r1=1.41&r2=1.42&diff_format=u
Index: pear/MDB2/MDB2/Extended.php
diff -u pear/MDB2/MDB2/Extended.php:1.41 pear/MDB2/MDB2/Extended.php:1.42
--- pear/MDB2/MDB2/Extended.php:1.41 Sat May 20 10:32:16 2006
+++ pear/MDB2/MDB2/Extended.php Sat May 20 10:57:19 2006
@@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
-// $Id: Extended.php,v 1.41 2006/05/20 10:32:16 lsmith Exp $
+// $Id: Extended.php,v 1.42 2006/05/20 10:57:19 lsmith Exp $
/**
* @package MDB2
@@ -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
@@ -68,12 +69,12 @@
// {{{ autoPrepare()
/**
- * Make automaticaly an insert or update query and call prepare() with it
+ * Generate an insert, update or delete query and call prepare() on it
*
* @param string table
* @param array the fields names
- * @param int type of query to make (MDB2_AUTOQUERY_INSERT or MDB2_AUTOQUERY_UPDATE)
- * @param string (in case of update queries, this string will be put after the sql WHERE
statement)
+ * @param int type of query to make (MDB2_AUTOQUERY_INSERT, MDB2_AUTOQUERY_UPDATE or
MDB2_AUTOQUERY_DELETE)
+ * @param string (in case of update and delete queries, this string will be put after the sql
WHERE statement)
* @param array that contains the types of the placeholders
*
* @return resource handle for the query
@@ -99,12 +100,12 @@
// {{{ autoExecute()
/**
- * Make automaticaly an insert or update query and call prepare() and execute() with it
+ * Generate an insert, update or delete query and call prepare() and execute() on it
*
* @param string name of the table
* @param array assoc ($key=>$value) where $key is a field name and $value its value
- * @param int type of query to make (MDB2_AUTOQUERY_INSERT or MDB2_AUTOQUERY_UPDATE)
- * @param string (in case of update queries, this string will be put after the sql WHERE
statement)
+ * @param int type of query to make (MDB2_AUTOQUERY_INSERT, MDB2_AUTOQUERY_UPDATE or
MDB2_AUTOQUERY_DELETE)
+ * @param string (in case of update and delete queries, this string will be put after the sql
WHERE statement)
* @param array that contains the types of the placeholders
* @param string which specifies which result class to use
*
@@ -116,6 +117,7 @@
function &autoExecute($table, $fields_values, $mode = MDB2_AUTOQUERY_INSERT,
$where = false, $types = null, $result_class = true)
{
+ $fields_values = (array)$fields_values;
$stmt = $this->autoPrepare($table, array_keys($fields_values), $mode, $where, $types);
if (PEAR::isError($stmt)) {
return $stmt;
@@ -135,13 +137,13 @@
* Example : buildManipSQL('table_sql', array('field1', 'field2',
'field3'), MDB2_AUTOQUERY_INSERT)
* will return the string : INSERT INTO table_sql (field1,field2,field3) VALUES
(?,?,?)
* 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/DELETE query, all
+ * the records of the table will be updated/deleted !
*
* @param string name of the table
* @param ordered array containing the fields names
- * @param int type of query to make (MDB2_AUTOQUERY_INSERT or MDB2_AUTOQUERY_UPDATE)
- * @param string (in case of update queries, this string will be put after the sql WHERE
statement)
+ * @param int type of query to make (MDB2_AUTOQUERY_INSERT, MDB2_AUTOQUERY_UPDATE or
MDB2_AUTOQUERY_DELETE)
+ * @param string (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
@@ -153,16 +155,25 @@
return $db;
}
- $count = count($table_fields);
- if ($count == 0) {
- return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
- }
- if ($db->options['quote_identifier']) {
- $table_fields = array_values($table_fields);
- for ($i = 0; $i < $count; ++$i) {
- $table_fields[$i] = $db->quoteIdentifier($table_fields[$i]);
+ if ($mode !== MDB2_AUTOQUERY_DELETE) {
+ $count = count($table_fields);
+ if ($count == 0) {
+ return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
}
+ if ($db->options['quote_identifier']) {
+ $table_fields = array_values($table_fields);
+ for ($i = 0; $i < $count; ++$i) {
+ $table_fields[$i] = $db->quoteIdentifier($table_fields[$i]);
+ }
+ }
+ }
+ if ($where !== false && !is_null($where)) {
+ if (is_array($where)) {
+ $where = implode(' AND ', $where);
+ }
+ $where = ' WHERE '.$where;
}
+
switch ($mode) {
case MDB2_AUTOQUERY_INSERT:
$cols = implode(', ', $table_fields);
@@ -171,10 +182,11 @@
break;
case MDB2_AUTOQUERY_UPDATE:
$set = implode(' = ?, ', $table_fields).' = ?';
- $sql = 'UPDATE '.$table.' SET '.$set;
- if ($where !== false) {
- $sql.= ' WHERE '.$where;
- }
+ $sql = 'UPDATE '.$table.' SET '.$set.$where;
+ return $sql;
+ break;
+ case MDB2_AUTOQUERY_DELETE:
+ $sql = 'DELETE FROM '.$table.$where;
return $sql;
break;
}
http://cvs.php.net/viewcvs.cgi/pear/MDB2/tests/MDB2_extended_testcase.php?r1=1.1&r2=1.2&diff_format=u
Index: pear/MDB2/tests/MDB2_extended_testcase.php
diff -u pear/MDB2/tests/MDB2_extended_testcase.php:1.1
pear/MDB2/tests/MDB2_extended_testcase.php:1.2
--- pear/MDB2/tests/MDB2_extended_testcase.php:1.1 Sat May 20 10:33:30 2006
+++ pear/MDB2/tests/MDB2_extended_testcase.php Sat May 20 10:57:19 2006
@@ -41,7 +41,7 @@
// | Author: Paul Cooper <pgc@ucecom.com> |
// +----------------------------------------------------------------------+
//
-// $Id: MDB2_extended_testcase.php,v 1.1 2006/05/20 10:33:30 lsmith Exp $
+// $Id: MDB2_extended_testcase.php,v 1.2 2006/05/20 10:57:19 lsmith Exp $
require_once 'MDB2_testcase.php';
@@ -89,6 +89,21 @@
$this->verifyFetchedValues($result, null, $data);
$result->free();
+ $where = array($where, 'user_name =
'.$this->db->quote($data['user_name'], 'text'));
+ $result = $this->db->extended->autoExecute('users', null,
MDB2_AUTOQUERY_DELETE, $where, null);
+
+ if (PEAR::isError($result)) {
+ $this->assertTrue(false, 'Error auto executing insert:
'.$result->getMessage());
+ }
+
+ $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
+ $result = $this->db->query('SELECT * FROM users', $this->fields);
+ if (PEAR::isError($result)) {
+ $this->assertTrue(false, 'Error selecting from users:
'.$result->getMessage());
+ }
+
+ $this->assertEquals($result->numRows(), 0, 'No rows were expected to be
returned');
+ $result->free();
}
}