改名
This commit is contained in:
+24
-72
@@ -4,10 +4,13 @@ declare(strict_types=1);
|
||||
namespace Database\Orm;
|
||||
|
||||
|
||||
|
||||
use Database\Connection;
|
||||
use Database\Mysql\Schema;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Snowflake\Abstracts\BaseObject;
|
||||
use Database\ActiveQuery;
|
||||
use Exception;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Change
|
||||
@@ -51,20 +54,23 @@ class Change extends BaseObject
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @param Schema $db
|
||||
* @param array $attributes
|
||||
* @param $condition
|
||||
* @return array|string
|
||||
* @throws
|
||||
* @throws Exception
|
||||
*/
|
||||
public function batchUpdate(string $table, array $attributes, $condition): array|string
|
||||
public function batchUpdate(string $table, Schema $db, array $attributes, $condition): array|string
|
||||
{
|
||||
$param = [];
|
||||
$_attributes = [];
|
||||
|
||||
$format = $db->getColumns()->table($table);
|
||||
foreach ($attributes as $key => $val) {
|
||||
if ($val === null) {
|
||||
continue;
|
||||
}
|
||||
$_attributes[':' . $key] = $this->valueEncode($val, true);
|
||||
$_attributes[':' . $key] = $format->fieldFormat($key, $val);
|
||||
$param[] = $key . '=:' . $key;
|
||||
}
|
||||
if (empty($param)) {
|
||||
@@ -81,10 +87,10 @@ class Change extends BaseObject
|
||||
* @param $table
|
||||
* @param $params
|
||||
* @param $condition
|
||||
* @return string
|
||||
* @return string|bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function mathematics($table, $params, $condition): string
|
||||
public function mathematics($table, $params, $condition): string|bool
|
||||
{
|
||||
$_tmp = $newParam = [];
|
||||
if (isset($params['incr']) && is_array($params['incr'])) {
|
||||
@@ -94,7 +100,7 @@ class Change extends BaseObject
|
||||
$_tmp = $this->assemble($params['decr'], ' - ', $_tmp);
|
||||
}
|
||||
if (empty($_tmp)) {
|
||||
throw new Exception('Not has IncrBy or DecrBy values.');
|
||||
return $this->addError('Not has IncrBy or DecrBy values.');
|
||||
}
|
||||
$_tmp = implode(',', $_tmp);
|
||||
if (!empty($condition)) {
|
||||
@@ -112,40 +118,12 @@ class Change extends BaseObject
|
||||
*/
|
||||
private function assemble($params, $op, array $_tmp): array
|
||||
{
|
||||
$message = 'Incr And Decr action. The value must a numeric.';
|
||||
foreach ($params as $key => $val) {
|
||||
$_tmp[] = $key . '=' . $key . $op . $val;
|
||||
if (!is_numeric($val)) {
|
||||
throw new Exception($message);
|
||||
}
|
||||
$_tmp[] = sprintf('%s=%s%s%d', $key, $key, $op, $val);
|
||||
}
|
||||
|
||||
return $_tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
* @param array $params
|
||||
* @return string
|
||||
*/
|
||||
public function insertOrUpdateByDUPLICATE($table, array $params): string
|
||||
{
|
||||
$keys = implode(',', array_keys($params));
|
||||
|
||||
$onValues = [];
|
||||
$values = array_values($params);
|
||||
foreach ($values as $key => $val) {
|
||||
$onValues[] = $this->valueEncode($val, true);
|
||||
}
|
||||
|
||||
$onUpdates = [];
|
||||
foreach ($params as $key => $val) {
|
||||
$onUpdates[] = $key . '=' . $this->valueEncode($val, true);
|
||||
}
|
||||
$newSql = $this->inserts($table, $keys, '(' . implode(',', $onValues) . ')');
|
||||
|
||||
return $newSql . ' ON DUPLICATE KEY UPDATE ' . implode(',', $onUpdates);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
@@ -171,24 +149,29 @@ class Change extends BaseObject
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
* @param $attributes
|
||||
* @param Schema $db
|
||||
* @param array|NULL $params
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function batchInsert($table, $attributes, array $params = NULL): array
|
||||
public function batchInsert($table, Schema $db, array $params = NULL): array
|
||||
{
|
||||
if (empty($params)) {
|
||||
throw new Exception("save data param not find.");
|
||||
}
|
||||
$insert = $insertData = [];
|
||||
|
||||
$format = $db->getColumns()->table($table);
|
||||
|
||||
$attributes = array_keys(current($params));
|
||||
|
||||
foreach ($params as $key => $val) {
|
||||
if (!is_array($val)) {
|
||||
continue;
|
||||
}
|
||||
array_push($insert, '(:' . implode($key . ',:', $attributes) . $key . ')');
|
||||
foreach ($attributes as $myVal) {
|
||||
$insertData[':' . $myVal . $key] = $this->valueEncode($val[$myVal], true);
|
||||
$insertData[':' . $myVal . $key] = $format->fieldFormat($myVal, $val[$myVal]);
|
||||
}
|
||||
}
|
||||
if (empty($insertData) || empty($insert)) {
|
||||
@@ -206,40 +189,9 @@ class Change extends BaseObject
|
||||
* @return string
|
||||
* 构建SQL语句
|
||||
*/
|
||||
private function inserts($table, $fields, $data): string
|
||||
#[Pure] private function inserts($table, $fields, $data): string
|
||||
{
|
||||
$query = [
|
||||
'INSERT IGNORE INTO', '%s', '(%s)', 'VALUES %s'
|
||||
];
|
||||
$query = implode(' ', $query);
|
||||
|
||||
return sprintf($query, $table, $fields, $data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
* @param $attributes
|
||||
* @param $condition
|
||||
* @return bool|string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function updateAll($table, $attributes, $condition): bool|string
|
||||
{
|
||||
$param = [];
|
||||
foreach ($attributes as $key => $val) {
|
||||
if ($val === null || $val === '') {
|
||||
continue;
|
||||
}
|
||||
$param[] = $key . '=' . $this->valueEncode($val);
|
||||
}
|
||||
if (empty($param)) return true;
|
||||
|
||||
$param = implode(',', $param);
|
||||
if (!empty($condition)) {
|
||||
$param .= $this->builderWhere($condition);
|
||||
}
|
||||
return 'UPDATE ' . $table . ' SET ' . $param;
|
||||
return sprintf('INSERT IGNORE INTO' . ' %s (%s) VALUES %s', $table, $fields, $data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+87
-59
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
namespace Database\Orm;
|
||||
|
||||
|
||||
use Database\Condition\OrCondition;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionException;
|
||||
use Snowflake\Core\Json;
|
||||
use Snowflake\Core\Str;
|
||||
@@ -61,7 +63,7 @@ trait Condition
|
||||
* @param $join
|
||||
* @return string
|
||||
*/
|
||||
private function builderJoin($join): string
|
||||
#[Pure] private function builderJoin($join): string
|
||||
{
|
||||
if (!empty($join)) {
|
||||
return ' ' . implode(' ', $join);
|
||||
@@ -76,77 +78,107 @@ trait Condition
|
||||
*/
|
||||
private function builderWhere($where): string
|
||||
{
|
||||
if (empty($where)) {
|
||||
return '';
|
||||
}
|
||||
if (is_string($where)) {
|
||||
return sprintf(' WHERE %s', $where);
|
||||
}
|
||||
$_tmp = [];
|
||||
$where = array_filter($where);
|
||||
foreach ($where as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$value = $this->arrayMap($value);
|
||||
} else if (!is_numeric($key)) {
|
||||
$value = $key . '=' . $this->valueEncode($value);
|
||||
}
|
||||
if ($value === null) {
|
||||
continue;
|
||||
}
|
||||
if (empty($where)) return '';
|
||||
foreach ($where as $value) {
|
||||
$_value = is_string($value) ? $value : $this->conditionMap($value);
|
||||
|
||||
if (empty($_value)) continue;
|
||||
|
||||
$_tmp[] = $value;
|
||||
}
|
||||
|
||||
if (!empty($_tmp)) {
|
||||
return ' WHERE ' . implode(' AND ', $_tmp);
|
||||
return sprintf(' WHERE %s', implode(' AND ', $_tmp));
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
* @param $condition
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function arrayMap($value): mixed
|
||||
private function conditionMap($condition): string
|
||||
{
|
||||
$classMap = ConditionClassMap::$conditionMap;
|
||||
if (isset($value[0])) {
|
||||
if (!isset($classMap[strtoupper($value[0])])) {
|
||||
return $value[0];
|
||||
$array = [];
|
||||
if (is_string($condition) || empty($condition)) {
|
||||
return $condition;
|
||||
}
|
||||
foreach ($condition as $key => $value) {
|
||||
if (empty($value)) continue;
|
||||
if (!is_numeric($key)) {
|
||||
$array[] = sprintf('%s=%s', $key, $value);
|
||||
} else if (is_array($value)) {
|
||||
$array = $this->_arrayMap($value, $array);
|
||||
} else {
|
||||
$array[] = sprintf('%s', $value);
|
||||
}
|
||||
$value[0] = strtoupper($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;
|
||||
return implode(' AND ', $array);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return mixed
|
||||
* @param $condition
|
||||
* @param $array
|
||||
* @return array
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function classMap($value): mixed
|
||||
private function _arrayMap($condition, $array): mixed
|
||||
{
|
||||
[$option['opera'], $option['column'], $option['value']] = $value;
|
||||
|
||||
$class = ConditionClassMap::$conditionMap[strtoupper($option['opera'])];
|
||||
if (!is_array($class)) {
|
||||
$class = ['class' => $class];
|
||||
if (!isset($condition[0])) {
|
||||
return $this->_arrayHash($array, $condition);
|
||||
}
|
||||
$option = array_merge($option, $class);
|
||||
|
||||
/** @var Condition $class */
|
||||
return Snowflake::createObject($option)->builder();
|
||||
$stroppier = strtoupper($condition[0]);
|
||||
if (str_contains($stroppier, 'OR')) {
|
||||
if (!is_string($condition[2])) {
|
||||
$condition[2] = $this->_hashMap($condition[2]);
|
||||
}
|
||||
return Snowflake::createObject(['class' => OrCondition::class, 'value' => $condition[2], 'column' => $condition[1], 'oldParams' => $array]);
|
||||
}
|
||||
if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
|
||||
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
|
||||
$create = array_merge($defaultConfig, ['column' => $condition[1], 'value' => $condition[2]]);
|
||||
$array[] = Snowflake::createObject($create);
|
||||
} else {
|
||||
$array[] = Snowflake::createObject(['class' => HashCondition::class, 'value' => $condition]);
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $array
|
||||
* @param $condition
|
||||
* @return mixed
|
||||
*/
|
||||
private function _arrayHash($array, $condition): array
|
||||
{
|
||||
$array[] = implode(' AND ', $this->_hashMap($condition));
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
private function _hashMap($condition): array
|
||||
{
|
||||
$_array = [];
|
||||
foreach ($condition as $key => $value) {
|
||||
if (!is_numeric($key)) {
|
||||
$_array[] = sprintf('%s=%s', $key, $value);
|
||||
} else {
|
||||
$_array[] = $value;
|
||||
}
|
||||
}
|
||||
return $_array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $group
|
||||
* @return string
|
||||
@@ -163,7 +195,7 @@ trait Condition
|
||||
* @param $order
|
||||
* @return string
|
||||
*/
|
||||
private function builderOrder($order): string
|
||||
#[Pure] private function builderOrder($order): string
|
||||
{
|
||||
if (!empty($order)) {
|
||||
return ' ORDER BY ' . implode(',', $order);
|
||||
@@ -176,19 +208,15 @@ trait Condition
|
||||
* @param ActiveQuery $query
|
||||
* @return string
|
||||
*/
|
||||
private function builderLimit(ActiveQuery $query): string
|
||||
#[Pure] private function builderLimit(ActiveQuery $query): string
|
||||
{
|
||||
$limit = $query->limit;
|
||||
if (!is_numeric($limit) || $limit < 1) {
|
||||
if (!is_numeric($query->limit) || $query->limit < 1) {
|
||||
return "";
|
||||
}
|
||||
$offset = $query->offset;
|
||||
|
||||
if ($offset === null) {
|
||||
return ' LIMIT ' . $limit;
|
||||
if ($query->offset !== null) {
|
||||
return ' LIMIT ' . $query->offset . ',' . $query->limit;
|
||||
}
|
||||
|
||||
return ' LIMIT ' . $offset . ',' . $limit;
|
||||
return ' LIMIT ' . $query->limit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user