|
@@ -2,10 +2,13 @@
|
|
namespace Qii\Driver;
|
|
namespace Qii\Driver;
|
|
use controller\error;
|
|
use controller\error;
|
|
use Qii\Autoloader\Psr4;
|
|
use Qii\Autoloader\Psr4;
|
|
|
|
+use Qii\Driver\Trait\Cache;
|
|
|
|
+use Qii\Driver\Trait\Database;
|
|
use Qii\Exceptions\InvalidFormat;
|
|
use Qii\Exceptions\InvalidFormat;
|
|
use Qii\Exceptions\InvalidParams;
|
|
use Qii\Exceptions\InvalidParams;
|
|
use Qii\Exceptions\TableException;
|
|
use Qii\Exceptions\TableException;
|
|
use Qii\Exceptions\Variable;
|
|
use Qii\Exceptions\Variable;
|
|
|
|
+use Qii\Driver\Trait\SQL;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Class Base
|
|
* Class Base
|
|
@@ -41,21 +44,9 @@ use Qii\Exceptions\Variable;
|
|
class Base
|
|
class Base
|
|
{
|
|
{
|
|
const VERSION = '1.3';
|
|
const VERSION = '1.3';
|
|
- use TraitDatabase;
|
|
|
|
- use TraitCache;
|
|
|
|
- //query 方法
|
|
|
|
- protected $_query = array(
|
|
|
|
- "INSERT" => "INSERT INTO %s(%s) VALUES('%s')",
|
|
|
|
- "REPLACE" => "REPLACE %s (%s) VALUES('%s')",
|
|
|
|
- "SELECT" => "SELECT %s FROM %s %s",
|
|
|
|
- "UPDATE" => "UPDATE %s SET ",
|
|
|
|
- "DELETE" => "DELETE FROM %s %s",
|
|
|
|
- "WHERE" => " WHERE %s",
|
|
|
|
- "OR" => " `%s` = '%s' ",
|
|
|
|
- "ORDER" => " ORDER BY %s %s",
|
|
|
|
- "GROUP" => " GROUP BY %s",
|
|
|
|
- "LIMIT" => " LIMIT %d, %d"
|
|
|
|
- );
|
|
|
|
|
|
+ use Database;
|
|
|
|
+ use Cache;
|
|
|
|
+ use SQL;
|
|
//load类
|
|
//load类
|
|
public $load;
|
|
public $load;
|
|
//cache类
|
|
//cache类
|
|
@@ -64,8 +55,6 @@ class Base
|
|
public $language;
|
|
public $language;
|
|
//响应内容
|
|
//响应内容
|
|
public $response;
|
|
public $response;
|
|
- //执行的sql语句
|
|
|
|
- public $modelSQL = "";
|
|
|
|
/**
|
|
/**
|
|
* @var string sql 引用
|
|
* @var string sql 引用
|
|
*/
|
|
*/
|
|
@@ -95,10 +84,7 @@ class Base
|
|
public $whereCondition = array();
|
|
public $whereCondition = array();
|
|
public $joinCondition = array();
|
|
public $joinCondition = array();
|
|
|
|
|
|
- public $fields = "*";
|
|
|
|
- public $sets = array();
|
|
|
|
protected $groupBy;
|
|
protected $groupBy;
|
|
- protected $limit;
|
|
|
|
protected $orderBy;
|
|
protected $orderBy;
|
|
|
|
|
|
protected $_modelAlias = array('updateRows' => 'update', 'selectRows' => 'selectAll', 'select' => 'selectRow', 'getOne' => 'selectOne', 'getRow' => 'selectRow', 'getAll' => 'selectAll', 'remove' => 'delete', 'deleteRows' => 'delete');
|
|
protected $_modelAlias = array('updateRows' => 'update', 'selectRows' => 'selectAll', 'select' => 'selectRow', 'getOne' => 'selectOne', 'getRow' => 'selectRow', 'getAll' => 'selectAll', 'remove' => 'delete', 'deleteRows' => 'delete');
|
|
@@ -124,7 +110,7 @@ class Base
|
|
{
|
|
{
|
|
$this->where($where);
|
|
$this->where($where);
|
|
}
|
|
}
|
|
- $sql = $this->createSelectSQL($table);
|
|
|
|
|
|
+ $sql = $this->selectSQL($table);
|
|
return $this->getAll($sql);
|
|
return $this->getAll($sql);
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
@@ -133,7 +119,7 @@ class Base
|
|
*/
|
|
*/
|
|
final function rs($table)
|
|
final function rs($table)
|
|
{
|
|
{
|
|
- $sql = $this->createSelectSQL($table);
|
|
|
|
|
|
+ $sql = $this->selectSQL($table);
|
|
return $this->setQuery($sql);
|
|
return $this->setQuery($sql);
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
@@ -149,7 +135,7 @@ class Base
|
|
{
|
|
{
|
|
$this->where($where);
|
|
$this->where($where);
|
|
}
|
|
}
|
|
- $sql = $this->createSelectSQL($table);
|
|
|
|
|
|
+ $sql = $this->selectSQL($table);
|
|
return $this->getRow($sql);
|
|
return $this->getRow($sql);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -178,7 +164,7 @@ class Base
|
|
}
|
|
}
|
|
throw new TableException("表名必须是字符串加下划线,目标字符为". gettype($table));
|
|
throw new TableException("表名必须是字符串加下划线,目标字符为". gettype($table));
|
|
}
|
|
}
|
|
- $sql = $this->createSelectSQL($table);
|
|
|
|
|
|
+ $sql = $this->selectSQL($table);
|
|
return $this->getOne($sql);
|
|
return $this->getOne($sql);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -190,27 +176,6 @@ class Base
|
|
public function getOne($sql) {
|
|
public function getOne($sql) {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 格式化插入值,新增null值插入
|
|
|
|
- *
|
|
|
|
- * @param $arr
|
|
|
|
- * @return array|mixed
|
|
|
|
- */
|
|
|
|
- protected function formatInsertObject($arr) {
|
|
|
|
- if(is_array($arr)) {
|
|
|
|
- $values = array();
|
|
|
|
- foreach ($arr as $val) {
|
|
|
|
- if(strtoupper(gettype($val)) == 'NULL') {
|
|
|
|
- $values[] = 'null';
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- $values[] = "'". $val . "'";
|
|
|
|
- }
|
|
|
|
- return $values;
|
|
|
|
- }
|
|
|
|
- return $arr;
|
|
|
|
- }
|
|
|
|
/**
|
|
/**
|
|
* 插入数据
|
|
* 插入数据
|
|
* @param $table
|
|
* @param $table
|
|
@@ -220,19 +185,10 @@ class Base
|
|
*/
|
|
*/
|
|
final function insertObject($table, $dataArray)
|
|
final function insertObject($table, $dataArray)
|
|
{
|
|
{
|
|
- if (empty($table)) {
|
|
|
|
- throw new InvalidParams(_i('%s is invalid', 'table'), __LINE__);
|
|
|
|
- }
|
|
|
|
- $replaceObj = $this->createInsertReplaceObj($dataArray);
|
|
|
|
- if(empty($replaceObj['fields']) || empty($replaceObj['values']))
|
|
|
|
- {
|
|
|
|
- throw new Variable(_i('Invalid %s format', 'data'), __LINE__);
|
|
|
|
- }
|
|
|
|
- // 针对 null 值单独处理,并将值的两端加上单引号 '
|
|
|
|
- $values = $this->formatInsertObject($replaceObj['values']);
|
|
|
|
- $this->executeSQL = $this->modelSQL = $sql = "INSERT INTO " . $this->getTable($table) . "(`" . join("`, `", $replaceObj['fields']) . "`) VALUES(". join(',', $values) . ")";
|
|
|
|
-
|
|
|
|
|
|
+ $sql = $this->insertSQL($table, $dataArray);
|
|
|
|
+ $this->startTime($sql);
|
|
$this->setQuery($sql);
|
|
$this->setQuery($sql);
|
|
|
|
+ $this->endTime($sql);
|
|
$this->setError();
|
|
$this->setError();
|
|
return $this->lastInsertId();
|
|
return $this->lastInsertId();
|
|
}
|
|
}
|
|
@@ -244,53 +200,11 @@ class Base
|
|
*/
|
|
*/
|
|
final function replaceObject($table, $dataArray)
|
|
final function replaceObject($table, $dataArray)
|
|
{
|
|
{
|
|
- if (empty($table)) {
|
|
|
|
- throw new InvalidParams(_i('%s is invalid', 'table'), __LINE__);
|
|
|
|
- }
|
|
|
|
- $replaceObj = $this->createInsertReplaceObj($dataArray);
|
|
|
|
- if(empty($replaceObj['fields']) || empty($replaceObj['values']))
|
|
|
|
- {
|
|
|
|
- throw new Variable(_i('Invalid %s format', 'data'), __LINE__);
|
|
|
|
- }
|
|
|
|
- // 针对 null 值单独处理,并将值的两端加上单引号 '
|
|
|
|
- $values = $this->formatInsertObject($replaceObj['values']);
|
|
|
|
-
|
|
|
|
- $this->executeSQL = $this->modelSQL = $sql = "REPLACE INTO " . $this->getTable($table) . "(`" . join("`, `", $replaceObj['fields']) . "`) VALUES(". join(',', $values) . ")";
|
|
|
|
|
|
+ $sql = $this->replaceSQL($table, $dataArray);
|
|
$rs = $this->setQuery($sql);
|
|
$rs = $this->setQuery($sql);
|
|
$this->setError();
|
|
$this->setError();
|
|
return $this->AffectedRows($rs);
|
|
return $this->AffectedRows($rs);
|
|
}
|
|
}
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 创建插入或替换的内容
|
|
|
|
- *
|
|
|
|
- * @param $dataArray
|
|
|
|
- * @return array
|
|
|
|
- * @throws InvalidFormat
|
|
|
|
- */
|
|
|
|
- protected function createInsertReplaceObj($dataArray)
|
|
|
|
- {
|
|
|
|
- if(gettype($dataArray) == 'object') {
|
|
|
|
- $dataArray = get_object_vars($dataArray);
|
|
|
|
- }
|
|
|
|
- if (sizeof($dataArray) > 0) {
|
|
|
|
- $keys = array();
|
|
|
|
- $values = array();
|
|
|
|
- foreach ($dataArray AS $key => $value) {
|
|
|
|
- $keys[] = $key;
|
|
|
|
- if (is_array($value)) {
|
|
|
|
- throw new InvalidFormat(_i('Invalid %s format', $key), __LINE__);
|
|
|
|
- }
|
|
|
|
- if(strtoupper(gettype($value)) == 'NULL') {
|
|
|
|
- $values[] = null;
|
|
|
|
- }else{
|
|
|
|
- $values[] = $this->setQuote($value);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return array('fields' => $keys, 'values' => $values);
|
|
|
|
- }
|
|
|
|
- return array('fields' => array(), 'values' => array());
|
|
|
|
- }
|
|
|
|
/**
|
|
/**
|
|
* 更新表中指定数据 结合set、where等方法是用
|
|
* 更新表中指定数据 结合set、where等方法是用
|
|
*
|
|
*
|
|
@@ -300,21 +214,11 @@ class Base
|
|
*/
|
|
*/
|
|
public function update($table)
|
|
public function update($table)
|
|
{
|
|
{
|
|
- if(!$table) {
|
|
|
|
- throw new InvalidParams(_i('%s is invalid', '表名'), __LINE__);
|
|
|
|
- }
|
|
|
|
- $set = join(",", $this->sets);
|
|
|
|
- $this->sets = array();
|
|
|
|
- if(count($this->whereCondition) > 0 && $this->isOperator($this->whereCondition[0]))
|
|
|
|
- {
|
|
|
|
- array_shift($this->whereCondition);
|
|
|
|
- }
|
|
|
|
- $where = count($this->whereCondition) > 0 ? " WHERE ". join(" ", $this->whereCondition) : "";
|
|
|
|
- $this->whereCondition = array();
|
|
|
|
-
|
|
|
|
- $alias = $this->getTableAlias($table);
|
|
|
|
- $this->executeSQL = $this->modelSQL = $sql = sprintf($this->_query['UPDATE'], $alias['name'] . $alias['alias']) . $set. $where;
|
|
|
|
- return $this->exec($sql);
|
|
|
|
|
|
+ $sql = $this->updateSQL($table);
|
|
|
|
+ $this->startTime($sql);
|
|
|
|
+ $res = $this->exec($sql);
|
|
|
|
+ $this->endTime($sql);
|
|
|
|
+ return $res;
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
* 更新表的数据,同时可以使用update方法达到同样的效果
|
|
* 更新表的数据,同时可以使用update方法达到同样的效果
|
|
@@ -326,9 +230,6 @@ class Base
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
final function updateObject($table, $dataArray, $where = array()){
|
|
final function updateObject($table, $dataArray, $where = array()){
|
|
- if(!$table) {
|
|
|
|
- throw new InvalidParams(_i('%s is invalid', '表名'), __LINE__);
|
|
|
|
- }
|
|
|
|
return $this->set($dataArray)->where($where)->update($table);
|
|
return $this->set($dataArray)->where($where)->update($table);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -341,15 +242,11 @@ class Base
|
|
*/
|
|
*/
|
|
final function delete($table)
|
|
final function delete($table)
|
|
{
|
|
{
|
|
- if(!$table) {
|
|
|
|
- throw new \Exception('未指定更新的表名', __LINE__);
|
|
|
|
- }
|
|
|
|
- $where = count($this->whereCondition) > 0 ? " WHERE ". join(" ", $this->whereCondition) : "";
|
|
|
|
- $this->whereCondition = array();
|
|
|
|
-
|
|
|
|
- $alias = $this->getTableAlias($table);
|
|
|
|
- $this->executeSQL = $this->modelSQL = $sql = sprintf($this->_query['DELETE'], $alias['name'], $where);
|
|
|
|
- return $this->exec($sql);
|
|
|
|
|
|
+ $sql = $this->deleteSQL($table);
|
|
|
|
+ $this->startTime($sql);
|
|
|
|
+ $res = $this->exec($sql);
|
|
|
|
+ $this->endTime($sql);
|
|
|
|
+ return $res;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -367,135 +264,6 @@ class Base
|
|
}
|
|
}
|
|
return $this->where($where)->delete($table);
|
|
return $this->where($where)->delete($table);
|
|
}
|
|
}
|
|
- /**
|
|
|
|
- * like语句
|
|
|
|
- * @param mix $arr
|
|
|
|
- * @return $this
|
|
|
|
- */
|
|
|
|
- final function like($arr)
|
|
|
|
- {
|
|
|
|
- if(!is_array($arr)) {
|
|
|
|
- if(!empty($arr)) {
|
|
|
|
- $this->where($arr);
|
|
|
|
- }
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- foreach($arr as $key => $val) {
|
|
|
|
- $arr[$key .":like"] = $val;
|
|
|
|
- unset($arr[$key]);
|
|
|
|
- }
|
|
|
|
- $this->where($arr);
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- /**
|
|
|
|
- * like语句
|
|
|
|
- * @param mix $arr
|
|
|
|
- * @return $this
|
|
|
|
- */
|
|
|
|
- final function llike($arr)
|
|
|
|
- {
|
|
|
|
- if(!is_array($arr)) {
|
|
|
|
- if(!empty($arr)) {
|
|
|
|
- $this->where($arr);
|
|
|
|
- }
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- foreach($arr as $key => $val) {
|
|
|
|
- $arr[$key .":llike"] = $val;
|
|
|
|
- unset($arr[$key]);
|
|
|
|
- }
|
|
|
|
- $this->where($arr);
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- /**
|
|
|
|
- * like语句
|
|
|
|
- * @param mix $arr
|
|
|
|
- * @return $this
|
|
|
|
- */
|
|
|
|
- final function rlike($arr)
|
|
|
|
- {
|
|
|
|
- if(!is_array($arr)) {
|
|
|
|
- if(!empty($arr)) {
|
|
|
|
- $this->where($arr);
|
|
|
|
- }
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- foreach($arr as $key => $val) {
|
|
|
|
- $arr[$key .":rlike"] = $val;
|
|
|
|
- unset($arr[$key]);
|
|
|
|
- }
|
|
|
|
- $this->where($arr);
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- *
|
|
|
|
- * Limit函数,如果省略第二个参数则第一个为0,第二个参数值取第一个
|
|
|
|
- * @param int $limit
|
|
|
|
- * @param int $offset
|
|
|
|
- */
|
|
|
|
- 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 === null) {
|
|
|
|
- $this->limit = sprintf($this->_query["LIMIT"], 0, $limit);
|
|
|
|
- } else {
|
|
|
|
- $this->limit = sprintf($this->_query["LIMIT"], $limit, $offset);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * sql中的set条件
|
|
|
|
- *
|
|
|
|
- * @param array $set
|
|
|
|
- * @param null $res
|
|
|
|
- * @param string $defaultOperator
|
|
|
|
- * @return $this
|
|
|
|
- */
|
|
|
|
- final public function set($set, &$res = null, $defaultOperator = 'equal')
|
|
|
|
- {
|
|
|
|
- if(!is_array($set))
|
|
|
|
- {
|
|
|
|
- if(!empty($set))
|
|
|
|
- {
|
|
|
|
- $this->sets[] = $set;
|
|
|
|
- }
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- if(count($set) == 0) {
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- $operator = array("equal" => "%s`%s` = '%s'", "unequal" => "%s`%s` != '%s'", "plus" => "%s`%s` = %s`%s`+%s", "minus" => "%s`%s` = %s`%s`-%s", "multiply" => "%s`%s` = %s`%s`*%s", "divide" => "%s`%s` = %s`%s`/%s");
|
|
|
|
- foreach($set as $key => $val)
|
|
|
|
- {
|
|
|
|
- if(strtoupper(gettype($val)) != 'NULL') {
|
|
|
|
- $val = $this->setQuote($val);
|
|
|
|
- }
|
|
|
|
- $alias = $this->getFieldAlias($key);
|
|
|
|
- $operate = $this->getFieldOperator($alias['name']);
|
|
|
|
- if($operate['operator'] == '') {
|
|
|
|
- $operate['operator'] = $defaultOperator;
|
|
|
|
- }
|
|
|
|
- $opt = $operator[$operate['operator']];
|
|
|
|
- if(strtoupper(gettype($val)) == 'NULL') {
|
|
|
|
- $val = 'NULL';
|
|
|
|
- $opt = str_replace("'", '', $opt);
|
|
|
|
- }
|
|
|
|
- if($operate['operator'] == 'equal') {
|
|
|
|
- $this->sets[] = sprintf($opt, $alias['alias'], $operate['field'], $val);
|
|
|
|
- }else{
|
|
|
|
- $this->sets[] = sprintf($opt, $alias['alias'], $operate['field'], $alias['alias'], $operate['field'], $val);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* set 字段加
|
|
* set 字段加
|
|
@@ -519,7 +287,7 @@ class Base
|
|
if(substr($key, -5) == ':plus') {
|
|
if(substr($key, -5) == ':plus') {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
- $set[$key . ":plus"] = $val;
|
|
|
|
|
|
+ $set[$key . ":incr"] = $val;
|
|
unset($set[$key]);
|
|
unset($set[$key]);
|
|
}
|
|
}
|
|
return $this->set($set);
|
|
return $this->set($set);
|
|
@@ -545,104 +313,12 @@ class Base
|
|
if(substr($key, -6) == ':minus') {
|
|
if(substr($key, -6) == ':minus') {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
- $set[$key . ":minus"] = $val;
|
|
|
|
|
|
+ $set[$key . ":decr"] = $val;
|
|
unset($set[$key]);
|
|
unset($set[$key]);
|
|
}
|
|
}
|
|
return $this->set($set);
|
|
return $this->set($set);
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 处理where条件
|
|
|
|
- *
|
|
|
|
- * @param $where
|
|
|
|
- * @return $this|array
|
|
|
|
- * @throws \Exception
|
|
|
|
- */
|
|
|
|
- public function handleWhereFields($where) {
|
|
|
|
- if(!is_array($where)) {
|
|
|
|
- return $where;
|
|
|
|
- }
|
|
|
|
- $fields = [];
|
|
|
|
- foreach ($where as $key => $val) {
|
|
|
|
- //如果是操作符,直接放入操作符中
|
|
|
|
- if(is_integer($key) && $this->isOperator($val)) {
|
|
|
|
- $fields[] = $val;
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- $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);
|
|
|
|
- if(is_array($val)) {
|
|
|
|
- $arr2 = [];
|
|
|
|
- foreach ($val as $k1 => $v1) {
|
|
|
|
- $keyArr = explode(':', $k1);
|
|
|
|
- $lenKey = count($keyArr);
|
|
|
|
- if($lenKey > 2) {
|
|
|
|
- $opt1 = $keyArr[$lenKey-1];
|
|
|
|
- $keyArrSlice = array_slice($keyArr, 0, $lenKey-1);
|
|
|
|
- foreach ($keyArrSlice as $k2 => $v2) {
|
|
|
|
- $arr2[$v2 .':'. $opt1] = $v1;
|
|
|
|
- if($k2 < $lenKey-2) {
|
|
|
|
- $arr2[] = 'or';
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if(count($arr2) > 0) {
|
|
|
|
- $fields[] = $arr2;
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- $tmp = [];
|
|
|
|
- foreach ($arr1 as $key1) {
|
|
|
|
- $tmp[$key1 . $opt] = $val;
|
|
|
|
- }
|
|
|
|
- if(count($tmp) > 0) {
|
|
|
|
- $fields[] = $tmp;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return $fields;
|
|
|
|
- }
|
|
|
|
- /**
|
|
|
|
- * where条件
|
|
|
|
- * @param array $where where语句
|
|
|
|
- * @param null $res
|
|
|
|
- * @return $this
|
|
|
|
- */
|
|
|
|
- public function where($where, &$res = null)
|
|
|
|
- {
|
|
|
|
- if(!is_array($where)) {
|
|
|
|
- if(!empty($where))
|
|
|
|
- {
|
|
|
|
- if(!empty($this->whereCondition) && !$this->isOperator($where)) {
|
|
|
|
- $this->whereCondition[] = "and";
|
|
|
|
- }
|
|
|
|
- if($this->isOperator($where)) {
|
|
|
|
- $this->whereCondition[] = $where;
|
|
|
|
- }else{
|
|
|
|
- $this->whereCondition[] = "(". $where . ")";
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- if(count($where) == 0) {
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- $where = $this->handleWhereFields($where);
|
|
|
|
- $slices = $this->groupContents($where);
|
|
|
|
- $this->handleCondition($slices, $res);
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* OR 条件
|
|
* OR 条件
|
|
* 用法:
|
|
* 用法:
|
|
@@ -659,52 +335,9 @@ class Base
|
|
* @return $this
|
|
* @return $this
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
*/
|
|
*/
|
|
- public function orTerms($where, $defaultOperater = 'or', &$res = null)
|
|
|
|
|
|
+ public function orTerms($where)
|
|
{
|
|
{
|
|
- if(!is_array($where))
|
|
|
|
- {
|
|
|
|
- if($where != '') {
|
|
|
|
- if(!empty($this->whereCondition) && !$this->isOperator($where)) {
|
|
|
|
- $this->whereCondition[] = $defaultOperater;
|
|
|
|
- }
|
|
|
|
- $this->whereCondition[] = "(". $where . ")";
|
|
|
|
- }
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- if(count($where) == 0) {
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- $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 $key1 => $field) {
|
|
|
|
- $extra[$field . $opt] = $val;
|
|
|
|
- if($key1 < count($arr) - 1) {
|
|
|
|
- $extra[] = 'OR';
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- unset($where[$key]);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if(count($where) > 0) {
|
|
|
|
- $this->handleCondition(array(array($defaultOperater, $where)), $res);
|
|
|
|
- }
|
|
|
|
- if(count($extra) > 0) {
|
|
|
|
- $this->handleCondition(array(array($defaultOperater, $extra)), $res);
|
|
|
|
- /*foreach ($extra as $key => $val) {
|
|
|
|
- $this->handleCondition(array(array($defaultOperater, [$key => $val])), $res);
|
|
|
|
- }*/
|
|
|
|
- }
|
|
|
|
- return $this;
|
|
|
|
|
|
+ return $this->or($where);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -738,9 +371,7 @@ class Base
|
|
$where[$key . ":in"] = $val;
|
|
$where[$key . ":in"] = $val;
|
|
unset($where[$key]);
|
|
unset($where[$key]);
|
|
}
|
|
}
|
|
- $where = $this->handleWhereFields($where);
|
|
|
|
- $this->handleCondition(array(array('and', $where)), $res);
|
|
|
|
- return $this;
|
|
|
|
|
|
+ return $this->where($where);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -760,36 +391,13 @@ class Base
|
|
}
|
|
}
|
|
foreach($where as $key => $value) {
|
|
foreach($where as $key => $value) {
|
|
if($this->getFieldOperator($key)['operator'] == '') {
|
|
if($this->getFieldOperator($key)['operator'] == '') {
|
|
- $where[$key . ":unequal"] = $value;
|
|
|
|
|
|
+ $where[$key . ":neq"] = $value;
|
|
unset($where[$key]);
|
|
unset($where[$key]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $this->where($where);
|
|
return $this->where($where);
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * join语句
|
|
|
|
- *
|
|
|
|
- * @param mix $val
|
|
|
|
- * @param reference $res 用于返回值
|
|
|
|
- * @return $this
|
|
|
|
- * @throws \Exception
|
|
|
|
- */
|
|
|
|
- public function join($val, &$res = null, $joinType = 'leftJoin') {
|
|
|
|
- if(!is_array($val)) {
|
|
|
|
- if(!empty($val)) {
|
|
|
|
- $this->joinCondition[] = $val;
|
|
|
|
- }
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- if(count($val) == 0) {
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- $slices = $this->groupContents($val);
|
|
|
|
- $this->handleTableJoin($slices, $joinType, $res);
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* left join
|
|
* left join
|
|
* @param $table
|
|
* @param $table
|
|
@@ -799,8 +407,7 @@ class Base
|
|
*/
|
|
*/
|
|
final function leftJoinObject($table, $on)
|
|
final function leftJoinObject($table, $on)
|
|
{
|
|
{
|
|
- $alias = $this->getTableAlias($table);
|
|
|
|
- return $this->join(" LEFT JOIN ". $this->getTable($alias['name']) . " " . $alias['alias'] ." ON ". $on);
|
|
|
|
|
|
+ return $this->join($table, $on);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -810,23 +417,7 @@ class Base
|
|
*/
|
|
*/
|
|
public function groupBy($group)
|
|
public function groupBy($group)
|
|
{
|
|
{
|
|
- if(!is_array($group))
|
|
|
|
- {
|
|
|
|
- if(!empty($group))
|
|
|
|
- {
|
|
|
|
- $this->groupBy = stristr($group, 'GROUP BY') === false? "GROUP BY ". $group: $group;
|
|
|
|
- }
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- if(count($group) == 0) {
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- foreach($group as $index => $val)
|
|
|
|
- {
|
|
|
|
- $alias = $this->getFieldAlias($val);
|
|
|
|
- $group[$index] = $alias['alias'] . "`". $alias['name'] ."`";
|
|
|
|
- }
|
|
|
|
- $this->groupBy = " GROUP BY ". join(",", $group);
|
|
|
|
|
|
+ $this->group($group);
|
|
return $this;
|
|
return $this;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -839,32 +430,7 @@ class Base
|
|
*/
|
|
*/
|
|
public function orderBy($order)
|
|
public function orderBy($order)
|
|
{
|
|
{
|
|
- if(!is_array($order))
|
|
|
|
- {
|
|
|
|
- if(!empty($order)) {
|
|
|
|
- $this->orderBy = stristr($order, 'ORDER BY') === false ? "ORDER BY ". $order: $order;
|
|
|
|
- }
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- if(count($order) == 0) {
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- $allowSortOff = array('asc', 'desc');
|
|
|
|
- $orderBy = array();
|
|
|
|
- $i = 0;
|
|
|
|
- $countOrder = count($order);
|
|
|
|
- foreach($order as $key => $sort)
|
|
|
|
- {
|
|
|
|
- if(!in_array(strtolower($sort), $allowSortOff) && $countOrder == $i)
|
|
|
|
- {
|
|
|
|
- throw new InvalidParams(_i('%s is invalid', 'sort'), __LINE__);
|
|
|
|
- }
|
|
|
|
- $alias = $this->getFieldAlias($key);
|
|
|
|
-
|
|
|
|
- $orderBy[] = $alias['alias'] . "`". $alias['name'] ."` ". $sort;
|
|
|
|
- $i++;
|
|
|
|
- }
|
|
|
|
- $this->orderBy = " ORDER BY ". join(",", $orderBy);
|
|
|
|
|
|
+ $this->order($order);
|
|
return $this;
|
|
return $this;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -877,71 +443,14 @@ class Base
|
|
*/
|
|
*/
|
|
public function orderByStr($orderBy)
|
|
public function orderByStr($orderBy)
|
|
{
|
|
{
|
|
- return $this->orderBy($orderBy);
|
|
|
|
- }
|
|
|
|
- /**
|
|
|
|
- * 过滤值
|
|
|
|
- *
|
|
|
|
- * @param string|array $word
|
|
|
|
- * @return array|string
|
|
|
|
- */
|
|
|
|
- final function setQuote($word)
|
|
|
|
- {
|
|
|
|
- if (ini_get("magic_quotes_gpc")) {
|
|
|
|
- return $word;
|
|
|
|
- }
|
|
|
|
- if(in_array(gettype($word), array("object", "resource","resource (closed)"))) {
|
|
|
|
- throw new \Exception('期待参数为数组或字符串,获取到的是:'. gettype($word)."(". json_encode($word) .")");
|
|
|
|
- }
|
|
|
|
- return is_array($word) ? array_map('addslashes', $word) : addslashes($word);
|
|
|
|
- }
|
|
|
|
- /**
|
|
|
|
- *
|
|
|
|
- * 查询的字段
|
|
|
|
- * @param string|array $fields
|
|
|
|
- * @return $this
|
|
|
|
- */
|
|
|
|
- final function fields($fields = "*", $append = false)
|
|
|
|
- {
|
|
|
|
- if (empty($fields) && !$append) $fields = "*";
|
|
|
|
- if (is_array($fields)) {
|
|
|
|
- if(count($fields) == 0) {
|
|
|
|
- $fields = $append ? '' : '*';
|
|
|
|
- }else{
|
|
|
|
- foreach($fields as $key => $val)
|
|
|
|
- {
|
|
|
|
- $alias = explode('.', $val);
|
|
|
|
- if(count($alias) > 1)
|
|
|
|
- {
|
|
|
|
- if(strpos("*", $alias[1]) !== false) {
|
|
|
|
- $fields[$key] = $val;
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- $fields[$key] = $alias[0] . ".`".join(".", array_slice($alias, 1))."`";
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- $fields = join(',', $fields);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if($append) {
|
|
|
|
- if($fields != "") $this->fields .= ','. $fields;
|
|
|
|
- }else{
|
|
|
|
- $this->fields = $fields;
|
|
|
|
- }
|
|
|
|
- return $this;
|
|
|
|
|
|
+ return $this->order($orderBy);
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
* 清空Data数据
|
|
* 清空Data数据
|
|
*/
|
|
*/
|
|
final public function cleanCondition()
|
|
final public function cleanCondition()
|
|
{
|
|
{
|
|
- $this->fields = '*';
|
|
|
|
- $this->whereCondition = array();
|
|
|
|
- $this->joinCondition = array();
|
|
|
|
- $this->groupBy = "";
|
|
|
|
- $this->orderBy = "";
|
|
|
|
- $this->limit = null;
|
|
|
|
- $this->sets = array();
|
|
|
|
|
|
+ $this->clean();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -1013,47 +522,6 @@ class Base
|
|
$this->executeSQL = &$sql;
|
|
$this->executeSQL = &$sql;
|
|
return $this;
|
|
return $this;
|
|
}
|
|
}
|
|
- /**
|
|
|
|
- * 通过where\set\limit等方法自动生成SQL语句
|
|
|
|
- *
|
|
|
|
- * @param string $table 必填
|
|
|
|
- * @return string
|
|
|
|
- * @throws \Exception
|
|
|
|
- */
|
|
|
|
- public function createSelectSQL($table)
|
|
|
|
- {
|
|
|
|
- if(!$table) {
|
|
|
|
- throw new InvalidParams(_i('%s is invalid', '表名'), __LINE__);
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- $aliases = $this->getTableAlias($table);
|
|
|
|
-
|
|
|
|
- $fields = (trim($this->fields) != '') ? $this->fields : "*";
|
|
|
|
- //$this->fields = '*';
|
|
|
|
-
|
|
|
|
- if(count($this->whereCondition) > 0 && $this->isOperator($this->whereCondition[0])) {
|
|
|
|
- array_shift($this->whereCondition);
|
|
|
|
- }
|
|
|
|
- $where = count($this->whereCondition) > 0 ? " WHERE ". join(" ", $this->whereCondition) : "";
|
|
|
|
- //$this->whereCondition = array();
|
|
|
|
-
|
|
|
|
- $join = count($this->joinCondition) > 0 ? join("\n", $this->joinCondition) : "";
|
|
|
|
- //$this->joinCondition = array();
|
|
|
|
-
|
|
|
|
- $groupBy = " ". $this->groupBy;
|
|
|
|
- //$this->groupBy = "";
|
|
|
|
-
|
|
|
|
- $orderBy = " ". $this->orderBy;
|
|
|
|
- //$this->orderBy = "";
|
|
|
|
-
|
|
|
|
- $limit = $this->limit;
|
|
|
|
- //$this->limit = null;
|
|
|
|
-
|
|
|
|
- $sql = sprintf($this->_query['SELECT'], $fields, $aliases['name'], $aliases['alias']) . $join . $where . $groupBy . $orderBy . $limit;
|
|
|
|
- $this->cleanCondition();
|
|
|
|
- $this->executeSQL = $this->modelSQL = $sql;
|
|
|
|
- return $sql;
|
|
|
|
- }
|
|
|
|
/**
|
|
/**
|
|
* 获取操作符
|
|
* 获取操作符
|
|
*
|
|
*
|
|
@@ -1088,49 +556,6 @@ class Base
|
|
return array('alias' => $aliases[0] . $connector, 'name' => join(".", array_slice($aliases, 1)));
|
|
return array('alias' => $aliases[0] . $connector, 'name' => join(".", array_slice($aliases, 1)));
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取表的别名
|
|
|
|
- *
|
|
|
|
- * @param string $name 名字
|
|
|
|
- * @return array
|
|
|
|
- */
|
|
|
|
- protected function getTableAlias($name)
|
|
|
|
- {
|
|
|
|
- if (!$this->verifyTable($name)) {
|
|
|
|
- if (gettype($name) == 'string') {
|
|
|
|
- throw new TableException("表名不能包含怪字符且不能以数字开头,获取到的是". $name);
|
|
|
|
- }
|
|
|
|
- throw new TableException("表名必须是字符串加下划线,目标字符为". gettype($name));
|
|
|
|
- }
|
|
|
|
- //去掉table前后的``符号
|
|
|
|
- $name = str_replace('`', '', $name);
|
|
|
|
- $aliases = explode(' ', $name);
|
|
|
|
- //检查表名中时候含数据库名,有数据表名的时候需要单独处理
|
|
|
|
- $hasDatabaseName = false;
|
|
|
|
- if(stristr($name, '.')) {
|
|
|
|
- $hasDatabaseName = true;
|
|
|
|
- }
|
|
|
|
- if(count($aliases) == 1) {
|
|
|
|
- if($hasDatabaseName) {
|
|
|
|
- $names = explode(".", $name);
|
|
|
|
- $name = $names[0] . ".`". $names[1] . "`";
|
|
|
|
- }else{
|
|
|
|
- $name = "`". $name ."`";
|
|
|
|
- }
|
|
|
|
- return array('alias' => '', 'name' => $name);
|
|
|
|
- }
|
|
|
|
- $res = array();
|
|
|
|
- $res['alias'] = array_pop($aliases);
|
|
|
|
- $res['name'] = join(" ", array_slice($aliases, -1));
|
|
|
|
- if($hasDatabaseName) {
|
|
|
|
- $names = explode(".", $res['name']);
|
|
|
|
- $res['name'] = $names[0] . ".`". $names[1] . "`";
|
|
|
|
- }else{
|
|
|
|
- $res['name'] = "`". $res['name'] ."`";
|
|
|
|
- }
|
|
|
|
- return $res;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 把条件根据『链接字符串,『字段:值』』等内容分组
|
|
* 把条件根据『链接字符串,『字段:值』』等内容分组
|
|
*
|
|
*
|
|
@@ -1276,85 +701,6 @@ class Base
|
|
return in_array($val, $this->operateTable);
|
|
return in_array($val, $this->operateTable);
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * where条件子句组合
|
|
|
|
- *
|
|
|
|
- * @param array $values ['字段1' => '值1', '字段2' => '值2', ...]
|
|
|
|
- * @param string $defaultOperate 默认字句的链接方式
|
|
|
|
- * ..... 额外的参数,用于字段与字段之间的连接
|
|
|
|
- * @return string
|
|
|
|
- * @throws \Exception
|
|
|
|
- */
|
|
|
|
- protected function handleSubCondition($values, $defaultOperate = 'equal')
|
|
|
|
- {
|
|
|
|
- $extraParams = array();
|
|
|
|
- if(func_num_args() > 2)
|
|
|
|
- {
|
|
|
|
- $extraParams = array_slice(func_get_args(), 2);
|
|
|
|
- }
|
|
|
|
- $where = array();
|
|
|
|
- $operator = $this->shortExpression;
|
|
|
|
- $lastIsOperator = false;
|
|
|
|
- $lastIsValue = null;
|
|
|
|
- $i = 0;
|
|
|
|
- foreach($values as $key => $val)
|
|
|
|
- {
|
|
|
|
- $isOperator = preg_match("/^[0-9].*/", $key) && $this->isOperator($val);//如果是操作符,上一个不是操作符就清空tmpWhere,并放入slices
|
|
|
|
- $isValue = !$isOperator;
|
|
|
|
- if($lastIsValue && $isOperator)//如果当前是操作符,上一个是值,就将操作符加入到条件中
|
|
|
|
- {
|
|
|
|
- $where[] = $val;
|
|
|
|
- $lastIsValue = $isValue;
|
|
|
|
- $lastIsOperator = $isOperator;
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- //如果上一次是操作符,这次又是操作符,就是用当前的操作符,去掉上一个操作符
|
|
|
|
- if($lastIsOperator && $isOperator)
|
|
|
|
- {
|
|
|
|
- if(!$this->faultTolerant) throw new InvalidParams(_i('Unsupported operator'), __LINE__);
|
|
|
|
- array_pop($where);
|
|
|
|
- $where[] = $val;
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- if($lastIsValue && $isValue)//需要添加操作符
|
|
|
|
- {
|
|
|
|
- $where[] = count($extraParams) > 0 && isset($extraParams[$i-1]) ? $extraParams[$i-1] : "and";
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //$aliases = explode(".", $key);
|
|
|
|
- $aliases = $this->getFieldAlias($key);
|
|
|
|
- $alias = $aliases['alias'];
|
|
|
|
- $operate = $this->getFieldOperator($aliases['name']);
|
|
|
|
- $opt = $operate['operator'] ? $operate['operator'] : $defaultOperate;
|
|
|
|
- $name = $operate['field'];
|
|
|
|
- if($opt == 'in')
|
|
|
|
- {
|
|
|
|
- if(is_array($val)) {
|
|
|
|
- $where[] = sprintf($operator[$opt], $alias, $name, "'". join("','", $val) . "'");
|
|
|
|
- }else{
|
|
|
|
- $where[] = sprintf($operator[$opt], $alias, $name, $val);
|
|
|
|
- }
|
|
|
|
- }else{
|
|
|
|
- if(!isset($operator[$opt])) {
|
|
|
|
- throw new \Exception("Unknow operator " . $opt, __LINE__);
|
|
|
|
- }
|
|
|
|
- // between and 不做转换
|
|
|
|
- $val = $val;
|
|
|
|
- if($opt != 'between') {
|
|
|
|
- $val = $this->setQuote($val);
|
|
|
|
- }
|
|
|
|
- if($opt == 'between' && is_array($val)) {
|
|
|
|
- $val = "'". join("' AND '", $val) . "'";
|
|
|
|
- }
|
|
|
|
- $where[] = sprintf($operator[$opt], $alias, $name, $val);
|
|
|
|
- }
|
|
|
|
- $lastIsValue = $isValue;
|
|
|
|
- $lastIsOperator = $isOperator;
|
|
|
|
- $i++;
|
|
|
|
- }
|
|
|
|
- return join(" ", $where);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 处理table join
|
|
* 处理table join
|
|
* @param array $slices [['leftJoin', ['table' => '表名', 'alias' => 'a', 'on' => 'a.id = b.id']]]
|
|
* @param array $slices [['leftJoin', ['table' => '表名', 'alias' => 'a', 'on' => 'a.id = b.id']]]
|
|
@@ -1399,107 +745,6 @@ class Base
|
|
return $this;
|
|
return $this;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 处理 where 条件
|
|
|
|
- * @param array $condition
|
|
|
|
- * @param null $res
|
|
|
|
- * @return $this
|
|
|
|
- * @throws \Exception
|
|
|
|
- */
|
|
|
|
- protected function handleCondition($condition, &$res = null)
|
|
|
|
- {
|
|
|
|
- if(!is_array($condition))
|
|
|
|
- {
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
- $count = count($condition);
|
|
|
|
- $whereCondition = array();
|
|
|
|
- for($i = 0; $i < $count; $i++)
|
|
|
|
- {
|
|
|
|
- $v = $condition[$i];
|
|
|
|
- if(!is_array($v) && $this->isOperator($v)) {
|
|
|
|
- $whereCondition[] = ' '.$v.' ';
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- //如果有两个及上的操作符
|
|
|
|
- //默认第一个为连接其他条件的操作符,第二个位字段和值之间的操作符, 从第三个开始即为字段与字段之间的连接符
|
|
|
|
- //最里层的字段与值之间的操作符优先级最高
|
|
|
|
-
|
|
|
|
- //例如 1: 'or/and','like','or', 'and', ['uid' => 1, 'name' => '名字', 'status' => 激活];
|
|
|
|
- //结果 1:or/and (uid like '%1%' or name like '%名字%' and status like '%激活%')
|
|
|
|
-
|
|
|
|
- //例如 2: 'or/and','like','or', 'and', ['uid' => 1, 'name' => '名字', 'status:equal' => 激活];
|
|
|
|
- //结果 2:or/and (uid like '%1%' or name like '%名字%' and status = '激活')
|
|
|
|
-
|
|
|
|
- $operateCondition = array();
|
|
|
|
- $values = array();
|
|
|
|
- $countCondition = count($v);
|
|
|
|
- $handleTable = false;
|
|
|
|
- for($j = 0; $j < $countCondition; $j++)
|
|
|
|
- {
|
|
|
|
- $handleTable = false;
|
|
|
|
- if($this->isOperateTable($v[$j]))
|
|
|
|
- {
|
|
|
|
- /*
|
|
|
|
- $operateCondition[] = $v[$j];
|
|
|
|
- $joinTable[$v[$j]] = $this->handleTableJoin($v);
|
|
|
|
- */
|
|
|
|
- //如果是操作数据表就直接退出循环单独处理
|
|
|
|
- $handleTable = true;
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- if($this->isOperator($v[$j]))
|
|
|
|
- {
|
|
|
|
- if(empty($operateCondition)) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- $operateCondition[] = $v[$j];
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- if(!$this->isOperator($v[$j]))
|
|
|
|
- {
|
|
|
|
- if(!is_array($v[$j])) {
|
|
|
|
- $whereCondition[] = $v[0];
|
|
|
|
- $whereCondition[] = "(". $v[1] . ")";
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- $values = is_array($v[$j]) ? array_merge($values, $v[$j]) : array_merge($values, array($v[$j]));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if($handleTable)
|
|
|
|
- {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- if(count($operateCondition) > 0 && count($values) > 0)
|
|
|
|
- {
|
|
|
|
- $whereCondition[] = array_shift($operateCondition);
|
|
|
|
- }
|
|
|
|
- if(count($values) > 0) {
|
|
|
|
- if(count($operateCondition) > 0) {
|
|
|
|
- array_unshift($operateCondition, $values);
|
|
|
|
- $whereCondition[] = "(". call_user_func_array(array($this, 'handleSubCondition'), $operateCondition) .")";
|
|
|
|
- }else{
|
|
|
|
- $whereCondition[] = "(". $this->handleSubCondition($values) .")";
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if(!empty($this->whereCondition))
|
|
|
|
- {
|
|
|
|
- $lastCondition = $this->whereCondition[count($this->whereCondition) - 1];
|
|
|
|
- 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);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- $this->whereCondition = $whereCondition;
|
|
|
|
- }
|
|
|
|
- $res = $whereCondition;
|
|
|
|
- unset($whereCondition);
|
|
|
|
- return $this;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 验证数据表名是否符合规范
|
|
* 验证数据表名是否符合规范
|
|
* 表名不能以数字开头,
|
|
* 表名不能以数字开头,
|