markjaquith/wherewithal
Composer 安装命令:
composer require markjaquith/wherewithal
包简介
Given constraints, parses a string of conditions into a valid MySQL WHERE clause
README 文档
README
Given constraints, parses a string of conditions into a valid MySQL WHERE clause
Installation
composer require markjaquith/wherewithal
Usage
use MarkJaquith\Wherewithal\{Parser, Config}; $config = (new Config) ->addOperators('<', '>', '<=', '>=', '=', '/') // Or add a subset. ->addColumn('column_name', 'column_alias1', 'column_alias2') ->addColumn('quantity') ->addColumn('price', 'cost'); $parser = new Parser($config); $structure = $parser->parse('quantity > 5 and (price < 3.00 or column_alias2 = 10')); $structure->toString(); /* string(57) "quantity > ? and ( price < ? or price / column_name = ? )" */ $structure->getBindings()]); /* array(3) { [0]=> string(1) "5" [1]=> string(4) "3.00" [2]=> string(2) "10" } */
You can also map simple column names to complex expressions like so:
$structure->mapColumns(function($col) { return [ 'column_name' => '`some_long_table_name`.`long_column_name`', ]($col) ?? $col; })->toString(); /* string(92) "(`some_long_table_name`.`long_column_name`) > ? and ( price < ? or price / column_name = ? )" */
Columns that you don't put in the config will be assumed to be values. Values
always use the placeholder token ?.
You should combine the resulting WHERE (or HAVING) clause using your database
layer. Here's how you'd do it in Laravel:
$orders = DB::table('orders') ->whereRaw($structure->toString(), $structure->getBindings()) ->get();
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-09