Files
kiri-databases/Traits/Builder.php
T

212 lines
4.2 KiB
PHP
Raw Normal View History

2022-01-09 03:49:51 +08:00
<?php
2023-04-16 01:45:33 +08:00
declare(strict_types=1);
2022-01-09 03:49:51 +08:00
namespace Database\Traits;
use Database\ActiveQuery;
use Database\Base\ConditionClassMap;
use Database\Condition\HashCondition;
use Database\Condition\OrCondition;
use Database\Query;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Exception\NotFindClassException;
2022-01-12 14:10:32 +08:00
use Kiri;
2022-01-09 03:49:51 +08:00
use ReflectionException;
/**
* Trait Builder
* @package Database\Traits
*/
trait Builder
{
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param $alias
* @return string
*/
private function builderAlias($alias): string
{
return " AS " . $alias;
}
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param $table
* @return string
* @throws Exception
*/
private function builderFrom($table): string
{
if ($table instanceof ActiveQuery) {
$table = '(' . $table->toSql() . ')';
}
return " FROM " . $table;
}
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param $join
* @return string
*/
#[Pure] private function builderJoin($join): string
{
if (!empty($join)) {
return ' ' . implode(' ', $join);
}
return '';
}
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
/**
2023-04-01 00:25:22 +08:00
* @param string $select
2022-01-09 03:49:51 +08:00
* @return string
*/
2023-04-01 00:25:22 +08:00
#[Pure] private function builderSelect(string $select = "*"): string
2022-01-09 03:49:51 +08:00
{
2023-04-01 00:25:22 +08:00
return "SELECT " . $select;
2022-01-09 03:49:51 +08:00
}
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
/**
2023-04-10 17:13:24 +08:00
* @param string $group
2022-01-09 03:49:51 +08:00
* @return string
*/
2023-04-10 17:13:24 +08:00
private function builderGroup(string $group): string
2022-01-09 03:49:51 +08:00
{
2023-04-10 17:13:24 +08:00
if ($group != '') {
2022-01-09 03:49:51 +08:00
return '';
}
return ' GROUP BY ' . $group;
}
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param $order
* @return string
*/
#[Pure] private function builderOrder($order): string
{
if (!empty($order)) {
return ' ORDER BY ' . implode(',', $order);
} else {
return '';
}
}
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param ActiveQuery|Query $query
* @return string
*/
2023-04-10 17:26:28 +08:00
#[Pure] private function builderLimit(ActiveQuery|Query $query): string
2022-01-09 03:49:51 +08:00
{
2023-04-25 23:09:16 +08:00
if ($query->limit > 0) {
return ' LIMIT ' . $query->offset . ',' . $query->limit;
} else {
return ' OFFSET ' . $query->offset;
}
2022-01-09 03:49:51 +08:00
}
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
/**
2023-04-07 18:23:27 +08:00
* @param array $where
2022-01-09 03:49:51 +08:00
* @return string
2023-04-07 18:23:27 +08:00
* @throws NotFindClassException
* @throws ReflectionException
2022-01-09 03:49:51 +08:00
*/
2023-04-07 18:23:27 +08:00
private function where(array $where): string
2022-01-09 03:49:51 +08:00
{
2023-04-07 18:23:27 +08:00
if (count($where) < 1) {
return '';
}
2022-01-09 03:49:51 +08:00
$_tmp = [];
foreach ($where as $key => $value) {
2023-04-07 18:23:27 +08:00
$_tmp[] = $this->resolveCondition($key, $value, $_tmp);
2022-01-09 03:49:51 +08:00
}
2023-04-07 18:23:27 +08:00
if (count($_tmp) > 0) {
2022-01-09 03:49:51 +08:00
return implode(' AND ', $_tmp);
2023-04-01 00:25:22 +08:00
} else {
return '';
2022-01-09 03:49:51 +08:00
}
}
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param $field
* @param $condition
* @param $_tmp
* @return string
* @throws NotFindClassException
* @throws ReflectionException|Exception
*/
private function resolveCondition($field, $condition, $_tmp): string
{
if (is_string($field)) {
2023-04-10 17:13:24 +08:00
$this->query->bindParam(':where' . $field, $condition);
return $field . ' = ' . ':where' . $field;
2022-01-09 03:49:51 +08:00
} else if (is_string($condition)) {
2023-04-01 00:25:22 +08:00
return $condition;
2022-01-09 03:49:51 +08:00
} else {
2023-04-01 00:25:22 +08:00
return $this->_arrayMap($condition, $_tmp);
2022-01-09 03:49:51 +08:00
}
}
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param $condition
* @param $array
* @return string
* @throws Exception
*/
private function _arrayMap($condition, $array): string
{
if (!isset($condition[0])) {
return implode(' AND ', $this->_hashMap($condition));
}
$stroppier = strtoupper($condition[0]);
if (str_contains($stroppier, 'OR')) {
if (!is_string($condition[2])) {
$condition[2] = $this->_hashMap($condition[2]);
}
$builder = Kiri::getDi()->get(OrCondition::class);
$builder->setValue($condition[2]);
$builder->setColumn($condition[1]);
$builder->oldParams = $array;
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
$class = $defaultConfig['class'];
unset($defaultConfig['class']);
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
$builder = Kiri::getDi()->make($class, [], $defaultConfig);
$builder->setValue($condition[2]);
$builder->setColumn($condition[1]);
} else {
$builder = Kiri::getDi()->get(HashCondition::class);
$builder->setValue($condition);
}
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
$array[] = $builder->builder();
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
return implode(' AND ', $array);
}
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param $condition
* @return array
*/
private function _hashMap($condition): array
{
$_array = [];
foreach ($condition as $key => $value) {
2023-04-10 17:13:24 +08:00
$this->query->bindParam(':hash' . $key, $value);
$_array[] = $key . '=:hash' . $key;
2022-01-09 03:49:51 +08:00
}
return $_array;
}
2023-04-07 18:23:27 +08:00
2022-01-09 03:49:51 +08:00
}