292 lines
8.1 KiB
PHP
292 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\Repository;
|
|
|
|
use Ulmus\{Common\EntityResolver, Query, Common};
|
|
|
|
trait QueryBuildingTrait
|
|
{
|
|
public function truncate(? string $table = null, ? string $alias = null, ? string $schema = null) : self
|
|
{
|
|
$schema = $schema ?: $this->entityResolver->schemaName();
|
|
|
|
$this->queryBuilder->truncate($this->escapeTable($table ?: $this->entityResolver->tableName()), $this->escapeIdentifier($alias ?: $this->alias), $this->escapedDatabase(), $schema ? $this->escapeSchema($schema) : null);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function select(array|string|\Stringable $fields, bool $distinct = false) : self
|
|
{
|
|
$this->queryBuilder->select($fields, $distinct);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function distinct(array|string|\Stringable $fields) : self
|
|
{
|
|
$this->queryBuilder->select($fields);
|
|
$this->queryBuilder->getFragment(Query\Select::class)->distinct = true;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function insert(array $fieldlist, string $table, string $alias, ? string $schema, bool $replace = false) : self
|
|
{
|
|
$this->queryBuilder->insert($fieldlist, $this->escapeTable($table), $this->escapeIdentifier($alias), $this->escapedDatabase(), $schema, $replace);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function values(array $dataset) : self
|
|
{
|
|
$this->queryBuilder->values($dataset);
|
|
return $this;
|
|
}
|
|
|
|
public function update(string $table, string $alias, ? string $schema) : self
|
|
{
|
|
$this->queryBuilder->update($this->escapeTable($table), $alias ? $this->escapeIdentifier($alias) : null, $this->escapedDatabase(), $schema);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function set(array $dataset) : self
|
|
{
|
|
$keys = array_keys($dataset);
|
|
$escapedFields = array_combine($keys, array_map([ $this, 'escapeField' ], $keys));
|
|
|
|
$this->queryBuilder->set($dataset, $escapedFields);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function delete(...$args) : self
|
|
{
|
|
$this->queryBuilder->delete();
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function from(string $table, ? string $alias, ? string $schema) : self
|
|
{
|
|
$this->queryBuilder->from($this->escapeTable($table), $alias ? $this->escapeIdentifier($alias) : null, $this->escapedDatabase(), $schema ? $this->escapeSchema($schema) : null);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function join(string $type, $table, string|\Stringable $field, mixed $value, ? string $alias = null, ? callable $callback = null) : self
|
|
{
|
|
$join = $this->queryBuilder->withJoin($type, $this->escapeTable($table), $field, $value, false, $alias ? $this->escapeIdentifier($alias) : null);
|
|
|
|
if ( $callback ) {
|
|
$callback($join);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function outerJoin(string $type, $table, string|\Stringable $field, mixed $value, ? string $alias = null, ? callable $callback = null) : self
|
|
{
|
|
$join = $this->queryBuilder->withJoin($type, $this->escapeTable($table), $field, $value, true, $alias ? $this->escapeIdentifier($alias) : null);
|
|
|
|
if ( $callback ) {
|
|
$callback($join);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function match() : self
|
|
{
|
|
|
|
}
|
|
|
|
public function notMatch() : self
|
|
{
|
|
|
|
}
|
|
|
|
public function between() : self
|
|
{
|
|
|
|
}
|
|
|
|
public function notBetween() : self
|
|
{
|
|
|
|
}
|
|
|
|
public function groupBy(string|\Stringable $field) : self
|
|
{
|
|
$this->queryBuilder->groupBy($field);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function groups(array $groups) : self
|
|
{
|
|
foreach($groups as $field ) {
|
|
$this->groupBy($field);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function orderBy(string|\Stringable $field, null|string|\Stringable $direction = null) : self
|
|
{
|
|
$this->queryBuilder->orderBy($field, $direction);
|
|
|
|
return $this;
|
|
}
|
|
|
|
# @UNTESTED
|
|
public function randomizeOrder() : self
|
|
{
|
|
$this->queryBuilder->orderBy(Common\Sql::function('RAND', Common\Sql::identifier('CURDATE()+0')));
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function orders(array $orderList) : self
|
|
{
|
|
foreach($orderList as $field => $direction) {
|
|
if (is_numeric($field)) {
|
|
$this->orderBy($direction);
|
|
}
|
|
else {
|
|
# Associative array with direction
|
|
$this->orderBy($field, $direction);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function limit(int $value) : self
|
|
{
|
|
$this->queryBuilder->limit($value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function offset(int $value) : self
|
|
{
|
|
$this->queryBuilder->offset($value);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getSqlQuery(bool $flush = true) : string
|
|
{
|
|
$result = $this->queryBuilder->render();
|
|
|
|
$flush and $this->queryBuilder->reset();
|
|
|
|
return $result;
|
|
}
|
|
|
|
|
|
public function resetQuery() : self
|
|
{
|
|
$this->queryBuilder->reset();
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function insertSqlQuery(array $dataset, bool $replace = false) : self
|
|
{
|
|
if ( null === $insert = $this->queryBuilder->getFragment(Query\Insert::class) ) {
|
|
$this->insert(array_map([ $this, 'escapeField' ] , array_keys($dataset)), $this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName(), $replace);
|
|
}
|
|
else {
|
|
$insert->replace = $replace;
|
|
}
|
|
|
|
$this->values($dataset);
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function updateSqlQuery(array $dataset) : self
|
|
{
|
|
if ( null === $this->queryBuilder->getFragment(Query\Update::class) ) {
|
|
$this->update($this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
|
|
}
|
|
|
|
$this->set($dataset);
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function selectSqlQuery() : self
|
|
{
|
|
if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) {
|
|
$fields = $this->entityResolver->fieldList(EntityResolver::KEY_COLUMN_NAME, true);
|
|
$this->select($this->entityClass::fields(array_map(fn($f) => $f->object->name ?? $f->name, $fields)));
|
|
}
|
|
|
|
if ( null === $this->queryBuilder->getFragment(Query\From::class) ) {
|
|
$this->from($this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function deleteSqlQuery() : self
|
|
{
|
|
if ( null === $this->queryBuilder->getFragment(Query\Delete::class) ) {
|
|
$this->delete();
|
|
}
|
|
|
|
if ( null === $this->queryBuilder->getFragment(Query\From::class) ) {
|
|
$this->from($this->entityResolver->tableName(), null, $this->entityResolver->schemaName());
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function createSqlQuery() : self
|
|
{
|
|
if ( null === $this->queryBuilder->getFragment(Query\Create::class) ) {
|
|
$this->queryBuilder->create($this->adapter->adapter(), $this->escapeFieldList($this->entityResolver->fieldList(EntityResolver::KEY_ENTITY_NAME, true)), $this->escapeTable($this->entityResolver->tableName()), $this->entityResolver->schemaName());
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function alterSqlQuery(array $fields) : self
|
|
{
|
|
if ( null === $this->queryBuilder->getFragment(Query\Alter::class) ) {
|
|
$this->queryBuilder->alter($this->adapter->adapter(), $fields, $this->escapeTable($this->entityResolver->tableName()), $this->entityResolver->schemaName());
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function showDatabasesSqlQuery() : self
|
|
{
|
|
if ( null === $this->queryBuilder->getFragment(Query\Show::class) ) {
|
|
$this->queryBuilder->showDatabases();
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function showTablesSqlQuery() : self
|
|
{
|
|
if ( null === $this->queryBuilder->getFragment(Query\Show::class) ) {
|
|
$this->queryBuilder->showTables();
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function showColumnsSqlQuery(string $table) : self
|
|
{
|
|
if ( null === $this->queryBuilder->getFragment(Query\Show::class) ) {
|
|
$this->queryBuilder->showColumns($table);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
} |