diff --git a/db.class.php b/db.class.php index 938a434..09d2a1f 100644 --- a/db.class.php +++ b/db.class.php @@ -62,7 +62,7 @@ class DB return $mysql; } - protected static function nonSQLError($message) { + public static function nonSQLError($message) { if (DB::$throw_exception_on_nonsql_error) { $e = new MeekroDBException($message); throw $e; @@ -564,7 +564,9 @@ class WhereClause { public $clauses = array(); 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() { @@ -598,11 +600,8 @@ class WhereClause { return count($this->clauses); } - function text($minimal = false) { - if (count($this->clauses) == 0) { - if ($minimal) return '(1)'; - else return ''; - } + function text() { + if (count($this->clauses) == 0) return '(1)'; $A = array(); foreach ($this->clauses as $clause) { diff --git a/simpletest/WhereClauseTest.php b/simpletest/WhereClauseTest.php new file mode 100644 index 0000000..c3169da --- /dev/null +++ b/simpletest/WhereClauseTest.php @@ -0,0 +1,28 @@ +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'); + } + + +} + +?> diff --git a/simpletest/test.php b/simpletest/test.php index 17dfd72..80a797c 100755 --- a/simpletest/test.php +++ b/simpletest/test.php @@ -27,10 +27,12 @@ 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', );