Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a1f01a4f87 | ||
|
|
80cc78edcd | ||
|
|
5050205e58 | ||
|
|
9f3aa571dc | ||
|
|
a2800ef04b | ||
|
|
164c7157a6 | ||
|
|
f09308a3f7 | ||
|
|
69bfe15ebb | ||
|
|
7621b50a02 | ||
|
|
4122fe52c1 | ||
|
|
8072f6ab07 | ||
|
|
860da8d18a |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -0,0 +1 @@
|
|||||||
|
simpletest/test_setup.php
|
||||||
|
|||||||
145
db.class.php
145
db.class.php
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
class DB
|
class DB
|
||||||
{
|
{
|
||||||
|
public static $internal_mysql = null;
|
||||||
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;
|
||||||
@@ -33,24 +34,48 @@ class DB
|
|||||||
public static $port = null;
|
public static $port = null;
|
||||||
public static $encoding = 'latin1';
|
public static $encoding = 'latin1';
|
||||||
public static $queryMode = 'queryAllRows';
|
public static $queryMode = 'queryAllRows';
|
||||||
|
public static $param_char = '%';
|
||||||
|
|
||||||
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 $nonsql_error_handler = null;
|
||||||
|
public static $throw_exception_on_nonsql_error = false;
|
||||||
|
|
||||||
|
|
||||||
public static function get() {
|
public static function get() {
|
||||||
static $mysql = null;
|
$mysql = DB::$internal_mysql;
|
||||||
|
|
||||||
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');
|
||||||
DB::$current_db = DB::$dbName;
|
DB::$current_db = DB::$dbName;
|
||||||
$mysql = new mysqli(DB::$host, DB::$user, DB::$password, DB::$dbName, DB::$port);
|
$mysql = new mysqli(DB::$host, DB::$user, DB::$password, DB::$dbName, DB::$port);
|
||||||
|
|
||||||
|
if ($mysql->connect_error) {
|
||||||
|
DB::nonSQLError('Unable to connect to MySQL server! Error: ' . $mysql->connect_error);
|
||||||
|
}
|
||||||
|
|
||||||
$mysql->set_charset(DB::$encoding);
|
$mysql->set_charset(DB::$encoding);
|
||||||
|
DB::$internal_mysql = $mysql;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $mysql;
|
return $mysql;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function nonSQLError($message) {
|
||||||
|
if (DB::$throw_exception_on_nonsql_error) {
|
||||||
|
$e = new MeekroDBException($message);
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
|
||||||
|
$error_handler = is_callable(DB::$nonsql_error_handler) ? DB::$nonsql_error_handler : 'meekrodb_error_handler';
|
||||||
|
|
||||||
|
call_user_func($error_handler, array(
|
||||||
|
'type' => 'nonsql',
|
||||||
|
'error' => $message
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
public static function debugMode($handler = true) {
|
public static function debugMode($handler = true) {
|
||||||
DB::$success_handler = $handler;
|
DB::$success_handler = $handler;
|
||||||
}
|
}
|
||||||
@@ -64,7 +89,7 @@ class DB
|
|||||||
public static function setDB($dbName) {
|
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)) DB::nonSQLError("Unable to set database to $dbName");
|
||||||
DB::$current_db = $dbName;
|
DB::$current_db = $dbName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,6 +111,20 @@ class DB
|
|||||||
return $db->real_escape_string($str);
|
return $db->real_escape_string($str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function sanitize($value) {
|
||||||
|
if (is_object($value) && ($value instanceof MeekroDBEval)) {
|
||||||
|
$value = $value->text;
|
||||||
|
} else {
|
||||||
|
if (is_array($value) || is_object($value)) $value = serialize($value);
|
||||||
|
|
||||||
|
if (is_string($value)) $value = "'" . DB::escape($value) . "'";
|
||||||
|
else if (is_null($value)) $value = 'NULL';
|
||||||
|
else if (is_bool($value)) $value = ($value ? 1 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
private static function formatTableName($table) {
|
private static function formatTableName($table) {
|
||||||
$table = str_replace('`', '', $table);
|
$table = str_replace('`', '', $table);
|
||||||
if (strpos($table, '.')) {
|
if (strpos($table, '.')) {
|
||||||
@@ -130,13 +169,7 @@ 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) {
|
||||||
if (is_object($value) && ($value instanceof MeekroDBEval)) {
|
$value = DB::sanitize($value);
|
||||||
$value = $value->text;
|
|
||||||
} else {
|
|
||||||
if (is_array($value)) $value = serialize($value);
|
|
||||||
$value = (is_int($value) ? $value : "'" . DB::escape($value) . "'");
|
|
||||||
}
|
|
||||||
|
|
||||||
$keyval[] = "`" . $key . "`=" . $value;
|
$keyval[] = "`" . $key . "`=" . $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +178,7 @@ class DB
|
|||||||
call_user_func_array('DB::queryNull', $args);
|
call_user_func_array('DB::queryNull', $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function insertOrReplace($which, $table, $datas) {
|
public static function insertOrReplace($which, $table, $datas, $options=array()) {
|
||||||
$datas = unserialize(serialize($datas)); // break references within array
|
$datas = unserialize(serialize($datas)); // break references within array
|
||||||
$keys = null;
|
$keys = null;
|
||||||
|
|
||||||
@@ -165,14 +198,9 @@ class DB
|
|||||||
$insert_values = array();
|
$insert_values = array();
|
||||||
|
|
||||||
foreach ($keys as $key) {
|
foreach ($keys as $key) {
|
||||||
if ($many && !isset($data[$key])) die("insert/replace many: each assoc array must have the same keys!");
|
if ($many && !isset($data[$key])) DB::nonSQLError('insert/replace many: each assoc array must have the same keys!');
|
||||||
$datum = $data[$key];
|
$datum = $data[$key];
|
||||||
|
$datum = DB::sanitize($datum);
|
||||||
if (is_object($datum) && ($datum instanceof MeekroDBEval)) {
|
|
||||||
$datum = $datum->text;
|
|
||||||
} else {
|
|
||||||
$datum = (is_int($datum) ? $datum : "'" . DB::escape($datum) . "'");
|
|
||||||
}
|
|
||||||
$insert_values[] = $datum;
|
$insert_values[] = $datum;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,15 +212,47 @@ class DB
|
|||||||
$keys_str = implode(', ', DB::wrapStr($keys, '`'));
|
$keys_str = implode(', ', DB::wrapStr($keys, '`'));
|
||||||
$values_str = implode(',', $values);
|
$values_str = implode(',', $values);
|
||||||
|
|
||||||
DB::queryNull("$which INTO $table ($keys_str) VALUES $values_str");
|
if (isset($options['ignore']) && $options['ignore'] && strtolower($which) == 'insert') {
|
||||||
|
DB::queryNull("INSERT IGNORE INTO $table ($keys_str) VALUES $values_str");
|
||||||
|
|
||||||
|
} else if (isset($options['update']) && $options['update'] && strtolower($which) == 'insert') {
|
||||||
|
DB::queryNull("INSERT INTO $table ($keys_str) VALUES $values_str ON DUPLICATE KEY UPDATE {$options['update']}");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
DB::queryNull("$which INTO $table ($keys_str) VALUES $values_str");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function insert($table, $data) {
|
public static function insert($table, $data) { return DB::insertOrReplace('INSERT', $table, $data); }
|
||||||
return DB::insertOrReplace('INSERT', $table, $data);
|
public static function insertIgnore($table, $data) { return DB::insertOrReplace('INSERT', $table, $data, array('ignore' => true)); }
|
||||||
}
|
public static function replace($table, $data) { return DB::insertOrReplace('REPLACE', $table, $data); }
|
||||||
|
|
||||||
public static function replace($table, $data) {
|
public static function insertUpdate() {
|
||||||
return DB::insertOrReplace('REPLACE', $table, $data);
|
$args = func_get_args();
|
||||||
|
$table = array_shift($args);
|
||||||
|
$data = array_shift($args);
|
||||||
|
|
||||||
|
if (! isset($args[0])) { // update will have all the data of the insert
|
||||||
|
if (isset($data[0]) && is_array($data[0])) { //multiple insert rows specified -- failing!
|
||||||
|
DB::nonSQLError("Badly formatted insertUpdate() query -- you didn't specify the update component!");
|
||||||
|
}
|
||||||
|
|
||||||
|
$args[0] = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($args[0])) {
|
||||||
|
$keyval = array();
|
||||||
|
foreach ($args[0] as $key => $value) {
|
||||||
|
$value = DB::sanitize($value);
|
||||||
|
$keyval[] = "`" . $key . "`=" . $value;
|
||||||
|
}
|
||||||
|
$updatestr = implode(', ', $keyval);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$updatestr = call_user_func_array('DB::parseQueryParams', $args);
|
||||||
|
}
|
||||||
|
|
||||||
|
return DB::insertOrReplace('INSERT', $table, $data, array('update' => $updatestr));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function delete() {
|
public static function delete() {
|
||||||
@@ -230,11 +290,11 @@ class DB
|
|||||||
foreach ($args as $arg) {
|
foreach ($args as $arg) {
|
||||||
$type = array_shift($types);
|
$type = array_shift($types);
|
||||||
$pos = strpos($sql, '?');
|
$pos = strpos($sql, '?');
|
||||||
if ($pos === false) die("Badly formatted SQL query: $sql");
|
if ($pos === false) DB::nonSQLError("Badly formatted SQL query: $sql");
|
||||||
|
|
||||||
if ($type == 's') $replacement = "'" . DB::escape($arg) . "'";
|
if ($type == 's') $replacement = "'" . DB::escape($arg) . "'";
|
||||||
else if ($type == 'i') $replacement = intval($arg);
|
else if ($type == 'i') $replacement = intval($arg);
|
||||||
else die("Badly formatted SQL query: $sql");
|
else DB::nonSQLError("Badly formatted SQL query: $sql");
|
||||||
|
|
||||||
$sql = substr_replace($sql, $replacement, $pos, 1);
|
$sql = substr_replace($sql, $replacement, $pos, 1);
|
||||||
}
|
}
|
||||||
@@ -285,7 +345,7 @@ class DB
|
|||||||
$result = "'%" . DB::escape(str_replace(array('%', '_'), array('\%', '\_'), $arg)) . "%'";
|
$result = "'%" . DB::escape(str_replace(array('%', '_'), array('\%', '\_'), $arg)) . "%'";
|
||||||
} else {
|
} else {
|
||||||
$array_type = true;
|
$array_type = true;
|
||||||
if (! is_array($arg)) die("Badly formatted SQL query: $sql -- expecting array, but didn't get one!");
|
if (! is_array($arg)) DB::nonSQLError("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);
|
||||||
@@ -293,7 +353,7 @@ class DB
|
|||||||
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) DB::nonSQLError("Badly formatted SQL query: $sql");
|
||||||
|
|
||||||
if (is_array($result)) {
|
if (is_array($result)) {
|
||||||
if (! $array_type) $result = $result[0];
|
if (! $array_type) $result = $result[0];
|
||||||
@@ -328,13 +388,16 @@ class DB
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function queryNull() { $args = func_get_args(); return DB::prependCall('DB::queryHelper', $args, 'null'); }
|
public static function queryNull() { $args = func_get_args(); return DB::prependCall('DB::queryHelper', $args, 'null'); }
|
||||||
|
public static function queryRaw() { $args = func_get_args(); return DB::prependCall('DB::queryHelper', $args, 'buffered'); }
|
||||||
public static function queryBuf() { $args = func_get_args(); return DB::prependCall('DB::queryHelper', $args, 'buffered'); }
|
public static function queryBuf() { $args = func_get_args(); return DB::prependCall('DB::queryHelper', $args, 'buffered'); }
|
||||||
public static function queryUnbuf() { $args = func_get_args(); return DB::prependCall('DB::queryHelper', $args, 'unbuffered'); }
|
public static function queryUnbuf() { $args = func_get_args(); return DB::prependCall('DB::queryHelper', $args, 'unbuffered'); }
|
||||||
|
|
||||||
public static function queryHelper() {
|
public static function queryHelper() {
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
$type = array_shift($args);
|
$type = array_shift($args);
|
||||||
if ($type != 'buffered' && $type != 'unbuffered' && $type != 'null') die("Error -- first argument to queryHelper must be buffered or unbuffered!");
|
if ($type != 'buffered' && $type != 'unbuffered' && $type != 'null') {
|
||||||
|
DB::nonSQLError('Error -- first argument to queryHelper must be buffered or unbuffered!');
|
||||||
|
}
|
||||||
$is_buffered = ($type == 'buffered');
|
$is_buffered = ($type == 'buffered');
|
||||||
$is_null = ($type == 'null');
|
$is_null = ($type == 'null');
|
||||||
|
|
||||||
@@ -351,6 +414,7 @@ class DB
|
|||||||
$error_handler = is_callable(DB::$error_handler) ? DB::$error_handler : 'meekrodb_error_handler';
|
$error_handler = is_callable(DB::$error_handler) ? DB::$error_handler : 'meekrodb_error_handler';
|
||||||
|
|
||||||
call_user_func($error_handler, array(
|
call_user_func($error_handler, array(
|
||||||
|
'type' => 'sql',
|
||||||
'query' => $sql,
|
'query' => $sql,
|
||||||
'error' => $error
|
'error' => $error
|
||||||
));
|
));
|
||||||
@@ -531,7 +595,9 @@ class WhereClause {
|
|||||||
public $clauses = array();
|
public $clauses = array();
|
||||||
|
|
||||||
function __construct($type) {
|
function __construct($type) {
|
||||||
$this->type = strtolower($type);
|
$type = strtolower($type);
|
||||||
|
if ($type != 'or' && $type != 'and') DB::nonSQLError('you must use either WhereClause(and) or WhereClause(or)');
|
||||||
|
$this->type = $type;
|
||||||
}
|
}
|
||||||
|
|
||||||
function add() {
|
function add() {
|
||||||
@@ -548,7 +614,13 @@ class WhereClause {
|
|||||||
|
|
||||||
function negateLast() {
|
function negateLast() {
|
||||||
$i = count($this->clauses) - 1;
|
$i = count($this->clauses) - 1;
|
||||||
$this->clauses[$i] = 'NOT (' . $this->clauses[$i] . ')';
|
if (!isset($this->clauses[$i])) return;
|
||||||
|
|
||||||
|
if ($this->clauses[$i] instanceof WhereClause) {
|
||||||
|
$this->clauses[$i]->negate();
|
||||||
|
} else {
|
||||||
|
$this->clauses[$i] = 'NOT (' . $this->clauses[$i] . ')';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function negate() {
|
function negate() {
|
||||||
@@ -565,11 +637,8 @@ class WhereClause {
|
|||||||
return count($this->clauses);
|
return count($this->clauses);
|
||||||
}
|
}
|
||||||
|
|
||||||
function text($minimal = false) {
|
function text() {
|
||||||
if (count($this->clauses) == 0) {
|
if (count($this->clauses) == 0) return '(1)';
|
||||||
if ($minimal) return '(1)';
|
|
||||||
else return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
$A = array();
|
$A = array();
|
||||||
foreach ($this->clauses as $clause) {
|
foreach ($this->clauses as $clause) {
|
||||||
@@ -615,8 +684,8 @@ class MeekroDBException extends Exception {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function meekrodb_error_handler($params) {
|
function meekrodb_error_handler($params) {
|
||||||
$out[] = "QUERY: " . $params['query'];
|
if (isset($params['query'])) $out[] = "QUERY: " . $params['query'];
|
||||||
$out[] = "ERROR: " . $params['error'];
|
if (isset($params['error'])) $out[] = "ERROR: " . $params['error'];
|
||||||
$out[] = "";
|
$out[] = "";
|
||||||
|
|
||||||
if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
|
if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
|
||||||
|
|||||||
@@ -1,26 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
function new_error_callback($params) {
|
|
||||||
global $error_callback_worked;
|
|
||||||
|
|
||||||
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $error_callback_worked = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
function my_debug_handler($params) {
|
|
||||||
global $debug_callback_worked;
|
|
||||||
if (substr_count($params['query'], 'SELECT')) $debug_callback_worked = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class BasicTest extends SimpleTest {
|
class BasicTest extends SimpleTest {
|
||||||
function __construct() {
|
function __construct() {
|
||||||
error_reporting(E_ALL);
|
foreach (DB::tableList() as $table) {
|
||||||
require_once '../db.class.php';
|
DB::query("DROP TABLE $table");
|
||||||
DB::$user = 'libdb_user';
|
}
|
||||||
DB::$password = 'sdf235sklj';
|
|
||||||
DB::$dbName = 'libdb_test';
|
|
||||||
DB::query("DROP DATABASE libdb_test");
|
|
||||||
DB::query("CREATE DATABASE libdb_test");
|
|
||||||
DB::useDB('libdb_test');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -30,7 +13,8 @@ class BasicTest extends SimpleTest {
|
|||||||
`username` VARCHAR( 255 ) NOT NULL ,
|
`username` VARCHAR( 255 ) NOT NULL ,
|
||||||
`password` VARCHAR( 255 ) NOT NULL ,
|
`password` VARCHAR( 255 ) NOT NULL ,
|
||||||
`age` INT NOT NULL DEFAULT '10',
|
`age` INT NOT NULL DEFAULT '10',
|
||||||
`height` DOUBLE NOT NULL DEFAULT '10.0'
|
`height` DOUBLE NOT NULL DEFAULT '10.0',
|
||||||
|
`favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi'
|
||||||
) ENGINE = InnoDB");
|
) ENGINE = InnoDB");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,12 +57,13 @@ class BasicTest extends SimpleTest {
|
|||||||
'age' => 15,
|
'age' => 15,
|
||||||
'height' => 10.371
|
'height' => 10.371
|
||||||
));
|
));
|
||||||
|
$dbname = DB::$dbName;
|
||||||
DB::insert('`libdb_test`.`accounts`', array(
|
DB::insert("`$dbname`.`accounts`", array(
|
||||||
'username' => 'Charlie\'s Friend',
|
'username' => 'Charlie\'s Friend',
|
||||||
'password' => 'goodbye',
|
'password' => 'goodbye',
|
||||||
'age' => 30,
|
'age' => 30,
|
||||||
'height' => 155.23
|
'height' => 155.23,
|
||||||
|
'favorite_word' => null,
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assert(DB::insertId() === 3);
|
$this->assert(DB::insertId() === 3);
|
||||||
@@ -86,6 +71,9 @@ 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));
|
||||||
|
|
||||||
|
$password = DB::queryFirstField("SELECT password FROM accounts WHERE favorite_word IS NULL");
|
||||||
|
$this->assert($password === 'goodbye');
|
||||||
|
|
||||||
DB::$param_char = '###';
|
DB::$param_char = '###';
|
||||||
$bart = DB::queryFirstRow("SELECT * FROM accounts WHERE age IN ###li AND height IN ###ld AND username IN ###ls",
|
$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'));
|
||||||
@@ -109,6 +97,12 @@ class BasicTest extends SimpleTest {
|
|||||||
$this->assert($username === 'Bart');
|
$this->assert($username === 'Bart');
|
||||||
$this->assert($password === 'hello');
|
$this->assert($password === 'hello');
|
||||||
$this->assert($age == 15);
|
$this->assert($age == 15);
|
||||||
|
|
||||||
|
$mysqli_result = DB::queryRaw("SELECT * FROM accounts WHERE favorite_word IS NULL");
|
||||||
|
$this->assert($mysqli_result instanceof MySQLi_Result);
|
||||||
|
$row = $mysqli_result->fetch_assoc();
|
||||||
|
$this->assert($row['password'] === 'goodbye');
|
||||||
|
$this->assert($mysqli_result->fetch_assoc() === null);
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_4_query() {
|
function test_4_query() {
|
||||||
@@ -120,7 +114,7 @@ class BasicTest extends SimpleTest {
|
|||||||
$this->assert(count($results) === 2);
|
$this->assert(count($results) === 2);
|
||||||
|
|
||||||
$columnlist = DB::columnList('accounts');
|
$columnlist = DB::columnList('accounts');
|
||||||
$this->assert(count($columnlist) === 5);
|
$this->assert(count($columnlist) === 6);
|
||||||
$this->assert($columnlist[0] === 'id');
|
$this->assert($columnlist[0] === 'id');
|
||||||
$this->assert($columnlist[4] === 'height');
|
$this->assert($columnlist[4] === 'height');
|
||||||
|
|
||||||
@@ -129,7 +123,7 @@ class BasicTest extends SimpleTest {
|
|||||||
$this->assert($tablelist[0] === 'accounts');
|
$this->assert($tablelist[0] === 'accounts');
|
||||||
|
|
||||||
$tablelist = null;
|
$tablelist = null;
|
||||||
$tablelist = DB::tableList('libdb_test');
|
$tablelist = DB::tableList(DB::$dbName);
|
||||||
$this->assert(count($tablelist) === 1);
|
$this->assert(count($tablelist) === 1);
|
||||||
$this->assert($tablelist[0] === 'accounts');
|
$this->assert($tablelist[0] === 'accounts');
|
||||||
}
|
}
|
||||||
@@ -148,11 +142,13 @@ class BasicTest extends SimpleTest {
|
|||||||
|
|
||||||
DB::update('accounts', array(
|
DB::update('accounts', array(
|
||||||
'password' => DB::sqleval("REPEAT('blah', %i)", 4),
|
'password' => DB::sqleval("REPEAT('blah', %i)", 4),
|
||||||
|
'favorite_word' => null,
|
||||||
), 'username=%s', 'newguy');
|
), 'username=%s', 'newguy');
|
||||||
|
|
||||||
$row = null;
|
$row = null;
|
||||||
$row = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy');
|
$row = DB::queryOneRow("SELECT * FROM accounts WHERE username=%s", 'newguy');
|
||||||
$this->assert($row['password'] === 'blahblahblahblah');
|
$this->assert($row['password'] === 'blahblahblahblah');
|
||||||
|
$this->assert($row['favorite_word'] === null);
|
||||||
|
|
||||||
DB::query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
|
DB::query("DELETE FROM accounts WHERE password=%s", 'blahblahblahblah');
|
||||||
$this->assert(DB::affectedRows() === 1);
|
$this->assert(DB::affectedRows() === 1);
|
||||||
@@ -203,77 +199,107 @@ class BasicTest extends SimpleTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_5_error_handler() {
|
|
||||||
global $error_callback_worked, $static_error_callback_worked, $nonstatic_error_callback_worked,
|
|
||||||
$anonymous_error_callback_worked;
|
|
||||||
|
|
||||||
DB::$error_handler = 'new_error_callback';
|
|
||||||
DB::query("SELET * FROM accounts");
|
|
||||||
$this->assert($error_callback_worked === 1);
|
|
||||||
|
|
||||||
DB::$error_handler = array('BasicTest', 'static_error_callback');
|
function test_5_insert_blobs() {
|
||||||
DB::query("SELET * FROM accounts");
|
DB::query("CREATE TABLE `storedata` (
|
||||||
$this->assert($static_error_callback_worked === 1);
|
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
|
||||||
|
`picture` BLOB
|
||||||
|
) ENGINE = InnoDB");
|
||||||
|
|
||||||
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) {
|
$smile = file_get_contents('smile1.jpg');
|
||||||
global $anonymous_error_callback_worked;
|
DB::insert('storedata', array(
|
||||||
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $anonymous_error_callback_worked = 1;
|
'picture' => $smile,
|
||||||
};
|
));
|
||||||
DB::query("SELET * FROM accounts");
|
DB::query("INSERT INTO storedata (picture) VALUES (%s)", $smile);
|
||||||
$this->assert($anonymous_error_callback_worked === 1);
|
DB::query("INSERT INTO storedata (picture) VALUES (?)", 's', $smile);
|
||||||
|
|
||||||
|
$getsmile = DB::queryFirstField("SELECT picture FROM storedata WHERE id=1");
|
||||||
|
$getsmile2 = DB::queryFirstField("SELECT picture FROM storedata WHERE id=2");
|
||||||
|
$getsmile3 = DB::queryFirstField("SELECT picture FROM storedata WHERE id=3");
|
||||||
|
$this->assert($smile === $getsmile);
|
||||||
|
$this->assert($smile === $getsmile2);
|
||||||
|
$this->assert($smile === $getsmile3);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function static_error_callback($params) {
|
function test_6_insert_ignore() {
|
||||||
global $static_error_callback_worked;
|
DB::insertIgnore('accounts', array(
|
||||||
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $static_error_callback_worked = 1;
|
'id' => 1, //duplicate primary key
|
||||||
|
'username' => 'gonesoon',
|
||||||
|
'password' => 'something',
|
||||||
|
'age' => 61,
|
||||||
|
'height' => 199.194
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function nonstatic_error_callback($params) {
|
function test_7_insert_update() {
|
||||||
global $nonstatic_error_callback_worked;
|
DB::insertUpdate('accounts', array(
|
||||||
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $nonstatic_error_callback_worked = 1;
|
'id' => 2, //duplicate primary key
|
||||||
}
|
'username' => 'gonesoon',
|
||||||
|
'password' => 'something',
|
||||||
|
'age' => 61,
|
||||||
|
'height' => 199.194
|
||||||
|
), 'age = age + %i', 1);
|
||||||
|
|
||||||
function test_6_exception_catch() {
|
$this->assert(DB::affectedRows() === 2); // a quirk of MySQL, even though only 1 row was updated
|
||||||
DB::$error_handler = '';
|
|
||||||
DB::$throw_exception_on_error = true;
|
|
||||||
try {
|
|
||||||
DB::query("SELET * FROM accounts");
|
|
||||||
} catch(MeekroDBException $e) {
|
|
||||||
$this->assert(substr_count($e->getMessage(), 'You have an error in your SQL syntax'));
|
|
||||||
$this->assert($e->getQuery() === 'SELET * FROM accounts');
|
|
||||||
$exception_was_caught = 1;
|
|
||||||
}
|
|
||||||
$this->assert($exception_was_caught === 1);
|
|
||||||
|
|
||||||
try {
|
$result = DB::query("SELECT * FROM accounts WHERE age = %i", 16);
|
||||||
DB::insert('`libdb_test`.`accounts`', array(
|
$this->assert(count($result) === 1);
|
||||||
'id' => 2,
|
$this->assert($result[0]['height'] === '10.371');
|
||||||
'username' => 'Another Dude\'s \'Mom"',
|
|
||||||
'password' => 'asdfsdse',
|
|
||||||
'age' => 35,
|
|
||||||
'height' => 555.23
|
|
||||||
));
|
|
||||||
} catch(MeekroDBException $e) {
|
|
||||||
$this->assert(substr_count($e->getMessage(), 'Duplicate entry'));
|
|
||||||
$exception_was_caught = 2;
|
|
||||||
}
|
|
||||||
$this->assert($exception_was_caught === 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
function test_7_debugmode_handler() {
|
DB::insertUpdate('accounts', array(
|
||||||
global $debug_callback_worked;
|
'id' => 2, //duplicate primary key
|
||||||
|
'username' => 'blahblahdude',
|
||||||
|
'age' => 233,
|
||||||
|
'height' => 199.194
|
||||||
|
));
|
||||||
|
|
||||||
DB::debugMode('my_debug_handler');
|
$result = DB::query("SELECT * FROM accounts WHERE age = %i", 233);
|
||||||
DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
|
$this->assert(count($result) === 1);
|
||||||
|
$this->assert($result[0]['height'] === '199.194');
|
||||||
|
$this->assert($result[0]['username'] === 'blahblahdude');
|
||||||
|
|
||||||
$this->assert($debug_callback_worked === 1);
|
DB::insertUpdate('accounts', array(
|
||||||
|
'id' => 2, //duplicate primary key
|
||||||
|
'username' => 'gonesoon',
|
||||||
|
'password' => 'something',
|
||||||
|
'age' => 61,
|
||||||
|
'height' => 199.194
|
||||||
|
), array(
|
||||||
|
'age' => 74,
|
||||||
|
));
|
||||||
|
|
||||||
DB::debugMode(false);
|
$result = DB::query("SELECT * FROM accounts WHERE age = %i", 74);
|
||||||
|
$this->assert(count($result) === 1);
|
||||||
|
$this->assert($result[0]['height'] === '199.194');
|
||||||
|
$this->assert($result[0]['username'] === 'blahblahdude');
|
||||||
|
|
||||||
|
$multiples[] = array(
|
||||||
|
'id' => 3, //duplicate primary key
|
||||||
|
'username' => 'gonesoon',
|
||||||
|
'password' => 'something',
|
||||||
|
'age' => 61,
|
||||||
|
'height' => 199.194
|
||||||
|
);
|
||||||
|
$multiples[] = array(
|
||||||
|
'id' => 1, //duplicate primary key
|
||||||
|
'username' => 'gonesoon',
|
||||||
|
'password' => 'something',
|
||||||
|
'age' => 61,
|
||||||
|
'height' => 199.194
|
||||||
|
);
|
||||||
|
|
||||||
|
DB::insertUpdate('accounts', $multiples, array('age' => 914));
|
||||||
|
$this->assert(DB::affectedRows() === 4);
|
||||||
|
|
||||||
|
$result = DB::query("SELECT * FROM accounts WHERE age=914 ORDER BY id ASC");
|
||||||
|
$this->assert(count($result) === 2);
|
||||||
|
$this->assert($result[0]['username'] === 'Abe');
|
||||||
|
$this->assert($result[1]['username'] === 'Charlie\'s Friend');
|
||||||
|
|
||||||
|
DB::query("UPDATE accounts SET age=15, username='Bart' WHERE age=%i", 74);
|
||||||
|
$this->assert(DB::affectedRows() === 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
83
simpletest/ErrorTest.php
Normal file
83
simpletest/ErrorTest.php
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
<?
|
||||||
|
|
||||||
|
function new_error_callback($params) {
|
||||||
|
global $error_callback_worked;
|
||||||
|
|
||||||
|
if (substr_count($params['error'], 'You have an error in your SQL syntax')) $error_callback_worked = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function my_debug_handler($params) {
|
||||||
|
global $debug_callback_worked;
|
||||||
|
if (substr_count($params['query'], 'SELECT')) $debug_callback_worked = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ErrorTest extends SimpleTest {
|
||||||
|
function test_1_error_handler() {
|
||||||
|
global $error_callback_worked, $static_error_callback_worked, $nonstatic_error_callback_worked;
|
||||||
|
|
||||||
|
DB::$error_handler = 'new_error_callback';
|
||||||
|
DB::query("SELET * FROM accounts");
|
||||||
|
$this->assert($error_callback_worked === 1);
|
||||||
|
|
||||||
|
DB::$error_handler = array('ErrorTest', '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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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_2_exception_catch() {
|
||||||
|
$dbname = DB::$dbName;
|
||||||
|
DB::$error_handler = '';
|
||||||
|
DB::$throw_exception_on_error = true;
|
||||||
|
try {
|
||||||
|
DB::query("SELET * FROM accounts");
|
||||||
|
} catch(MeekroDBException $e) {
|
||||||
|
$this->assert(substr_count($e->getMessage(), 'You have an error in your SQL syntax'));
|
||||||
|
$this->assert($e->getQuery() === 'SELET * FROM accounts');
|
||||||
|
$exception_was_caught = 1;
|
||||||
|
}
|
||||||
|
$this->assert($exception_was_caught === 1);
|
||||||
|
|
||||||
|
try {
|
||||||
|
DB::insert("`$dbname`.`accounts`", array(
|
||||||
|
'id' => 2,
|
||||||
|
'username' => 'Another Dude\'s \'Mom"',
|
||||||
|
'password' => 'asdfsdse',
|
||||||
|
'age' => 35,
|
||||||
|
'height' => 555.23
|
||||||
|
));
|
||||||
|
} catch(MeekroDBException $e) {
|
||||||
|
$this->assert(substr_count($e->getMessage(), 'Duplicate entry'));
|
||||||
|
$exception_was_caught = 2;
|
||||||
|
}
|
||||||
|
$this->assert($exception_was_caught === 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_3_debugmode_handler() {
|
||||||
|
global $debug_callback_worked;
|
||||||
|
|
||||||
|
DB::debugMode('my_debug_handler');
|
||||||
|
DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend");
|
||||||
|
|
||||||
|
$this->assert($debug_callback_worked === 1);
|
||||||
|
|
||||||
|
DB::debugMode(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
19
simpletest/ErrorTest_53.php
Normal file
19
simpletest/ErrorTest_53.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?
|
||||||
|
class ErrorTest_53 extends SimpleTest {
|
||||||
|
function test_1_error_handler() {
|
||||||
|
global $anonymous_error_callback_worked;
|
||||||
|
|
||||||
|
DB::$throw_exception_on_error = false;
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
64
simpletest/WhereClauseTest.php
Normal file
64
simpletest/WhereClauseTest.php
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
<?
|
||||||
|
class WhereClauseTest extends SimpleTest {
|
||||||
|
function test_1_basic_where() {
|
||||||
|
$where = new WhereClause('and');
|
||||||
|
$where->add('username=%s', 'Bart');
|
||||||
|
$where->add('password=%s', 'hello');
|
||||||
|
|
||||||
|
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
|
||||||
|
$this->assert(count($result) === 1);
|
||||||
|
$this->assert($result[0]['age'] === '15');
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_2_simple_grouping() {
|
||||||
|
$where = new WhereClause('and');
|
||||||
|
$where->add('password=%s', 'hello');
|
||||||
|
$subclause = $where->addClause('or');
|
||||||
|
$subclause->add('age=%i', 15);
|
||||||
|
$subclause->add('age=%i', 14);
|
||||||
|
|
||||||
|
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
|
||||||
|
$this->assert(count($result) === 1);
|
||||||
|
$this->assert($result[0]['age'] === '15');
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_3_negate_last() {
|
||||||
|
$where = new WhereClause('and');
|
||||||
|
$where->add('password=%s', 'hello');
|
||||||
|
$subclause = $where->addClause('or');
|
||||||
|
$subclause->add('username!=%s', 'Bart');
|
||||||
|
$subclause->negateLast();
|
||||||
|
|
||||||
|
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
|
||||||
|
$this->assert(count($result) === 1);
|
||||||
|
$this->assert($result[0]['age'] === '15');
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_4_negate_last_query() {
|
||||||
|
$where = new WhereClause('and');
|
||||||
|
$where->add('password=%s', 'hello');
|
||||||
|
$subclause = $where->addClause('or');
|
||||||
|
$subclause->add('username!=%s', 'Bart');
|
||||||
|
$where->negateLast();
|
||||||
|
|
||||||
|
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
|
||||||
|
$this->assert(count($result) === 1);
|
||||||
|
$this->assert($result[0]['age'] === '15');
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_5_negate() {
|
||||||
|
$where = new WhereClause('and');
|
||||||
|
$where->add('password=%s', 'hello');
|
||||||
|
$subclause = $where->addClause('or');
|
||||||
|
$subclause->add('username!=%s', 'Bart');
|
||||||
|
$subclause->negate();
|
||||||
|
|
||||||
|
$result = DB::query("SELECT * FROM accounts WHERE %l", $where->text());
|
||||||
|
$this->assert(count($result) === 1);
|
||||||
|
$this->assert($result[0]['age'] === '15');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
BIN
simpletest/smile1.jpg
Normal file
BIN
simpletest/smile1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 48 KiB |
@@ -11,37 +11,34 @@ class SimpleTest {
|
|||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function __listfiles($dir, $regex, $type='files', $rec = false) {
|
|
||||||
$A = array();
|
|
||||||
|
|
||||||
if (! $dir_handler = @opendir($dir)) return $A;
|
|
||||||
|
|
||||||
while (false !== ($filename = @readdir($dir_handler))) {
|
|
||||||
if ($filename == '.' || $filename == '..') continue;
|
|
||||||
if ($rec && is_dir("$dir/$filename")) $A = array_merge($A, File::listfiles("$dir/$filename", $regex, $type, true));
|
|
||||||
|
|
||||||
if (! preg_match($regex, $filename)) continue;
|
|
||||||
if ($type == 'files' && ! is_file("$dir/$filename")) continue;
|
|
||||||
if ($type == 'dirs' && ! is_dir("$dir/$filename")) continue;
|
|
||||||
if ($type == 'symlinks' && ! is_link("$dir/$filename")) continue;
|
|
||||||
|
|
||||||
$A[] = $dir . DIRECTORY_SEPARATOR . $filename;
|
|
||||||
}
|
|
||||||
return $A;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$files = SimpleTest::__listfiles(dirname(__FILE__), '/^.*php$/i');
|
if (phpversion() >= '5.3') $is_php_53 = true;
|
||||||
|
else $is_php_53 = false;
|
||||||
|
|
||||||
$classes_to_test = array();
|
error_reporting(E_ALL);
|
||||||
foreach ($files as $fullpath) {
|
require_once '../db.class.php';
|
||||||
$filename = basename($fullpath);
|
DB::$user = 'meekrodb_test_us';
|
||||||
if ($fullpath == __FILE__) continue;
|
|
||||||
require_once($fullpath);
|
include 'test_setup.php'; //test config values go here
|
||||||
$classes_to_test[] = str_replace('.php', '', $filename);
|
DB::$password = $set_password;
|
||||||
|
DB::$dbName = $set_db;
|
||||||
|
DB::$host = $set_host;
|
||||||
|
|
||||||
|
require_once 'BasicTest.php';
|
||||||
|
require_once 'WhereClauseTest.php';
|
||||||
|
require_once 'ErrorTest.php';
|
||||||
|
|
||||||
|
$classes_to_test = array(
|
||||||
|
'BasicTest',
|
||||||
|
'WhereClauseTest',
|
||||||
|
'ErrorTest',
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($is_php_53) {
|
||||||
|
require_once 'ErrorTest_53.php';
|
||||||
|
$classes_to_test[] = 'ErrorTest_53';
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($classes_to_test as $class) {
|
foreach ($classes_to_test as $class) {
|
||||||
|
|||||||
Reference in New Issue
Block a user