|
@@ -71,6 +71,20 @@ class Base
|
|
|
public $executeSQL = '';
|
|
|
|
|
|
public $operateVal = array('or', 'and', 'like');//连接条件操作;
|
|
|
+ public $shortExpression = array(
|
|
|
+ 'equal' => "%s`%s` = '%s'",
|
|
|
+ 'in' => "%s`%s` in (%s)",
|
|
|
+ 'unequal' => "%s`%s` != '%s'",
|
|
|
+ 'greater' => "%s`%s` > '%s'",
|
|
|
+ 'gt' => "%s`%s` > '%s'",
|
|
|
+ 'greaterEqual' => "%s`%s` >= '%s'",
|
|
|
+ 'gte' => "%s`%s` >= '%s'",
|
|
|
+ 'less' => "%s`%s` < '%s'",
|
|
|
+ 'lessEqual' => "%s`%s` <= '%s'",
|
|
|
+ 'lte' => "%s`%s` <= '%s'",
|
|
|
+ 'like' => "%s`%s` like '%%%s%%'"
|
|
|
+ );
|
|
|
+
|
|
|
public $operateTable = array('leftJoin', 'rightJoin', 'innerJoin');//链接表的操作
|
|
|
public $faultTolerant = true;
|
|
|
|
|
@@ -346,14 +360,14 @@ class Base
|
|
|
* @param int $limit
|
|
|
* @param int $offset
|
|
|
*/
|
|
|
- final function limit($limit, $offset = 0)
|
|
|
+ final function limit($limit, $offset = null)
|
|
|
{
|
|
|
$this->limit = null;
|
|
|
if($limit === '' || $limit === null) {
|
|
|
throw new InvalidParams(_i('%s is invalid', 'Limit'), __LINE__);
|
|
|
}
|
|
|
if ($limit !== '') {
|
|
|
- if (!$offset) {
|
|
|
+ if ($offset === null) {
|
|
|
$this->limit = sprintf($this->_query["LIMIT"], 0, $limit);
|
|
|
} else {
|
|
|
$this->limit = sprintf($this->_query["LIMIT"], $limit, $offset);
|
|
@@ -462,6 +476,38 @@ class Base
|
|
|
}
|
|
|
return $this->set($set);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理where条件
|
|
|
+ *
|
|
|
+ * @param $where
|
|
|
+ * @return $this|array
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function handleWhereFields($where) {
|
|
|
+ if(!is_array($where)) {
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+ $fields = [];
|
|
|
+ foreach ($where as $key => $val) {
|
|
|
+ $arr = explode(':', $key);
|
|
|
+ $len = count($arr);
|
|
|
+ $maxLen = 1;
|
|
|
+ $opt = '';
|
|
|
+ if(!$this->isExpression($arr[$len - 1])) {
|
|
|
+ $maxLen = 0;
|
|
|
+ }else{
|
|
|
+ $opt = ':' .$arr[$len - 1];
|
|
|
+ }
|
|
|
+ if($len > $maxLen) {
|
|
|
+ $arr1 = array_slice($arr, 0, count($arr) - $maxLen);
|
|
|
+ foreach ($arr1 as $key) {
|
|
|
+ $fields[$key . $opt] = $val;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $fields;
|
|
|
+ }
|
|
|
/**
|
|
|
* where条件
|
|
|
* @param array $where where语句
|
|
@@ -487,6 +533,7 @@ class Base
|
|
|
if(count($where) == 0) {
|
|
|
return $this;
|
|
|
}
|
|
|
+ $where = $this->handleWhereFields($where);
|
|
|
$slices = $this->groupContents($where);
|
|
|
$this->handleCondition($slices, $res);
|
|
|
return $this;
|
|
@@ -494,6 +541,14 @@ class Base
|
|
|
|
|
|
/**
|
|
|
* OR 条件
|
|
|
+ * 用法:
|
|
|
+ * 1:
|
|
|
+ * $this->orTerms(['field1:like|equal....' => val1])->orTerms(....)
|
|
|
+ * $this->orTerms(['field1:field2...:like|equal....' => val1])
|
|
|
+ * == (field1 like|equal.... val1) or (field2 like|equal.... val1) ....
|
|
|
+ * 2:
|
|
|
+ * $this->orTerm(['field1:like|equal...' => val1, 'field2:like|equal...' => val1])
|
|
|
+ * == (field1 like|equal.... val1 AND field2 like|equal.... val1 ....)
|
|
|
* @param $where
|
|
|
* @param string $defaultOperater
|
|
|
* @param null $res
|
|
@@ -515,7 +570,33 @@ class Base
|
|
|
if(count($where) == 0) {
|
|
|
return $this;
|
|
|
}
|
|
|
- $this->handleCondition(array(array($defaultOperater, $where)), $res);
|
|
|
+ $extra = [];
|
|
|
+ foreach ($where as $key => $val) {
|
|
|
+ $fields = explode(':', $key);
|
|
|
+ $len = count($fields);
|
|
|
+ $min = 2;
|
|
|
+ $opt = '';
|
|
|
+ if($this->isExpression($fields[$len - 1])) {
|
|
|
+ $min = 3;
|
|
|
+ $opt = ':'. $fields[$len - 1];
|
|
|
+ }
|
|
|
+ if($len >= $min) {
|
|
|
+ $arr = array_slice($fields, 0, ($opt != '' ? $len - 1 : $len));
|
|
|
+ foreach ($arr as $field) {
|
|
|
+ $extra[$field . $opt] = $val;
|
|
|
+ }
|
|
|
+ unset($where[$key]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(count($where) > 0) {
|
|
|
+ $this->handleCondition(array(array($defaultOperater, $where)), $res);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(count($extra) > 0) {
|
|
|
+ foreach ($extra as $key => $val) {
|
|
|
+ $this->handleCondition(array(array($defaultOperater, [$key => $val])), $res);
|
|
|
+ }
|
|
|
+ }
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
@@ -550,6 +631,7 @@ class Base
|
|
|
$where[$key . ":in"] = $val;
|
|
|
unset($where[$key]);
|
|
|
}
|
|
|
+ $where = $this->handleWhereFields($where);
|
|
|
$this->handleCondition(array(array('and', $where)), $res);
|
|
|
return $this;
|
|
|
}
|
|
@@ -1037,6 +1119,24 @@ class Base
|
|
|
return in_array($val, $this->operateVal) || in_array($val, $this->operateTable);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 判断是否是表达式 equal unequal ...
|
|
|
+ *
|
|
|
+ * @param string $val
|
|
|
+ * @return bool
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ protected function isExpression($val) {
|
|
|
+ if(is_array($val)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if(in_array(gettype($val), array("array", "object", "resource","resource (closed)"))) {
|
|
|
+ throw new \Exception("期待参数为字符串,获取到的为: " . gettype($val) ."(". json_encode($val) . ")");
|
|
|
+ }
|
|
|
+
|
|
|
+ return isset($this->shortExpression[$val]);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 是否是操作表
|
|
|
*
|
|
@@ -1068,16 +1168,7 @@ class Base
|
|
|
$extraParams = array_slice(func_get_args(), 2);
|
|
|
}
|
|
|
$where = array();
|
|
|
- $operator = array('equal' => "%s`%s` = '%s'",
|
|
|
- 'in' => "%s`%s` in (%s)",
|
|
|
- 'unequal' => "%s`%s` != '%s'",
|
|
|
- 'greater' => "%s`%s` > '%s'",
|
|
|
- 'greaterEqual' => "%s`%s` >= '%s'",
|
|
|
- 'gte' => "%s`%s` >= '%s'",
|
|
|
- 'less' => "%s`%s` < '%s'",
|
|
|
- 'lessEqual' => "%s`%s` <= '%s'",
|
|
|
- 'lte' => "%s`%s` <= '%s'",
|
|
|
- 'like' => "%s`%s` like '%%%s%%'");
|
|
|
+ $operator = $this->shortExpression;
|
|
|
$lastIsOperator = false;
|
|
|
$lastIsValue = null;
|
|
|
$i = 0;
|
|
@@ -1255,7 +1346,7 @@ class Base
|
|
|
if(!empty($this->whereCondition))
|
|
|
{
|
|
|
$lastCondition = $this->whereCondition[count($this->whereCondition) - 1];
|
|
|
- if(!$this->isOperator($whereCondition[0]) && !$this->isOperator($lastCondition)) {
|
|
|
+ if(count($whereCondition) > 0 && !$this->isOperator($whereCondition[0]) && !$this->isOperator($lastCondition)) {
|
|
|
$this->whereCondition = array_merge($this->whereCondition, array('and'));
|
|
|
}
|
|
|
$this->whereCondition = array_merge($this->whereCondition, $whereCondition);
|