diff --git a/db.class.php b/db.class.php index 9b1ada3..70b78c8 100644 --- a/db.class.php +++ b/db.class.php @@ -389,10 +389,12 @@ class MeekroDB { $this->param_char . 'li', // list of integers $this->param_char . 'ld', // list of decimals $this->param_char . 'lb', // list of backticks + $this->param_char . 'lt', // list of timestamps $this->param_char . 's', // string $this->param_char . 'i', // integer $this->param_char . 'd', // double / decimal $this->param_char . 'b', // backtick + $this->param_char . 't', // timestamp $this->param_char . '?', // infer type $this->param_char . 'ss' // search string (like string, surrounded with %'s) ); @@ -491,12 +493,17 @@ class MeekroDB { else return $this->escape($value); } + protected function parseTS($ts) { + if (is_string($ts)) return date('Y-m-d H:i:s', strtotime($ts)); + else if (is_object($ts) && ($ts instanceof DateTime)) return $ts->format('Y-m-d H:i:s'); + } + protected function parseQueryParams() { $args = func_get_args(); $chunkyQuery = call_user_func_array(array($this, 'preparseQueryParams'), $args); $query = ''; - $array_types = array('ls', 'li', 'ld', 'lb', 'll'); + $array_types = array('ls', 'li', 'ld', 'lb', 'll', 'lt'); foreach ($chunkyQuery as $chunk) { if (is_string($chunk)) { @@ -520,12 +527,14 @@ class MeekroDB { else if ($type == 'b') $result = $this->formatTableName($arg); else if ($type == 'l') $result = $arg; else if ($type == 'ss') $result = "%" . $this->escape(str_replace(array('%', '_'), array('\%', '\_'), $arg)) . "%"; + else if ($type == 't') $result = $this->escape($this->parseTS($arg)); else if ($type == 'ls') $result = array_map(array($this, 'escape'), $arg); else if ($type == 'li') $result = array_map('intval', $arg); else if ($type == 'ld') $result = array_map('doubleval', $arg); else if ($type == 'lb') $result = array_map(array($this, 'formatTableName'), $arg); else if ($type == 'll') $result = $arg; + else if ($type == 'lt') $result = array_map(array($this, 'escape'), array_map(array($this, 'parseTS'), $arg)); else if ($type == '?') $result = $this->sanitize($arg); diff --git a/simpletest/BasicTest.php b/simpletest/BasicTest.php index 9a5552a..209d129 100644 --- a/simpletest/BasicTest.php +++ b/simpletest/BasicTest.php @@ -15,7 +15,8 @@ class BasicTest extends SimpleTest { `password` VARCHAR( 255 ) NULL , `age` INT NOT NULL DEFAULT '10', `height` DOUBLE NOT NULL DEFAULT '10.0', - `favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi' + `favorite_word` VARCHAR( 255 ) NULL DEFAULT 'hi', + `birthday` TIMESTAMP NOT NULL ) ENGINE = InnoDB"); DB::query("CREATE TABLE `profile` ( @@ -76,11 +77,11 @@ class BasicTest extends SimpleTest { 'height' => 155.23, 'favorite_word' => null, )); - + $this->assert(DB::insertId() === 3); $counter = DB::queryFirstField("SELECT COUNT(*) FROM accounts"); $this->assert($counter === strval(3)); - + DB::insert('`accounts`', array( 'username' => 'Deer', 'password' => '', @@ -126,15 +127,18 @@ class BasicTest extends SimpleTest { } function test_4_query() { - $results = DB::query("SELECT * FROM accounts WHERE username=%s", 'Charlie\'s Friend'); + DB::query("UPDATE %b SET birthday=%t WHERE username=%s", 'accounts', new DateTime('10 September 2000 13:13:13'), 'Charlie\'s Friend'); + + $results = DB::query("SELECT * FROM accounts WHERE username=%s AND birthday IN %lt", 'Charlie\'s Friend', array('September 10 2000 13:13:13')); $this->assert(count($results) === 1); - $this->assert($results[0]['age'] == 30 && $results[0]['password'] == 'goodbye'); + $this->assert($results[0]['age'] === '30' && $results[0]['password'] === 'goodbye'); + $this->assert($results[0]['birthday'] == '2000-09-10 13:13:13'); $results = DB::query("SELECT * FROM accounts WHERE username!=%s", "Charlie's Friend"); $this->assert(count($results) === 3); $columnlist = DB::columnList('accounts'); - $this->assert(count($columnlist) === 7); + $this->assert(count($columnlist) === 8); $this->assert($columnlist[0] === 'id'); $this->assert($columnlist[5] === 'height'); diff --git a/simpletest/test.php b/simpletest/test.php index 88f9e5a..654e9ae 100755 --- a/simpletest/test.php +++ b/simpletest/test.php @@ -23,6 +23,8 @@ function microtime_float() if (phpversion() >= '5.3') $is_php_53 = true; else $is_php_53 = false; +ini_set('date.timezone', 'America/Los_Angeles'); + error_reporting(E_ALL | E_STRICT); require_once '../db.class.php'; include 'test_setup.php'; //test config values go here