can now refer to a specific argument in a query

e.g. %s2 for the third argument, taken as a string
This commit is contained in:
Sergey Tsalkov
2011-12-17 19:54:55 -08:00
parent a1f01a4f87
commit 7825f90080
2 changed files with 17 additions and 3 deletions

View File

@@ -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;
}

View File

@@ -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);