This commit is contained in:
2023-07-26 11:00:08 +08:00
parent 4abc1a621b
commit 9ab0aa1fe7
+248 -242
View File
@@ -18,298 +18,304 @@ use Kiri\Abstracts\Component;
class SqlBuilder extends Component class SqlBuilder extends Component
{ {
use Builder; use Builder;
public ActiveQuery|Query|null $query; /**
* @var ActiveQuery|Query|ISqlBuilder|null
*/
public ActiveQuery|Query|ISqlBuilder|null $query;
public function __construct(ActiveQuery|Query|null $config) /**
{ * @param ActiveQuery|Query|ISqlBuilder|null $config
parent::__construct(); */
public function __construct(ActiveQuery|Query|null|ISqlBuilder $config)
{
parent::__construct();
$this->query = $config; $this->query = $config;
} }
/** /**
* @param ISqlBuilder|null $query * @param ISqlBuilder|null $query
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
public static function builder(ISqlBuilder|null $query): static public static function builder(ISqlBuilder|null $query): static
{ {
return new static($query); return new static($query);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function getCondition(): string public function getCondition(): string
{ {
return $this->conditionToString(); return $this->conditionToString();
} }
/** /**
* @param array $compiler * @param array $compiler
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function hashCompiler(array $compiler): string public function hashCompiler(array $compiler): string
{ {
return $this->where($compiler); return $this->where($compiler);
} }
/** /**
* @param array $attributes * @param array $attributes
* @return bool|array * @return bool|array
* @throws Exception * @throws Exception
*/ */
public function update(array $attributes): bool|string public function update(array $attributes): bool|string
{ {
return $this->__updateBuilder($this->builderParams($attributes)); return $this->__updateBuilder($this->builderParams($attributes));
} }
/** /**
* @param array $attributes * @param array $attributes
* @param string $opera * @param string $opera
* @return bool|array * @return bool|array
* @throws Exception * @throws Exception
*/ */
public function mathematics(array $attributes, string $opera = '+'): bool|string public function mathematics(array $attributes, string $opera = '+'): bool|string
{ {
$string = []; $string = [];
foreach ($attributes as $attribute => $value) { foreach ($attributes as $attribute => $value) {
$string[] = $attribute . '=' . $attribute . $opera . $value; $string[] = $attribute . '=' . $attribute . $opera . $value;
} }
return $this->__updateBuilder($string); return $this->__updateBuilder($string);
} }
/** /**
* @param array $string * @param array $string
* @return string|bool * @return string|bool
* @throws Exception * @throws Exception
*/ */
private function __updateBuilder(array $string): string|bool private function __updateBuilder(array $string): string|bool
{ {
if (empty($string)) { if (empty($string)) {
return \Kiri::getLogger()->addError('None data update.'); return \Kiri::getLogger()->addError('None data update.');
} }
return 'UPDATE ' . $this->query->from . ' SET ' . implode(',', $string) . $this->_prefix(); return 'UPDATE ' . $this->query->from . ' SET ' . implode(',', $string) . $this->_prefix();
} }
/** /**
* @param array $attributes * @param array $attributes
* @param false $isBatch * @param false $isBatch
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
public function insert(array $attributes, bool $isBatch = false): array public function insert(array $attributes, bool $isBatch = false): array
{ {
$update = 'INSERT INTO ' . $this->query->from; $update = 'INSERT INTO ' . $this->query->from;
if ($isBatch === false) { if ($isBatch === false) {
$attributes = [$attributes]; $attributes = [$attributes];
} }
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES '; $update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
$order = 0; $order = 0;
$keys = []; $keys = [];
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
$_keys = $this->builderParams($attribute, true, $order); $_keys = $this->builderParams($attribute, true, $order);
$keys[] = implode(',', $_keys); $keys[] = implode(',', $_keys);
$order++; $order++;
} }
return [$update . '(' . implode('),(', $keys) . ')', $this->query->attributes]; return [$update . '(' . implode('),(', $keys) . ')', $this->query->attributes];
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function delete(): string public function delete(): string
{ {
return 'DELETE FROM ' . $this->query->from . $this->_prefix(); return 'DELETE FROM ' . $this->query->from . $this->_prefix();
} }
/** /**
* @param $attributes * @param $attributes
* @return array * @return array
*/ */
#[Pure] private function getFields($attributes): array #[Pure] private function getFields($attributes): array
{ {
return array_keys(current($attributes)); return array_keys(current($attributes));
} }
/** /**
* @param array $attributes * @param array $attributes
* @param bool $isInsert * @param bool $isInsert
* @param int $order * @param int $order
* @return array[] * @return array[]
* a=:b, * a=:b,
*/ */
private function builderParams(array $attributes, bool $isInsert = false, int $order = 0): array private function builderParams(array $attributes, bool $isInsert = false, int $order = 0): array
{ {
$keys = []; $keys = [];
foreach ($attributes as $key => $value) { foreach ($attributes as $key => $value) {
if ($isInsert === true) { if ($isInsert === true) {
$keys[] = ':save' . $key . $order; $keys[] = ':save' . $key . $order;
$this->query->bindParam(':save' . $key . $order, $value); $this->query->bindParam(':save' . $key . $order, $value);
} else { } else {
$keys = $this->resolveParams($key, $value, $order, $keys); $keys = $this->resolveParams($key, $value, $order, $keys);
} }
} }
return $keys; return $keys;
} }
/** /**
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param int $order * @param int $order
* @param array $keys * @param array $keys
* @return array * @return array
*/ */
private function resolveParams(string $key, mixed $value, int $order, array $keys): array private function resolveParams(string $key, mixed $value, int $order, array $keys): array
{ {
if (is_null($value)) { if (is_null($value)) {
return $keys; return $keys;
} }
if (is_string($value) && (str_starts_with($value, '+ ') || if (is_string($value) && (str_starts_with($value, '+ ') ||
str_starts_with($value, '- '))) { str_starts_with($value, '- '))) {
$keys[] = $key . '=' . $key . ' ' . $value; $keys[] = $key . '=' . $key . ' ' . $value;
} else { } else {
$this->query->bindParam(':update' . $key . $order, $value); $this->query->bindParam(':update' . $key . $order, $value);
$keys[] = $key . '=:update' . $key . $order; $keys[] = $key . '=:update' . $key . $order;
} }
return $keys; return $keys;
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function one(): string public function one(): string
{ {
if (count($this->query->select) < 1) { if (count($this->query->select) < 1) {
$this->query->select = ['*']; $this->query->select = ['*'];
} }
return $this->_selectPrefix($this->query->select) . $this->_prefix() . $this->builderLimit($this->query); return $this->_selectPrefix($this->query->select) . $this->_prefix() . $this->builderLimit($this->query);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function all(): string public function all(): string
{ {
if (count($this->query->select) < 1) { if (count($this->query->select) < 1) {
$this->query->select = ['*']; $this->query->select = ['*'];
} }
return $this->_selectPrefix($this->query->select) . $this->_prefix() . $this->builderLimit($this->query); return $this->_selectPrefix($this->query->select) . $this->_prefix() . $this->builderLimit($this->query);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function count(): string public function count(): string
{ {
return $this->_selectPrefix(['COUNT(*) as row_count']) . $this->_prefix(); return $this->_selectPrefix(['COUNT(*) as row_count']) . $this->_prefix();
} }
/** /**
* @param $table * @param $table
* @return string * @return string
*/ */
public function columns($table): string public function columns($table): string
{ {
return 'SHOW FULL FIELDS FROM ' . $table; return 'SHOW FULL FIELDS FROM ' . $table;
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function _prefix(): string private function _prefix(): string
{ {
$select = ''; $select = '';
if (($condition = $this->conditionToString()) != '') { if (($condition = $this->conditionToString()) != '') {
$select .= " WHERE $condition"; $select .= " WHERE $condition";
} }
if ($this->query->group != "") { if ($this->query->group != "") {
$select .= ' GROUP BY ' . $this->query->group; $select .= ' GROUP BY ' . $this->query->group;
} }
if (count($this->query->order) > 0) { if (count($this->query->order) > 0) {
$select .= ' ORDER BY ' . implode(',', $this->query->order); $select .= ' ORDER BY ' . implode(',', $this->query->order);
} }
return $select; return $select;
} }
/** /**
* @param array $select * @param array $select
* @return string * @return string
*/ */
private function _selectPrefix(array $select = ['*']): string private function _selectPrefix(array $select = ['*']): string
{ {
$select = "SELECT " . implode(',', $select) . " FROM " . $this->query->from; $select = "SELECT " . implode(',', $select) . " FROM " . $this->query->from;
if ($this->query->alias != "") { if ($this->query->alias != "") {
$select .= " AS " . $this->query->alias; $select .= " AS " . $this->query->alias;
} }
if (count($this->query->join) > 0) { if (count($this->query->join) > 0) {
$select .= ' ' . implode(' ', $this->query->join); $select .= ' ' . implode(' ', $this->query->join);
} }
return $select; return $select;
} }
/** /**
* @param false $isCount * @param false $isCount
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function get(bool $isCount = false): string public function get(bool $isCount = false): string
{ {
if ($isCount === false) { if ($isCount === false) {
return $this->all(); return $this->all();
} }
return $this->count(); return $this->count();
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function truncate(): string public function truncate(): string
{ {
return sprintf('TRUNCATE %s', $this->query->from); return sprintf('TRUNCATE %s', $this->query->from);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function conditionToString(): string private function conditionToString(): string
{ {
return $this->where($this->query->where); return $this->where($this->query->where);
} }
} }