From b92e8792f0e822f37c944bbd318e1422951687a7 Mon Sep 17 00:00:00 2001 From: Arsen Mirzaev Tatyano-Muradovich Date: Tue, 29 Jun 2021 09:07:43 +1000 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=84=D0=B8=D0=BB=D1=8C=D1=82=D1=80=D0=B0?= =?UTF-8?q?=20STARTS=5FWIDTH=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B5=D0=B3?= =?UTF-8?q?=D0=B8=D1=81=D1=82=D1=80=D0=BE=D0=BD=D0=B5=D0=B7=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D1=81=D0=B8=D0=BC=D0=BE=D0=B3=D0=BE=20=D0=BF=D0=BE=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mirzaev/yii2/arangodb/Query.php | 73 +++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/mirzaev/yii2/arangodb/Query.php b/mirzaev/yii2/arangodb/Query.php index 972e4ef..be0f689 100644 --- a/mirzaev/yii2/arangodb/Query.php +++ b/mirzaev/yii2/arangodb/Query.php @@ -81,6 +81,18 @@ class Query extends Component implements QueryInterface */ public string $searchType = 'START'; + /** + * Поиск + * + * [свойство => его значение] + */ + public array $filter; + + /** + * Тип поиска + */ + public string $filterType = 'START'; + public $orderBy; public $indexBy; @@ -229,13 +241,21 @@ class Query extends Component implements QueryInterface return $this; } + /** + */ + public function filter(array $text, string $type = 'START'): self + { + $this->filter = $text; + $this->filterType = $type; + + return $this; + } + /** * Обойти коллекцию вершин по направлению * * Генерация AQL выражения * - * @see https://www.arangodb.com/docs/3.7/aql/operations-let.html - * * @param mixed $vertex Коллекция вершин из которой требуется обход * @param string $direction Направление ('INBOUND', 'OUTBOUND', 'ANY') */ @@ -851,6 +871,7 @@ class Query extends Component implements QueryInterface $query->genForeach($query->foreach), $query->genWhere($query->where, $params), isset($query->search) ? $query->genSearch($query->search, $query->searchType) : null, + isset($query->filter) ? $query->genFilter($query->filter, $query->filterType) : null, $query->genOrderBy($query->orderBy, $params), $query->genLimit($query->limit, $query->offset, $params), $query->genSelect($query->select, $params), @@ -1115,6 +1136,24 @@ class Query extends Component implements QueryInterface return match (strtoupper($type)) { 'START', 'STARTS', 'STARTS_WITH' => $query . $this->filterStartsWith($expression), + 'START_SENSETIVE' => $query . $this->filterStartsWith($expression, sensetive: true), + 'CONTAINS', 'LIKE' => $query . $this->filterContains($expression), + default => $query . Serializer::encode($expression) + }; + } + + /** + * @param $collection + * @param $columns + * @return string + */ + protected function genFilter(array $expression, string $type = 'START'): string + { + $query = 'FILTER '; + + return match (strtoupper($type)) { + 'START', 'STARTS', 'STARTS_WITH' => $query . $this->filterStartsWith($expression), + 'START_SENSETIVE' => $query . $this->filterStartsWith($expression, sensetive: true), 'CONTAINS', 'LIKE' => $query . $this->filterContains($expression), default => $query . Serializer::encode($expression) }; @@ -1157,8 +1196,6 @@ class Query extends Component implements QueryInterface * * Генерация AQL выражения * - * @see https://www.arangodb.com/docs/3.7/aql/operations-let.html - * * @param string $direction Направление * @param mixed $vertex Коллекция вершин из которой требуется обход */ @@ -1303,7 +1340,7 @@ class Query extends Component implements QueryInterface { $this->foreach = match (true) { empty($this->foreach) => [$expression], - default => $this->foreach []= [$expression] + default => $this->foreach[] = [$expression] }; return $this; @@ -1399,20 +1436,38 @@ class Query extends Component implements QueryInterface return $this; } - public function filterStartsWith(array $expression): string + public function filterStartsWith(array $expression, bool $sensetive = false): string { // Генерация foreach ($expression as $key => $value) { - if (isset($return)) { - $return .= ' OR STARTS_WITH(' . $this->quoteCollectionName($this->collection) . ".$key, \"$value\")"; + if ($sensetive) { + if (isset($return)) { + $return .= ' OR STARTS_WITH(' . $this->filterLower($this->quoteCollectionName($this->collection) . ".$key") . ", " . $this->filterLower("\"$value\"") . ")"; + } else { + $return = 'STARTS_WITH(' . $this->filterLower($this->quoteCollectionName($this->collection) . ".$key") . ", " . $this->filterLower("\"$value\"") . ")"; + } } else { - $return = 'STARTS_WITH(' . $this->quoteCollectionName($this->collection) . ".$key, \"$value\")"; + if (isset($return)) { + $return .= ' OR STARTS_WITH(' . $this->quoteCollectionName($this->collection) . ".$key, \"$value\")"; + } else { + $return = 'STARTS_WITH(' . $this->quoteCollectionName($this->collection) . ".$key, \"$value\")"; + } } } return $return; } + public function filterUpper(string $target): string + { + return "UPPER($target)"; + } + + public function filterLower(string $target): string + { + return "LOWER($target)"; + } + public function filterContains(array $expression): string { // Инициализация