改名
This commit is contained in:
+28
-107
@@ -13,6 +13,7 @@ use Snowflake\Abstracts\Component;
|
||||
use Database\Orm\Select;
|
||||
use Database\Traits\QueryTrait;
|
||||
use Exception;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
@@ -45,32 +46,26 @@ class ActiveQuery extends Component
|
||||
public array $attributes = [];
|
||||
|
||||
|
||||
private SqlBuilder $builder;
|
||||
|
||||
|
||||
/**
|
||||
* Comply constructor.
|
||||
* @param $model
|
||||
* @param array $config
|
||||
* @throws
|
||||
*/
|
||||
public function __construct($model = null, $config = [])
|
||||
public function __construct($model, $config = [])
|
||||
{
|
||||
if (empty($model)) {
|
||||
return parent::__construct($config);
|
||||
}
|
||||
if (!is_object($model)) {
|
||||
$model = Snowflake::createObject($model);
|
||||
}
|
||||
$this->modelClass = $model;
|
||||
return parent::__construct($config);
|
||||
|
||||
$this->builder = SqlBuilder::builder($this);
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function afterInit()
|
||||
{
|
||||
$this->debug(get_called_class() . ' AFTER INIT.');
|
||||
$this->clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除不完整数据
|
||||
@@ -133,16 +128,24 @@ class ActiveQuery extends Component
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $sql
|
||||
* @return mixed
|
||||
*/
|
||||
public function execute($sql): Command
|
||||
{
|
||||
return $this->modelClass::getDb()->createCommand($sql);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return ActiveRecord|array|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function first(): ActiveRecord|array|null
|
||||
{
|
||||
$data = $this->modelClass::getDb()
|
||||
->createCommand($this->oneLimit()->queryBuilder())
|
||||
->one();
|
||||
|
||||
$data = $this->execute($this->builder->one())->one();
|
||||
if (empty($data)) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -168,8 +171,7 @@ class ActiveQuery extends Component
|
||||
*/
|
||||
public function flush(): array|bool|int|string|null
|
||||
{
|
||||
$sql = $this->getChange()->truncate($this->getTable());
|
||||
return $this->command($sql)->flush();
|
||||
return $this->execute($this->builder->truncate())->exec();
|
||||
}
|
||||
|
||||
|
||||
@@ -206,7 +208,7 @@ class ActiveQuery extends Component
|
||||
*/
|
||||
public function all(): Collection|array
|
||||
{
|
||||
$data = $this->modelClass::getDb()->createCommand($this->queryBuilder())->all();
|
||||
$data = $this->execute($this->builder->all())->all();
|
||||
$collect = new Collection($this, $data, $this->modelClass);
|
||||
if ($this->asArray) {
|
||||
return $collect->toArray();
|
||||
@@ -251,9 +253,7 @@ class ActiveQuery extends Component
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
$data = $this->modelClass::getDb()
|
||||
->createCommand($this->getBuild()->count($this))
|
||||
->one();
|
||||
$data = $this->execute($this->builder->count())->one();
|
||||
if ($data && is_array($data)) {
|
||||
return (int)array_shift($data);
|
||||
}
|
||||
@@ -268,8 +268,7 @@ class ActiveQuery extends Component
|
||||
*/
|
||||
public function batchUpdate(array $data): Command|array|bool|int|string
|
||||
{
|
||||
return $this->getDb()->createCommand()
|
||||
->batchUpdate($this->getTable(), $data, $this->getCondition())
|
||||
return $this->execute($this->builder->update($data))
|
||||
->exec();
|
||||
}
|
||||
|
||||
@@ -280,8 +279,7 @@ class ActiveQuery extends Component
|
||||
*/
|
||||
public function batchInsert(array $data): bool
|
||||
{
|
||||
return $this->getDb()->createCommand()
|
||||
->batchInsert($this->getTable(), $data)
|
||||
return $this->execute($this->builder->insert($data, true))
|
||||
->exec();
|
||||
}
|
||||
|
||||
@@ -293,8 +291,7 @@ class ActiveQuery extends Component
|
||||
*/
|
||||
public function value($filed)
|
||||
{
|
||||
$first = $this->first()->toArray();
|
||||
return $first[$filed] ?? null;
|
||||
return $this->first()[$filed] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -303,10 +300,7 @@ class ActiveQuery extends Component
|
||||
*/
|
||||
public function exists(): bool
|
||||
{
|
||||
$column = $this->modelClass::getDb()
|
||||
->createCommand($this->queryBuilder())
|
||||
->fetchColumn();
|
||||
return $column !== false;
|
||||
return $this->execute($this->builder->one())->fetchColumn() !== false;
|
||||
}
|
||||
|
||||
|
||||
@@ -316,79 +310,6 @@ class ActiveQuery extends Component
|
||||
*/
|
||||
public function delete(): bool
|
||||
{
|
||||
return $this->modelClass::getDb()
|
||||
->createCommand($this->queryBuilder())
|
||||
->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getCondition(): string
|
||||
{
|
||||
return $this->getBuild()->getWhere($this->where);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function queryBuilder(): string
|
||||
{
|
||||
return $this->getBuild()->getQuery($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $sql
|
||||
* @param array $attr
|
||||
* @return Command
|
||||
* @throws Exception
|
||||
*/
|
||||
private function command($sql, $attr = []): Command
|
||||
{
|
||||
if (!empty($attr) && is_array($attr)) {
|
||||
$attr = array_merge($this->attributes, $attr);
|
||||
} else {
|
||||
$attr = $this->attributes;
|
||||
}
|
||||
return $this->getDb()->createCommand($sql, $attr)
|
||||
->setModelName($this->modelClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Select
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getBuild(): Select
|
||||
{
|
||||
return $this->getDb()->getSchema()->getQueryBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return orm\Change
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getChange(): orm\Change
|
||||
{
|
||||
return $this->getDb()->getSchema()->getChange();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Connection
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getDb(): Connection
|
||||
{
|
||||
return $this->modelClass::getDb();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getPrimary(): mixed
|
||||
{
|
||||
return $this->modelClass->getPrimary();
|
||||
return $this->execute($this->builder->all())->delete();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,11 @@ namespace Database;
|
||||
use Annotation\Model\Get;
|
||||
use Exception;
|
||||
use Database\Base\BaseActiveRecord;
|
||||
use ReflectionException;
|
||||
use Snowflake\Core\ArrayAccess;
|
||||
use Snowflake\Error\Logger;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Coroutine;
|
||||
|
||||
@@ -113,12 +115,15 @@ class ActiveRecord extends BaseActiveRecord
|
||||
/**
|
||||
* @param array $condition
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
* @return bool|ActiveRecord
|
||||
* @throws ComponentException
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function findOrCreate(array $condition, array $attributes = []): mixed
|
||||
public static function findOrCreate(array $condition, array $attributes = []): bool|static
|
||||
{
|
||||
/** @var static $select */
|
||||
$select = static::find()->where($condition)->first();
|
||||
if (!empty($select)) {
|
||||
return $select;
|
||||
@@ -134,7 +139,7 @@ class ActiveRecord extends BaseActiveRecord
|
||||
|
||||
$select->attributes = $attributes;
|
||||
if (!$select->save()) {
|
||||
throw new Exception($select->getLastError());
|
||||
return \logger()->addError($select->getLastError(), 'mysql');
|
||||
}
|
||||
return $select;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ use Annotation\Event;
|
||||
use Annotation\Inject;
|
||||
use Annotation\Model\Get;
|
||||
use ArrayAccess;
|
||||
use Database\SqlBuilder;
|
||||
use HttpServer\Http\Context;
|
||||
use ReflectionException;
|
||||
use Snowflake\Abstracts\Component;
|
||||
@@ -405,14 +406,13 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
return FALSE;
|
||||
}
|
||||
$dbConnection = static::getDb();
|
||||
$change = $dbConnection->getSchema()->getChange();
|
||||
$sqlBuilder = $change->insert(static::getTable(), $attributes, $param);
|
||||
|
||||
[$sql, $param] = SqlBuilder::builder(static::find())->insert($param);
|
||||
|
||||
$trance = $dbConnection->beginTransaction();
|
||||
try {
|
||||
$commandExec = $dbConnection->createCommand($sqlBuilder, $param);
|
||||
if (!($lastId = (int)$commandExec->save(true, $this))) {
|
||||
throw new Exception('保存失败.' . $sqlBuilder);
|
||||
if (!($lastId = (int)$dbConnection->createCommand($sql, $param)->save(true, $this))) {
|
||||
throw new Exception('保存失败.' . $sql);
|
||||
}
|
||||
$trance->commit();
|
||||
$lastId = $this->setPrimary($lastId, $param);
|
||||
@@ -452,33 +452,36 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
|
||||
|
||||
/**
|
||||
* @param $param
|
||||
* @param $fields
|
||||
* @param $condition
|
||||
* @param $attributes
|
||||
* @param $param
|
||||
* @return $this|bool
|
||||
* @throws Exception
|
||||
*/
|
||||
private function update($attributes, $condition, $param): bool|static
|
||||
private function update($fields, $condition, $param): bool|static
|
||||
{
|
||||
if (empty($param)) {
|
||||
return true;
|
||||
}
|
||||
$command = static::getDb();
|
||||
$change = $command->getSchema()->getChange();
|
||||
$sql = $change->update(static::getTable(), $attributes, $condition, $param);
|
||||
|
||||
if ($this->hasPrimary()) {
|
||||
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
|
||||
}
|
||||
|
||||
[$sql, $param] = SqlBuilder::builder(static::find()->where($condition))->update($param);
|
||||
|
||||
$trance = $command->beginTransaction();
|
||||
if (!$command->createCommand($sql, $param)->save(false, $this)) {
|
||||
$trance->rollback();
|
||||
$result = false;
|
||||
} else {
|
||||
$trance->commit();
|
||||
|
||||
$this->event->dispatch(self::AFTER_SAVE, [$attributes, $param]);
|
||||
|
||||
$result = true;
|
||||
return false;
|
||||
}
|
||||
return $result;
|
||||
|
||||
$trance->commit();
|
||||
|
||||
$this->event->dispatch(self::AFTER_SAVE, [$fields, $param]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -488,9 +491,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
*/
|
||||
public function save($data = NULL): static|bool
|
||||
{
|
||||
if (is_array($data)) {
|
||||
$this->_attributes = array_merge($this->_attributes, $data);
|
||||
if (!is_null($data)) {
|
||||
$this->attributes = $data;
|
||||
}
|
||||
|
||||
if (!$this->validator($this->rules())) {
|
||||
return false;
|
||||
}
|
||||
@@ -500,15 +504,12 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
}
|
||||
|
||||
static::getDb()->enablingTransactions();
|
||||
[$attributes, $condition, $param] = $this->filtration_and_separation();
|
||||
if (($primary = $this->getPrimary()) !== null) {
|
||||
$condition = [[$primary => $this->getPrimaryValue()]];
|
||||
}
|
||||
[$change, $condition, $fields] = $this->filtration_and_separation();
|
||||
|
||||
if (!$this->isNewExample) {
|
||||
return $this->update($param, $condition, $attributes);
|
||||
return $this->update($fields, $condition, $change);
|
||||
}
|
||||
return $this->insert($attributes, $param);
|
||||
return $this->insert($change, $fields);
|
||||
}
|
||||
|
||||
|
||||
@@ -574,8 +575,8 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
|
||||
$condition = [];
|
||||
$columns = static::getColumns();
|
||||
foreach ($this->_attributes as $key => $val) {
|
||||
if ($val === NULL) continue;
|
||||
$oldValue = $this->_oldAttributes[$key] ?? null;
|
||||
|
||||
if ($val !== $oldValue) {
|
||||
$_tmp[$key] = $columns->fieldFormat($key, $val);
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,232 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Database;
|
||||
|
||||
|
||||
use Database\Base\ConditionClassMap;
|
||||
use Database\Condition\HashCondition;
|
||||
use Database\Condition\OrCondition;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
trait Builder
|
||||
{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param $alias
|
||||
* @return string
|
||||
*/
|
||||
private function builderAlias($alias): string
|
||||
{
|
||||
return " AS " . $alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function builderFrom($table): string
|
||||
{
|
||||
if ($table instanceof ActiveQuery) {
|
||||
$table = '(' . $table->getBuild()->getQuery($table) . ')';
|
||||
}
|
||||
return " FROM " . $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $join
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] private function builderJoin($join): string
|
||||
{
|
||||
if (!empty($join)) {
|
||||
return ' ' . implode(' ', $join);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param null $select
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] private function builderSelect($select = NULL): string
|
||||
{
|
||||
if (empty($select)) {
|
||||
return "SELECT *";
|
||||
}
|
||||
if (is_array($select)) {
|
||||
return "SELECT " . implode(',', $select);
|
||||
} else {
|
||||
return "SELECT " . $select;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $group
|
||||
* @return string
|
||||
*/
|
||||
private function builderGroup($group): string
|
||||
{
|
||||
if (empty($group)) {
|
||||
return '';
|
||||
}
|
||||
return ' GROUP BY ' . $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $order
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] private function builderOrder($order): string
|
||||
{
|
||||
if (!empty($order)) {
|
||||
return ' ORDER BY ' . implode(',', $order);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ActiveQuery $query
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] private function builderLimit(ActiveQuery $query): string
|
||||
{
|
||||
if (!is_numeric($query->limit) || $query->limit < 1) {
|
||||
return "";
|
||||
}
|
||||
if ($query->offset !== null) {
|
||||
return ' LIMIT ' . $query->offset . ',' . $query->limit;
|
||||
}
|
||||
return ' LIMIT ' . $query->limit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param $where
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function where($where): string
|
||||
{
|
||||
$_tmp = [];
|
||||
if (empty($where)) return '';
|
||||
if (is_string($where)) return $where;
|
||||
foreach ($where as $key => $value) {
|
||||
$_value = is_string($value) ? $value : $this->conditionMap($value);
|
||||
|
||||
if (empty($_value)) continue;
|
||||
|
||||
$_tmp[] = $_value;
|
||||
}
|
||||
if (!empty($_tmp)) {
|
||||
return sprintf(' WHERE %s', implode(' AND ', $_tmp));
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $condition
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function conditionMap($condition): string
|
||||
{
|
||||
$array = [];
|
||||
if (is_string($condition) || empty($condition)) {
|
||||
return $condition;
|
||||
}
|
||||
|
||||
foreach ($condition as $key => $value) {
|
||||
$array = $this->resolve($array, $key, $value);
|
||||
}
|
||||
if (is_array($array)) {
|
||||
return implode(' AND ', $array);
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $array
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return mixed
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function resolve($array, $key, $value): mixed
|
||||
{
|
||||
if (empty($value)) return $array;
|
||||
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);
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $condition
|
||||
* @param $array
|
||||
* @return string
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
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]);
|
||||
}
|
||||
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 implode(' AND ', $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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+7
-12
@@ -165,20 +165,15 @@ class Collection extends AbstractCollection
|
||||
public function delete(): bool
|
||||
{
|
||||
$model = $this->getModel();
|
||||
if (!$model->hasPrimary()) {
|
||||
return false;
|
||||
}
|
||||
if (!$model->hasPrimary()) return false;
|
||||
$ids = [];
|
||||
foreach ($this as $item) {
|
||||
$ids[] = $item->getPrimaryValue();
|
||||
$id = $item->getPrimaryValue();
|
||||
if (!empty($id)) {
|
||||
$ids[] = $id;
|
||||
}
|
||||
}
|
||||
$ids = array_filter($ids);
|
||||
if (empty($ids)) {
|
||||
return false;
|
||||
}
|
||||
return $model::find()
|
||||
->in($model->getPrimary(), $ids)
|
||||
->delete();
|
||||
return $model::find()->in($model->getPrimary(), $ids)->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -240,7 +235,7 @@ class Collection extends AbstractCollection
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty(): bool
|
||||
#[Pure] public function isEmpty(): bool
|
||||
{
|
||||
return $this->size() < 1;
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ class Change extends BaseObject
|
||||
}
|
||||
$where = implode(',', $_tmp);
|
||||
if (!empty($condition)) {
|
||||
$where .= $this->builderWhere($condition);
|
||||
$where .= $this->where($condition);
|
||||
}
|
||||
return "UPDATE " . $model . ' SET ' . $where;
|
||||
}
|
||||
@@ -104,7 +104,7 @@ class Change extends BaseObject
|
||||
}
|
||||
$_tmp = implode(',', $_tmp);
|
||||
if (!empty($condition)) {
|
||||
$_tmp .= $this->builderWhere($condition);
|
||||
$_tmp .= $this->where($condition);
|
||||
}
|
||||
return 'UPDATE ' . $table . ' SET ' . $_tmp;
|
||||
}
|
||||
@@ -206,7 +206,7 @@ class Change extends BaseObject
|
||||
$query->from = $query->getTable();
|
||||
}
|
||||
|
||||
$condition = $this->builderWhere($query->where);
|
||||
$condition = $this->where($query->where);
|
||||
if (empty($condition) && !$query->ifNotWhere) {
|
||||
throw new Exception('clear data must has condition.');
|
||||
}
|
||||
|
||||
+5
-203
@@ -4,16 +4,7 @@ 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;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Database\ActiveQuery;
|
||||
use Database\Base\ConditionClassMap;
|
||||
use Database\Condition\HashCondition;
|
||||
use Database\Builder;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
@@ -23,6 +14,9 @@ use Exception;
|
||||
trait Condition
|
||||
{
|
||||
|
||||
|
||||
use Builder;
|
||||
|
||||
/**
|
||||
* @param $query
|
||||
* @return string
|
||||
@@ -30,198 +24,6 @@ trait Condition
|
||||
*/
|
||||
public function getWhere($query): string
|
||||
{
|
||||
return $this->builderWhere($query);
|
||||
return $this->where($query);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $alias
|
||||
* @return string
|
||||
*/
|
||||
private function builderAlias($alias): string
|
||||
{
|
||||
return " AS " . $alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function builderFrom($table): string
|
||||
{
|
||||
if ($table instanceof ActiveQuery) {
|
||||
$table = '(' . $table->getBuild()->getQuery($table) . ')';
|
||||
}
|
||||
return " FROM " . $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $join
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] private function builderJoin($join): string
|
||||
{
|
||||
if (!empty($join)) {
|
||||
return ' ' . implode(' ', $join);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $where
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function builderWhere($where): string
|
||||
{
|
||||
$_tmp = [];
|
||||
if (empty($where)) return '';
|
||||
if (is_string($where)) return $where;
|
||||
foreach ($where as $key => $value) {
|
||||
$_value = is_string($value) ? $value : $this->conditionMap($value);
|
||||
|
||||
if (empty($_value)) continue;
|
||||
|
||||
$_tmp[] = $_value;
|
||||
}
|
||||
if (!empty($_tmp)) {
|
||||
return sprintf(' WHERE %s', implode(' AND ', $_tmp));
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $condition
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function conditionMap($condition): string
|
||||
{
|
||||
$array = [];
|
||||
if (is_string($condition) || empty($condition)) {
|
||||
return $condition;
|
||||
}
|
||||
|
||||
foreach ($condition as $key => $value) {
|
||||
$array = $this->resolve($array, $key, $value);
|
||||
}
|
||||
if (is_array($array)) {
|
||||
return implode(' AND ', $array);
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $array
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return mixed
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function resolve($array, $key, $value): mixed
|
||||
{
|
||||
if (empty($value)) return $array;
|
||||
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);
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $condition
|
||||
* @param $array
|
||||
* @return string
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
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]);
|
||||
}
|
||||
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 implode(' AND ', $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
|
||||
*/
|
||||
private function builderGroup($group): string
|
||||
{
|
||||
if (empty($group)) {
|
||||
return '';
|
||||
}
|
||||
return ' GROUP BY ' . $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $order
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] private function builderOrder($order): string
|
||||
{
|
||||
if (!empty($order)) {
|
||||
return ' ORDER BY ' . implode(',', $order);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ActiveQuery $query
|
||||
* @return string
|
||||
*/
|
||||
#[Pure] private function builderLimit(ActiveQuery $query): string
|
||||
{
|
||||
if (!is_numeric($query->limit) || $query->limit < 1) {
|
||||
return "";
|
||||
}
|
||||
if ($query->offset !== null) {
|
||||
return ' LIMIT ' . $query->offset . ',' . $query->limit;
|
||||
}
|
||||
return ' LIMIT ' . $query->limit;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-20
@@ -56,7 +56,7 @@ class Select extends BaseObject
|
||||
$this->builderFrom($query->from),
|
||||
$this->builderAlias($query->alias),
|
||||
$this->builderJoin($query->join),
|
||||
$this->builderWhere($query->where),
|
||||
$this->where($query->where),
|
||||
$this->builderGroup($query->group)
|
||||
], function ($value) {
|
||||
return !empty($value);
|
||||
@@ -74,25 +74,6 @@ class Select extends BaseObject
|
||||
return implode('', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $select
|
||||
* @param bool $isCount
|
||||
* @return string
|
||||
*/
|
||||
private function builderSelect($select = NULL, $isCount = false): string
|
||||
{
|
||||
if ($isCount === true) {
|
||||
return 'SELECT COUNT(*)';
|
||||
}
|
||||
if (empty($select)) {
|
||||
return "SELECT *";
|
||||
}
|
||||
if (is_array($select)) {
|
||||
return "SELECT " . implode(',', $select);
|
||||
} else {
|
||||
return "SELECT " . $select;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Database;
|
||||
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Snowflake\Abstracts\Component;
|
||||
|
||||
|
||||
/**
|
||||
* Class SqlBuilder
|
||||
* @package Database
|
||||
*/
|
||||
class SqlBuilder extends Component
|
||||
{
|
||||
|
||||
use Builder;
|
||||
|
||||
|
||||
private ActiveQuery $query;
|
||||
|
||||
|
||||
/**
|
||||
* @param $query
|
||||
* @return $this
|
||||
*/
|
||||
public static function builder($query): static
|
||||
{
|
||||
return new static(['query' => $query]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $attributes
|
||||
* @return bool|array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update(array $attributes): bool|array
|
||||
{
|
||||
[$string, $array] = $this->builderParams($attributes);
|
||||
if (empty($string) || empty($array)) {
|
||||
return $this->addError('None data update.');
|
||||
}
|
||||
|
||||
$update = 'UPDATE ' . $this->tableName() . ' SET ' . $string . $this->conditionToString();
|
||||
$update .= $this->builderLimit($this->query);
|
||||
|
||||
return [$update, $array];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $attributes
|
||||
* @param false $isBatch
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function insert(array $attributes, $isBatch = false): array
|
||||
{
|
||||
$update = sprintf('INSERT INTO %s', $this->tableName());
|
||||
if ($isBatch === true) {
|
||||
$attributes = [$attributes];
|
||||
}
|
||||
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
|
||||
|
||||
$order = 0;
|
||||
$keys = $params = [];
|
||||
foreach ($attributes as $attribute) {
|
||||
[$_keys, $params] = $this->builderParams($attribute, true, $params, $order);
|
||||
|
||||
$keys[] = implode(',', $_keys);
|
||||
$order++;
|
||||
}
|
||||
return [$update . '(' . implode('),(', $keys) . ')', $params];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $attributes
|
||||
* @return array
|
||||
*/
|
||||
#[Pure] private function getFields($attributes): array
|
||||
{
|
||||
return array_keys(current($attributes));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $attributes
|
||||
* @param bool $isInsert
|
||||
* @param array $params
|
||||
* @param int $order
|
||||
* @return array[]
|
||||
* a=:b,
|
||||
*/
|
||||
private function builderParams(array $attributes, bool $isInsert = false, $params = [], $order = 0): array
|
||||
{
|
||||
$keys = [];
|
||||
foreach ($attributes as $key => $value) {
|
||||
if ($isInsert === true) {
|
||||
$keys[] = ':' . $key . $order;
|
||||
} else {
|
||||
$keys[] = $key . '=:' . $key . $order;
|
||||
}
|
||||
$params[$key . $order] = $value;
|
||||
}
|
||||
return [$keys, $params];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function one(): string
|
||||
{
|
||||
return $this->_prefix(true) . ' LIMIT 0,1';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function all(): string
|
||||
{
|
||||
return $this->_prefix(true) . $this->builderLimit($this->query);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function count(): string
|
||||
{
|
||||
return $this->_prefix();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param bool $hasOrder
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function _prefix($hasOrder = false): string
|
||||
{
|
||||
$select = $this->builderSelect($this->query->select);
|
||||
if (is_array($select)) {
|
||||
$select = implode(',', $select);
|
||||
}
|
||||
$select = 'SELECT ' . $select . ' FROM ' . $this->tableName();
|
||||
if (!empty($condition = $this->conditionToString())) {
|
||||
$select = sprintf('%s %s', $select, $condition);
|
||||
}
|
||||
if (!empty($this->query->group)) {
|
||||
$select .= ' GROUP BY ' . $this->query->group;
|
||||
}
|
||||
if ($hasOrder === true && !empty($this->query->order)) {
|
||||
$select .= $this->builderOrder($this->query->order);
|
||||
}
|
||||
return $select;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function truncate(): string
|
||||
{
|
||||
return sprintf('TRUNCATE %s', $this->tableName());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function conditionToString(): string
|
||||
{
|
||||
return $this->where($this->query->where);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function tableName(): string
|
||||
{
|
||||
return $this->query->modelClass::getTable();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user