提交 09a69fb2 编写于 作者: weixin_47267244's avatar weixin_47267244

Query->where() 条件的值支持传原始 SQL

上级 1946e8b9
...@@ -16,6 +16,8 @@ v2.1.0 版本相比 v2.0.0 版本不会出现非常大的不兼容更改,可 ...@@ -16,6 +16,8 @@ v2.1.0 版本相比 v2.0.0 版本不会出现非常大的不兼容更改,可
* [枚举类增加 validate() 和 assert()](/components/struct/enum.html) * [枚举类增加 validate() 和 assert()](/components/struct/enum.html)
* [Query->where() 条件的值支持传原始 SQL](/components/db/index.html)
### v2.1.1 ### v2.1.1
* [定时任务的最小、最大延迟执行秒数](/components/task/cron.html) * [定时任务的最小、最大延迟执行秒数](/components/task/cron.html)
......
...@@ -563,6 +563,9 @@ Db::query()->where('id', '=', 1); ...@@ -563,6 +563,9 @@ Db::query()->where('id', '=', 1);
// id > 1 // id > 1
Db::query()->where('id', '>', 1); Db::query()->where('id', '>', 1);
// 使用 Raw 原样代入值,例:value = 1 + 2
Db::query()->where('value', '=', new \Imi\Db\Query\Raw('1 + 2'));
// title like '%test%' // title like '%test%'
Db::query()->where('title', 'like', '%test%'); Db::query()->where('title', 'like', '%test%');
......
...@@ -7,6 +7,7 @@ namespace Imi\Db\Query\Where; ...@@ -7,6 +7,7 @@ namespace Imi\Db\Query\Where;
use Imi\Db\Mysql\Consts\LogicalOperator; use Imi\Db\Mysql\Consts\LogicalOperator;
use Imi\Db\Query\Interfaces\IQuery; use Imi\Db\Query\Interfaces\IQuery;
use Imi\Db\Query\Interfaces\IWhere; use Imi\Db\Query\Interfaces\IWhere;
use Imi\Db\Query\Raw;
use Imi\Db\Query\Traits\TRaw; use Imi\Db\Query\Traits\TRaw;
class Where extends BaseWhere implements IWhere class Where extends BaseWhere implements IWhere
...@@ -138,6 +139,10 @@ class Where extends BaseWhere implements IWhere ...@@ -138,6 +139,10 @@ class Where extends BaseWhere implements IWhere
{ {
case 'between': case 'between':
case 'not between': case 'not between':
if (!\is_array($thisValues) || !isset($thisValues[0],$thisValues[1]))
{
throw new \RuntimeException(sprintf('where %s value must be [beginValue, endValue]', $operation));
}
$begin = $query->getAutoParamName(); $begin = $query->getAutoParamName();
$end = $query->getAutoParamName(); $end = $query->getAutoParamName();
$result .= "{$begin} and {$end}"; $result .= "{$begin} and {$end}";
...@@ -148,18 +153,36 @@ class Where extends BaseWhere implements IWhere ...@@ -148,18 +153,36 @@ class Where extends BaseWhere implements IWhere
case 'not in': case 'not in':
$result .= '('; $result .= '(';
$valueNames = []; $valueNames = [];
foreach ($thisValues as $value) if (\is_array($thisValues))
{
foreach ($thisValues as $value)
{
$paramName = $query->getAutoParamName();
$valueNames[] = $paramName;
$binds[$paramName] = $value;
}
$result .= implode(',', $valueNames) . ')';
}
elseif ($thisValues instanceof Raw)
{
$result .= $thisValues->toString($query) . ')';
}
else
{ {
$paramName = $query->getAutoParamName(); throw new \RuntimeException(sprintf('Invalid value type %s of where %s', \gettype($thisValues), $operation));
$valueNames[] = $paramName;
$binds[$paramName] = $value;
} }
$result .= implode(',', $valueNames) . ')';
break; break;
default: default:
$value = $query->getAutoParamName(); if ($thisValues instanceof Raw)
$result .= $value; {
$binds[$value] = $thisValues; $result .= $thisValues->toString($query);
}
else
{
$value = $query->getAutoParamName();
$result .= $value;
$binds[$value] = $thisValues;
}
break; break;
} }
......
...@@ -6,6 +6,7 @@ namespace Imi\Test\Component\Tests\Db; ...@@ -6,6 +6,7 @@ namespace Imi\Test\Component\Tests\Db;
use Imi\Db\Db; use Imi\Db\Db;
use Imi\Db\Mysql\Query\Lock\MysqlLock; use Imi\Db\Mysql\Query\Lock\MysqlLock;
use Imi\Db\Query\Raw;
use Imi\Test\BaseTest; use Imi\Test\BaseTest;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
...@@ -421,4 +422,17 @@ abstract class QueryCurdBaseTest extends BaseTest ...@@ -421,4 +422,17 @@ abstract class QueryCurdBaseTest extends BaseTest
'json_data' => '{"a": "1", "uid": "' . $uid . '", "name": "bbb", "list1": [{"id": "2"}], "list2": [1, 2, 3]}', 'json_data' => '{"a": "1", "uid": "' . $uid . '", "name": "bbb", "list1": [{"id": "2"}], "list2": [1, 2, 3]}',
], $result->get()); ], $result->get());
} }
/**
* @depends testInsert
*/
public function testRaw(array $args): void
{
['ids' => $ids] = $args;
$id = $ids[0];
$query = Db::query($this->poolName);
$record = $query->from($this->tableArticle)->where('id', '=', new Raw((string) $id))->select()->get();
Assert::assertEquals($id, $record['id']);
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册