5 Commits
1.0.0 ... 1.0.6

Author SHA1 Message Date
Gregory Letellier
d961d2a80f ajout model de type readonly pour une requête de select uniquement 2024-06-18 13:02:18 +02:00
Gregory Letellier
0db845a116 add orWhere 2023-11-15 11:50:15 +01:00
Gregory Letellier
35188860ce gestion du charset en mysql 2023-11-14 16:41:09 +01:00
Gregory Letellier
c8d6e911a6 meilleure gestion des noms de tables sur les modèles hérités 2023-11-14 15:55:08 +01:00
Gregory Letellier
03d26d9167 fix bug in array host reading 2023-11-14 15:33:19 +01:00
4 changed files with 105 additions and 33 deletions

View File

@@ -18,6 +18,7 @@ class Connection
private string $database;
private ?string $port;
private ?string $host;
private ?string $charset;
private string $error;
private function __construct($name, $config)
@@ -40,10 +41,11 @@ class Connection
$key = (strtolower(trim($this->name)));
$this->type = isset($config[$key]["TYPE"]) ? $config[$key]["TYPE"] : null;
$this->user = isset($config[$key]["USER"]) ? $config[$key]["USER"] : null;
$this->host = isset($config[$key]["HOST"]) ? $config[$config[$key]["HOST"]] : null;
$this->host = isset($config[$key]["HOST"]) ? $config[$key]["HOST"] : null;
$this->password = isset($config[$key]["PASSWORD"]) ? $config[$key]["PASSWORD"] : null;
$this->database = isset($config[$key]["DATABASE"]) ? $config[$key]["DATABASE"] : null;
$this->port = isset($config[$key]["PORT"]) ? $config[$key]["PORT"] : null;
$this->charset = isset($config[$key]["CHARSET"]) ? $config[$key]["CHARSET"] : null;
} catch (\Throwable $th) {
$this->error = $th->getMessage();
}
@@ -70,6 +72,7 @@ class Connection
$this->password = isset($_ENV[$this->getKey("PASSWORD", $key)]) ? $_ENV[$this->getKey("PASSWORD", $key)] : null;
$this->database = isset($_ENV[$this->getKey("DATABASE", $key)]) ? $_ENV[$this->getKey("DATABASE", $key)] : null;
$this->port = isset($_ENV[$this->getKey("PORT", $key)]) ? $_ENV[$this->getKey("PORT", $key)] : null;
$this->charset = isset($_ENV[$this->getKey("CHARSET", $key)]) ? $_ENV[$this->getKey("CHARSET", $key)] : null;
} catch (\Throwable $th) {
$this->error = $th->getMessage();
}
@@ -117,10 +120,19 @@ class Connection
return $stmt;
}
private function getMySQLOptions()
{
$ret = "";
if ($this->charset) {
$ret .= ";charset=" . $this->charset;
}
return $ret;
}
private function getConnString()
{
$this->connstring = match ($this->type) {
"mysql" => "mysql:host=" . $this->host . ":" . $this->port . ";dbname=" . $this->database,
"mysql" => "mysql:host=" . $this->host . ":" . $this->port . ";dbname=" . $this->database . $this->getMySQLOptions() ,
"sqlite" => "sqlite:" . $this->database,
};
}

View File

@@ -8,19 +8,36 @@ class Model implements JsonSerializable
{
protected mixed $values;
protected array $updated;
protected string $table;
protected string $table = "";
protected string $connection = "";
protected string $pk;
private bool $new;
private bool $ro;
public function __construct()
public function __construct($ro = false)
{
$this->new = true;
$this->values = array();
$this->updated = array();
$this->pk = "id";
$this->ro = $ro;
$this->initTable();
}
private function initTable(): void
{
if(!$this->ro)
{
if ($this->table == "") {
$this->table = $this->getDefaultTableName();
}
}
else
{
$this->table = "ReadOnlyModel";
$this->pk = "__idpk";
}
}
public function getTable(): string
{
@@ -82,6 +99,8 @@ class Model implements JsonSerializable
public function save(): bool
{
$ret = false;
if(!$this->ro)
{
$qb = $this->newQueryBuilder();
if ($this->new) {
$query = $qb->getPreparedQuery($this->values);
@@ -107,6 +126,7 @@ class Model implements JsonSerializable
}
}
}
}
return $ret;
}

View File

@@ -6,6 +6,7 @@ class QueryBuilder
{
protected array $columns;
protected array $wheres;
protected array $orwheres;
protected array $orders;
protected array $groupby;
protected string $table;
@@ -16,6 +17,9 @@ class QueryBuilder
protected string $raw_query;
protected bool $distinct;
public const MODE_AND = 0;
public const MODE_OR = 1;
public function __construct($table, $pk = "id", $connection = "")
{
$this->table = $table;
@@ -28,6 +32,7 @@ class QueryBuilder
{
$this->columns = [];
$this->wheres = [];
$this->orwheres = [];
$this->orders = [];
$this->groupby = [];
$this->offset = null;
@@ -128,12 +133,28 @@ class QueryBuilder
return $this;
}
public function orWhere($condition, $operator, $value = null)
{
if ($value === null) {
$this->orwheres[] = new Expression($condition, "=", $operator);
} else {
$this->orwheres[] = new Expression($condition, $operator, $value);
}
return $this;
}
public function whereRaw(string $raw)
{
$this->wheres[] = $raw;
return $this;
}
public function orWhereRaw(string $raw)
{
$this->orwheres[] = $raw;
return $this;
}
public function find($id): mixed
{
$this->reset();
@@ -213,10 +234,17 @@ class QueryBuilder
$query = "SELECT " . $distinct . " " . $columns . " FROM " . $this->table ;
$where = $this->getWhere();
$Orwhere = $this->getWhere(QueryBuilder::MODE_OR);
$this->fillData($ret);
if($where!="" && $Orwhere!="")
{
$Orwhere = " OR " . $Orwhere;
}
if ($where != "") {
$query .= " WHERE " . $where;
$query .= " WHERE " . $where . $Orwhere;
}
$query .= $this->getClause("GROUP By", $this->groupby);
@@ -229,7 +257,7 @@ class QueryBuilder
private function fillData(Query &$query): void
{
foreach ($this->wheres as $expression) {
foreach (array_merge($this->wheres,$this->orwheres) as $expression) {
if ($expression instanceof Expression) {
if ($expression->hasData()) {
$query->addData($expression->getValue());
@@ -238,10 +266,18 @@ class QueryBuilder
}
}
private function getWhere(): string
private function getWhere(int $mode = QueryBuilder::MODE_AND): string
{
$where = "";
foreach ($this->wheres as $expression) {
$collection = match ($mode) {
QueryBuilder::MODE_AND => $this->wheres,
QueryBuilder::MODE_OR => $this->orwheres,
};
$keyword = match ($mode) {
QueryBuilder::MODE_AND => " AND ",
QueryBuilder::MODE_OR => " OR ",
};
foreach ($collection as $expression) {
$sw = "";
if ($expression instanceof Expression) {
$sw = $expression->raw();
@@ -252,7 +288,7 @@ class QueryBuilder
}
}
if ($where != "" && $sw != "") {
$where .= " AND ";
$where .= " $keyword ";
}
$where .= $sw;
}

View File

@@ -98,6 +98,10 @@ test("querybuilder", function() {
$qb->reset();
$rows = $qb->whereNotNull("col1")->get();
expect(count($rows))->toBe(2);
$qb->reset();
$rows = $qb->where("id",1)->orWhere("id",2)->get();
expect(count($rows))->toBe(2);
});
test("extending model",function()