From 7825f900800c270d5257c556ba2b93ea64a3c41e Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Sat, 17 Dec 2011 19:54:55 -0800 Subject: [PATCH] can now refer to a specific argument in a query e.g. %s2 for the third argument, taken as a string --- db.class.php | 17 ++++++++++++++--- simpletest/BasicTest.php | 3 +++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/db.class.php b/db.class.php index 254a135..7eb619d 100644 --- a/db.class.php +++ b/db.class.php @@ -304,6 +304,7 @@ class DB public static function parseQueryParamsNew() { $args = func_get_args(); $sql = array_shift($args); + $args_all = $args; $posList = array(); $pos_adj = 0; $param_char_length = strlen(DB::$param_char); @@ -333,10 +334,20 @@ class DB ksort($posList); foreach ($posList as $pos => $type) { - $arg = array_shift($args); $type = substr($type, $param_char_length); $length_type = strlen($type) + $param_char_length; + if ($arg_number_length = strspn($sql, '0123456789', $pos + $pos_adj + $length_type)) { + $arg_number = substr($sql, $pos + $pos_adj + $length_type, $arg_number_length); + if (! isset($args_all[$arg_number])) DB::nonSQLError("Non existent argument reference (arg $arg_number): $sql"); + + $arg = $args_all[$arg_number]; + + } else { + $arg_number = 0; + $arg = array_shift($args); + } + if (in_array($type, array('s', 'i', 'd', 'b', 'l'))) { $array_type = false; $arg = array($arg); @@ -360,8 +371,8 @@ class DB else $result = '(' . implode(',', $result) . ')'; } - $sql = substr_replace($sql, $result, $pos + $pos_adj, $length_type); - $pos_adj += strlen($result) - $length_type; + $sql = substr_replace($sql, $result, $pos + $pos_adj, $length_type + $arg_number_length); + $pos_adj += strlen($result) - ($length_type + $arg_number_length); } return $sql; } diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php index 3b92cdc..32f9fcd 100644 --- a/simpletest/BasicTest.php +++ b/simpletest/BasicTest.php @@ -165,6 +165,9 @@ class BasicTest extends SimpleTest { $ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', 199.194); $this->assert(intval($ct) === 1); + $ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s1 AND height=%d0 AND height=%d", 199.194, 'gonesoon'); + $this->assert(intval($ct) === 1); + DB::delete('accounts', 'username=%s AND age=%i AND height=%d', 'gonesoon', '61', '199.194'); $this->assert(DB::affectedRows() === 1);