Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ed2837f3d | ||
|
|
65cae974c5 | ||
|
|
96892fa6c2 | ||
|
|
ad4889da09 | ||
|
|
12604d7854 | ||
|
|
8c5299c2e2 | ||
|
|
964a36e4c2 | ||
|
|
68774532e1 | ||
|
|
2985815750 | ||
|
|
4faebb957c |
167
db.class.php
167
db.class.php
@@ -22,13 +22,11 @@ class DB
|
|||||||
public static $insert_id = 0;
|
public static $insert_id = 0;
|
||||||
public static $num_rows = 0;
|
public static $num_rows = 0;
|
||||||
public static $affected_rows = 0;
|
public static $affected_rows = 0;
|
||||||
public static $stmt = null;
|
|
||||||
public static $queryResult = null;
|
public static $queryResult = null;
|
||||||
public static $queryResultType = null;
|
public static $queryResultType = null;
|
||||||
public static $old_db = null;
|
public static $old_db = null;
|
||||||
public static $current_db = null;
|
public static $current_db = null;
|
||||||
public static $current_db_limit = 0;
|
public static $dbName = '';
|
||||||
public static $dbName = null;
|
|
||||||
public static $user = '';
|
public static $user = '';
|
||||||
public static $password = '';
|
public static $password = '';
|
||||||
public static $host = 'localhost';
|
public static $host = 'localhost';
|
||||||
@@ -36,17 +34,17 @@ class DB
|
|||||||
public static $encoding = 'latin1';
|
public static $encoding = 'latin1';
|
||||||
public static $queryMode = 'queryAllRows';
|
public static $queryMode = 'queryAllRows';
|
||||||
public static $success_handler = false;
|
public static $success_handler = false;
|
||||||
public static $error_handler = 'meekrodb_error_handler';
|
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($dbName = '') {
|
public static function get() {
|
||||||
static $mysql = null;
|
static $mysql = null;
|
||||||
|
|
||||||
if ($mysql == null) {
|
if ($mysql == null) {
|
||||||
if (! DB::$port) DB::$port = ini_get('mysqli.default_port');
|
if (! DB::$port) DB::$port = ini_get('mysqli.default_port');
|
||||||
if (DB::$dbName != '') $dbName = DB::$dbName;
|
DB::$current_db = DB::$dbName;
|
||||||
DB::$current_db = $dbName;
|
$mysql = new mysqli(DB::$host, DB::$user, DB::$password, DB::$dbName, DB::$port);
|
||||||
$mysql = new mysqli(DB::$host, DB::$user, DB::$password, $dbName, DB::$port);
|
|
||||||
$mysql->set_charset(DB::$encoding);
|
$mysql->set_charset(DB::$encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,12 +61,11 @@ class DB
|
|||||||
public static function numRows() { return DB::$num_rows; }
|
public static function numRows() { return DB::$num_rows; }
|
||||||
|
|
||||||
public static function useDB() { $args = func_get_args(); return call_user_func_array('DB::setDB', $args); }
|
public static function useDB() { $args = func_get_args(); return call_user_func_array('DB::setDB', $args); }
|
||||||
public static function setDB($dbName, $limit=0) {
|
public static function setDB($dbName) {
|
||||||
$db = DB::get();
|
$db = DB::get();
|
||||||
DB::$old_db = DB::$current_db;
|
DB::$old_db = DB::$current_db;
|
||||||
if (! $db->select_db($dbName)) die("unable to set db to $dbName");
|
if (! $db->select_db($dbName)) die("unable to set db to $dbName");
|
||||||
DB::$current_db = $dbName;
|
DB::$current_db = $dbName;
|
||||||
DB::$current_db_limit = $limit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -85,7 +82,7 @@ class DB
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function escape($str) {
|
public static function escape($str) {
|
||||||
$db = DB::get(DB::$dbName);
|
$db = DB::get();
|
||||||
return $db->real_escape_string($str);
|
return $db->real_escape_string($str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +130,14 @@ class DB
|
|||||||
$buildquery = "UPDATE " . self::formatTableName($table) . " SET ";
|
$buildquery = "UPDATE " . self::formatTableName($table) . " SET ";
|
||||||
$keyval = array();
|
$keyval = array();
|
||||||
foreach ($params as $key => $value) {
|
foreach ($params as $key => $value) {
|
||||||
$keyval[] = "`" . $key . "`=" . (is_int($value) ? $value : "'" . DB::escape($value) . "'");
|
if (is_object($value) && ($value instanceof MeekroDBEval)) {
|
||||||
|
$value = $value->text;
|
||||||
|
} else {
|
||||||
|
if (is_array($value)) $value = serialize($value);
|
||||||
|
$value = (is_int($value) ? $value : "'" . DB::escape($value) . "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
$keyval[] = "`" . $key . "`=" . $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
$buildquery = "UPDATE " . self::formatTableName($table) . " SET " . implode(', ', $keyval) . " WHERE " . $where;
|
$buildquery = "UPDATE " . self::formatTableName($table) . " SET " . implode(', ', $keyval) . " WHERE " . $where;
|
||||||
@@ -141,19 +145,46 @@ class DB
|
|||||||
call_user_func_array('DB::queryNull', $args);
|
call_user_func_array('DB::queryNull', $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function insertOrReplace($which, $table, $data) {
|
public static function insertOrReplace($which, $table, $datas) {
|
||||||
$data = unserialize(serialize($data)); // break references within array
|
$datas = unserialize(serialize($datas)); // break references within array
|
||||||
$keys_str = implode(', ', DB::wrapStr(array_keys($data), '`'));
|
$keys = null;
|
||||||
|
|
||||||
foreach ($data as &$datum) {
|
if (isset($datas[0]) && is_array($datas[0])) {
|
||||||
if (is_array($datum)) $datum = serialize($datum);
|
$many = true;
|
||||||
$datum = "'" . DB::escape($datum) . "'";
|
} else {
|
||||||
|
$datas = array($datas);
|
||||||
|
$many = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($datas as $data) {
|
||||||
|
if (! $keys) {
|
||||||
|
$keys = array_keys($data);
|
||||||
|
if ($many) sort($keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
$insert_values = array();
|
||||||
|
|
||||||
|
foreach ($keys as $key) {
|
||||||
|
if ($many && !isset($data[$key])) die("insert/replace many: each assoc array must have the same keys!");
|
||||||
|
$datum = $data[$key];
|
||||||
|
|
||||||
|
if (is_object($datum) && ($datum instanceof MeekroDBEval)) {
|
||||||
|
$datum = $datum->text;
|
||||||
|
} else {
|
||||||
|
$datum = (is_int($datum) ? $datum : "'" . DB::escape($datum) . "'");
|
||||||
|
}
|
||||||
|
$insert_values[] = $datum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$values[] = '(' . implode(', ', $insert_values) . ')';
|
||||||
}
|
}
|
||||||
$values_str = implode(', ', array_values($data));
|
|
||||||
|
|
||||||
$table = self::formatTableName($table);
|
$table = self::formatTableName($table);
|
||||||
|
$keys_str = implode(', ', DB::wrapStr($keys, '`'));
|
||||||
|
$values_str = implode(',', $values);
|
||||||
|
|
||||||
DB::queryNull("$which INTO $table ($keys_str) VALUES ($values_str)");
|
DB::queryNull("$which INTO $table ($keys_str) VALUES $values_str");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function insert($table, $data) {
|
public static function insert($table, $data) {
|
||||||
@@ -164,21 +195,30 @@ class DB
|
|||||||
return DB::insertOrReplace('REPLACE', $table, $data);
|
return DB::insertOrReplace('REPLACE', $table, $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function delete() {
|
||||||
|
$args = func_get_args();
|
||||||
|
$table = self::formatTableName(array_shift($args));
|
||||||
|
$where = array_shift($args);
|
||||||
|
$buildquery = "DELETE FROM $table WHERE $where";
|
||||||
|
array_unshift($args, $buildquery);
|
||||||
|
call_user_func_array('DB::queryNull', $args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function sqleval() {
|
||||||
|
$args = func_get_args();
|
||||||
|
$text = call_user_func_array('DB::parseQueryParams', $args);
|
||||||
|
return new MeekroDBEval($text);
|
||||||
|
}
|
||||||
|
|
||||||
public static function columnList($table) {
|
public static function columnList($table) {
|
||||||
return DB::queryOneColumn('Field', "SHOW COLUMNS FROM $table");
|
return DB::queryOneColumn('Field', "SHOW COLUMNS FROM $table");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function tableList($db = null) {
|
public static function tableList($db = null) {
|
||||||
if ($db) DB::useDB($db);
|
if ($db) DB::useDB($db);
|
||||||
$db = DB::$current_db;
|
$result = DB::queryFirstColumn('SHOW TABLES');
|
||||||
return DB::queryOneColumn('Tables_in_' . $db, "SHOW TABLES");
|
if ($db && DB::$old_db) DB::useDB(DB::$old_db);
|
||||||
}
|
return $result;
|
||||||
|
|
||||||
private static function checkUseDB() {
|
|
||||||
if (DB::$current_db_limit > 0) {
|
|
||||||
DB::$current_db_limit -= 1;
|
|
||||||
if (DB::$current_db_limit == 0) DB::useDB(DB::$old_db);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function parseQueryParamsOld() {
|
public static function parseQueryParamsOld() {
|
||||||
@@ -201,26 +241,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;
|
||||||
@@ -235,26 +274,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)) {
|
||||||
@@ -309,8 +347,10 @@ class DB
|
|||||||
if (DB::$success_handler) $runtime = microtime(true) - $starttime;
|
if (DB::$success_handler) $runtime = microtime(true) - $starttime;
|
||||||
|
|
||||||
if (!$sql || $error = DB::checkError()) {
|
if (!$sql || $error = DB::checkError()) {
|
||||||
if (function_exists(DB::$error_handler)) {
|
if (DB::$error_handler) {
|
||||||
call_user_func(DB::$error_handler, array(
|
$error_handler = is_callable(DB::$error_handler) ? DB::$error_handler : 'meekrodb_error_handler';
|
||||||
|
|
||||||
|
call_user_func($error_handler, array(
|
||||||
'query' => $sql,
|
'query' => $sql,
|
||||||
'error' => $error
|
'error' => $error
|
||||||
));
|
));
|
||||||
@@ -322,7 +362,7 @@ class DB
|
|||||||
}
|
}
|
||||||
} else if (DB::$success_handler) {
|
} else if (DB::$success_handler) {
|
||||||
$runtime = sprintf('%f', $runtime * 1000);
|
$runtime = sprintf('%f', $runtime * 1000);
|
||||||
$success_handler = function_exists(DB::$success_handler) ? DB::$success_handler : 'meekrodb_debugmode_handler';
|
$success_handler = is_callable(DB::$success_handler) ? DB::$success_handler : 'meekrodb_debugmode_handler';
|
||||||
|
|
||||||
call_user_func($success_handler, array(
|
call_user_func($success_handler, array(
|
||||||
'query' => $sql,
|
'query' => $sql,
|
||||||
@@ -338,9 +378,6 @@ class DB
|
|||||||
if ($is_buffered) DB::$num_rows = $result->num_rows;
|
if ($is_buffered) DB::$num_rows = $result->num_rows;
|
||||||
else DB::$num_rows = null;
|
else DB::$num_rows = null;
|
||||||
|
|
||||||
//TODO: fix DB switch back
|
|
||||||
//DB::checkUseDB();
|
|
||||||
|
|
||||||
if ($is_null) {
|
if ($is_null) {
|
||||||
DB::freeResult($result);
|
DB::freeResult($result);
|
||||||
DB::$queryResult = DB::$queryResultType = null;
|
DB::$queryResult = DB::$queryResultType = null;
|
||||||
@@ -602,4 +639,12 @@ function meekrodb_debugmode_handler($params) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MeekroDBEval {
|
||||||
|
public $text = '';
|
||||||
|
|
||||||
|
function __construct($text) {
|
||||||
|
$this->text = $text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -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');
|
||||||
@@ -116,14 +118,124 @@ class BasicTest extends SimpleTest {
|
|||||||
|
|
||||||
$results = DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
|
$results = DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
|
||||||
$this->assert(count($results) === 2);
|
$this->assert(count($results) === 2);
|
||||||
|
|
||||||
|
$columnlist = DB::columnList('accounts');
|
||||||
|
$this->assert(count($columnlist) === 5);
|
||||||
|
$this->assert($columnlist[0] === 'id');
|
||||||
|
$this->assert($columnlist[4] === 'height');
|
||||||
|
|
||||||
|
$tablelist = DB::tableList();
|
||||||
|
$this->assert(count($tablelist) === 1);
|
||||||
|
$this->assert($tablelist[0] === 'accounts');
|
||||||
|
|
||||||
|
$tablelist = null;
|
||||||
|
$tablelist = DB::tableList('libdb_test');
|
||||||
|
$this->assert(count($tablelist) === 1);
|
||||||
|
$this->assert($tablelist[0] === 'accounts');
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_4_1_query() {
|
||||||
|
DB::insert('accounts', array(
|
||||||
|
'username' => 'newguy',
|
||||||
|
'password' => DB::sqleval("REPEAT('blah', %i)", '3'),
|
||||||
|
'age' => DB::sqleval('171+1'),
|
||||||
|
'height' => 111.15
|
||||||
|
));
|
||||||
|
|
||||||
|
$row = DB::queryOneRow("SELECT * FROM accounts WHERE password=%s", 'blahblahblah');
|
||||||
|
$this->assert($row['username'] === 'newguy');
|
||||||
|
$this->assert($row['age'] === '172');
|
||||||
|
|
||||||
|
DB::update('accounts', array(
|
||||||
|
'password' => DB::sqleval("REPEAT('blah', %i)", 4),
|
||||||
|
), 'username=%s', 'newguy');
|
||||||
|
|
||||||
|
$row = null;
|
||||||
|
$row = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy');
|
||||||
|
$this->assert($row['password'] === 'blahblahblahblah');
|
||||||
|
|
||||||
|
DB::query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
|
||||||
|
$this->assert(DB::affectedRows() === 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_4_2_delete() {
|
||||||
|
DB::insert('accounts', array(
|
||||||
|
'username' => 'gonesoon',
|
||||||
|
'password' => 'something',
|
||||||
|
'age' => 61,
|
||||||
|
'height' => 199.194
|
||||||
|
));
|
||||||
|
|
||||||
|
$ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', 199.194);
|
||||||
|
$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);
|
||||||
|
|
||||||
|
$ct = DB::queryFirstField("SELECT COUNT(*) FROM accounts WHERE username=%s AND height=%d", 'gonesoon', '199.194');
|
||||||
|
$this->assert(intval($ct) === 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_4_3_insertmany() {
|
||||||
|
$ins[] = array(
|
||||||
|
'username' => '1ofmany',
|
||||||
|
'password' => 'something',
|
||||||
|
'age' => 23,
|
||||||
|
'height' => 190.194
|
||||||
|
);
|
||||||
|
$ins[] = array(
|
||||||
|
'password' => 'somethingelse',
|
||||||
|
'username' => '2ofmany',
|
||||||
|
'age' => 25,
|
||||||
|
'height' => 190.194
|
||||||
|
);
|
||||||
|
|
||||||
|
DB::insert('accounts', $ins);
|
||||||
|
$this->assert(DB::affectedRows() === 2);
|
||||||
|
|
||||||
|
$rows = DB::query("SELECT * FROM accounts WHERE height=%d ORDER BY age ASC", 190.194);
|
||||||
|
$this->assert(count($rows) === 2);
|
||||||
|
$this->assert($rows[0]['username'] === '1ofmany');
|
||||||
|
$this->assert($rows[0]['age'] === '23');
|
||||||
|
$this->assert($rows[1]['age'] === '25');
|
||||||
|
$this->assert($rows[1]['password'] === 'somethingelse');
|
||||||
|
$this->assert($rows[1]['username'] === '2ofmany');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_5_error_handler() {
|
function test_5_error_handler() {
|
||||||
global $error_callback_worked;
|
global $error_callback_worked, $static_error_callback_worked, $nonstatic_error_callback_worked,
|
||||||
|
$anonymous_error_callback_worked;
|
||||||
|
|
||||||
DB::$error_handler = 'new_error_callback';
|
DB::$error_handler = 'new_error_callback';
|
||||||
DB::query("SELET * FROM accounts");
|
DB::query("SELET * FROM accounts");
|
||||||
$this->assert($error_callback_worked === 1);
|
$this->assert($error_callback_worked === 1);
|
||||||
|
|
||||||
|
DB::$error_handler = array('BasicTest', 'static_error_callback');
|
||||||
|
DB::query("SELET * FROM accounts");
|
||||||
|
$this->assert($static_error_callback_worked === 1);
|
||||||
|
|
||||||
|
DB::$error_handler = array($this, 'nonstatic_error_callback');
|
||||||
|
DB::query("SELET * FROM accounts");
|
||||||
|
$this->assert($nonstatic_error_callback_worked === 1);
|
||||||
|
|
||||||
|
DB::$error_handler = function($params) {
|
||||||
|
global $anonymous_error_callback_worked;
|
||||||
|
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $anonymous_error_callback_worked = 1;
|
||||||
|
};
|
||||||
|
DB::query("SELET * FROM accounts");
|
||||||
|
$this->assert($anonymous_error_callback_worked === 1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function static_error_callback($params) {
|
||||||
|
global $static_error_callback_worked;
|
||||||
|
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $static_error_callback_worked = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function nonstatic_error_callback($params) {
|
||||||
|
global $nonstatic_error_callback_worked;
|
||||||
|
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $nonstatic_error_callback_worked = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_6_exception_catch() {
|
function test_6_exception_catch() {
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ foreach ($classes_to_test as $class) {
|
|||||||
$object = new $class();
|
$object = new $class();
|
||||||
|
|
||||||
foreach (get_class_methods($object) as $method) {
|
foreach (get_class_methods($object) as $method) {
|
||||||
if (substr($method, 0, 2) == '__') continue;
|
if (substr($method, 0, 4) != 'test') continue;
|
||||||
echo "Running $class::$method..\n";
|
echo "Running $class::$method..\n";
|
||||||
$object->$method();
|
$object->$method();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user