https://github.com/markjaquith/wherewithal
Given constraints, parses a string of conditions into a valid MySQL WHERE clause
https://github.com/markjaquith/wherewithal
Last synced: about 1 year ago
JSON representation
Given constraints, parses a string of conditions into a valid MySQL WHERE clause
- Host: GitHub
- URL: https://github.com/markjaquith/wherewithal
- Owner: markjaquith
- License: mit
- Created: 2021-03-08T19:41:13.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-12T05:11:16.000Z (over 5 years ago)
- Last Synced: 2024-11-16T23:34:29.563Z (over 1 year ago)
- Language: PHP
- Size: 48.8 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Wherewithal
Given constraints, parses a string of conditions into a valid MySQL WHERE clause

## Installation
`composer require markjaquith/wherewithal`
## Usage
```php
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:
```php
$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:
```php
$orders = DB::table('orders')
->whereRaw($structure->toString(), $structure->getBindings())
->get();
```