Added REGEXP operator for API query filter (closes #1174)

This commit is contained in:
Bernd Bestel 2020-12-12 10:44:27 +01:00
parent d3883ba93a
commit 59aad1c180
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
4 changed files with 15 additions and 6 deletions

View File

@ -199,10 +199,12 @@
- `=` equal
- `!=` not equal
- `~` LIKE
- `!~` not LIKE
- `<` less
- `>` greater
- `>=` greater or equal
- `<=` less or equal
- `>=` greater or equal
- `§` regular expression
- `<value>` is the value to search for
- New endpoint `/stock/shoppinglist/add-overdue-products` to add all currently in-stock but overdue products to a shopping list (thanks @m-byte)
- New endpoint `/stock/shoppinglist/add-expired-products` to add all currently in-stock but expired products to a shopping list

View File

@ -9,8 +9,8 @@ class BaseApiController extends BaseController
protected $OpenApiSpec = null;
const PATTERN_FIELD = '[A-Za-z_][A-Za-z0-9_]+';
const PATTERN_OPERATOR = '!?(=|~|<|>|(>=)|(<=))';
const PATTERN_VALUE = '[A-Za-z_0-9.]+';
const PATTERN_OPERATOR = '!?(=|~|<|>|(>=)|(<=)|(§))';
const PATTERN_VALUE = '[A-Za-z_0-9.$#^|]+';
public function __construct(\DI\Container $container)
{
@ -123,6 +123,9 @@ class BaseApiController extends BaseController
case '<=':
$data = $data->where($matches['field'] . ' <= ?', $matches['value']);
break;
case '§':
$data = $data->where($matches['field'] . ' REGEXP ?', $matches['value']);
break;
}
}

View File

@ -5093,7 +5093,7 @@
"in": "query",
"name": "query[]",
"required": false,
"description": "An array of filter conditions, each of them is a string in the form of `<field><condition><value>` where<br>`<field>` is a valid field name<br>`<condition>` is a comparison operator, one of<br>&nbsp;&nbsp;`=` equal<br>&nbsp;&nbsp;`!=` not equal<br>&nbsp;&nbsp;`~` LIKE<br>&nbsp;&nbsp;`<` less<br>&nbsp;&nbsp;`>` greater<br>&nbsp;&nbsp;`<=` less or equal<br>&nbsp;&nbsp;`>=` (greater or equal<br>`<value>` is the value to search for",
"description": "An array of filter conditions, each of them is a string in the form of `<field><condition><value>` where<br>`<field>` is a valid field name<br>`<condition>` is a comparison operator, one of<br>&nbsp;&nbsp;`=` equal<br>&nbsp;&nbsp;`!=` not equal<br>&nbsp;&nbsp;`~` LIKE<br>&nbsp;&nbsp;`!~` not LIKE<br>&nbsp;&nbsp;`<` less<br>&nbsp;&nbsp;`>` greater<br>&nbsp;&nbsp;`<=` less or equal<br>&nbsp;&nbsp;`>=` greater or equal<br>&nbsp;&nbsp;`§` regular expression<br>`<value>` is the value to search for",
"schema": {
"type": "array",
"items": {

View File

@ -5,9 +5,7 @@ namespace Grocy\Services;
class DatabaseService
{
private static $DbConnection = null;
private static $DbConnectionRaw = null;
private static $instance = null;
/**
@ -67,6 +65,12 @@ class DatabaseService
{
$pdo = new \PDO('sqlite:' . $this->GetDbFilePath());
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$pdo->sqliteCreateFunction('regexp', function ($pattern, $value) {
mb_regex_encoding('UTF-8');
return (false !== mb_ereg($pattern, $value)) ? 1 : 0;
});
self::$DbConnectionRaw = $pdo;
}