增加Expression类及相关方法

This commit is contained in:
thinkphp
2018-04-12 09:25:46 +08:00
parent 5d3c891a39
commit 8ae0fdef46
8 changed files with 169 additions and 19 deletions

View File

@@ -86,13 +86,16 @@ class Mysql extends Builder
* @param array $options
* @return string
*/
protected function parseKey($key, $options = [])
protected function parseKey($key, $options = [], $strict = false)
{
if (is_int($key)) {
return $key;
}
$key = trim($key);
if (strpos($key, '$.') && false === strpos($key, '(')) {
// JSON字段支持
list($field, $name) = explode('$.', $key);
$key = 'json_extract(' . $field . ', \'$.' . $name . '\')';
return 'json_extract(' . $field . ', \'$.' . $name . '\')';
} elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) {
list($table, $key) = explode('.', $key, 2);
if ('__TABLE__' == $table) {
@@ -102,7 +105,7 @@ class Mysql extends Builder
$table = $options['alias'][$table];
}
}
if (!preg_match('/[,\'\"\*\(\)`.\s]/', $key)) {
if ($strict || !preg_match('/[,\'\"\*\(\)`.\s]/', $key)) {
$key = '`' . $key . '`';
}
if (isset($table)) {

View File

@@ -48,7 +48,7 @@ class Pgsql extends Builder
* @param array $options
* @return string
*/
protected function parseKey($key, $options = [])
protected function parseKey($key, $options = [], $strict = false)
{
$key = trim($key);
if (strpos($key, '$.') && false === strpos($key, '(')) {

View File

@@ -56,7 +56,7 @@ class Sqlite extends Builder
* @param array $options
* @return string
*/
protected function parseKey($key, $options = [])
protected function parseKey($key, $options = [], $strict = false)
{
$key = trim($key);
if (strpos($key, '.')) {

View File

@@ -12,6 +12,7 @@
namespace think\db\builder;
use think\db\Builder;
use think\db\Expression;
/**
* Sqlsrv数据库驱动
@@ -37,7 +38,9 @@ class Sqlsrv extends Builder
if (is_array($order)) {
$array = [];
foreach ($order as $key => $val) {
if (is_numeric($key)) {
if ($val instanceof Expression) {
$array[] = $val->getValue();
} elseif (is_numeric($key)) {
if (false === strpos($val, '(')) {
$array[] = $this->parseKey($val, $options);
} elseif ('[rand]' == $val) {
@@ -72,8 +75,11 @@ class Sqlsrv extends Builder
* @param array $options
* @return string
*/
protected function parseKey($key, $options = [])
protected function parseKey($key, $options = [], $strict = false)
{
if (is_int($key)) {
return $key;
}
$key = trim($key);
if (strpos($key, '.') && !preg_match('/[,\'\"\(\)\[\s]/', $key)) {
list($table, $key) = explode('.', $key, 2);
@@ -84,7 +90,7 @@ class Sqlsrv extends Builder
$table = $options['alias'][$table];
}
}
if (!is_numeric($key) && !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) {
if ($strict || !preg_match('/[,\'\"\*\(\)\[.\s]/', $key)) {
$key = '[' . $key . ']';
}
if (isset($table)) {