diff --git a/library/think/db/driver.php b/library/think/db/driver.php index 06417f0f..0fdc6460 100644 --- a/library/think/db/driver.php +++ b/library/think/db/driver.php @@ -15,7 +15,6 @@ use PDO; use think\Config; use think\Debug; use think\Exception; -use think\Lang; use think\Log; abstract class Driver @@ -58,7 +57,7 @@ abstract class Driver 'slave_no' => '', // 指定从服务器序号 ]; // 数据库表达式 - protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'notin' => 'NOT IN','not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN']; + protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN']; // 查询表达式 protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%LOCK%%COMMENT%'; // 查询次数 @@ -636,7 +635,7 @@ abstract class Driver $data = is_string($val[1]) ? explode(',', $val[1]) : $val[1]; $whereStr .= $key . ' ' . $this->exp[$exp] . ' ' . $this->parseValue($data[0]) . ' AND ' . $this->parseValue($data[1]); } else { - throw new Exception('where express error:'. $val[0]); + throw new Exception('where express error:' . $val[0]); } } else { $count = count($val); @@ -715,7 +714,7 @@ abstract class Driver */ 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 (false === strpos($val, '(')) { $array[] = $this->parseKey($val); + } elseif ('[rand]' == $val) { + $array[] = $this->parseRand(); } } else { $sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : ''; diff --git a/library/think/db/driver/mysql.php b/library/think/db/driver/mysql.php index aa1434b6..5e05bb4a 100644 --- a/library/think/db/driver/mysql.php +++ b/library/think/db/driver/mysql.php @@ -107,4 +107,14 @@ class Mysql extends Driver return $key; } + + /** + * 随机排序 + * @access protected + * @return string + */ + protected function parseRand() + { + return 'rand()'; + } } diff --git a/library/think/db/driver/oracle.php b/library/think/db/driver/oracle.php index ce7d94eb..259cdc45 100644 --- a/library/think/db/driver/oracle.php +++ b/library/think/db/driver/oracle.php @@ -189,4 +189,14 @@ class Oracle extends Driver } return $key; } + + /** + * 随机排序 + * @access protected + * @return string + */ + protected function parseRand() + { + return 'DBMS_RANDOM.value'; + } } diff --git a/library/think/db/driver/pgsql.php b/library/think/db/driver/pgsql.php index b980bf1d..6fb29400 100644 --- a/library/think/db/driver/pgsql.php +++ b/library/think/db/driver/pgsql.php @@ -110,4 +110,14 @@ class Pgsql extends Driver } return $key; } + + /** + * 随机排序 + * @access protected + * @return string + */ + protected function parseRand() + { + return 'RANDOM()'; + } } diff --git a/library/think/db/driver/sqlite.php b/library/think/db/driver/sqlite.php index d2ae2401..3a7c7429 100644 --- a/library/think/db/driver/sqlite.php +++ b/library/think/db/driver/sqlite.php @@ -91,4 +91,14 @@ class Sqlite extends Driver } return $limitStr; } + + /** + * 随机排序 + * @access protected + * @return string + */ + protected function parseRand() + { + return 'RANDOM()'; + } } diff --git a/library/think/db/driver/sqlsrv.php b/library/think/db/driver/sqlsrv.php index 7bffeef8..f6d17177 100644 --- a/library/think/db/driver/sqlsrv.php +++ b/library/think/db/driver/sqlsrv.php @@ -93,24 +93,13 @@ class Sqlsrv extends Driver } /** - * order分析 + * 随机排序 * @access protected - * @param mixed $order * @return string */ - protected function parseOrder($order) + protected function parseRand() { - $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()'; + return 'rand()'; } /**