Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d961d2a80f | ||
|
|
0db845a116 |
@@ -12,21 +12,31 @@ class Model implements JsonSerializable
|
|||||||
protected string $connection = "";
|
protected string $connection = "";
|
||||||
protected string $pk;
|
protected string $pk;
|
||||||
private bool $new;
|
private bool $new;
|
||||||
|
private bool $ro;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct($ro = false)
|
||||||
{
|
{
|
||||||
$this->new = true;
|
$this->new = true;
|
||||||
$this->values = array();
|
$this->values = array();
|
||||||
$this->updated = array();
|
$this->updated = array();
|
||||||
$this->pk = "id";
|
$this->pk = "id";
|
||||||
|
$this->ro = $ro;
|
||||||
$this->initTable();
|
$this->initTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function initTable(): void
|
private function initTable(): void
|
||||||
{
|
{
|
||||||
if ($this->table == "") {
|
if(!$this->ro)
|
||||||
$this->table = $this->getDefaultTableName();
|
{
|
||||||
|
if ($this->table == "") {
|
||||||
|
$this->table = $this->getDefaultTableName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$this->table = "ReadOnlyModel";
|
||||||
|
$this->pk = "__idpk";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTable(): string
|
public function getTable(): string
|
||||||
@@ -89,31 +99,34 @@ class Model implements JsonSerializable
|
|||||||
public function save(): bool
|
public function save(): bool
|
||||||
{
|
{
|
||||||
$ret = false;
|
$ret = false;
|
||||||
$qb = $this->newQueryBuilder();
|
if(!$this->ro)
|
||||||
if ($this->new) {
|
{
|
||||||
$query = $qb->getPreparedQuery($this->values);
|
$qb = $this->newQueryBuilder();
|
||||||
$id = Connection::getInstance($this->connection)->insertQuery($query);
|
if ($this->new) {
|
||||||
if ($id !== false) {
|
$query = $qb->getPreparedQuery($this->values);
|
||||||
$this->values[$this->pk] = $id;
|
$id = Connection::getInstance($this->connection)->insertQuery($query);
|
||||||
$this->new = false;
|
if ($id !== false) {
|
||||||
$ret = true;
|
$this->values[$this->pk] = $id;
|
||||||
}
|
$this->new = false;
|
||||||
} else {
|
$ret = true;
|
||||||
if (isset($this->values[$this->pk])) {
|
}
|
||||||
if (count($this->updated) > 0) {
|
} else {
|
||||||
$data = [];
|
if (isset($this->values[$this->pk])) {
|
||||||
$data[$this->pk] = $this->values[$this->pk];
|
if (count($this->updated) > 0) {
|
||||||
foreach ($this->updated as $updatecol) {
|
$data = [];
|
||||||
$data[$updatecol] = $this->values[$updatecol];
|
$data[$this->pk] = $this->values[$this->pk];
|
||||||
}
|
foreach ($this->updated as $updatecol) {
|
||||||
$query = $qb->getPreparedQuery($data, false);
|
$data[$updatecol] = $this->values[$updatecol];
|
||||||
$ret = Connection::getInstance($this->connection)->updateQuery($query);
|
}
|
||||||
if ($ret) {
|
$query = $qb->getPreparedQuery($data, false);
|
||||||
$this->updated = [];
|
$ret = Connection::getInstance($this->connection)->updateQuery($query);
|
||||||
|
if ($ret) {
|
||||||
|
$this->updated = [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ class QueryBuilder
|
|||||||
{
|
{
|
||||||
protected array $columns;
|
protected array $columns;
|
||||||
protected array $wheres;
|
protected array $wheres;
|
||||||
|
protected array $orwheres;
|
||||||
protected array $orders;
|
protected array $orders;
|
||||||
protected array $groupby;
|
protected array $groupby;
|
||||||
protected string $table;
|
protected string $table;
|
||||||
@@ -16,6 +17,9 @@ class QueryBuilder
|
|||||||
protected string $raw_query;
|
protected string $raw_query;
|
||||||
protected bool $distinct;
|
protected bool $distinct;
|
||||||
|
|
||||||
|
public const MODE_AND = 0;
|
||||||
|
public const MODE_OR = 1;
|
||||||
|
|
||||||
public function __construct($table, $pk = "id", $connection = "")
|
public function __construct($table, $pk = "id", $connection = "")
|
||||||
{
|
{
|
||||||
$this->table = $table;
|
$this->table = $table;
|
||||||
@@ -28,6 +32,7 @@ class QueryBuilder
|
|||||||
{
|
{
|
||||||
$this->columns = [];
|
$this->columns = [];
|
||||||
$this->wheres = [];
|
$this->wheres = [];
|
||||||
|
$this->orwheres = [];
|
||||||
$this->orders = [];
|
$this->orders = [];
|
||||||
$this->groupby = [];
|
$this->groupby = [];
|
||||||
$this->offset = null;
|
$this->offset = null;
|
||||||
@@ -128,12 +133,28 @@ class QueryBuilder
|
|||||||
return $this;
|
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)
|
public function whereRaw(string $raw)
|
||||||
{
|
{
|
||||||
$this->wheres[] = $raw;
|
$this->wheres[] = $raw;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function orWhereRaw(string $raw)
|
||||||
|
{
|
||||||
|
$this->orwheres[] = $raw;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function find($id): mixed
|
public function find($id): mixed
|
||||||
{
|
{
|
||||||
$this->reset();
|
$this->reset();
|
||||||
@@ -213,10 +234,17 @@ class QueryBuilder
|
|||||||
$query = "SELECT " . $distinct . " " . $columns . " FROM " . $this->table ;
|
$query = "SELECT " . $distinct . " " . $columns . " FROM " . $this->table ;
|
||||||
|
|
||||||
$where = $this->getWhere();
|
$where = $this->getWhere();
|
||||||
|
$Orwhere = $this->getWhere(QueryBuilder::MODE_OR);
|
||||||
|
|
||||||
$this->fillData($ret);
|
$this->fillData($ret);
|
||||||
|
|
||||||
|
if($where!="" && $Orwhere!="")
|
||||||
|
{
|
||||||
|
$Orwhere = " OR " . $Orwhere;
|
||||||
|
}
|
||||||
|
|
||||||
if ($where != "") {
|
if ($where != "") {
|
||||||
$query .= " WHERE " . $where;
|
$query .= " WHERE " . $where . $Orwhere;
|
||||||
}
|
}
|
||||||
|
|
||||||
$query .= $this->getClause("GROUP By", $this->groupby);
|
$query .= $this->getClause("GROUP By", $this->groupby);
|
||||||
@@ -228,8 +256,8 @@ class QueryBuilder
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function fillData(Query &$query): void
|
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 instanceof Expression) {
|
||||||
if ($expression->hasData()) {
|
if ($expression->hasData()) {
|
||||||
$query->addData($expression->getValue());
|
$query->addData($expression->getValue());
|
||||||
@@ -238,10 +266,18 @@ class QueryBuilder
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getWhere(): string
|
private function getWhere(int $mode = QueryBuilder::MODE_AND): string
|
||||||
{
|
{
|
||||||
$where = "";
|
$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 = "";
|
$sw = "";
|
||||||
if ($expression instanceof Expression) {
|
if ($expression instanceof Expression) {
|
||||||
$sw = $expression->raw();
|
$sw = $expression->raw();
|
||||||
@@ -252,7 +288,7 @@ class QueryBuilder
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($where != "" && $sw != "") {
|
if ($where != "" && $sw != "") {
|
||||||
$where .= " AND ";
|
$where .= " $keyword ";
|
||||||
}
|
}
|
||||||
$where .= $sw;
|
$where .= $sw;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,6 +98,10 @@ test("querybuilder", function() {
|
|||||||
$qb->reset();
|
$qb->reset();
|
||||||
$rows = $qb->whereNotNull("col1")->get();
|
$rows = $qb->whereNotNull("col1")->get();
|
||||||
expect(count($rows))->toBe(2);
|
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()
|
test("extending model",function()
|
||||||
|
|||||||
Reference in New Issue
Block a user