cvs: pear /DB/DB common.php
| From: | Fabien MARTY | Date: | Sun, 09 Jun 2002 15:01:19 +0000 |
| Subject: | cvs: pear /DB/DB common.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-4505@lists.php.net to get a copy of this message | ||
fab Sun Jun 9 11:01:19 2002 EDT
Modified files:
/pear/DB/DB common.php
Log:
Add of autoPrepare()
Index: pear/DB/DB/common.php
diff -u pear/DB/DB/common.php:1.5 pear/DB/DB/common.php:1.6
--- pear/DB/DB/common.php:1.5 Tue Jun 4 10:17:16 2002
+++ pear/DB/DB/common.php Sun Jun 9 11:01:16 2002
@@ -16,7 +16,7 @@
// | Author: Stig Bakken <ssb@fast.no> |
// +----------------------------------------------------------------------+
//
-// $Id: common.php,v 1.5 2002/06/04 14:17:16 cox Exp $
+// $Id: common.php,v 1.6 2002/06/09 15:01:16 fab Exp $
//
// Base class for DB implementations.
//
@@ -426,6 +426,60 @@
$this->prepared_queries[$k] = &$query;
return $k;
+ }
+
+ // }}}
+ // {{{ autoPrepare()
+
+ /**
+ * Make automaticaly an insert or update query and call prepare() with it
+ *
+ * NB : This belongs more to a SQL Builder class, but this is a simple facility
+ *
+ * @param string $table name of the table
+ * @param array $array assoc ($key=>$value), $key are fields names, $value are not used
+ * @param int $mode type of queries to make (DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE)
+ * @param string $where in case of update queries, this string will be put after the sql WHERE
statement
+ * @return resource handle for the query
+ * @access public
+ */
+ function autoPrepare($table, $array, $mode = DB_AUTOQUERY_INSERT, $where = NULL)
+ {
+ if (count($array)==0) {
+ $this->raiseError(DB_ERROR_NEED_MORE_DATA);
+ }
+ $first = true;
+ switch($mode) {
+ case DB_AUTOQUERY_INSERT:
+ $values = '';
+ $names = '';
+ while (list($key, $value) = each($array)) {
+ if ($first) {
+ $first = false;
+ } else {
+ $names .= ',';
+ $values .= ',';
+ }
+ $names .= $key;
+ $values .= '?';
+ }
+ return $this->prepare("INSERT INTO $table ($names) VALUES ($values)");
+ break;
+ case DB_AUTOQUERY_UPDATE:
+ $set = '';
+ while (list($key, $value) = each($array)) {
+ if ($first) {
+ $first = false;
+ } else {
+ $set .= ',';
+ }
+ $set .= "$key = ?";
+ }
+ return $this->prepare("UPDATE $table SET $set WHERE $where");
+ break;
+ default:
+ $this->raiseError(DB_ERROR_SYNTAX);
+ }
}
// }}}