diff --git a/src/Expression.php b/src/Expression.php index 2def1f4..803a024 100644 --- a/src/Expression.php +++ b/src/Expression.php @@ -17,6 +17,14 @@ class Expression public function raw(): string { + $operator = trim(strtolower($this->operator)); + if ($operator == "in") { + $values = implode(",", str_split(str_repeat("?", count($this->value)))); + return "(" . $this->condition . " " . $this->operator . " (" . $values . ") )"; + } + if ($operator == "between" && count($this->value) == 2) { + return "(" . $this->condition . " " . $this->operator . " ? AND ? )"; + } return "(" . $this->condition . " " . $this->operator . " ? )"; } diff --git a/src/Query.php b/src/Query.php index 1515898..a90b796 100644 --- a/src/Query.php +++ b/src/Query.php @@ -15,10 +15,13 @@ class Query public function addData($value) { - $this->data[] = $value; + if (is_array($value)) { + $this->data = array_merge($this->data, $value); + } else { + $this->data[] = $value; + } } - public function getQuery(): string { return $this->query; diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index e427774..76e3427 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -86,6 +86,18 @@ class QueryBuilder return $this; } + public function whereIn($condition, array $values) + { + $this->wheres[] = new Expression($condition, "in", $values); + return $this; + } + + public function whereBetween($condition, $value1, $value2) + { + $this->wheres[] = new Expression($condition, "between", [$value1,$value2]); + return $this; + } + public function where($condition, $operator, $value = null) { if ($value === null) { diff --git a/tests/DbTest.php b/tests/DbTest.php index b7db0df..193d1d9 100644 --- a/tests/DbTest.php +++ b/tests/DbTest.php @@ -110,4 +110,31 @@ test("event", function() { $qb->find(1); expect($ret)->toBeObject(); +}); + +test("where array", function() { + + $row3 = new Model(); + $row3->setTable("test"); + $row3->col1 = "test3"; + $row3->date1 = new \DateTime(); + $ret3 = $row3->save(); + + $qb = new QueryBuilder("test"); + $all = $qb->get(); + + expect(count($all))->toBe(3); + + $qb->reset(); + + $between = $qb->whereBetween("id",2,3)->get(); + expect(count($between))->toBe(2); + + expect($between[0]->id)->toBe(2); + + $qb->reset(); + $in = $qb->whereIn("id",[2,3])->get(); + expect(count($in))->toBe(2); + expect($in[0]->id)->toBe(2); + }); \ No newline at end of file