composer update + SQL Compare refactored
This commit is contained in:
@@ -7,7 +7,7 @@ use Core\Configuration\Settings;
|
||||
use Core\Driver\SQL\Condition\Compare;
|
||||
use Core\Driver\SQL\Condition\CondLike;
|
||||
use Core\Driver\SQL\Condition\CondOr;
|
||||
use Core\Driver\SQL\Join;
|
||||
use Core\Driver\SQL\Join\InnerJoin;
|
||||
use Core\Driver\SQL\SQL;
|
||||
use Firebase\JWT\JWT;
|
||||
use Core\Objects\DatabaseEntity\Language;
|
||||
@@ -179,11 +179,11 @@ class Context {
|
||||
|
||||
public function loadApiKey(string $apiKey): bool {
|
||||
$this->user = User::findBy(User::createBuilder($this->sql, true)
|
||||
->addJoin(new Join("INNER","ApiKey", "ApiKey.user_id", "User.id"))
|
||||
->where(new Compare("ApiKey.api_key", $apiKey))
|
||||
->where(new Compare("valid_until", $this->sql->currentTimestamp(), ">"))
|
||||
->where(new Compare("ApiKey.active", true))
|
||||
->where(new Compare("User.confirmed", true))
|
||||
->addJoin(new InnerJoin("ApiKey", "ApiKey.user_id", "User.id"))
|
||||
->whereEq("ApiKey.api_key", $apiKey)
|
||||
->whereGt("valid_until", $this->sql->currentTimestamp())
|
||||
->whereTrue("ApiKey.active", true)
|
||||
->whereTrue("User.confirmed", true)
|
||||
->fetchEntities());
|
||||
|
||||
return $this->user !== null;
|
||||
|
||||
@@ -40,7 +40,7 @@ abstract class DatabaseEntity {
|
||||
$handler = self::getHandler($sql);
|
||||
if ($fetchEntities) {
|
||||
return DatabaseEntityQuery::fetchOne(self::getHandler($sql))
|
||||
->where(new Compare($handler->getTableName() . ".id", $id))
|
||||
->whereEq($handler->getTableName() . ".id", $id)
|
||||
->fetchEntities($fetchRecursive)
|
||||
->execute();
|
||||
} else {
|
||||
@@ -52,7 +52,7 @@ abstract class DatabaseEntity {
|
||||
$handler = self::getHandler($sql);
|
||||
$res = $sql->select($sql->count())
|
||||
->from($handler->getTableName())
|
||||
->where(new Compare($handler->getTableName() . ".id", $id))
|
||||
->whereEq($handler->getTableName() . ".id", $id)
|
||||
->execute();
|
||||
|
||||
return $res !== false && $res[0]["count"] !== 0;
|
||||
|
||||
@@ -20,7 +20,7 @@ use Core\Driver\SQL\Column\FloatColumn;
|
||||
use Core\Driver\SQL\Condition\CondNot;
|
||||
use Core\Driver\SQL\Condition\CondOr;
|
||||
use Core\Driver\SQL\Constraint\ForeignKey;
|
||||
use Core\Driver\SQL\Join;
|
||||
use Core\Driver\SQL\Join\InnerJoin;
|
||||
use Core\Driver\SQL\Query\CreateProcedure;
|
||||
use Core\Driver\SQL\Query\CreateTable;
|
||||
use Core\Driver\SQL\Query\Insert;
|
||||
@@ -323,7 +323,7 @@ class DatabaseEntityHandler implements Persistable {
|
||||
|
||||
// delete from n:m table if no longer exists
|
||||
$deleteStatement = $this->sql->delete($nmTable)
|
||||
->where(new Compare($thisIdColumn, $entity->getId()));
|
||||
->whereEq($thisIdColumn, $entity->getId());
|
||||
|
||||
if (!empty($dataColumns)) {
|
||||
$conditions = [];
|
||||
@@ -439,9 +439,8 @@ class DatabaseEntityHandler implements Persistable {
|
||||
$dataColumns = $nmRelation->getDataColumns();
|
||||
|
||||
$relEntityQuery = DatabaseEntityQuery::fetchAll($otherHandler)
|
||||
->addJoin(new Join("INNER", $nmTable, "$nmTable.$refIdColumn", "$refTableName.id"))
|
||||
->where(new CondIn(new Column($thisIdColumn), $entityIds))
|
||||
->getQuery();
|
||||
->addJoin(new InnerJoin($nmTable, "$nmTable.$refIdColumn", "$refTableName.id"))
|
||||
->where(new CondIn(new Column($thisIdColumn), $entityIds));
|
||||
|
||||
$relEntityQuery->addColumn($thisIdColumn);
|
||||
foreach ($dataColumns as $tableDataColumns) {
|
||||
@@ -498,7 +497,7 @@ class DatabaseEntityHandler implements Persistable {
|
||||
|
||||
public function fetchOne(int $id): DatabaseEntity|bool|null {
|
||||
$res = $this->getSelectQuery()
|
||||
->where(new Compare($this->tableName . ".id", $id))
|
||||
->whereEq($this->tableName . ".id", $id)
|
||||
->first()
|
||||
->execute();
|
||||
|
||||
@@ -637,7 +636,7 @@ class DatabaseEntityHandler implements Persistable {
|
||||
|
||||
$entity->preInsert($row);
|
||||
$query = $this->sql->update($this->tableName)
|
||||
->where(new Compare($this->tableName . ".id", $entity->getId()));
|
||||
->whereEq($this->tableName . ".id", $entity->getId());
|
||||
|
||||
foreach ($row as $columnName => $value) {
|
||||
$query->set($columnName, $value);
|
||||
@@ -693,7 +692,7 @@ class DatabaseEntityHandler implements Persistable {
|
||||
public function delete(int $id) {
|
||||
return $this->sql
|
||||
->delete($this->tableName)
|
||||
->where(new Compare($this->tableName . ".id", $id))
|
||||
->whereEq($this->tableName . ".id", $id)
|
||||
->execute();
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ use Core\Driver\SQL\SQL;
|
||||
* this class is similar to \Driver\SQL\Query\Select but with reduced functionality
|
||||
* and more adapted to entities.
|
||||
*/
|
||||
class DatabaseEntityQuery {
|
||||
class DatabaseEntityQuery extends Select {
|
||||
|
||||
const FETCH_NONE = 0;
|
||||
const FETCH_DIRECT = 1;
|
||||
@@ -20,22 +20,22 @@ class DatabaseEntityQuery {
|
||||
|
||||
private Logger $logger;
|
||||
private DatabaseEntityHandler $handler;
|
||||
private Select $selectQuery;
|
||||
private int $resultType;
|
||||
private bool $logVerbose;
|
||||
|
||||
private int $fetchSubEntities;
|
||||
|
||||
private function __construct(DatabaseEntityHandler $handler, int $resultType) {
|
||||
parent::__construct($handler->getSQL(), ...$handler->getColumnNames());
|
||||
$this->handler = $handler;
|
||||
$this->selectQuery = $handler->getSelectQuery();
|
||||
$this->logger = new Logger("DB-EntityQuery", $handler->getSQL());
|
||||
$this->resultType = $resultType;
|
||||
$this->logVerbose = false;
|
||||
$this->fetchSubEntities = self::FETCH_NONE;
|
||||
|
||||
$this->from($handler->getTableName());
|
||||
$this->fetchSubEntities = self::FETCH_NONE;
|
||||
if ($this->resultType === SQL::FETCH_ONE) {
|
||||
$this->selectQuery->first();
|
||||
$this->first();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,36 +52,6 @@ class DatabaseEntityQuery {
|
||||
return new DatabaseEntityQuery($handler, SQL::FETCH_ONE);
|
||||
}
|
||||
|
||||
public function limit(int $limit): DatabaseEntityQuery {
|
||||
$this->selectQuery->limit($limit);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function offset(int $offset): static {
|
||||
$this->selectQuery->offset($offset);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function where(Condition ...$condition): DatabaseEntityQuery {
|
||||
$this->selectQuery->where(...$condition);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function orderBy(string ...$column): DatabaseEntityQuery {
|
||||
$this->selectQuery->orderBy(...$column);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function ascending(): DatabaseEntityQuery {
|
||||
$this->selectQuery->ascending();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function descending(): DatabaseEntityQuery {
|
||||
$this->selectQuery->descending();
|
||||
return $this;
|
||||
}
|
||||
|
||||
// TODO: clean this up
|
||||
public function fetchEntities(bool $recursive = false): DatabaseEntityQuery {
|
||||
|
||||
@@ -110,9 +80,9 @@ class DatabaseEntityQuery {
|
||||
|
||||
|
||||
if ($isNullable) {
|
||||
$this->selectQuery->leftJoin($referencedTable, "$tableName.$foreignColumnName", "$alias.id", $alias);
|
||||
$this->leftJoin($referencedTable, "$tableName.$foreignColumnName", "$alias.id", $alias);
|
||||
} else {
|
||||
$this->selectQuery->innerJoin($referencedTable, "$tableName.$foreignColumnName", "$alias.id", $alias);
|
||||
$this->innerJoin($referencedTable, "$tableName.$foreignColumnName", "$alias.id", $alias);
|
||||
}
|
||||
|
||||
$relationColumnPrefix .= DatabaseEntityHandler::getColumnName($propertyName) . "_";
|
||||
@@ -120,7 +90,7 @@ class DatabaseEntityQuery {
|
||||
foreach ($relationHandler->getColumns() as $relPropertyName => $relColumn) {
|
||||
$relColumnName = $relColumn->getName();
|
||||
if (!isset($recursiveRelations[$relPropertyName]) || $recursive) {
|
||||
$this->selectQuery->addValue("$alias.$relColumnName as $relationColumnPrefix$relColumnName");
|
||||
$this->addValue("$alias.$relColumnName as $relationColumnPrefix$relColumnName");
|
||||
if (isset($recursiveRelations[$relPropertyName]) && $recursive) {
|
||||
$this->fetchRelation($relPropertyName, $alias, $relationHandler, $recursiveRelations[$relPropertyName], $relIndex, $recursive, $relationColumnPrefix);
|
||||
}
|
||||
@@ -132,11 +102,11 @@ class DatabaseEntityQuery {
|
||||
|
||||
if ($this->logVerbose) {
|
||||
$params = [];
|
||||
$query = $this->selectQuery->build($params);
|
||||
$query = $this->build($params);
|
||||
$this->logger->debug("QUERY: $query\nARGS: " . print_r($params, true));
|
||||
}
|
||||
|
||||
$res = $this->selectQuery->execute();
|
||||
$res = parent::execute();
|
||||
if ($res === null || $res === false) {
|
||||
return null;
|
||||
}
|
||||
@@ -167,13 +137,4 @@ class DatabaseEntityQuery {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function addJoin(Join $join): DatabaseEntityQuery {
|
||||
$this->selectQuery->addJoin($join);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getQuery(): Select {
|
||||
return $this->selectQuery;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user