refactoring création fonction select, ajout test pour le order by et group by, ajout group by, ne pas pouvoir sauver si la clef primaire n'est pas définie

This commit is contained in:
Gregory Letellier
2023-10-27 10:35:53 +02:00
parent 3f1c0c144c
commit 9e760fe6e5
4 changed files with 104 additions and 33 deletions

3
.gitignore vendored
View File

@@ -4,4 +4,5 @@ vendor/
var/cache/* var/cache/*
!var/log/.gitkeep !var/log/.gitkeep
var/log/* var/log/*
.env .env
.vscode

View File

@@ -64,16 +64,18 @@ class Model
$ret = true; $ret = true;
} }
} else { } else {
if (count($this->updated) > 0) { if (isset($this->values[$this->pk])) {
$data = []; if (count($this->updated) > 0) {
$data[$this->pk] = $this->values[$this->pk]; $data = [];
foreach ($this->updated as $updatecol) { $data[$this->pk] = $this->values[$this->pk];
$data[$updatecol] = $this->values[$updatecol]; foreach ($this->updated as $updatecol) {
} $data[$updatecol] = $this->values[$updatecol];
$query = $qb->getPreparedQuery($data, false); }
$ret = $this->db->updateQuery($query); $query = $qb->getPreparedQuery($data, false);
if ($ret) { $ret = $this->db->updateQuery($query);
$this->updated = []; if ($ret) {
$this->updated = [];
}
} }
} }
} }

View File

@@ -8,6 +8,7 @@ class QueryBuilder
protected array $columns; protected array $columns;
protected array $wheres; protected array $wheres;
protected array $orders; protected array $orders;
protected array $groupby;
protected string $table; protected string $table;
protected string $pk; protected string $pk;
protected ?int $take; protected ?int $take;
@@ -27,6 +28,7 @@ class QueryBuilder
$this->columns = []; $this->columns = [];
$this->wheres = []; $this->wheres = [];
$this->orders = []; $this->orders = [];
$this->groupby = [];
$this->offset = null; $this->offset = null;
$this->take = null; $this->take = null;
$this->raw_query = ""; $this->raw_query = "";
@@ -73,6 +75,17 @@ class QueryBuilder
return $this; 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) public function where($condition, $operator, $value = null)
{ {
if ($value === null) { if ($value === null) {
@@ -164,14 +177,41 @@ class QueryBuilder
} }
$columns = implode(",", $this->columns); $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 = ""; $where = "";
foreach ($this->wheres as $expression) { foreach ($this->wheres as $expression) {
$sw = ""; $sw = "";
if ($expression instanceof Expression) { if ($expression instanceof Expression) {
$sw = $expression->raw(); $sw = $expression->raw();
$ret->addData($expression->getValue());
} }
if ($expression instanceof string) { if (is_string($expression)) {
if (trim($expression) !== "") { if (trim($expression) !== "") {
$sw = "(" . $expression . ")"; $sw = "(" . $expression . ")";
} }
@@ -181,25 +221,23 @@ class QueryBuilder
} }
$where .= $sw; $where .= $sw;
} }
return $where;
}
$query = "SELECT " . $columns . " FROM " . $this->table ; private function getClause(string $word, array $data): string
if ($where != "") { {
$query .= " WHERE " . $where; $ret = "";
} if (count($data) > 0) {
if (count($this->orders) > 0) { $ret = " $word ";
$query .= " ORDER BY "; $iClause = 0;
$iOrder = 0; foreach ($data as $clause) {
foreach ($this->orders as $order) { if ($iClause > 0) {
if ($iOrder > 0) { $ret .= ",";
$query .= ",";
} }
$query .= $order; $ret .= $clause;
$iOrder++; $iClause++;
} }
} }
$query .= $this->getLimit();
$ret->setQuery($query);
return $ret; return $ret;
} }

View File

@@ -18,8 +18,15 @@ test("insert row",function () {
$row->col1 = "test"; $row->col1 = "test";
$row->date1 = new \DateTime(); $row->date1 = new \DateTime();
$ret = $row->save(); $ret = $row->save();
$row2 = new Model();
$row2->setTable("test");
$row2->col1 = "test2";
$row2->date1 = new \DateTime();
$ret2 = $row2->save();
expect($ret)->toBe(true); expect($ret)->toBe(true);
expect($ret2)->toBe(true);
}); });
test("querybuilder", function() { test("querybuilder", function() {
@@ -37,7 +44,7 @@ test("querybuilder", function() {
expect(count($rows))->toBe(1); expect(count($rows))->toBe(1);
$qb->reset(); $qb->reset();
$rows = $qb->where("id",">",1)->columns("*")->get(); $rows = $qb->where("id",">",2)->columns("*")->get();
expect($rows)->toBeArray(); expect($rows)->toBeArray();
expect(count($rows))->toBe(0); expect(count($rows))->toBe(0);
@@ -53,18 +60,41 @@ test("querybuilder", function() {
expect(count($rows))->toBe(1); 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() { test("update row", function() {
$qb = new QueryBuilder("test","id"); $qb = new QueryBuilder("test","id");
$row = $qb->find(1); $row = $qb->find(1);
$row->col1 = "testupdate";
$row->col1 = "test2";
$res = $row->save(); $res = $row->save();
expect($res)->toBe(true); expect($res)->toBe(true);
$qb->reset(); $qb->reset();
$rows = $qb->where("id",1)->columns("*")->get(); $rows = $qb->where("id",1)->columns("*")->get();
expect($rows)->toBeArray(); expect($rows)->toBeArray();
expect(count($rows))->toBe(1); expect(count($rows))->toBe(1);
expect($rows[0]->col1)->toBe("test2"); expect($rows[0]->col1)->toBe("testupdate");
}); });