Files
kiri-databases/SqlBuilder.php
T

334 lines
6.8 KiB
PHP
Raw Normal View History

2022-01-09 03:49:51 +08:00
<?php
namespace Database;
use Database\Traits\Builder;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component;
/**
* Class SqlBuilder
* @package Database
*/
class SqlBuilder extends Component
{
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
use Builder;
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
public ActiveQuery|Query|null $query;
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param ISqlBuilder|null $query
* @return $this
* @throws Exception
*/
public static function builder(ISqlBuilder|null $query): static
{
return new static(['query' => $query]);
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return string
* @throws Exception
*/
public function getCondition(): string
{
return $this->conditionToString();
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param array $compiler
* @return string
* @throws Exception
*/
public function hashCompiler(array $compiler): string
{
return $this->where($compiler);
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param array $attributes
* @return bool|array
* @throws Exception
*/
public function update(array $attributes): bool|array
{
[$string, $array] = $this->builderParams($attributes);
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
return $this->__updateBuilder($string, $array);
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param array $attributes
* @param string $opera
* @return bool|array
* @throws Exception
*/
public function mathematics(array $attributes, string $opera = '+'): bool|array
{
$string = [];
foreach ($attributes as $attribute => $value) {
$string[] = $attribute . '=' . $attribute . $opera . $value;
}
return $this->__updateBuilder($string, []);
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param array $string
* @param array $params
* @return array|bool
* @throws Exception
*/
private function __updateBuilder(array $string, array $params): array|bool
{
if (empty($string)) {
2022-02-23 16:32:08 +08:00
return $this->logger->addError('None data update.');
2022-01-09 03:49:51 +08:00
}
2023-04-01 00:25:22 +08:00
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $this->_prefix();
2022-01-09 03:49:51 +08:00
return [$update, $params];
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param array $attributes
* @param false $isBatch
* @return array
* @throws Exception
*/
public function insert(array $attributes, bool $isBatch = false): array
{
$update = 'INSERT INTO ' . $this->tableName();
if ($isBatch === false) {
$attributes = [$attributes];
}
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
$order = 0;
$keys = $params = [];
foreach ($attributes as $attribute) {
[$_keys, $params] = $this->builderParams($attribute, true, $params, $order);
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
$keys[] = implode(',', $_keys);
$order++;
}
return [$update . '(' . implode('),(', $keys) . ')', $params];
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return string
* @throws Exception
*/
public function delete(): string
{
2023-04-01 00:25:22 +08:00
return 'DELETE FROM ' . $this->tableName() . ' WHERE ' . $this->_prefix();
2022-01-09 03:49:51 +08:00
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param $attributes
* @return array
*/
#[Pure] private function getFields($attributes): array
{
return array_keys(current($attributes));
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param array $attributes
* @param bool $isInsert
* @param array $params
* @param int $order
* @return array[]
* a=:b,
*/
#[Pure] private function builderParams(array $attributes, bool $isInsert = false, array $params = [], int $order = 0): array
{
$keys = [];
foreach ($attributes as $key => $value) {
if ($isInsert === true) {
$keys[] = ':' . $key . $order;
$params[$key . $order] = $value;
} else {
[$keys, $params] = $this->resolveParams($key, $value, $order, $params, $keys);
}
}
return [$keys, $params];
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param string $key
* @param mixed $value
* @param int $order
* @param array $params
* @param array $keys
* @return array
*/
private function resolveParams(string $key, mixed $value, int $order, array $params, array $keys): array
{
2023-02-08 17:33:07 +08:00
if (is_null($value)) {
return [$keys, $params];
}
2022-01-09 03:49:51 +08:00
if (
str_starts_with($value, '+ ') ||
str_starts_with($value, '- ')
) {
$keys[] = $key . '=' . $key . ' ' . $value;
} else {
$params[$key . $order] = $value;
$keys[] = $key . '=:' . $key . $order;
}
return [$keys, $params];
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return string
* @throws Exception
*/
public function one(): string
{
2023-04-01 00:32:04 +08:00
return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query);
2022-01-09 03:49:51 +08:00
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return string
* @throws Exception
*/
public function all(): string
{
2023-04-01 00:32:04 +08:00
return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query);
2022-01-09 03:49:51 +08:00
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return string
* @throws Exception
*/
public function count(): string
{
2023-04-01 00:25:22 +08:00
$this->query->select('COUNT(*)');
2023-04-01 00:32:04 +08:00
return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query);
2022-01-09 03:49:51 +08:00
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param $table
* @return string
*/
public function columns($table): string
{
return 'SHOW FULL FIELDS FROM ' . $table;
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return string
* @throws Exception
*/
2023-04-01 00:25:22 +08:00
private function _prefix(): string
2022-01-09 03:49:51 +08:00
{
$select = '';
2023-04-01 00:25:22 +08:00
if (($condition = $this->conditionToString()) != '') {
2023-04-01 00:27:08 +08:00
$select .= " WHERE $condition";
2022-01-09 03:49:51 +08:00
}
2023-04-01 00:25:22 +08:00
if ($this->query->group != "") {
$select .= ' GROUP BY ' . $this->query->group;
2022-01-09 03:49:51 +08:00
}
2023-04-01 00:29:28 +08:00
if (count($this->query->order) > 0) {
2023-04-01 00:25:22 +08:00
$select .= ' ORDER BY ' . implode(',', $this->query->order);
2022-01-09 03:49:51 +08:00
}
2023-04-01 00:32:04 +08:00
return $select;
2022-01-09 03:49:51 +08:00
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return string
* @throws Exception
*/
2023-04-01 00:25:22 +08:00
private function _selectPrefix(): string
2022-01-09 03:49:51 +08:00
{
2023-04-01 00:25:22 +08:00
if (count($this->query->select) < 1) {
$this->query->select = ['*'];
2022-01-09 03:49:51 +08:00
}
2023-04-01 00:25:22 +08:00
$select = "SELECT " . implode(',', $this->query->select) . " FROM " . $this->tableName();
if ($this->query->alias != "") {
$select .= " AS " . $this->query->alias;
2022-01-09 03:49:51 +08:00
}
2023-04-01 00:25:22 +08:00
if (count($this->query->join) > 0) {
$select .= ' ' . implode(' ', $this->query->join);
2022-01-09 03:49:51 +08:00
}
return $select;
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param false $isCount
* @return string
* @throws Exception
*/
public function get(bool $isCount = false): string
{
if ($isCount === false) {
return $this->all();
}
return $this->count();
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return string
* @throws Exception
*/
public function truncate(): string
{
return sprintf('TRUNCATE %s', $this->tableName());
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return string
* @throws Exception
*/
private function conditionToString(): string
{
return $this->where($this->query->where);
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return string
* @throws Exception
*/
public function tableName(): string
{
2023-04-07 18:23:27 +08:00
if ($this->query->from === null) {
return $this->query->modelClass->getTable();
}
2022-01-09 03:49:51 +08:00
if ($this->query->from instanceof \Closure) {
2023-04-07 18:23:27 +08:00
return $this->query->from = '(' . $this->query->makeClosureFunction($this->query->from) . ')';
2022-01-09 03:49:51 +08:00
}
if ($this->query->from instanceof ActiveQuery) {
2023-04-07 18:23:27 +08:00
return $this->query->from = '(' . SqlBuilder::builder($this->query->from)->get($this->query->from) . ')';
2023-04-01 00:25:22 +08:00
} else {
return $this->query->from;
2022-01-09 03:49:51 +08:00
}
}
2023-04-01 00:25:22 +08:00
2022-01-09 03:49:51 +08:00
}