改名
This commit is contained in:
@@ -0,0 +1,276 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Database\Orm;
|
||||
|
||||
|
||||
use Snowflake\Abstracts\BaseObject;
|
||||
use Database\ActiveQuery;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class Change
|
||||
* @package Yoc\db
|
||||
*/
|
||||
class Change extends BaseObject
|
||||
{
|
||||
|
||||
use Condition;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $model
|
||||
* @param $attributes
|
||||
* @param $condition
|
||||
* @param $params
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update($model, $attributes, $condition, &$params)
|
||||
{
|
||||
if (empty($params)) {
|
||||
throw new Exception("Not has update values.");
|
||||
}
|
||||
$_tmp = [];
|
||||
foreach ($attributes as $val) {
|
||||
if (!isset($params[$val])) {
|
||||
continue;
|
||||
}
|
||||
$_tmp[] = $val . '=:' . $val;
|
||||
}
|
||||
if (empty($_tmp)) {
|
||||
return '';
|
||||
}
|
||||
$where = implode(',', $_tmp);
|
||||
if (!empty($condition)) {
|
||||
$where .= $this->builderWhere($condition);
|
||||
}
|
||||
return "UPDATE " . $model . ' SET ' . $where;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @param array $attributes
|
||||
* @param $condition
|
||||
* @return array|string
|
||||
* @throws
|
||||
*/
|
||||
public function batchUpdate(string $table, array $attributes, $condition)
|
||||
{
|
||||
$param = [];
|
||||
$_attributes = [];
|
||||
foreach ($attributes as $key => $val) {
|
||||
if ($val === null) {
|
||||
continue;
|
||||
}
|
||||
$_attributes[':' . $key] = $this->valueEncode($val, true);
|
||||
$param[] = $key . '=:' . $key;
|
||||
}
|
||||
if (empty($param)) {
|
||||
return '';
|
||||
}
|
||||
$param = implode(',', $param);
|
||||
if (!empty($condition)) {
|
||||
$param .= $condition;
|
||||
}
|
||||
return ['UPDATE ' . $table . ' SET ' . $param, $_attributes];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
* @param $params
|
||||
* @param $condition
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function mathematics($table, $params, $condition)
|
||||
{
|
||||
$_tmp = $newParam = [];
|
||||
if (isset($params['incr']) && is_array($params['incr'])) {
|
||||
$_tmp = $this->assemble($params['incr'], ' + ', $_tmp);
|
||||
}
|
||||
if (isset($params['decr']) && is_array($params['decr'])) {
|
||||
$_tmp = $this->assemble($params['decr'], ' - ', $_tmp);
|
||||
}
|
||||
if (empty($_tmp)) {
|
||||
throw new Exception('Not has IncrBy or DecrBy values.');
|
||||
}
|
||||
$_tmp = implode(',', $_tmp);
|
||||
if (!empty($condition)) {
|
||||
$_tmp .= $this->builderWhere($condition);
|
||||
}
|
||||
return 'UPDATE ' . $table . ' SET ' . $_tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $params
|
||||
* @param $op
|
||||
* @param array $_tmp
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
private function assemble($params, $op, $_tmp)
|
||||
{
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
return $_tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
* @param array $params
|
||||
* @return string
|
||||
*/
|
||||
public function insertOrUpdateByDUPLICATE($table, array $params)
|
||||
{
|
||||
$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
|
||||
* @param $attributes
|
||||
* @param array|null $params
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function insert($table, $attributes, array $params = NULL)
|
||||
{
|
||||
$sql = $this->inserts($table, implode(',', $attributes), '(:' . implode(',:', $attributes) . ')');
|
||||
if (empty($params)) {
|
||||
throw new Exception("save data param not find.");
|
||||
}
|
||||
foreach ($params as $key => $val) {
|
||||
if (strpos($sql, ':' . $key) === FALSE) {
|
||||
throw new Exception("save $key data param not find.");
|
||||
}
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
* @param $attributes
|
||||
* @param array|NULL $params
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function batchInsert($table, $attributes, array $params = NULL)
|
||||
{
|
||||
if (empty($params)) {
|
||||
throw new Exception("save data param not find.");
|
||||
}
|
||||
$insert = $insertData = [];
|
||||
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);
|
||||
}
|
||||
}
|
||||
if (empty($insertData) || empty($insert)) {
|
||||
throw new Exception("save data is empty.");
|
||||
}
|
||||
$sql = $this->inserts($table, implode(',', $attributes), implode(',', $insert));
|
||||
return [$sql, $insertData];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
* @param $fields
|
||||
* @param $data
|
||||
* @return string
|
||||
* 构建SQL语句
|
||||
*/
|
||||
private function inserts($table, $fields, $data)
|
||||
{
|
||||
$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)
|
||||
{
|
||||
$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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ActiveQuery $query
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete($query)
|
||||
{
|
||||
if (empty($query->from)) {
|
||||
$query->from = $query->getTable();
|
||||
}
|
||||
|
||||
$condition = $this->builderWhere($query->where);
|
||||
if (empty($condition) && !$query->ifNotWhere) {
|
||||
throw new Exception('clear data must has condition.');
|
||||
}
|
||||
$query = $this->builderFrom($query->from) . $condition;
|
||||
|
||||
return 'DELETE ' . $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $tableName
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
public function truncate($tableName)
|
||||
{
|
||||
return 'TRUNCATE ' . $tableName;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Database\Orm;
|
||||
|
||||
use Snowflake\Core\JSON;
|
||||
use Snowflake\Core\Str;
|
||||
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
|
||||
*/
|
||||
public function getWhere($query)
|
||||
{
|
||||
return $this->builderWhere($query);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $alias
|
||||
* @return string
|
||||
*/
|
||||
private function builderAlias($alias)
|
||||
{
|
||||
return " AS " . $alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $table
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function builderFrom($table)
|
||||
{
|
||||
if ($table instanceof ActiveQuery) {
|
||||
$table = '(' . $table->getBuild()->getQuery($table) . ')';
|
||||
}
|
||||
return " FROM " . $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $join
|
||||
* @return string
|
||||
*/
|
||||
private function builderJoin($join)
|
||||
{
|
||||
if (!empty($join)) {
|
||||
return ' ' . implode(' ', $join);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $where
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* ['id=1', 'a'=>2, ['in', 'a', 'b']]
|
||||
*/
|
||||
private function builderWhere($where)
|
||||
{
|
||||
if (empty($where)) {
|
||||
return '';
|
||||
}
|
||||
if (is_string($where)) {
|
||||
return sprintf(' WHERE %s', $where);
|
||||
}
|
||||
|
||||
$_tmp = [];
|
||||
foreach ($where as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
$value = $this->arrayMap($value);
|
||||
} else if (!is_numeric($key)) {
|
||||
$value = $key . '=' . $this->valueEncode($value);
|
||||
}
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
$_tmp[] = $value;
|
||||
}
|
||||
|
||||
if (!empty($_tmp)) {
|
||||
return ' WHERE ' . implode(' AND ', $_tmp);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @return mixed|object|string|null
|
||||
* @throws Exception
|
||||
*/
|
||||
private function arrayMap($value)
|
||||
{
|
||||
$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
|
||||
* @return mixed|object
|
||||
* @throws Exception
|
||||
*/
|
||||
private function classMap($value)
|
||||
{
|
||||
[$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
|
||||
*/
|
||||
private function builderGroup($group)
|
||||
{
|
||||
if (empty($group)) {
|
||||
return '';
|
||||
}
|
||||
return ' GROUP BY ' . $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $order
|
||||
* @return string
|
||||
*/
|
||||
private function builderOrder($order)
|
||||
{
|
||||
if (!empty($order)) {
|
||||
return ' ORDER BY ' . implode(',', $order);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param QueryTrait $query
|
||||
* @return string
|
||||
*/
|
||||
private function builderLimit($query)
|
||||
{
|
||||
$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
|
||||
*/
|
||||
public function valueEncode($value, $isSearch = false)
|
||||
{
|
||||
if ($isSearch) {
|
||||
return $value;
|
||||
}
|
||||
if (is_numeric($value)) {
|
||||
return $value;
|
||||
} else {
|
||||
if (!is_null(JSON::decode($value))) {
|
||||
return $value;
|
||||
}
|
||||
return '\'' . Str::encode($value) . '\'';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Database\Orm;
|
||||
|
||||
|
||||
use Snowflake\Abstracts\BaseObject;
|
||||
use Database\ActiveQuery;
|
||||
use Database\Sql;
|
||||
use Database\Db;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class Select
|
||||
* @package Database\Orm
|
||||
*/
|
||||
class Select extends BaseObject
|
||||
{
|
||||
|
||||
use Condition;
|
||||
|
||||
/**
|
||||
* @param ActiveQuery|Db|Sql $query
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getQuery($query)
|
||||
{
|
||||
return $this->generate($query, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ActiveQuery|Db $query
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function count($query)
|
||||
{
|
||||
return $this->generate($query, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ActiveQuery|Db|Sql $query
|
||||
* @param $isCount
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function generate($query, $isCount = false)
|
||||
{
|
||||
if (empty($query->from)) {
|
||||
$query->from = $query->getTable();
|
||||
}
|
||||
$builder = array_filter([
|
||||
$this->builderSelect($query->select, $isCount),
|
||||
$this->builderFrom($query->from),
|
||||
$this->builderAlias($query->alias),
|
||||
$this->builderJoin($query->join),
|
||||
$this->builderWhere($query->where),
|
||||
$this->builderGroup($query->group)
|
||||
], function ($value) {
|
||||
return !empty($value);
|
||||
});
|
||||
if ($isCount) {
|
||||
return implode('', $builder);
|
||||
}
|
||||
|
||||
$order = $this->builderOrder($query->order);
|
||||
if (!empty($order)) {
|
||||
$builder[] = $order;
|
||||
}
|
||||
$builder[] = $this->builderLimit($query);
|
||||
|
||||
return implode('', $builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $select
|
||||
* @param bool $isCount
|
||||
* @return string
|
||||
*/
|
||||
private function builderSelect($select = NULL, $isCount = false)
|
||||
{
|
||||
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
|
||||
* @return string
|
||||
*/
|
||||
public function getColumn($table)
|
||||
{
|
||||
return 'SHOW FULL FIELDS FROM ' . $table;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user