Files
kiri-core/Database/Orm/Condition.php
T

216 lines
3.9 KiB
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 12:38:32 +08:00
namespace Database\Orm;
2020-12-17 14:12:44 +08:00
2020-12-17 14:09:14 +08:00
use ReflectionException;
2020-08-31 12:38:32 +08:00
use Snowflake\Core\JSON;
use Snowflake\Core\Str;
2020-12-17 14:09:14 +08:00
use Snowflake\Exception\NotFindClassException;
2020-08-31 12:38:32 +08:00
use Snowflake\Snowflake;
use Database\ActiveQuery;
use Database\Base\ConditionClassMap;
use Database\Condition\HashCondition;
use Database\Sql;
use Database\Traits\QueryTrait;
use Database\Condition\Condition as CCondition;
use Exception;
/**
* Trait Condition
* @package Database\Orm
*/
trait Condition
{
/**
* @param $query
* @return string
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function getWhere($query): string
2020-08-31 12:38:32 +08:00
{
return $this->builderWhere($query);
}
/**
* @param $alias
* @return string
*/
2020-12-17 14:09:14 +08:00
private function builderAlias($alias): string
2020-08-31 12:38:32 +08:00
{
return " AS " . $alias;
}
/**
* @param $table
* @return string
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
private function builderFrom($table): string
2020-08-31 12:38:32 +08:00
{
if ($table instanceof ActiveQuery) {
$table = '(' . $table->getBuild()->getQuery($table) . ')';
}
return " FROM " . $table;
}
/**
* @param $join
* @return string
*/
2020-12-17 14:12:44 +08:00
private function builderJoin($join): string
2020-08-31 12:38:32 +08:00
{
if (!empty($join)) {
return ' ' . implode(' ', $join);
}
return '';
}
/**
* @param $where
* @return string
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
private function builderWhere($where): string
2020-08-31 12:38:32 +08:00
{
if (empty($where)) {
return '';
}
if (is_string($where)) {
return sprintf(' WHERE %s', $where);
}
$_tmp = [];
2020-11-27 18:44:59 +08:00
$where = array_filter($where);
2020-08-31 12:38:32 +08:00
foreach ($where as $key => $value) {
if (is_array($value)) {
$value = $this->arrayMap($value);
} else if (!is_numeric($key)) {
$value = $key . '=' . $this->valueEncode($value);
}
2020-11-27 18:44:59 +08:00
if ($value === null) {
2020-08-31 12:38:32 +08:00
continue;
}
$_tmp[] = $value;
}
if (!empty($_tmp)) {
return ' WHERE ' . implode(' AND ', $_tmp);
}
return '';
}
/**
* @param $value
2020-12-17 14:09:14 +08:00
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
2020-08-31 12:38:32 +08:00
*/
2020-12-17 14:09:14 +08:00
private function arrayMap($value): mixed
2020-08-31 12:38:32 +08:00
{
$classMap = ConditionClassMap::$conditionMap;
if (isset($value[0])) {
$value[0] = strtoupper($value[0]);
if (!isset($classMap[$value[0]])) {
return $value[0];
}
$result = $this->classMap($value);
} else {
/** @var HashCondition $condition */
$condition = Snowflake::createObject(HashCondition::class);
$condition->setValue($value);
$condition->setOpera('=');
$result = $condition->builder();
}
return $result;
}
/**
* @param $value
2020-12-17 14:09:14 +08:00
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
2020-08-31 12:38:32 +08:00
*/
2020-12-17 14:09:14 +08:00
private function classMap($value): mixed
2020-08-31 12:38:32 +08:00
{
[$option['opera'], $option['column'], $option['value']] = $value;
$class = ConditionClassMap::$conditionMap[strtoupper($option['opera'])];
if (!is_array($class)) {
$class = ['class' => $class];
}
$option = array_merge($option, $class);
/** @var Condition $class */
return Snowflake::createObject($option)->builder();
}
/**
* @param $group
* @return string
*/
2020-12-17 14:09:14 +08:00
private function builderGroup($group): string
2020-08-31 12:38:32 +08:00
{
if (empty($group)) {
return '';
}
return ' GROUP BY ' . $group;
}
/**
* @param $order
* @return string
*/
2020-12-17 14:09:14 +08:00
private function builderOrder($order): string
2020-08-31 12:38:32 +08:00
{
if (!empty($order)) {
return ' ORDER BY ' . implode(',', $order);
} else {
return '';
}
}
/**
2020-11-18 15:21:07 +08:00
* @param ActiveQuery $query
2020-08-31 12:38:32 +08:00
* @return string
*/
2020-12-17 14:09:14 +08:00
private function builderLimit(ActiveQuery $query): string
2020-08-31 12:38:32 +08:00
{
$limit = $query->limit;
if (!is_numeric($limit) || $limit < 1) {
return "";
}
$offset = $query->offset;
if ($offset === null) {
return ' LIMIT ' . $limit;
}
return ' LIMIT ' . $offset . ',' . $limit;
}
/**
* @param $value
* @param bool $isSearch
* @return int|string
*/
2020-12-17 14:09:14 +08:00
public function valueEncode($value, $isSearch = false): int|string
2020-08-31 12:38:32 +08:00
{
if ($isSearch) {
return $value;
}
if (is_numeric($value)) {
return $value;
} else {
if (!is_null(JSON::decode($value))) {
return $value;
}
return '\'' . Str::encode($value) . '\'';
}
}
}