ajout méthodes random,index dans la collection, ajout clause distinct, whereNull , whereNotNull dans le querybuilder

This commit is contained in:
Gregory Letellier
2023-11-02 10:27:06 +01:00
parent fe6d2f27b8
commit 84bf765b84
5 changed files with 79 additions and 3 deletions

View File

@@ -15,6 +15,7 @@ class QueryBuilder
protected ?int $take;
protected ?int $offset;
protected string $raw_query;
protected bool $distinct;
public function __construct($table, $pk = "id", $connection = "default")
{
@@ -33,6 +34,7 @@ class QueryBuilder
$this->groupby = [];
$this->offset = null;
$this->take = null;
$this->distinct = false;
$this->raw_query = "";
}
@@ -54,6 +56,12 @@ class QueryBuilder
return $this;
}
public function distinct()
{
$this->distinct = true;
return $this;
}
public function orderBy($columns, $sort = "ASC")
{
if (is_array($columns)) {
@@ -100,6 +108,18 @@ class QueryBuilder
return $this;
}
public function whereNotNull($condition)
{
$this->wheres[] = new Expression($condition, "notnull", "");
return $this;
}
public function whereNull($condition)
{
$this->wheres[] = new Expression($condition, "isnull", "");
return $this;
}
public function where($condition, $operator, $value = null)
{
if ($value === null) {
@@ -172,7 +192,7 @@ class QueryBuilder
$model = new Model();
$ret[] = $model->setPk($this->pk)->setTable($this->table)->fillData($row);
}
return new Collection($ret);
return new Collection($ret);
}
private function getData(): mixed
@@ -192,7 +212,8 @@ class QueryBuilder
}
$columns = implode(",", $this->columns);
$query = "SELECT " . $columns . " FROM " . $this->table ;
$distinct = ($this->distinct) ? " DISTINCT " : "";
$query = "SELECT " . $distinct . " " . $columns . " FROM " . $this->table ;
$where = $this->getWhere();
$this->fillData($ret);
@@ -213,7 +234,9 @@ class QueryBuilder
{
foreach ($this->wheres as $expression) {
if ($expression instanceof Expression) {
$query->addData($expression->getValue());
if ($expression->hasData()) {
$query->addData($expression->getValue());
}
}
}
}