DB::$param_char can now be used to change the char that needs to appear in front of params (% is the default)
This commit is contained in:
51
db.class.php
51
db.class.php
@@ -37,6 +37,7 @@ class DB
|
||||
public static $success_handler = false;
|
||||
public static $error_handler = true;
|
||||
public static $throw_exception_on_error = false;
|
||||
public static $param_char = '%';
|
||||
|
||||
public static function get() {
|
||||
static $mysql = null;
|
||||
@@ -244,26 +245,25 @@ class DB
|
||||
return $sql;
|
||||
}
|
||||
|
||||
/*
|
||||
%s = string
|
||||
%i = integer
|
||||
%d = decimal / double
|
||||
%b = backtick
|
||||
%l = literal
|
||||
|
||||
%ls = list of strings
|
||||
%li = list of integers
|
||||
%ld = list of doubles
|
||||
%ll = list of literals
|
||||
%lb = list of backticks
|
||||
*/
|
||||
|
||||
public static function parseQueryParamsNew() {
|
||||
$args = func_get_args();
|
||||
$sql = array_shift($args);
|
||||
$posList = array();
|
||||
$pos_adj = 0;
|
||||
$types = array('%ll', '%ls', '%l', '%li', '%ld', '%lb', '%s', '%i', '%d', '%b', '%ss');
|
||||
$param_char_length = strlen(DB::$param_char);
|
||||
$types = array(
|
||||
DB::$param_char . 'll', // list of literals
|
||||
DB::$param_char . 'ls', // list of strings
|
||||
DB::$param_char . 'l', // literal
|
||||
DB::$param_char . 'li', // list of integers
|
||||
DB::$param_char . 'ld', // list of decimals
|
||||
DB::$param_char . 'lb', // list of backticks
|
||||
DB::$param_char . 's', // string
|
||||
DB::$param_char . 'i', // integer
|
||||
DB::$param_char . 'd', // double / decimal
|
||||
DB::$param_char . 'b', // backtick
|
||||
DB::$param_char . 'ss' // search string (like string, surrounded with %'s)
|
||||
);
|
||||
|
||||
foreach ($types as $type) {
|
||||
$lastPos = 0;
|
||||
@@ -278,26 +278,25 @@ class DB
|
||||
|
||||
foreach ($posList as $pos => $type) {
|
||||
$arg = array_shift($args);
|
||||
$type = substr($type, $param_char_length);
|
||||
$length_type = strlen($type) + $param_char_length;
|
||||
|
||||
if (in_array($type, array('%s', '%i', '%d', '%b', '%l'))) {
|
||||
if (in_array($type, array('s', 'i', 'd', 'b', 'l'))) {
|
||||
$array_type = false;
|
||||
$arg = array($arg);
|
||||
$length_type = strlen($type);
|
||||
$type = '%l' . substr($type, 1);
|
||||
} else if ($type == '%ss') {
|
||||
$type = 'l' . $type;
|
||||
} else if ($type == 'ss') {
|
||||
$result = "'%" . DB::escape(str_replace(array('%', '_'), array('\%', '\_'), $arg)) . "%'";
|
||||
$length_type = strlen($type);
|
||||
} else {
|
||||
$array_type = true;
|
||||
$length_type = strlen($type);
|
||||
if (! is_array($arg)) die("Badly formatted SQL query: $sql -- expecting array, but didn't get one!");
|
||||
}
|
||||
|
||||
if ($type == '%ls') $result = DB::wrapStr($arg, "'", true);
|
||||
else if ($type == '%li') $result = array_map('intval', $arg);
|
||||
else if ($type == '%ld') $result = array_map('floatval', $arg);
|
||||
else if ($type == '%lb') $result = array_map('DB::formatTableName', $arg);
|
||||
else if ($type == '%ll') $result = $arg;
|
||||
if ($type == 'ls') $result = DB::wrapStr($arg, "'", true);
|
||||
else if ($type == 'li') $result = array_map('intval', $arg);
|
||||
else if ($type == 'ld') $result = array_map('floatval', $arg);
|
||||
else if ($type == 'lb') $result = array_map('DB::formatTableName', $arg);
|
||||
else if ($type == 'll') $result = $arg;
|
||||
else if (! $result) die("Badly formatted SQL query: $sql");
|
||||
|
||||
if (is_array($result)) {
|
||||
|
||||
@@ -86,9 +86,11 @@ class BasicTest extends SimpleTest {
|
||||
$counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
|
||||
$this->assert($counter === strval(3));
|
||||
|
||||
$bart = DB::queryFirstRow("SELECT * FROM accounts WHERE age IN %li AND height IN %ld AND username IN %ls",
|
||||
DB::$param_char = '###';
|
||||
$bart = DB::queryFirstRow("SELECT * FROM accounts WHERE age IN ###li AND height IN ###ld AND username IN ###ls",
|
||||
array(15, 25), array(10.371, 150.123), array('Bart', 'Barts'));
|
||||
$this->assert($bart['username'] === 'Bart');
|
||||
DB::$param_char = '%';
|
||||
|
||||
$charlie_password = DB::queryFirstField("SELECT password FROM accounts WHERE username IN %ls AND username = %s",
|
||||
array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend');
|
||||
|
||||
Reference in New Issue
Block a user