改进model类order方法

添加json类型支持 支持json字段查询例如 $map['user$.name'] = 'thinkphp'
escapeString方法采用PDO::quote方法
This commit is contained in:
thinkphp
2015-11-21 12:06:00 +08:00
parent fb145db8c6
commit fa17feddf0
7 changed files with 92 additions and 74 deletions

View File

@@ -741,17 +741,18 @@ abstract class Driver
*/
protected function parseOrder($order)
{
if (is_array($order)) {
$array = [];
foreach ($order as $key => $val) {
if (is_numeric($key)) {
if (false === strpos($val, '(')) {
$array[] = $this->parseKey($val);
}
} else {
$array[] = $this->parseKey($key) . ' ' . $val;
$sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : '';
$array[] = $this->parseKey($key) . ' ' . $sort;
}
}
$order = implode(',', $array);
}
return !empty($order) ? ' ORDER BY ' . $order : '';
}
@@ -1125,7 +1126,7 @@ abstract class Driver
*/
public function escapeString($str)
{
return addslashes($str);
return $this->_linkID->quote($str);
}
/**

View File

@@ -89,6 +89,11 @@ class Mysql extends Driver
protected function parseKey(&$key)
{
$key = trim($key);
if (strpos($key, '$.') && false === strpos($key, '(')) {
// JSON字段支持
list($field, $name) = explode($key, '$.');
$key = 'jsn_extract(' . $field . ', \'$.\'.' . $name . ')';
}
if (!preg_match('/[,\'\"\*\(\)`.\s]/', $key)) {
$key = '`' . $key . '`';
}

View File

@@ -132,17 +132,6 @@ class Oracle extends Driver
return $info;
}
/**
* SQL指令安全过滤
* @access public
* @param string $str SQL指令
* @return string
*/
public function escapeString($str)
{
return str_ireplace("'", "''", $str);
}
/**
* limit
* @access public
@@ -176,4 +165,21 @@ class Oracle extends Driver
return ' FOR UPDATE NOWAIT ';
}
/**
* 字段和表名处理
* @access protected
* @param string $key
* @return string
*/
protected function parseKey(&$key)
{
$key = trim($key);
if (strpos($key, '$.') && false === strpos($key, '(')) {
// JSON字段支持
list($field, $name) = explode($key, '$.');
$key = $field . '."' . $name . '"';
}
return $key;
}
}

View File

@@ -94,4 +94,20 @@ class Pgsql extends Driver
return $limitStr;
}
/**
* 字段和表名处理
* @access protected
* @param string $key
* @return string
*/
protected function parseKey(&$key)
{
$key = trim($key);
if (strpos($key, '$.') && false === strpos($key, '(')) {
// JSON字段支持
list($field, $name) = explode($key, '$.');
$key = $field . '->>\'' . $name . '\'';
}
return $key;
}
}

View File

@@ -73,17 +73,6 @@ class Sqlite extends Driver
return $info;
}
/**
* SQL指令安全过滤
* @access public
* @param string $str SQL指令
* @return string
*/
public function escapeString($str)
{
return str_ireplace("'", "''", $str);
}
/**
* limit
* @access public

View File

@@ -100,6 +100,16 @@ class Sqlsrv extends Driver
*/
protected function parseOrder($order)
{
$array = [];
foreach ($order as $key => $val) {
if (is_numeric($key)) {
$array[] = $this->parseKey($val);
} else {
$sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : '';
$array[] = $this->parseKey($key) . ' ' . $sort;
}
}
$order = implode(',', $array);
return !empty($order) ? ' ORDER BY ' . $order : ' ORDER BY rand()';
}

View File

@@ -596,6 +596,8 @@ class Model
$data[$key] = floatval($data[$key]);
} elseif (false !== strpos($fieldType, 'bool')) {
$data[$key] = (bool) $data[$key];
} elseif (false !== strpos($fieldType, 'json') && is_array($data[$key])) {
$data[$key] = json_encode($data[$key]);
}
}
}
@@ -917,16 +919,11 @@ class Model
* SQL查询
* @access public
* @param string $sql SQL指令
* @param mixed $parse 是否需要解析SQL
* @return mixed
*/
public function query($sql, $parse = false)
public function query($sql)
{
if (!is_bool($parse) && !is_array($parse)) {
$parse = func_get_args();
array_shift($parse);
}
$sql = $this->parseSql($sql, $parse);
$sql = $this->parseSql($sql);
return $this->db->query($sql);
}
@@ -934,16 +931,11 @@ class Model
* 执行SQL语句
* @access public
* @param string $sql SQL指令
* @param mixed $parse 是否需要解析SQL
* @return false | integer
*/
public function execute($sql, $parse = false)
public function execute($sql)
{
if (!is_bool($parse) && !is_array($parse)) {
$parse = func_get_args();
array_shift($parse);
}
$sql = $this->parseSql($sql, $parse);
$sql = $this->parseSql($sql);
return $this->db->execute($sql);
}
@@ -951,24 +943,14 @@ class Model
* 解析SQL语句
* @access public
* @param string $sql SQL指令
* @param boolean $parse 是否需要解析SQL
* @return string
*/
protected function parseSql($sql, $parse)
protected function parseSql($sql)
{
// 分析表达式
if (true === $parse) {
$options = $this->_parseOptions();
$sql = $this->db->parseSql($sql, $options);
} elseif (is_array($parse)) {
// SQL预处理
$parse = array_map([$this->db, 'escapeString'], $parse);
$sql = vsprintf($sql, $parse);
} else {
$sql = strtr($sql, ['__TABLE__' => $this->getTableName(), '__PREFIX__' => $this->tablePrefix]);
$prefix = $this->tablePrefix;
$sql = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $sql);
}
$this->db->setModel($this->name);
return $sql;
}
@@ -1247,14 +1229,23 @@ class Model
}
/**
* 指定排序
* 指定排序 order('id','desc') 或者 order(['id'=>'desc','create_time'=>'desc'])
* @access public
* @param string|array $field 排序字段
* @param string $order 排序
* @return Model
*/
public function order($order)
public function order($field, $order = '')
{
$this->options['order'] = $order;
if (is_array($field)) {
$this->options['order'] = $field;
} else {
if (!empty($order)) {
$this->options['order'][$field] = $order;
} else {
$this->options['order'][] = $field;
}
}
return $this;
}