Re: cvs: pear /DB/DB common.php

From: Date: Sun, 09 Jun 2002 15:38:50 +0000
Subject: Re: cvs: pear /DB/DB common.php
References: 1  Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-4507@lists.php.net to get a copy of this message
Fabien MARTY wrote: > > fab Sun Jun 9 11:01:19 2002 EDT > > Modified files: > /pear/DB/DB common.php > Log: > Add of autoPrepare() > Umm, I'm asking my self why not directly execute the query too? return $this->query($sql, array_values($array)); would do the trick. btw the method could become something like autoExecute or buildExecute. Tomas V.V.Cox > > 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); > + } > } > > // }}} > > -- > PEAR CVS Mailing List (http://pear.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.pear.cvs (#4507) next »