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 $success_handler = false;
|
||||||
public static $error_handler = true;
|
public static $error_handler = true;
|
||||||
public static $throw_exception_on_error = false;
|
public static $throw_exception_on_error = false;
|
||||||
|
public static $param_char = '%';
|
||||||
|
|
||||||
public static function get() {
|
public static function get() {
|
||||||
static $mysql = null;
|
static $mysql = null;
|
||||||
@@ -244,26 +245,25 @@ class DB
|
|||||||
return $sql;
|
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() {
|
public static function parseQueryParamsNew() {
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
$sql = array_shift($args);
|
$sql = array_shift($args);
|
||||||
$posList = array();
|
$posList = array();
|
||||||
$pos_adj = 0;
|
$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) {
|
foreach ($types as $type) {
|
||||||
$lastPos = 0;
|
$lastPos = 0;
|
||||||
@@ -278,26 +278,25 @@ class DB
|
|||||||
|
|
||||||
foreach ($posList as $pos => $type) {
|
foreach ($posList as $pos => $type) {
|
||||||
$arg = array_shift($args);
|
$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;
|
$array_type = false;
|
||||||
$arg = array($arg);
|
$arg = array($arg);
|
||||||
$length_type = strlen($type);
|
$type = 'l' . $type;
|
||||||
$type = '%l' . substr($type, 1);
|
} else if ($type == 'ss') {
|
||||||
} else if ($type == '%ss') {
|
|
||||||
$result = "'%" . DB::escape(str_replace(array('%', '_'), array('\%', '\_'), $arg)) . "%'";
|
$result = "'%" . DB::escape(str_replace(array('%', '_'), array('\%', '\_'), $arg)) . "%'";
|
||||||
$length_type = strlen($type);
|
|
||||||
} else {
|
} else {
|
||||||
$array_type = true;
|
$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 (! is_array($arg)) die("Badly formatted SQL query: $sql -- expecting array, but didn't get one!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($type == '%ls') $result = DB::wrapStr($arg, "'", true);
|
if ($type == 'ls') $result = DB::wrapStr($arg, "'", true);
|
||||||
else if ($type == '%li') $result = array_map('intval', $arg);
|
else if ($type == 'li') $result = array_map('intval', $arg);
|
||||||
else if ($type == '%ld') $result = array_map('floatval', $arg);
|
else if ($type == 'ld') $result = array_map('floatval', $arg);
|
||||||
else if ($type == '%lb') $result = array_map('DB::formatTableName', $arg);
|
else if ($type == 'lb') $result = array_map('DB::formatTableName', $arg);
|
||||||
else if ($type == '%ll') $result = $arg;
|
else if ($type == 'll') $result = $arg;
|
||||||
else if (! $result) die("Badly formatted SQL query: $sql");
|
else if (! $result) die("Badly formatted SQL query: $sql");
|
||||||
|
|
||||||
if (is_array($result)) {
|
if (is_array($result)) {
|
||||||
|
|||||||
@@ -86,9 +86,11 @@ class BasicTest extends SimpleTest {
|
|||||||
$counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
|
$counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts");
|
||||||
$this->assert($counter === strval(3));
|
$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'));
|
array(15, 25), array(10.371, 150.123), array('Bart', 'Barts'));
|
||||||
$this->assert($bart['username'] === 'Bart');
|
$this->assert($bart['username'] === 'Bart');
|
||||||
|
DB::$param_char = '%';
|
||||||
|
|
||||||
$charlie_password = DB::queryFirstField("SELECT password FROM accounts WHERE username IN %ls AND username = %s",
|
$charlie_password = DB::queryFirstField("SELECT password FROM accounts WHERE username IN %ls AND username = %s",
|
||||||
array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend');
|
array('Charlie', 'Charlie\'s Friend'), 'Charlie\'s Friend');
|
||||||
|
|||||||
Reference in New Issue
Block a user