mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-09-02 13:31:38 +08:00
改进join方法和alias方法的解析 改进软删除的base方法
This commit is contained in:
@@ -89,7 +89,7 @@ abstract class Builder
|
||||
|
||||
$result = [];
|
||||
foreach ($data as $key => $val) {
|
||||
$item = $this->parseKey($key);
|
||||
$item = $this->parseKey($key, $options);
|
||||
if (!in_array($key, $fields, true)) {
|
||||
if ($options['strict']) {
|
||||
throw new Exception('fields not exists:[' . $key . ']');
|
||||
@@ -115,9 +115,10 @@ abstract class Builder
|
||||
* 字段名分析
|
||||
* @access protected
|
||||
* @param string $key
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
protected function parseKey($key)
|
||||
protected function parseKey($key, $options = [])
|
||||
{
|
||||
return $key;
|
||||
}
|
||||
@@ -146,10 +147,11 @@ abstract class Builder
|
||||
/**
|
||||
* field分析
|
||||
* @access protected
|
||||
* @param mixed $fields
|
||||
* @param mixed $fields
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
protected function parseField($fields)
|
||||
protected function parseField($fields, $options = [])
|
||||
{
|
||||
if ('*' == $fields || empty($fields)) {
|
||||
$fieldsStr = '*';
|
||||
@@ -158,9 +160,9 @@ abstract class Builder
|
||||
$array = [];
|
||||
foreach ($fields as $key => $field) {
|
||||
if (!is_numeric($key)) {
|
||||
$array[] = $this->parseKey($key) . ' AS ' . $this->parseKey($field);
|
||||
$array[] = $this->parseKey($key, $options) . ' AS ' . $this->parseKey($field, $options);
|
||||
} else {
|
||||
$array[] = $this->parseKey($field);
|
||||
$array[] = $this->parseKey($field, $options);
|
||||
}
|
||||
}
|
||||
$fieldsStr = implode(',', $array);
|
||||
@@ -172,9 +174,10 @@ abstract class Builder
|
||||
* table分析
|
||||
* @access protected
|
||||
* @param mixed $table
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
protected function parseTable($tables)
|
||||
protected function parseTable($tables, $options = [])
|
||||
{
|
||||
if (is_array($tables)) {
|
||||
// 支持别名定义
|
||||
@@ -183,12 +186,16 @@ abstract class Builder
|
||||
$this->parseKey($table) . ' ' . $this->parseKey($alias) :
|
||||
$this->parseKey($alias);
|
||||
}
|
||||
$tables = $array;
|
||||
$tables = implode(',', $array);
|
||||
} elseif (is_string($tables)) {
|
||||
$tables = $this->parseSqlTable($tables);
|
||||
$tables = array_map([$this, 'parseKey'], explode(',', $tables));
|
||||
if (isset($options['alias'][$tables])) {
|
||||
$tables = $this->parseKey($tables) . ' ' . $this->parseKey($options['alias'][$tables]);
|
||||
} else {
|
||||
$tables = $this->parseKey($tables);
|
||||
}
|
||||
}
|
||||
return implode(',', $tables);
|
||||
return $tables;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -263,7 +270,7 @@ abstract class Builder
|
||||
protected function parseWhereItem($field, $val, $rule = '', $options = [], $binds = [], $bindName = null)
|
||||
{
|
||||
// 字段分析
|
||||
$key = $field ? $this->parseKey($field) : '';
|
||||
$key = $field ? $this->parseKey($field, $options) : '';
|
||||
|
||||
// 查询规则和条件
|
||||
if (!is_array($val)) {
|
||||
@@ -425,14 +432,24 @@ abstract class Builder
|
||||
/**
|
||||
* join分析
|
||||
* @access protected
|
||||
* @param mixed $join
|
||||
* @param array $join
|
||||
* @param array $options 查询条件
|
||||
* @return string
|
||||
*/
|
||||
protected function parseJoin($join)
|
||||
protected function parseJoin($join, $options = [])
|
||||
{
|
||||
$joinStr = '';
|
||||
if (!empty($join)) {
|
||||
$joinStr = ' ' . implode(' ', $join) . ' ';
|
||||
foreach ($join as $item) {
|
||||
list($table, $type, $on) = $item;
|
||||
if (!empty($options['alias'])) {
|
||||
foreach ($options['alias'] as $key => $val) {
|
||||
$on = str_replace($key . '.', $this->parseKey($val, $options) . '.', $on);
|
||||
}
|
||||
}
|
||||
$table = $this->parseTable($table, $options);
|
||||
$joinStr .= ' ' . $type . ' JOIN ' . $table . ' ON ' . $on;
|
||||
}
|
||||
}
|
||||
return $joinStr;
|
||||
}
|
||||
@@ -450,13 +467,13 @@ abstract class Builder
|
||||
foreach ($order as $key => $val) {
|
||||
if (is_numeric($key)) {
|
||||
if (false === strpos($val, '(')) {
|
||||
$array[] = $this->parseKey($val);
|
||||
$array[] = $this->parseKey($val, $options);
|
||||
} elseif ('[rand]' == $val) {
|
||||
$array[] = $this->parseRand();
|
||||
}
|
||||
} else {
|
||||
$sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : '';
|
||||
$array[] = $this->parseKey($key) . ' ' . $sort;
|
||||
$array[] = $this->parseKey($key, $options) . ' ' . $sort;
|
||||
}
|
||||
}
|
||||
$order = implode(',', $array);
|
||||
@@ -572,10 +589,10 @@ abstract class Builder
|
||||
$sql = str_replace(
|
||||
['%TABLE%', '%DISTINCT%', '%FIELD%', '%JOIN%', '%WHERE%', '%GROUP%', '%HAVING%', '%ORDER%', '%LIMIT%', '%UNION%', '%LOCK%', '%COMMENT%', '%FORCE%'],
|
||||
[
|
||||
$this->parseTable($options['table']),
|
||||
$this->parseTable($options['table'], $options),
|
||||
$this->parseDistinct($options['distinct']),
|
||||
$this->parseField($options['field']),
|
||||
$this->parseJoin($options['join']),
|
||||
$this->parseField($options['field'], $options),
|
||||
$this->parseJoin($options['join'], $options),
|
||||
$this->parseWhere($options['where'], $options),
|
||||
$this->parseGroup($options['group']),
|
||||
$this->parseHaving($options['having']),
|
||||
@@ -611,7 +628,7 @@ abstract class Builder
|
||||
['%INSERT%', '%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
|
||||
[
|
||||
$replace ? 'REPLACE' : 'INSERT',
|
||||
$this->parseTable($options['table']),
|
||||
$this->parseTable($options['table'], $options),
|
||||
implode(' , ', $fields),
|
||||
implode(' , ', $values),
|
||||
$this->parseComment($options['comment']),
|
||||
@@ -657,7 +674,7 @@ abstract class Builder
|
||||
$sql = str_replace(
|
||||
['%TABLE%', '%FIELD%', '%DATA%', '%COMMENT%'],
|
||||
[
|
||||
$this->parseTable($options['table']),
|
||||
$this->parseTable($options['table'], $options),
|
||||
implode(' , ', $fields),
|
||||
implode(' UNION ALL ', $values),
|
||||
$this->parseComment($options['comment']),
|
||||
@@ -681,7 +698,7 @@ abstract class Builder
|
||||
}
|
||||
|
||||
$fields = array_map([$this, 'parseKey'], $fields);
|
||||
$sql = 'INSERT INTO ' . $this->parseTable($table) . ' (' . implode(',', $fields) . ') ' . $this->select($options);
|
||||
$sql = 'INSERT INTO ' . $this->parseTable($table, $options) . ' (' . implode(',', $fields) . ') ' . $this->select($options);
|
||||
return $sql;
|
||||
}
|
||||
|
||||
@@ -694,7 +711,7 @@ abstract class Builder
|
||||
*/
|
||||
public function update($data, $options)
|
||||
{
|
||||
$table = $this->parseTable($options['table']);
|
||||
$table = $this->parseTable($options['table'], $options);
|
||||
$data = $this->parseData($data, $options);
|
||||
if (empty($data)) {
|
||||
return '';
|
||||
@@ -706,9 +723,9 @@ abstract class Builder
|
||||
$sql = str_replace(
|
||||
['%TABLE%', '%SET%', '%JOIN%', '%WHERE%', '%ORDER%', '%LIMIT%', '%LOCK%', '%COMMENT%'],
|
||||
[
|
||||
$this->parseTable($options['table']),
|
||||
$this->parseTable($options['table'], $options),
|
||||
implode(',', $set),
|
||||
$this->parseJoin($options['join']),
|
||||
$this->parseJoin($options['join'], $options),
|
||||
$this->parseWhere($options['where'], $options),
|
||||
$this->parseOrder($options['order']),
|
||||
$this->parseLimit($options['limit']),
|
||||
@@ -730,9 +747,9 @@ abstract class Builder
|
||||
$sql = str_replace(
|
||||
['%TABLE%', '%USING%', '%JOIN%', '%WHERE%', '%ORDER%', '%LIMIT%', '%LOCK%', '%COMMENT%'],
|
||||
[
|
||||
$this->parseTable($options['table']),
|
||||
!empty($options['using']) ? ' USING ' . $this->parseTable($options['using']) . ' ' : '',
|
||||
$this->parseJoin($options['join']),
|
||||
$this->parseTable($options['table'], $options),
|
||||
!empty($options['using']) ? ' USING ' . $this->parseTable($options['using'], $options) . ' ' : '',
|
||||
$this->parseJoin($options['join'], $options),
|
||||
$this->parseWhere($options['where'], $options),
|
||||
$this->parseOrder($options['order']),
|
||||
$this->parseLimit($options['limit']),
|
||||
|
||||
Reference in New Issue
Block a user