增加随机排序支持

This commit is contained in:
thinkphp
2015-12-11 22:11:47 +08:00
parent 774736f41c
commit 7f851362b1
6 changed files with 48 additions and 18 deletions

View File

@@ -15,7 +15,6 @@ use PDO;
use think\Config; use think\Config;
use think\Debug; use think\Debug;
use think\Exception; use think\Exception;
use think\Lang;
use think\Log; use think\Log;
abstract class Driver abstract class Driver
@@ -715,7 +714,7 @@ abstract class Driver
*/ */
protected function parseLimit($limit) protected function parseLimit($limit)
{ {
return !empty($limit) ? ' LIMIT ' . $limit . ' ' : ''; return (!empty($limit) && false === strpos($limit, '(')) ? ' LIMIT ' . $limit . ' ' : '';
} }
/** /**
@@ -747,6 +746,8 @@ abstract class Driver
if (is_numeric($key)) { if (is_numeric($key)) {
if (false === strpos($val, '(')) { if (false === strpos($val, '(')) {
$array[] = $this->parseKey($val); $array[] = $this->parseKey($val);
} elseif ('[rand]' == $val) {
$array[] = $this->parseRand();
} }
} else { } else {
$sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : ''; $sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : '';

View File

@@ -107,4 +107,14 @@ class Mysql extends Driver
return $key; return $key;
} }
/**
* 随机排序
* @access protected
* @return string
*/
protected function parseRand()
{
return 'rand()';
}
} }

View File

@@ -189,4 +189,14 @@ class Oracle extends Driver
} }
return $key; return $key;
} }
/**
* 随机排序
* @access protected
* @return string
*/
protected function parseRand()
{
return 'DBMS_RANDOM.value';
}
} }

View File

@@ -110,4 +110,14 @@ class Pgsql extends Driver
} }
return $key; return $key;
} }
/**
* 随机排序
* @access protected
* @return string
*/
protected function parseRand()
{
return 'RANDOM()';
}
} }

View File

@@ -91,4 +91,14 @@ class Sqlite extends Driver
} }
return $limitStr; return $limitStr;
} }
/**
* 随机排序
* @access protected
* @return string
*/
protected function parseRand()
{
return 'RANDOM()';
}
} }

View File

@@ -93,24 +93,13 @@ class Sqlsrv extends Driver
} }
/** /**
* order分析 * 随机排序
* @access protected * @access protected
* @param mixed $order
* @return string * @return string
*/ */
protected function parseOrder($order) protected function parseRand()
{ {
$array = []; return 'rand()';
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()';
} }
/** /**