Skip to content

Commit 7686313

Browse files
committed
WhereClause can be referenced with %l or %?
MeekroDBParsedQuery (output of parse()) can also be referenced with %l or %?
1 parent 2b1b0fe commit 7686313

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/test.php
22
simpletest/test_setup.php
3-
.phpdoc
3+
.phpdoc
4+
/vendor/
5+
/composer.lock

db.class.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -899,14 +899,21 @@ function parse($query) {
899899
}
900900

901901
if ($val instanceof WhereClause) {
902-
if ($Part['type'] != 'l') {
903-
throw new MeekroDBException("WhereClause must be used with l arg, you used {$Part['type']} instead!");
902+
if ($Part['type'] != 'l' && $Part['type'] != '?') {
903+
throw new MeekroDBException("WhereClause must be used with l or ?, you used {$Part['type']} instead!");
904904
}
905905

906906
list($clause_sql, $clause_args) = $val->textAndArgs();
907907
$ParsedSubQuery = $this->parse($clause_sql, ...$clause_args);
908908
$ParsedQuery->add($ParsedSubQuery);
909909
}
910+
else if ($val instanceof MeekroDBParsedQuery) {
911+
if ($Part['type'] != 'l' && $Part['type'] != '?') {
912+
throw new MeekroDBException("a ParsedQuery must be used with l or ?, you used {$Part['type']} instead!");
913+
}
914+
915+
$ParsedQuery->add($val);
916+
}
910917
else {
911918
$ParsedSubQuery = $fn($val);
912919
if (! ($ParsedSubQuery instanceof MeekroDBParsedQuery)) {
@@ -1273,7 +1280,11 @@ function add() {
12731280

12741281
if ($sql instanceof WhereClause) {
12751282
$this->clauses[] = $sql;
1276-
} else {
1283+
}
1284+
else if ($sql instanceof MeekroDBParsedQuery) {
1285+
$this->clauses[] = array('sql' => '%?', 'args' => [$sql]);
1286+
}
1287+
else {
12771288
$this->clauses[] = array('sql' => $sql, 'args' => $args);
12781289
}
12791290
}

simpletest/WhereClauseTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function test_1_basic_where() {
55
$where->add('username=%s', 'Bart');
66
$where->add('password=%s', 'hello');
77

8-
$result = DB::query("SELECT * FROM accounts WHERE %l", $where);
8+
$result = DB::query("SELECT * FROM accounts WHERE %?", $where);
99
$this->assert(count($result) === 1);
1010
$this->assert($result[0]['age'] === '15');
1111
}
@@ -68,14 +68,27 @@ function test_6_negate_two() {
6868
$this->assert(count($result) === 7);
6969
}
7070

71+
// * WhereClause works with OR
72+
// * you can add() a parse() output to a WhereClause
7173
function test_7_or() {
7274
$where = new WhereClause('or');
7375
$where->add('username=%s', 'Bart');
74-
$where->add('username=%s', 'Abe');
75-
76+
$where->add(DB::parse('username=%s', 'Abe'));
7677
$result = DB::query("SELECT * FROM accounts WHERE %l", $where);
7778
$this->assert(count($result) === 2);
7879
}
80+
81+
// * parse() output can be used as part of a query with %l or %?
82+
function test_8_multipart() {
83+
$part = DB::parse('WHERE username=%s', 'Bart');
84+
$rows = DB::query("SELECT * FROM accounts %l", $part);
85+
$this->assert(count($rows) === 1);
86+
$this->assert($rows[0]['id'] === '2');
87+
88+
$rows = DB::query("SELECT * FROM accounts %?", $part);
89+
$this->assert(count($rows) === 1);
90+
$this->assert($rows[0]['id'] === '2');
91+
}
7992

8093

8194
}

0 commit comments

Comments
 (0)