|
@@ -176,9 +176,10 @@ class Model
|
|
|
|
|
|
public function __call($method,$args)
|
|
|
{
|
|
|
- if(in_array(strtolower($method), ['table','order','where','on','limit','having','group','lock','master','distinct','index','attr','key'],true))
|
|
|
+// Log::record("method={$method}, args={$args[0]}",Log::DEBUG);
|
|
|
+ if(in_array(strtolower($method), ['table','partition','order','where','on','limit','having','group','lock','master','distinct','index','attr','key'],true))
|
|
|
{
|
|
|
- $this->options[strtolower($method)] = $args[0];
|
|
|
+ $this->options[strtolower($method)] = $args[0];
|
|
|
if (strtolower($method) == 'table')
|
|
|
{
|
|
|
if (strpos($args[0],',') !== false)
|
|
@@ -775,7 +776,7 @@ class ModelDb
|
|
|
'not in'=>'NOT IN'];
|
|
|
|
|
|
// 查询表达式
|
|
|
- protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%INDEX%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%';
|
|
|
+ protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE% %PARTITION% %INDEX%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%';
|
|
|
|
|
|
public function select($options= [])
|
|
|
{
|
|
@@ -821,21 +822,25 @@ class ModelDb
|
|
|
|
|
|
public function parseSql($sql,$options= [])
|
|
|
{
|
|
|
- $sql = str_replace(
|
|
|
- ['%TABLE%','%DISTINCT%','%FIELD%','%JOIN%','%WHERE%','%GROUP%','%HAVING%','%ORDER%','%LIMIT%','%UNION%','%INDEX%'],
|
|
|
- [
|
|
|
- $this->parseTable($options),
|
|
|
- $this->parseDistinct(isset($options['distinct']) ? $options['distinct'] : false),
|
|
|
- $this->parseField(isset($options['field']) ? $options['field'] : '*'),
|
|
|
- $this->parseJoin(isset($options['on']) ? $options : []),
|
|
|
- $this->parseWhere(isset($options['where']) ? $options['where'] : ''),
|
|
|
- $this->parseGroup(isset($options['group']) ? $options['group'] : ''),
|
|
|
- $this->parseHaving(isset($options['having']) ? $options['having'] : ''),
|
|
|
- $this->parseOrder(isset($options['order']) ? $options['order'] : ''),
|
|
|
- $this->parseLimit(isset($options['limit']) ? $options['limit'] : ''),
|
|
|
- $this->parseUnion(isset($options['union']) ? $options['union'] : ''),
|
|
|
- $this->parseIndex(isset($options['index']) ? $options['index'] : '')
|
|
|
- ],$sql);
|
|
|
+ //todo $options['on'] 是否正确
|
|
|
+// Log::record("sql={$sql}", Log::DEBUG);
|
|
|
+ $fields = [
|
|
|
+ $this->parseTable($options),
|
|
|
+ $this->parseDistinct($options['distinct'] ?? false),
|
|
|
+ $this->parseField(isset($options['field']) ? $options['field'] : '*'),
|
|
|
+ $this->parseJoin(isset($options['on']) ? $options : []),
|
|
|
+ $this->parseWhere(isset($options['where']) ? $options['where'] : ''),
|
|
|
+ $this->parseGroup(isset($options['group']) ? $options['group'] : ''),
|
|
|
+ $this->parseHaving(isset($options['having']) ? $options['having'] : ''),
|
|
|
+ $this->parseOrder(isset($options['order']) ? $options['order'] : ''),
|
|
|
+ $this->parseLimit(isset($options['limit']) ? $options['limit'] : ''),
|
|
|
+ $this->parseUnion(isset($options['union']) ? $options['union'] : ''),
|
|
|
+ $this->parseIndex(isset($options['index']) ? $options['index'] : ''),
|
|
|
+ $this->parsePartition(isset($options['partition']) ? $options['partition'] : ''),
|
|
|
+ ];
|
|
|
+
|
|
|
+ $sql = str_replace(['%TABLE%', '%DISTINCT%', '%FIELD%', '%JOIN%', '%WHERE%', '%GROUP%', '%HAVING%', '%ORDER%', '%LIMIT%', '%UNION%', '%INDEX%','%PARTITION%'],
|
|
|
+ $fields, $sql);
|
|
|
return $sql;
|
|
|
}
|
|
|
|
|
@@ -852,6 +857,24 @@ class ModelDb
|
|
|
return empty($value) ? '':' USE INDEX ('.$value.') ';
|
|
|
}
|
|
|
|
|
|
+ protected function parsePartition($value)
|
|
|
+ {
|
|
|
+ if(empty($value)) {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ elseif(is_string($value)) {
|
|
|
+ return ' PARTITION (' . $value . ') ';
|
|
|
+ }
|
|
|
+ elseif (is_array($value))
|
|
|
+ {
|
|
|
+ $spart = implode(',', $value);
|
|
|
+ return ' PARTITION (' . $spart . ') ';
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
protected function parseValue($value)
|
|
|
{
|
|
|
//|| is_numeric($value)
|
|
@@ -1130,6 +1153,7 @@ class ModelDb
|
|
|
{
|
|
|
$sql = 'DELETE '.$this->parseAttr($options).' FROM '
|
|
|
.$this->parseTable($options)
|
|
|
+ .$this->parsePartition(isset($options['partition'])?$options['partition']:'')
|
|
|
.$this->parseWhere(isset($options['where'])?$options['where']:'')
|
|
|
.$this->parseOrder(isset($options['order'])?$options['order']:'')
|
|
|
.$this->parseLimit(isset($options['limit'])?$options['limit']:'');
|
|
@@ -1146,6 +1170,7 @@ class ModelDb
|
|
|
$sql = 'UPDATE '
|
|
|
.$this->parseAttr($options)
|
|
|
.$this->parseTable($options)
|
|
|
+ .$this->parsePartition(isset($options['partition'])?$options['partition']:'')
|
|
|
.$this->parseSet($data)
|
|
|
.$this->parseWhere(isset($options['where'])?$options['where']:'')
|
|
|
.$this->parseOrder(isset($options['order'])?$options['order']:'')
|
|
@@ -1289,7 +1314,7 @@ class ModelDb
|
|
|
return $str;
|
|
|
}
|
|
|
|
|
|
- protected function parseKey(&$key) {
|
|
|
+ protected function parseKey($key) {
|
|
|
return $key;
|
|
|
}
|
|
|
|