cvs: pear /DB/DB ibase.php

From: Date: Wed, 21 Jan 2004 18:45:16 +0000
Subject: cvs: pear /DB/DB ibase.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-15844@lists.php.net to get a copy of this message
danielc Wed Jan 21 13:45:16 2004 EDT Modified files: /pear/DB/DB ibase.php Log: prepare() now allows placeholder characters to be escaped so they can be used in literals. Bug 31. Update docblock. http://cvs.php.net/diff.php/pear/DB/DB/ibase.php?r1=1.32&r2=1.33&ty=u Index: pear/DB/DB/ibase.php diff -u pear/DB/DB/ibase.php:1.32 pear/DB/DB/ibase.php:1.33 --- pear/DB/DB/ibase.php:1.32 Wed Jan 21 01:11:43 2004 +++ pear/DB/DB/ibase.php Wed Jan 21 13:45:14 2004 @@ -16,7 +16,7 @@ // | Author: Sterling Hughes <sterling@php.net> | // +----------------------------------------------------------------------+ // -// $Id: ibase.php,v 1.32 2004/01/21 06:11:43 danielc Exp $ +// $Id: ibase.php,v 1.33 2004/01/21 18:45:14 danielc Exp $ // Bugs: @@ -30,7 +30,7 @@ * extension. * * @package DB - * @version $Id: ibase.php,v 1.32 2004/01/21 06:11:43 danielc Exp $ + * @version $Id: ibase.php,v 1.33 2004/01/21 18:45:14 danielc Exp $ * @category Database * @author Sterling Hughes <sterling@php.net> */ @@ -335,18 +335,36 @@ /** * Prepares a query for multiple execution with execute(). - * @param $query query to be prepared * - * @return DB statement resource + * prepare() requires a generic query as string like <code> + * INSERT INTO numbers VALUES (?, ?, ?) + * </code>. The <kbd>?</kbd> characters are placeholders. + * + * Three types of placeholders can be used: + * + <kbd>?</kbd> a quoted scalar value, i.e. strings, integers + * + <kbd>!</kbd> value is inserted 'as is' + * + <kbd>&</kbd> requires a file name. The file's contents get + * inserted into the query (i.e. saving binary + * data in a db) + * + * Use backslashes to escape placeholder characters if you don't want + * them to be interpreted as placeholders. Example: <code> + * "UPDATE foo SET col=? WHERE col='over \& under'" + * </code> + * + * @param string $query query to be prepared + * @return mixed DB statement resource on success. DB_Error on failure. */ function prepare($query) { - $tokens = split('[&?!]', $query); - $token = 0; - $types = array(); - $qlen = strlen($query); - for ($i = 0; $i < $qlen; $i++) { - switch ($query[$i]) { + $tokens = preg_split('/((?<!\\\)[&?!])/', $query, -1, + PREG_SPLIT_DELIM_CAPTURE); + $token = 0; + $types = array(); + $newquery = ''; + + foreach ($tokens as $key => $val) { + switch ($val) { case '?': $types[$token++] = DB_PARAM_SCALAR; break; @@ -356,9 +374,13 @@ case '!': $types[$token++] = DB_PARAM_MISC; break; + default: + $tokens[$key] = preg_replace('/\\\([&?!])/', "\\1", $val); + $newquery .= $tokens[$key] . '?'; } } - $newquery = strtr($query, '&', '?'); + + $newquery = substr($newquery, 0, -1); $this->last_query = $query; $newquery = $this->modifyQuery($newquery); $stmt = ibase_prepare($this->connection, $newquery);

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