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:
17
db.class.php
17
db.class.php
@@ -304,6 +304,7 @@ class DB
|
|||||||
public static function parseQueryParamsNew() {
|
public static function parseQueryParamsNew() {
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
$sql = array_shift($args);
|
$sql = array_shift($args);
|
||||||
|
$args_all = $args;
|
||||||
$posList = array();
|
$posList = array();
|
||||||
$pos_adj = 0;
|
$pos_adj = 0;
|
||||||
$param_char_length = strlen(DB::$param_char);
|
$param_char_length = strlen(DB::$param_char);
|
||||||
@@ -333,10 +334,20 @@ class DB
|
|||||||
ksort($posList);
|
ksort($posList);
|
||||||
|
|
||||||
foreach ($posList as $pos => $type) {
|
foreach ($posList as $pos => $type) {
|
||||||
$arg = array_shift($args);
|
|
||||||
$type = substr($type, $param_char_length);
|
$type = substr($type, $param_char_length);
|
||||||
$length_type = strlen($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'))) {
|
if (in_array($type, array('s', 'i', 'd', 'b', 'l'))) {
|
||||||
$array_type = false;
|
$array_type = false;
|
||||||
$arg = array($arg);
|
$arg = array($arg);
|
||||||
@@ -360,8 +371,8 @@ class DB
|
|||||||
else $result = '(' . implode(',', $result) . ')';
|
else $result = '(' . implode(',', $result) . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql = substr_replace($sql, $result, $pos + $pos_adj, $length_type);
|
$sql = substr_replace($sql, $result, $pos + $pos_adj, $length_type + $arg_number_length);
|
||||||
$pos_adj += strlen($result) - $length_type;
|
$pos_adj += strlen($result) - ($length_type + $arg_number_length);
|
||||||
}
|
}
|
||||||
return $sql;
|
return $sql;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -165,6 +165,9 @@ class BasicTest extends SimpleTest {
|
|||||||
$ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', 199.194);
|
$ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', 199.194);
|
||||||
$this->assert(intval($ct) === 1);
|
$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');
|
DB::delete('accounts', 'username=%s AND age=%i AND height=%d', 'gonesoon', '61', '199.194');
|
||||||
$this->assert(DB::affectedRows() === 1);
|
$this->assert(DB::affectedRows() === 1);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user