diff --git a/.gitignore b/.gitignore index a2628dd..cc39ada 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ vendor/ var/cache/* !var/log/.gitkeep var/log/* -.env \ No newline at end of file +.env +.vscode \ No newline at end of file diff --git a/src/Model.php b/src/Model.php index c50f95f..7cabadf 100644 --- a/src/Model.php +++ b/src/Model.php @@ -64,16 +64,18 @@ class Model $ret = true; } } else { - if (count($this->updated) > 0) { - $data = []; - $data[$this->pk] = $this->values[$this->pk]; - foreach ($this->updated as $updatecol) { - $data[$updatecol] = $this->values[$updatecol]; - } - $query = $qb->getPreparedQuery($data, false); - $ret = $this->db->updateQuery($query); - if ($ret) { - $this->updated = []; + if (isset($this->values[$this->pk])) { + if (count($this->updated) > 0) { + $data = []; + $data[$this->pk] = $this->values[$this->pk]; + foreach ($this->updated as $updatecol) { + $data[$updatecol] = $this->values[$updatecol]; + } + $query = $qb->getPreparedQuery($data, false); + $ret = $this->db->updateQuery($query); + if ($ret) { + $this->updated = []; + } } } } diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 349ac6d..b9747b7 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -8,6 +8,7 @@ class QueryBuilder protected array $columns; protected array $wheres; protected array $orders; + protected array $groupby; protected string $table; protected string $pk; protected ?int $take; @@ -27,6 +28,7 @@ class QueryBuilder $this->columns = []; $this->wheres = []; $this->orders = []; + $this->groupby = []; $this->offset = null; $this->take = null; $this->raw_query = ""; @@ -73,6 +75,17 @@ class QueryBuilder return $this; } + public function groupBy($columns) + { + if (is_array($columns)) { + $this->groupby = array_merge($this->groupby, $columns); + } + if (is_string($columns)) { + $this->groupby[] = $columns; + } + return $this; + } + public function where($condition, $operator, $value = null) { if ($value === null) { @@ -164,14 +177,41 @@ class QueryBuilder } $columns = implode(",", $this->columns); + $query = "SELECT " . $columns . " FROM " . $this->table ; + + $where = $this->getWhere(); + $this->fillData($ret); + + if ($where != "") { + $query .= " WHERE " . $where; + } + + $query .= $this->getClause("GROUP By", $this->groupby); + $query .= $this->getClause("ORDER By", $this->orders); + $query .= $this->getLimit(); + + $ret->setQuery($query); + return $ret; + } + + private function fillData(Query &$query): void + { + foreach ($this->wheres as $expression) { + if ($expression instanceof Expression) { + $query->addData($expression->getValue()); + } + } + } + + private function getWhere(): string + { $where = ""; foreach ($this->wheres as $expression) { $sw = ""; if ($expression instanceof Expression) { $sw = $expression->raw(); - $ret->addData($expression->getValue()); } - if ($expression instanceof string) { + if (is_string($expression)) { if (trim($expression) !== "") { $sw = "(" . $expression . ")"; } @@ -181,25 +221,23 @@ class QueryBuilder } $where .= $sw; } + return $where; + } - $query = "SELECT " . $columns . " FROM " . $this->table ; - if ($where != "") { - $query .= " WHERE " . $where; - } - if (count($this->orders) > 0) { - $query .= " ORDER BY "; - $iOrder = 0; - foreach ($this->orders as $order) { - if ($iOrder > 0) { - $query .= ","; + private function getClause(string $word, array $data): string + { + $ret = ""; + if (count($data) > 0) { + $ret = " $word "; + $iClause = 0; + foreach ($data as $clause) { + if ($iClause > 0) { + $ret .= ","; } - $query .= $order; - $iOrder++; + $ret .= $clause; + $iClause++; } } - $query .= $this->getLimit(); - - $ret->setQuery($query); return $ret; } diff --git a/tests/DbTest.php b/tests/DbTest.php index fa26128..aaf9827 100644 --- a/tests/DbTest.php +++ b/tests/DbTest.php @@ -18,8 +18,15 @@ test("insert row",function () { $row->col1 = "test"; $row->date1 = new \DateTime(); $ret = $row->save(); + + $row2 = new Model(); + $row2->setTable("test"); + $row2->col1 = "test2"; + $row2->date1 = new \DateTime(); + $ret2 = $row2->save(); expect($ret)->toBe(true); + expect($ret2)->toBe(true); }); test("querybuilder", function() { @@ -37,7 +44,7 @@ test("querybuilder", function() { expect(count($rows))->toBe(1); $qb->reset(); - $rows = $qb->where("id",">",1)->columns("*")->get(); + $rows = $qb->where("id",">",2)->columns("*")->get(); expect($rows)->toBeArray(); expect(count($rows))->toBe(0); @@ -53,18 +60,41 @@ test("querybuilder", function() { expect(count($rows))->toBe(1); }); +test("order by", function(){ + $qb = new QueryBuilder("test"); + $rows = $qb->orderBy("id","DESC")->get(); + + expect($rows)->toBeArray(); + expect(count($rows))->toBe(2); + + expect($rows[0]->col1)->toBe("test2"); +}); + +test("group by", function(){ + $qb = new QueryBuilder("test"); + $rows = $qb->columns("COUNT(*) as Nb")->get(); + + expect($rows)->toBeArray(); + expect(count($rows))->toBe(1); + expect($rows[0]->Nb)->toBe(2); + + $qb->reset(); + $rows = $qb->columns("COUNT(*) as Nb")->groupBy("id")->get(); + expect($rows)->toBeArray(); + expect(count($rows))->toBe(2); + expect($rows[0]->Nb)->toBe(1); +}); + test("update row", function() { $qb = new QueryBuilder("test","id"); $row = $qb->find(1); - - $row->col1 = "test2"; + $row->col1 = "testupdate"; $res = $row->save(); - expect($res)->toBe(true); $qb->reset(); $rows = $qb->where("id",1)->columns("*")->get(); expect($rows)->toBeArray(); expect(count($rows))->toBe(1); - expect($rows[0]->col1)->toBe("test2"); + expect($rows[0]->col1)->toBe("testupdate"); });