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:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,3 +5,4 @@ var/cache/*
|
|||||||
!var/log/.gitkeep
|
!var/log/.gitkeep
|
||||||
var/log/*
|
var/log/*
|
||||||
.env
|
.env
|
||||||
|
.vscode
|
||||||
@@ -64,6 +64,7 @@ class Model
|
|||||||
$ret = true;
|
$ret = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (isset($this->values[$this->pk])) {
|
||||||
if (count($this->updated) > 0) {
|
if (count($this->updated) > 0) {
|
||||||
$data = [];
|
$data = [];
|
||||||
$data[$this->pk] = $this->values[$this->pk];
|
$data[$this->pk] = $this->values[$this->pk];
|
||||||
@@ -77,6 +78,7 @@ class Model
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
$ret = " $word ";
|
||||||
|
$iClause = 0;
|
||||||
|
foreach ($data as $clause) {
|
||||||
|
if ($iClause > 0) {
|
||||||
|
$ret .= ",";
|
||||||
}
|
}
|
||||||
if (count($this->orders) > 0) {
|
$ret .= $clause;
|
||||||
$query .= " ORDER BY ";
|
$iClause++;
|
||||||
$iOrder = 0;
|
|
||||||
foreach ($this->orders as $order) {
|
|
||||||
if ($iOrder > 0) {
|
|
||||||
$query .= ",";
|
|
||||||
}
|
|
||||||
$query .= $order;
|
|
||||||
$iOrder++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$query .= $this->getLimit();
|
|
||||||
|
|
||||||
$ret->setQuery($query);
|
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,14 @@ test("insert row",function () {
|
|||||||
$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");
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user