cvs: pear /MDB2/MDB2 Extended.php /MDB2/tests MDB2_extended_testcase.php
| From: | Lukas Smith | Date: | Sat, 17 Jun 2006 12:07:01 +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-40782@lists.php.net to get a copy of this message | ||
lsmith Sat Jun 17 12:07:01 2006 UTC
Modified files:
/pear/MDB2/MDB2 Extended.php
/pear/MDB2/tests MDB2_extended_testcase.php
Log:
- added MDB2_AUTOQUERY_SELECT (Request #7817)
http://cvs.php.net/viewcvs.cgi/pear/MDB2/MDB2/Extended.php?r1=1.44&r2=1.45&diff_format=u
Index: pear/MDB2/MDB2/Extended.php
diff -u pear/MDB2/MDB2/Extended.php:1.44 pear/MDB2/MDB2/Extended.php:1.45
--- pear/MDB2/MDB2/Extended.php:1.44 Thu May 25 08:19:24 2006
+++ pear/MDB2/MDB2/Extended.php Sat Jun 17 12:07:00 2006
@@ -42,7 +42,7 @@
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
//
-// $Id: Extended.php,v 1.44 2006/05/25 08:19:24 lsmith Exp $
+// $Id: Extended.php,v 1.45 2006/06/17 12:07:00 lsmith Exp $
/**
* @package MDB2
@@ -56,6 +56,7 @@
define('MDB2_AUTOQUERY_INSERT', 1);
define('MDB2_AUTOQUERY_UPDATE', 2);
define('MDB2_AUTOQUERY_DELETE', 3);
+define('MDB2_AUTOQUERY_SELECT', 4);
/**
* MDB2_Extended: class which adds several high level methods to MDB2
@@ -73,16 +74,23 @@
*
* @param string table
* @param array the fields names
- * @param int type of query to make (MDB2_AUTOQUERY_INSERT, MDB2_AUTOQUERY_UPDATE or
MDB2_AUTOQUERY_DELETE)
+ * @param int type of query to build
+ * MDB2_AUTOQUERY_INSERT
+ * MDB2_AUTOQUERY_UPDATE
+ * MDB2_AUTOQUERY_DELETE
+ * MDB2_AUTOQUERY_SELECT
* @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 mixed array that contains the types of the columns in
+ * the result set or MDB2_PREPARE_RESULT, if set to
+ * MDB2_PREPARE_MANIP the query is handled as a manipulation query
*
* @return resource handle for the query
* @see buildManipSQL
* @access public
*/
function autoPrepare($table, $table_fields, $mode = MDB2_AUTOQUERY_INSERT,
- $where = false, $types = null)
+ $where = false, $types = null, $result_types = MDB2_PREPARE_MANIP)
{
$query = $this->buildManipSQL($table, $table_fields, $mode, $where);
if (PEAR::isError($query)) {
@@ -93,7 +101,7 @@
return $db;
}
- return $db->prepare($query, $types, MDB2_PREPARE_MANIP);
+ return $db->prepare($query, $types, $result_types);
}
// }}}
@@ -104,10 +112,17 @@
*
* @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, MDB2_AUTOQUERY_UPDATE or
MDB2_AUTOQUERY_DELETE)
+ * @param int type of query to build
+ * MDB2_AUTOQUERY_INSERT
+ * MDB2_AUTOQUERY_UPDATE
+ * MDB2_AUTOQUERY_DELETE
+ * MDB2_AUTOQUERY_SELECT
* @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
+ * @param mixed array that contains the types of the columns in
+ * the result set or MDB2_PREPARE_RESULT, if set to
+ * MDB2_PREPARE_MANIP the query is handled as a manipulation query
*
* @return bool|MDB2_Error true on success, a MDB2 error on failure
* @see buildManipSQL
@@ -115,10 +130,15 @@
* @access public
*/
function &autoExecute($table, $fields_values, $mode = MDB2_AUTOQUERY_INSERT,
- $where = false, $types = null, $result_class = true)
+ $where = false, $types = null, $result_class = true, $result_types = MDB2_PREPARE_MANIP)
{
$fields_values = (array)$fields_values;
- $stmt = $this->autoPrepare($table, array_keys($fields_values), $mode, $where, $types);
+ if (MDB2_AUTOQUERY_SELECT && is_array($result_types)) {
+ $keys = array_keys($result_types);
+ } else {
+ $keys = array_keys($fields_values);
+ }
+ $stmt = $this->autoPrepare($table, $keys, $mode, $where, $types, $result_types);
if (PEAR::isError($stmt)) {
return $stmt;
}
@@ -142,7 +162,11 @@
*
* @param string name of the table
* @param ordered array containing the fields names
- * @param int type of query to make (MDB2_AUTOQUERY_INSERT, MDB2_AUTOQUERY_UPDATE or
MDB2_AUTOQUERY_DELETE)
+ * @param int type of query to build
+ * MDB2_AUTOQUERY_INSERT
+ * MDB2_AUTOQUERY_UPDATE
+ * MDB2_AUTOQUERY_DELETE
+ * MDB2_AUTOQUERY_SELECT
* @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()
@@ -155,7 +179,7 @@
return $db;
}
- if ($mode !== MDB2_AUTOQUERY_DELETE) {
+ if ($mode == MDB2_AUTOQUERY_INSERT || $mode == MDB2_AUTOQUERY_UPDATE) {
$count = count($table_fields);
if ($count == 0) {
return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
@@ -189,6 +213,11 @@
$sql = 'DELETE FROM '.$table.$where;
return $sql;
break;
+ case MDB2_AUTOQUERY_SELECT:
+ $cols = is_array($table_fields) ? implode(', ', $table_fields) :
'*';
+ $sql = 'SELECT '.$cols.' FROM '.$table.$where;
+ return $sql;
+ break;
}
return $db->raiseError(MDB2_ERROR_SYNTAX);
}
http://cvs.php.net/viewcvs.cgi/pear/MDB2/tests/MDB2_extended_testcase.php?r1=1.4&r2=1.5&diff_format=u
Index: pear/MDB2/tests/MDB2_extended_testcase.php
diff -u pear/MDB2/tests/MDB2_extended_testcase.php:1.4
pear/MDB2/tests/MDB2_extended_testcase.php:1.5
--- pear/MDB2/tests/MDB2_extended_testcase.php:1.4 Tue May 23 10:11:49 2006
+++ pear/MDB2/tests/MDB2_extended_testcase.php Sat Jun 17 12:07:01 2006
@@ -41,7 +41,7 @@
// | Author: Paul Cooper <pgc@ucecom.com> |
// +----------------------------------------------------------------------+
//
-// $Id: MDB2_extended_testcase.php,v 1.4 2006/05/23 10:11:49 lsmith Exp $
+// $Id: MDB2_extended_testcase.php,v 1.5 2006/06/17 12:07:01 lsmith Exp $
require_once 'MDB2_testcase.php';
@@ -72,6 +72,15 @@
$this->verifyFetchedValues($result, null, $data);
$result->free();
+ $where = 'user_id = '.$this->db->quote($data['user_id'],
'integer');
+ $result = $this->db->extended->autoExecute('users', null,
MDB2_AUTOQUERY_SELECT, $where, null, true, $this->fields);
+ if (PEAR::isError($result)) {
+ $this->assertTrue(false, 'Error auto executing select:
'.$result->getMessage());
+ }
+
+ $this->verifyFetchedValues($result, null, $data);
+ $result->free();
+
$update_data = array();
$data['user_name'] = $update_data['user_name'] = 'foo';
@@ -92,7 +101,6 @@
$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());
}