Facturacion
This commit is contained in:
@ -11,4 +11,5 @@ interface Connection
|
||||
public function prepare(string $query): PDOStatement;
|
||||
public function execute(string $query, ?array $data = null): PDOStatement;
|
||||
public function getPDO(): PDO;
|
||||
public function getQueryBuilder(): Query\Builder;
|
||||
}
|
||||
|
@ -5,4 +5,5 @@ use JsonSerializable;
|
||||
|
||||
interface Model extends JsonSerializable
|
||||
{
|
||||
public function addFactory(string $property, Repository\Factory $factory): Model;
|
||||
}
|
||||
|
15
app/common/Define/Query.php
Normal file
15
app/common/Define/Query.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define;
|
||||
|
||||
interface Query
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function build(): string;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string;
|
||||
}
|
33
app/common/Define/Query/Builder.php
Normal file
33
app/common/Define/Query/Builder.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Query;
|
||||
|
||||
interface Builder
|
||||
{
|
||||
/**
|
||||
* @param string $table_name
|
||||
* @return Create
|
||||
*/
|
||||
public function create(string $table_name): Create;
|
||||
|
||||
/**
|
||||
* @param string|array $columns
|
||||
* @return Select
|
||||
*/
|
||||
public function select(string|array $columns = '*'): Select;
|
||||
|
||||
/**
|
||||
* @return Insert
|
||||
*/
|
||||
public function insert(): Insert;
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @return Update
|
||||
*/
|
||||
public function update(string $table): Update;
|
||||
|
||||
/**
|
||||
* @return Delete
|
||||
*/
|
||||
public function delete(): Delete;
|
||||
}
|
31
app/common/Define/Query/Create.php
Normal file
31
app/common/Define/Query/Create.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Query;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
interface Create extends Define\Query
|
||||
{
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Create
|
||||
*/
|
||||
public function table(string $name): Create;
|
||||
|
||||
/**
|
||||
* @param string|Create\CreateDefinition|array $create_definitions
|
||||
* @return Create
|
||||
*/
|
||||
public function definitions(string|Create\CreateDefinition|array $create_definitions): Create;
|
||||
|
||||
/**
|
||||
* @param string|array $table_options
|
||||
* @return Create
|
||||
*/
|
||||
public function options(string|array $table_options): Create;
|
||||
|
||||
/**
|
||||
* @param string|array $partition_options
|
||||
* @return Create
|
||||
*/
|
||||
public function partition(string|array $partition_options): Create;
|
||||
}
|
59
app/common/Define/Query/Create/CreateDefinition.php
Normal file
59
app/common/Define/Query/Create/CreateDefinition.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Query\Create;
|
||||
|
||||
interface CreateDefinition
|
||||
{
|
||||
const RESTRICT = 0;
|
||||
const CASCADE = 1;
|
||||
const SET_NULL = 2;
|
||||
const NO_ACTION = 3;
|
||||
const SET_DEFAULT = 4;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return CreateDefinition
|
||||
*/
|
||||
public function name(string $name): CreateDefinition;
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param int|null $size
|
||||
* @return CreateDefinition
|
||||
*/
|
||||
public function type(string $type, ?int $size = null): CreateDefinition;
|
||||
|
||||
/**
|
||||
* @return CreateDefinition
|
||||
*/
|
||||
public function primary(): CreateDefinition;
|
||||
|
||||
/**
|
||||
* @return CreateDefinition
|
||||
*/
|
||||
public function autoIncrement(): CreateDefinition;
|
||||
|
||||
/**
|
||||
* @return CreateDefinition
|
||||
*/
|
||||
public function unsigned(): CreateDefinition;
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return CreateDefinition
|
||||
*/
|
||||
public function default(mixed $value): CreateDefinition;
|
||||
|
||||
/**
|
||||
* @param string $reference_table
|
||||
* @param string $reference_column
|
||||
* @param int $on_delete
|
||||
* @param int $on_update
|
||||
* @return CreateDefinition
|
||||
*/
|
||||
public function foreign(string $reference_table, string $reference_column = 'id', int $on_delete = CreateDefinition::CASCADE, int $on_update = CreateDefinition::CASCADE): CreateDefinition;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string;
|
||||
}
|
12
app/common/Define/Query/Delete.php
Normal file
12
app/common/Define/Query/Delete.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Query;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
interface Delete extends Define\Query
|
||||
{
|
||||
public function from(string $table): Delete;
|
||||
public function where(string|array $conditions): Delete;
|
||||
public function order(string|array $sorting): Delete;
|
||||
public function limit(int $limit): Delete;
|
||||
}
|
32
app/common/Define/Query/Insert.php
Normal file
32
app/common/Define/Query/Insert.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Query;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
interface Insert extends Define\Query
|
||||
{
|
||||
/**
|
||||
* @param string $table
|
||||
* @param array|null $columns
|
||||
* @return Insert
|
||||
*/
|
||||
public function into(string $table, ?array $columns = null): Insert;
|
||||
|
||||
/**
|
||||
* @param array $columns
|
||||
* @return Insert
|
||||
*/
|
||||
public function columns(array $columns): Insert;
|
||||
|
||||
/**
|
||||
* @param array $values
|
||||
* @return Insert
|
||||
*/
|
||||
public function values(array $values): Insert;
|
||||
|
||||
/**
|
||||
* @param Select $select
|
||||
* @return Insert
|
||||
*/
|
||||
public function select(Select $select): Insert;
|
||||
}
|
62
app/common/Define/Query/Select.php
Normal file
62
app/common/Define/Query/Select.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Query;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
interface Select extends Define\Query
|
||||
{
|
||||
/**
|
||||
* @param string|array $expressions
|
||||
* @return Select
|
||||
*/
|
||||
public function columns(string|array $expressions): Select;
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @return Select
|
||||
*/
|
||||
public function from(string $table): Select;
|
||||
|
||||
/**
|
||||
* @param string|array $joins
|
||||
* @return Select
|
||||
*/
|
||||
public function joined(string|array $joins): Select;
|
||||
|
||||
/**
|
||||
* @param string|array $conditions
|
||||
* @return Select
|
||||
*/
|
||||
public function where(string|array $conditions): Select;
|
||||
|
||||
/**
|
||||
* @param string|array $grouping
|
||||
* @return Select
|
||||
*/
|
||||
public function group(string|array $grouping): Select;
|
||||
|
||||
/**
|
||||
* @param string|array $conditions
|
||||
* @return Select
|
||||
*/
|
||||
public function having(string|array $conditions): Select;
|
||||
|
||||
/**
|
||||
* @param string|array $sorting
|
||||
* @return Select
|
||||
*/
|
||||
public function order(string|array $sorting): Select;
|
||||
|
||||
/**
|
||||
* @param int $limit
|
||||
* @param int|null $offset
|
||||
* @return Select
|
||||
*/
|
||||
public function limit(int $limit, ?int $offset = null): Select;
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @return Select
|
||||
*/
|
||||
public function offset(int $offset): Select;
|
||||
}
|
13
app/common/Define/Query/Update.php
Normal file
13
app/common/Define/Query/Update.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Query;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
interface Update extends Define\Query
|
||||
{
|
||||
public function table(string $table): Update;
|
||||
public function set(string|array $column_pairs): Update;
|
||||
public function where(string|array $conditions): Update;
|
||||
public function order(string|array $ordering): Update;
|
||||
public function limit(int $limit): Update;
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Repository;
|
||||
|
||||
use Incoviba\Common\Define\Model;
|
||||
|
||||
interface Mapper
|
||||
{
|
||||
public function setProperty(string $property): Mapper;
|
||||
@ -12,4 +14,6 @@ interface Mapper
|
||||
public function hasFunction(): bool;
|
||||
public function hasFactory(): bool;
|
||||
public function hasDefault(): bool;
|
||||
|
||||
public function parse(Model &$model, string $column, ?array $data): bool;
|
||||
}
|
||||
|
12
app/common/Ideal/Query.php
Normal file
12
app/common/Ideal/Query.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Ideal;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
abstract class Query implements Define\Query
|
||||
{
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->build();
|
||||
}
|
||||
}
|
@ -31,18 +31,25 @@ abstract class Repository implements Define\Repository
|
||||
|
||||
public function remove(Define\Model $model): void
|
||||
{
|
||||
$query = "DELETE FROM `{$this->getTable()}` WHERE `{$this->getKey()}` = ?";
|
||||
$this->connection->execute($query, [$model->getId()]);
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->delete()->from($this->getTable())
|
||||
->where("{$this->getKey()} = ?");
|
||||
$this->connection->execute($query, [$model->id]);
|
||||
}
|
||||
|
||||
public function fetchById(int $id): Define\Model
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `{$this->getKey()}` = ?";
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where("{$this->getKey()} = ?");
|
||||
return $this->fetchOne($query, [$id]);
|
||||
}
|
||||
public function fetchAll(): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}`";
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable());
|
||||
return $this->fetchMany($query);
|
||||
}
|
||||
|
||||
@ -96,9 +103,11 @@ abstract class Repository implements Define\Repository
|
||||
}
|
||||
protected function saveNew(array $columns, array $values): int
|
||||
{
|
||||
$columns_string = implode(', ', array_map(function($column) {return "`{$column}`";}, $columns));
|
||||
$columns_questions = implode(', ', array_fill(0, count($columns), '?'));
|
||||
$query = "INSERT INTO `{$this->getTable()}` ({$columns_string}) VALUES ($columns_questions)";
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->insert()
|
||||
->into($this->getTable())
|
||||
->columns($columns)
|
||||
->values(array_fill(0, count($columns), '?'));
|
||||
$this->connection->execute($query, $values);
|
||||
return $this->connection->getPDO()->lastInsertId();
|
||||
}
|
||||
@ -117,7 +126,10 @@ abstract class Repository implements Define\Repository
|
||||
return $model;
|
||||
}
|
||||
$columns_string = implode(', ', array_map(function($property) {return "`{$property}` = ?";}, $changes));
|
||||
$query = "UPDATE `{$this->getTable()}` SET {$columns_string} WHERE `{$this->getKey()}` = ?";
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->update($this->getTable())
|
||||
->set($columns_string)
|
||||
->where("{$this->getKey()} = ?");
|
||||
$values []= $model->{$this->getKey()};
|
||||
$this->connection->execute($query, $values);
|
||||
return $this->fetchById($model->{$this->getKey()});
|
||||
|
@ -8,7 +8,7 @@ use Incoviba\Common\Define;
|
||||
|
||||
class Connection implements Define\Connection
|
||||
{
|
||||
public function __construct(protected Define\Database $database) {}
|
||||
public function __construct(protected Define\Database $database, protected Database\Query\Builder $queryBuilder) {}
|
||||
|
||||
protected PDO $connection;
|
||||
public function connect(): Define\Connection
|
||||
@ -27,6 +27,10 @@ class Connection implements Define\Connection
|
||||
$this->connect();
|
||||
return $this->connection;
|
||||
}
|
||||
public function getQueryBuilder(): Database\Query\Builder
|
||||
{
|
||||
return $this->queryBuilder;
|
||||
}
|
||||
|
||||
public function query(string $query): PDOStatement
|
||||
{
|
||||
|
29
app/common/Implement/Database/Query/Builder.php
Normal file
29
app/common/Implement/Database/Query/Builder.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Database\Query;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
|
||||
class Builder implements Define\Query\Builder
|
||||
{
|
||||
public function create(string $table_name): Create
|
||||
{
|
||||
return (new Create())->table($table_name);
|
||||
}
|
||||
|
||||
public function select(array|string $columns = '*'): Select
|
||||
{
|
||||
return (new Select())->columns($columns);
|
||||
}
|
||||
public function insert(): Insert
|
||||
{
|
||||
return new Insert();
|
||||
}
|
||||
public function update(string $table): Update
|
||||
{
|
||||
return (new Update())->table($table);
|
||||
}
|
||||
public function delete(): Delete
|
||||
{
|
||||
return new Delete();
|
||||
}
|
||||
}
|
102
app/common/Implement/Database/Query/Create.php
Normal file
102
app/common/Implement/Database/Query/Create.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Database\Query;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
|
||||
class Create extends Ideal\Query implements Define\Query\Create
|
||||
{
|
||||
protected string $name;
|
||||
protected array $definitions;
|
||||
protected array $options;
|
||||
protected array $partitions;
|
||||
|
||||
public function table(string $name): Define\Query\Create
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
public function definitions(Define\Query\Create\CreateDefinition|array|string $create_definitions): Define\Query\Create
|
||||
{
|
||||
if (is_array($create_definitions)) {
|
||||
foreach ($create_definitions as $definition) {
|
||||
$this->addDefinition($definition);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
return $this->addDefinition($create_definitions);
|
||||
}
|
||||
public function options(array|string $table_options): Define\Query\Create
|
||||
{
|
||||
if (is_string($table_options)) {
|
||||
return $this->addOption($table_options);
|
||||
}
|
||||
foreach ($table_options as $option) {
|
||||
$this->addOption($option);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function partition(array|string $partition_options): Define\Query\Create
|
||||
{
|
||||
if (is_string($partition_options)) {
|
||||
return $this->addPartition($partition_options);
|
||||
}
|
||||
foreach ($partition_options as $option) {
|
||||
$this->addPartition($option);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function build(): string
|
||||
{
|
||||
$query = [
|
||||
"CREATE TABLE {$this->name}",
|
||||
$this->getDefinitions(),
|
||||
$this->getOptions(),
|
||||
$this->getPartitions()
|
||||
];
|
||||
return implode('', $query);
|
||||
}
|
||||
|
||||
protected function addDefinition(string $definition): Create
|
||||
{
|
||||
if (!isset($this->definitions)) {
|
||||
$this->definitions = [];
|
||||
}
|
||||
$this->definitions []= $definition;
|
||||
return $this;
|
||||
}
|
||||
protected function addOption(string $option): Create
|
||||
{
|
||||
if (!isset($this->options)) {
|
||||
$this->options = [];
|
||||
}
|
||||
$this->options []= $option;
|
||||
return $this;
|
||||
}
|
||||
protected function addPartition(string $partition): Create
|
||||
{
|
||||
if (!isset($this->partitions)) {
|
||||
$this->partitions = [];
|
||||
}
|
||||
$this->partitions []= $partition;
|
||||
return $this;
|
||||
}
|
||||
protected function getDefinitions(): string
|
||||
{
|
||||
return ' (' . implode(', ', $this->definitions) . ')';
|
||||
}
|
||||
protected function getOptions(): string
|
||||
{
|
||||
if (!isset($this->options) or count($this->options) <= 0) {
|
||||
return '';
|
||||
}
|
||||
return ' ' . implode(' ', $this->options);
|
||||
}
|
||||
protected function getPartitions(): string
|
||||
{
|
||||
if (!isset($this->partitions) or count($this->partitions) <= 0) {
|
||||
return '';
|
||||
}
|
||||
return ' PARTITION BY (' . implode(', ', $this->partitions) . ')';
|
||||
}
|
||||
}
|
104
app/common/Implement/Database/Query/Create/Definition.php
Normal file
104
app/common/Implement/Database/Query/Create/Definition.php
Normal file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
namespace ProVM\Implement\Data\Query\Create;
|
||||
|
||||
use ProVM\Define\Query\Create\CreateDefinition;
|
||||
|
||||
class Definition implements CreateDefinition
|
||||
{
|
||||
protected string $name;
|
||||
protected string $type;
|
||||
protected int $size;
|
||||
protected bool $primary;
|
||||
protected bool $auto_increment;
|
||||
protected bool $unsigned;
|
||||
protected mixed $default;
|
||||
protected string $foreign_table;
|
||||
protected string $foreign_key;
|
||||
protected int $foreign_delete;
|
||||
protected int $foreign_update;
|
||||
|
||||
public function name(string $name): CreateDefinition
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
public function type(string $type, ?int $size = null): CreateDefinition
|
||||
{
|
||||
$this->type = $type;
|
||||
if ($size !== null) {
|
||||
$this->size = $size;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function primary(): CreateDefinition
|
||||
{
|
||||
$this->primary = true;
|
||||
return $this;
|
||||
}
|
||||
public function autoIncrement(): CreateDefinition
|
||||
{
|
||||
$this->auto_increment = true;
|
||||
return $this;
|
||||
}
|
||||
public function unsigned(): CreateDefinition
|
||||
{
|
||||
$this->unsigned = true;
|
||||
return $this;
|
||||
}
|
||||
public function default(mixed $value): CreateDefinition
|
||||
{
|
||||
$this->default = $value;
|
||||
return $this;
|
||||
}
|
||||
public function foreign(string $reference_table, string $reference_column = 'id', int $on_delete = CreateDefinition::CASCADE, int $on_update = CreateDefinition::CASCADE): CreateDefinition
|
||||
{
|
||||
$this->foreign_table = $reference_table;
|
||||
$this->foreign_key = $reference_column;
|
||||
$this->foreign_delete = $on_delete;
|
||||
$this->foreign_update = $on_update;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
$type = $this->type ?? 'int';
|
||||
if (isset($this->size)) {
|
||||
$type = "{$type}({$this->size})";
|
||||
} elseif (in_array($type, ['varchar'])) {
|
||||
$type = "{$type}(255)";
|
||||
}
|
||||
$output = [
|
||||
"`{$this->name}`",
|
||||
$type
|
||||
];
|
||||
if (isset($this->unsigned)) {
|
||||
$output []= 'UNSIGNED';
|
||||
}
|
||||
if (isset($this->default)) {
|
||||
$default = $this->default;
|
||||
if (in_array($this->type, ['varchar', 'text', 'char'])) {
|
||||
$default = "'{$default}'";
|
||||
}
|
||||
$output []= "DEFAULT {$default}";
|
||||
}
|
||||
if (isset($this->auto_increment)) {
|
||||
$output []= 'AUTO_INCREMENT';
|
||||
}
|
||||
if (isset($this->primary)) {
|
||||
$output []= 'PRIMARY KEY';
|
||||
}
|
||||
if (isset($this->foreign_table)) {
|
||||
$output []= "REFERENCES `{$this->foreign_table}` (`{$this->foreign_key}`)";
|
||||
$on = [
|
||||
'RESTRICT',
|
||||
'CASCADE',
|
||||
'SET_NULL',
|
||||
'NO_ACTION',
|
||||
'SET_DEFAULT'
|
||||
];
|
||||
$output []= "ON DELETE {$on[$this->foreign_delete]}";
|
||||
$output []= "ON UPDATE {$on[$this->foreign_update]}";
|
||||
}
|
||||
return implode(' ', $output);
|
||||
}
|
||||
}
|
89
app/common/Implement/Database/Query/Delete.php
Normal file
89
app/common/Implement/Database/Query/Delete.php
Normal file
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Database\Query;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
|
||||
class Delete extends Ideal\Query implements Define\Query\Delete
|
||||
{
|
||||
protected string $table;
|
||||
protected array $conditions;
|
||||
protected array $sorts;
|
||||
protected int $limit;
|
||||
|
||||
public function from(string $table): Delete
|
||||
{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
public function where(array|string $conditions): Delete
|
||||
{
|
||||
if (is_string($conditions)) {
|
||||
return $this->addCondition($conditions);
|
||||
}
|
||||
foreach ($conditions as $condition) {
|
||||
$this->addCondition($condition);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function order(array|string $sorting): Delete
|
||||
{
|
||||
if (is_string($sorting)) {
|
||||
return $this->addOrder($sorting);
|
||||
}
|
||||
foreach ($sorting as $order) {
|
||||
$this->addOrder($order);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function limit(int $limit): Delete
|
||||
{
|
||||
$this->limit = $limit;
|
||||
return $this;
|
||||
}
|
||||
public function build(): string
|
||||
{
|
||||
$query = [
|
||||
"DELETE FROM {$this->table}",
|
||||
$this->getConditions(),
|
||||
$this->getSorting(),
|
||||
$this->getLimit()
|
||||
];
|
||||
return implode('', $query);
|
||||
}
|
||||
|
||||
protected function addCondition(string $condition): Delete
|
||||
{
|
||||
if (!isset($this->conditions)) {
|
||||
$this->conditions = [];
|
||||
}
|
||||
$this->conditions []= $condition;
|
||||
return $this;
|
||||
}
|
||||
protected function getConditions(): string
|
||||
{
|
||||
return ' WHERE ' . implode(' AND ', $this->conditions);
|
||||
}
|
||||
protected function addOrder(string $order): Delete
|
||||
{
|
||||
if (!isset($this->sorts)) {
|
||||
$this->sorts = [];
|
||||
}
|
||||
$this->sorts []= $order;
|
||||
return $this;
|
||||
}
|
||||
protected function getSorting(): string
|
||||
{
|
||||
if (!isset($this->sorts) or count($this->sorts) === 0) {
|
||||
return '';
|
||||
}
|
||||
return ' ORDER BY ' . implode(', ', $this->sorts);
|
||||
}
|
||||
protected function getLimit(): string
|
||||
{
|
||||
if (!isset($this->limit) or $this->limit <= 0) {
|
||||
return '';
|
||||
}
|
||||
return " LIMIT {$this->limit}";
|
||||
}
|
||||
}
|
87
app/common/Implement/Database/Query/Insert.php
Normal file
87
app/common/Implement/Database/Query/Insert.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Database\Query;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
|
||||
class Insert extends Ideal\Query implements Define\Query\Insert
|
||||
{
|
||||
protected string $table;
|
||||
protected array $columns;
|
||||
protected array $values;
|
||||
protected Define\Query\Select $select;
|
||||
|
||||
public function into(string $table, ?array $columns = null): Insert
|
||||
{
|
||||
$this->table = $table;
|
||||
if ($columns !== null) {
|
||||
return $this->columns($columns);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function columns(array $columns): Insert
|
||||
{
|
||||
foreach ($columns as $column) {
|
||||
$this->addColumn($column);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function values(array $values): Insert
|
||||
{
|
||||
foreach ($values as $value) {
|
||||
$this->addValue($value);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function select(Define\Query\Select $select): Insert
|
||||
{
|
||||
$this->select = $select;
|
||||
return $this;
|
||||
}
|
||||
public function build(): string
|
||||
{
|
||||
$query = [
|
||||
"INSERT INTO {$this->table}"
|
||||
];
|
||||
if (isset($this->select)) {
|
||||
$query []= " {$this->select}";
|
||||
return implode('', $query);
|
||||
}
|
||||
$query []= $this->getColumns();
|
||||
$query []= $this->getValues();
|
||||
return implode('', $query);
|
||||
}
|
||||
|
||||
protected function addColumn(string $column): Insert
|
||||
{
|
||||
if (!isset($this->columns)) {
|
||||
$this->columns = [];
|
||||
}
|
||||
$this->columns []= $column;
|
||||
return $this;
|
||||
}
|
||||
protected function addValue(mixed $value): Insert
|
||||
{
|
||||
if (!isset($this->values)) {
|
||||
$this->values = [];
|
||||
}
|
||||
$this->values []= $value;
|
||||
return $this;
|
||||
}
|
||||
protected function getColumns(): string
|
||||
{
|
||||
return ' (' . implode(', ', array_map(function(string $column) {return "`{$column}`";}, $this->columns)) . ')';
|
||||
}
|
||||
protected function getValues(): string
|
||||
{
|
||||
return ' VALUES (' . implode(', ', array_map(function($value) {
|
||||
if ($value === '?') {
|
||||
return $value;
|
||||
}
|
||||
if ($value === (int) $value) {
|
||||
return $value;
|
||||
}
|
||||
return "'{$value}'";
|
||||
}, $this->values)) . ')';
|
||||
}
|
||||
}
|
215
app/common/Implement/Database/Query/Select.php
Normal file
215
app/common/Implement/Database/Query/Select.php
Normal file
@ -0,0 +1,215 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Database\Query;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
|
||||
class Select extends Ideal\Query implements Define\Query\Select
|
||||
{
|
||||
protected array $columns;
|
||||
protected string $table;
|
||||
protected array $joins;
|
||||
protected array $conditions;
|
||||
protected array $groups;
|
||||
protected array $haves;
|
||||
protected array $orders;
|
||||
protected int $limit;
|
||||
protected int $offset;
|
||||
|
||||
public function columns(array|string $expressions): Select
|
||||
{
|
||||
if (is_string($expressions)) {
|
||||
return $this->addColumn($expressions);
|
||||
}
|
||||
foreach ($expressions as $expression) {
|
||||
$this->addColumn($expression);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function from(string $table): Select
|
||||
{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
public function joined(array|string $joins): Select
|
||||
{
|
||||
if (is_string($joins)) {
|
||||
return $this->addJoin($joins);
|
||||
}
|
||||
foreach ($joins as $join) {
|
||||
$this->addJoin($join);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function where(array|string $conditions): Select
|
||||
{
|
||||
if (is_string($conditions)) {
|
||||
return $this->addCondition($conditions);
|
||||
}
|
||||
foreach ($conditions as $condition) {
|
||||
$this->addCondition($condition);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function group(array|string $grouping): Select
|
||||
{
|
||||
if (is_string($grouping)) {
|
||||
return $this->addGroup($grouping);
|
||||
}
|
||||
foreach ($grouping as $group) {
|
||||
$this->addGroup($group);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function having(array|string $conditions): Select
|
||||
{
|
||||
if (is_string($conditions)) {
|
||||
return $this->addCondition($conditions);
|
||||
}
|
||||
foreach ($conditions as $condition) {
|
||||
$this->addCondition($condition);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function order(array|string $sorting): Select
|
||||
{
|
||||
if (is_string($sorting)) {
|
||||
return $this->addOrder($sorting);
|
||||
}
|
||||
foreach ($sorting as $order) {
|
||||
$this->addOrder($order);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function limit(int $limit, ?int $offset = null): Select
|
||||
{
|
||||
$this->limit = $limit;
|
||||
if ($offset !== null) {
|
||||
return $this->offset($offset);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function offset(int $offset): Select
|
||||
{
|
||||
$this->offset = $offset;
|
||||
return $this;
|
||||
}
|
||||
public function build(): string
|
||||
{
|
||||
$query = [
|
||||
"SELECT {$this->getColumns()} FROM {$this->table}",
|
||||
$this->getJoins(),
|
||||
$this->getConditions(),
|
||||
$this->getGroups(),
|
||||
$this->getHaving(),
|
||||
$this->getOrder(),
|
||||
$this->getLimit()
|
||||
];
|
||||
return implode('', $query);
|
||||
}
|
||||
|
||||
protected function addColumn(string $expression): Select
|
||||
{
|
||||
if (!isset($this->columns)) {
|
||||
$this->columns = [];
|
||||
}
|
||||
$this->columns []= $expression;
|
||||
return $this;
|
||||
}
|
||||
protected function addJoin(string $join): Select
|
||||
{
|
||||
if (!isset($this->joins)) {
|
||||
$this->joins = [];
|
||||
}
|
||||
$this->joins []= $join;
|
||||
return $this;
|
||||
}
|
||||
protected function addCondition(string $condition): Select
|
||||
{
|
||||
if (!isset($this->coditions)) {
|
||||
$this->conditions = [];
|
||||
}
|
||||
$this->conditions []= $condition;
|
||||
return $this;
|
||||
}
|
||||
protected function addGroup(string $group): Select
|
||||
{
|
||||
if (!isset($this->groups)) {
|
||||
$this->groups = [];
|
||||
}
|
||||
$this->groups []= $group;
|
||||
return $this;
|
||||
}
|
||||
protected function addHaving(string $having): Select
|
||||
{
|
||||
if (!isset($this->haves)) {
|
||||
$this->haves = [];
|
||||
}
|
||||
$this->haves []= $having;
|
||||
return $this;
|
||||
}
|
||||
protected function addOrder(string $order): Select
|
||||
{
|
||||
if (!isset($this->orders)) {
|
||||
$this->orders = [];
|
||||
}
|
||||
$this->orders []= $order;
|
||||
return $this;
|
||||
}
|
||||
protected function getColumns(): string
|
||||
{
|
||||
if (!isset($this->columns) or count($this->columns) === 0) {
|
||||
return '*';
|
||||
}
|
||||
return implode(', ', $this->columns);
|
||||
}
|
||||
protected function getJoins(): string
|
||||
{
|
||||
if (!isset($this->joins) or count($this->joins) === 0) {
|
||||
return '';
|
||||
}
|
||||
return ' ' . implode(' ', $this->joins);
|
||||
}
|
||||
protected function getConditions(): string
|
||||
{
|
||||
if (!isset($this->conditions) or count($this->conditions) === 0) {
|
||||
return '';
|
||||
}
|
||||
return ' WHERE ' . implode(' AND ', $this->conditions);
|
||||
}
|
||||
protected function getGroups(): string
|
||||
{
|
||||
if (!isset($this->groups) or count($this->groups) === 0) {
|
||||
return '';
|
||||
}
|
||||
return ' GROUP BY ' . implode(', ', $this->groups);
|
||||
}
|
||||
protected function getHaving(): string
|
||||
{
|
||||
if (!isset($this->haves) or count($this->haves) === 0) {
|
||||
return '';
|
||||
}
|
||||
return ' HAVING ' . implode(' AND ', $this->haves);
|
||||
}
|
||||
protected function getOrder(): string
|
||||
{
|
||||
if (!isset($this->orders) or count($this->orders) === 0) {
|
||||
return '';
|
||||
}
|
||||
return ' ORDER BY ' . implode(', ', $this->orders);
|
||||
}
|
||||
protected function getLimit(): string
|
||||
{
|
||||
if (!isset($this->limit) or $this->limit <= 0) {
|
||||
return '';
|
||||
}
|
||||
return " LIMIT {$this->limit}{$this->getOffset()}";
|
||||
}
|
||||
protected function getOffset(): string
|
||||
{
|
||||
if (!isset($this->offset) or $this->offset <= 0) {
|
||||
return '';
|
||||
}
|
||||
return " OFFSET {$this->offset}";
|
||||
}
|
||||
}
|
113
app/common/Implement/Database/Query/Update.php
Normal file
113
app/common/Implement/Database/Query/Update.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Database\Query;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
|
||||
class Update extends Ideal\Query implements Define\Query\Update
|
||||
{
|
||||
protected string $table;
|
||||
protected array $setPairs;
|
||||
protected array $conditions;
|
||||
protected array $orders;
|
||||
protected int $limit;
|
||||
|
||||
public function table(string $table): Define\Query\Update
|
||||
{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
public function set(array|string $column_pairs): Define\Query\Update
|
||||
{
|
||||
if (is_string($column_pairs)) {
|
||||
return $this->addSet($column_pairs);
|
||||
}
|
||||
foreach ($column_pairs as $pair) {
|
||||
$this->addSet($pair);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function where(array|string $conditions): Define\Query\Update
|
||||
{
|
||||
if (is_string($conditions)) {
|
||||
return $this->addCondition($conditions);
|
||||
}
|
||||
foreach ($conditions as $condition) {
|
||||
$this->addCondition($condition);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function order(array|string $ordering): Define\Query\Update
|
||||
{
|
||||
if (is_string($ordering)) {
|
||||
return $this->addOrder($ordering);
|
||||
}
|
||||
foreach ($ordering as $order) {
|
||||
$this->addOrder($order);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
public function limit(int $limit): Define\Query\Update
|
||||
{
|
||||
$this->limit = $limit;
|
||||
return $this;
|
||||
}
|
||||
public function build(): string
|
||||
{
|
||||
$query = [
|
||||
"UPDATE {$this->table}",
|
||||
$this->getSet(),
|
||||
$this->getConditions(),
|
||||
$this->getOrder(),
|
||||
$this->getLimit()
|
||||
];
|
||||
return implode('', $query);
|
||||
}
|
||||
|
||||
protected function addSet(string $pair): Update
|
||||
{
|
||||
if (!isset($this->setPairs)) {
|
||||
$this->setPairs = [];
|
||||
}
|
||||
$this->setPairs []= $pair;
|
||||
return $this;
|
||||
}
|
||||
protected function addCondition(string $condition): Update
|
||||
{
|
||||
if (!isset($this->conditions)) {
|
||||
$this->conditions = [];
|
||||
}
|
||||
$this->conditions []= $condition;
|
||||
return $this;
|
||||
}
|
||||
protected function addOrder(string $order): Update
|
||||
{
|
||||
if (!isset($this->orders)) {
|
||||
$this->orders = [];
|
||||
}
|
||||
$this->orders []= $order;
|
||||
return $this;
|
||||
}
|
||||
protected function getSet(): string
|
||||
{
|
||||
return ' SET ' . implode(', ', $this->setPairs);
|
||||
}
|
||||
protected function getConditions(): string
|
||||
{
|
||||
return ' WHERE ' . implode(' AND ', $this->conditions);
|
||||
}
|
||||
protected function getOrder(): string
|
||||
{
|
||||
if (!isset($this->orders) or count($this->orders) === 0) {
|
||||
return '';
|
||||
}
|
||||
return ' ORDER BY ' . implode(', ', $this->orders);
|
||||
}
|
||||
protected function getLimit(): string
|
||||
{
|
||||
if (!isset($this->limit) or $this->limit <= 0) {
|
||||
return '';
|
||||
}
|
||||
return " LIMIT {$this->limit}";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user