mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 23:52:49 +08:00
改进聚合查询的安全性
This commit is contained in:
@@ -98,6 +98,10 @@ abstract class Builder
|
|||||||
|
|
||||||
$result = [];
|
$result = [];
|
||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
|
if ('*' != $options['field'] && !in_array($key, $fields, true)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$item = $this->parseKey($key, $options, true);
|
$item = $this->parseKey($key, $options, true);
|
||||||
if ($val instanceof Expression) {
|
if ($val instanceof Expression) {
|
||||||
$result[$item] = $val->getValue();
|
$result[$item] = $val->getValue();
|
||||||
|
|||||||
@@ -531,13 +531,36 @@ class Query
|
|||||||
public function count($field = '*')
|
public function count($field = '*')
|
||||||
{
|
{
|
||||||
if (isset($this->options['group'])) {
|
if (isset($this->options['group'])) {
|
||||||
|
if (!preg_match('/^[\w\.\*]+$/', $field)) {
|
||||||
|
throw new Exception('not support data:' . $field);
|
||||||
|
}
|
||||||
// 支持GROUP
|
// 支持GROUP
|
||||||
$options = $this->getOptions();
|
$options = $this->getOptions();
|
||||||
$subSql = $this->options($options)->field('count(' . $field . ')')->bind($this->bind)->buildSql();
|
$subSql = $this->options($options)->field('count(' . $field . ')')->bind($this->bind)->buildSql();
|
||||||
|
|
||||||
return $this->table([$subSql => '_group_count_'])->value('COUNT(*) AS tp_count', 0, true);
|
return $this->table([$subSql => '_group_count_'])->value('COUNT(*) AS tp_count', 0, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->value('COUNT(' . $field . ') AS tp_count', 0, true);
|
return $this->aggregate('COUNT', $field, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 聚合查询
|
||||||
|
* @access public
|
||||||
|
* @param string $aggregate 聚合方法
|
||||||
|
* @param string $field 字段名
|
||||||
|
* @param bool $force 强制转为数字类型
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function aggregate($aggregate, $field, $force = false)
|
||||||
|
{
|
||||||
|
if (!preg_match('/^[\w\.\*]+$/', $field)) {
|
||||||
|
throw new Exception('not support data:' . $field);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $this->value($aggregate . '(' . $field . ') AS tp_' . strtolower($aggregate), 0, $force);
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -548,7 +571,7 @@ class Query
|
|||||||
*/
|
*/
|
||||||
public function sum($field)
|
public function sum($field)
|
||||||
{
|
{
|
||||||
return $this->value('SUM(' . $field . ') AS tp_sum', 0, true);
|
return $this->aggregate('SUM', $field, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -560,7 +583,7 @@ class Query
|
|||||||
*/
|
*/
|
||||||
public function min($field, $force = true)
|
public function min($field, $force = true)
|
||||||
{
|
{
|
||||||
return $this->value('MIN(' . $field . ') AS tp_min', 0, $force);
|
return $this->aggregate('MIN', $field, $force);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -572,7 +595,7 @@ class Query
|
|||||||
*/
|
*/
|
||||||
public function max($field, $force = true)
|
public function max($field, $force = true)
|
||||||
{
|
{
|
||||||
return $this->value('MAX(' . $field . ') AS tp_max', 0, $force);
|
return $this->aggregate('MAX', $field, $force);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -583,7 +606,7 @@ class Query
|
|||||||
*/
|
*/
|
||||||
public function avg($field)
|
public function avg($field)
|
||||||
{
|
{
|
||||||
return $this->value('AVG(' . $field . ') AS tp_avg', 0, true);
|
return $this->aggregate('AVG', $field, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -109,6 +109,9 @@ class Mysql extends Builder
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) {
|
||||||
|
throw new Exception('not support data:' . $key);
|
||||||
|
}
|
||||||
if ('*' != $key && ($strict || !preg_match('/[,\'\"\*\(\)`.\s]/', $key))) {
|
if ('*' != $key && ($strict || !preg_match('/[,\'\"\*\(\)`.\s]/', $key))) {
|
||||||
$key = '`' . $key . '`';
|
$key = '`' . $key . '`';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,6 +94,10 @@ class Sqlsrv extends Builder
|
|||||||
$table = $options['alias'][$table];
|
$table = $options['alias'][$table];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($strict && !preg_match('/^[\w\.\*]+$/', $key)) {
|
||||||
|
throw new Exception('not support data:' . $key);
|
||||||
|
}
|
||||||
if ('*' != $key && ($strict || !preg_match('/[,\'\"\*\(\)\[.\s]/', $key))) {
|
if ('*' != $key && ($strict || !preg_match('/[,\'\"\*\(\)\[.\s]/', $key))) {
|
||||||
$key = '[' . $key . ']';
|
$key = '[' . $key . ']';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user