This commit is contained in:
2021-03-04 15:55:17 +08:00
parent 66c7d39a5a
commit e8e5641f41
+173 -157
View File
@@ -19,177 +19,193 @@ trait Builder
{ {
/** /**
* @param $alias * @param $alias
* @return string * @return string
*/ */
private function builderAlias($alias): string private function builderAlias($alias): string
{ {
return " AS " . $alias; return " AS " . $alias;
} }
/** /**
* @param $table * @param $table
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function builderFrom($table): string private function builderFrom($table): string
{ {
if ($table instanceof ActiveQuery) { if ($table instanceof ActiveQuery) {
$table = '(' . SqlBuilder::builder($table)->get($table) . ')'; $table = '(' . SqlBuilder::builder($table)->get($table) . ')';
} }
return " FROM " . $table; return " FROM " . $table;
} }
/** /**
* @param $join * @param $join
* @return string * @return string
*/ */
#[Pure] private function builderJoin($join): string #[Pure] private function builderJoin($join): string
{ {
if (!empty($join)) { if (!empty($join)) {
return ' ' . implode(' ', $join); return ' ' . implode(' ', $join);
} }
return ''; return '';
} }
/** /**
* @param null $select * @param null $select
* @return string * @return string
*/ */
#[Pure] private function builderSelect($select = NULL): string #[Pure] private function builderSelect($select = NULL): string
{ {
if (empty($select)) { if (empty($select)) {
return "SELECT *"; return "SELECT *";
} }
if (is_array($select)) { if (is_array($select)) {
return "SELECT " . implode(',', $select); return "SELECT " . implode(',', $select);
} else { } else {
return "SELECT " . $select; return "SELECT " . $select;
} }
} }
/** /**
* @param $group * @param $group
* @return string * @return string
*/ */
private function builderGroup($group): string private function builderGroup($group): string
{ {
if (empty($group)) { if (empty($group)) {
return ''; return '';
} }
return ' GROUP BY ' . $group; return ' GROUP BY ' . $group;
} }
/** /**
* @param $order * @param $order
* @return string * @return string
*/ */
#[Pure] private function builderOrder($order): string #[Pure] private function builderOrder($order): string
{ {
if (!empty($order)) { if (!empty($order)) {
return ' ORDER BY ' . implode(',', $order); return ' ORDER BY ' . implode(',', $order);
} else { } else {
return ''; return '';
} }
} }
/** /**
* @param ActiveQuery $query * @param ActiveQuery $query
* @return string * @return string
*/ */
#[Pure] private function builderLimit(ActiveQuery $query): string #[Pure] private function builderLimit(ActiveQuery $query): string
{ {
if (!is_numeric($query->limit) || $query->limit < 1) { if (!is_numeric($query->limit) || $query->limit < 1) {
return ""; return "";
} }
if ($query->offset !== null) { if ($query->offset !== null) {
return ' LIMIT ' . $query->offset . ',' . $query->limit; return ' LIMIT ' . $query->offset . ',' . $query->limit;
} }
return ' LIMIT ' . $query->limit; return ' LIMIT ' . $query->limit;
} }
/** /**
* @param $where * @param $where
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function where($where): string private function where($where): string
{ {
$_tmp = []; $_tmp = [];
if (empty($where)) return ''; if (empty($where)) return '';
if (is_string($where)) return $where; if (is_string($where)) return $where;
foreach ($where as $key => $value) { foreach ($where as $key => $value) {
if (is_string($key)) { $_value = $this->resolveCondition($key, $value, $_tmp);
$_value = sprintf('%s=\'%s\'', $key, $value);
} else if (is_string($value)) { if (empty($_value)) continue;
$_value = $value; $_tmp[] = $_value;
} else { }
$_value = $this->_arrayMap($value, $_tmp); if (!empty($_tmp)) {
} return sprintf(' WHERE %s', implode(' AND ', $_tmp));
if (empty($_value)) continue; }
$_tmp[] = $_value; return '';
} }
if (!empty($_tmp)) {
return sprintf(' WHERE %s', implode(' AND ', $_tmp));
}
return '';
}
/** /**
* @param $condition * @param $field
* @param $array * @param $condition
* @return string * @param $_tmp
* @throws NotFindClassException * @return string
* @throws ReflectionException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
private function _arrayMap($condition, $array): string private function resolveCondition($field, $condition, $_tmp): string
{ {
if (!isset($condition[0])) { if (is_string($field)) {
return implode(' AND ', $this->_hashMap($condition)); $_value = sprintf('%s=\'%s\'', $field, $condition);
} } else if (is_string($condition)) {
$stroppier = strtoupper($condition[0]); $_value = $condition;
if (str_contains($stroppier, 'OR')) { } else {
if (!is_string($condition[2])) { $_value = $this->_arrayMap($condition, $_tmp);
$condition[2] = $this->_hashMap($condition[2]); }
} return $_value;
$builder = Snowflake::createObject(['class' => OrCondition::class, 'value' => $condition[2], 'column' => $condition[1], 'oldParams' => $array]); }
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
$create = array_merge($defaultConfig, ['column' => $condition[1], 'value' => $condition[2]]);
$builder = Snowflake::createObject($create);
} else {
$builder = Snowflake::createObject(['class' => HashCondition::class, 'value' => $condition]);
}
$array[] = $builder->builder();
return implode(' AND ', $array);
}
/** /**
* @param $condition * @param $condition
* @return array * @param $array
*/ * @return string
private function _hashMap($condition): array * @throws NotFindClassException
{ * @throws ReflectionException
$_array = []; * @throws ReflectionException
foreach ($condition as $key => $value) { */
$value = is_numeric($value) ? $value : '\'' . $value . '\''; private function _arrayMap($condition, $array): string
if (!is_numeric($key)) { {
$_array[] = sprintf('%s=%s', $key, $value); if (!isset($condition[0])) {
} else { return implode(' AND ', $this->_hashMap($condition));
$_array[] = $value; }
} $stroppier = strtoupper($condition[0]);
} if (str_contains($stroppier, 'OR')) {
return $_array; if (!is_string($condition[2])) {
} $condition[2] = $this->_hashMap($condition[2]);
}
$builder = Snowflake::createObject(['class' => OrCondition::class, 'value' => $condition[2], 'column' => $condition[1], 'oldParams' => $array]);
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
$create = array_merge($defaultConfig, ['column' => $condition[1], 'value' => $condition[2]]);
$builder = Snowflake::createObject($create);
} else {
$builder = Snowflake::createObject(['class' => HashCondition::class, 'value' => $condition]);
}
$array[] = $builder->builder();
return implode(' AND ', $array);
}
/**
* @param $condition
* @return array
*/
private function _hashMap($condition): array
{
$_array = [];
foreach ($condition as $key => $value) {
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
if (!is_numeric($key)) {
$_array[] = sprintf('%s=%s', $key, $value);
} else {
$_array[] = $value;
}
}
return $_array;
}
} }