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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user