From 3355611e2008933d647e96316b2a488a7be705c5 Mon Sep 17 00:00:00 2001 From: RedHood Date: Wed, 20 Jan 2021 13:59:35 +1000 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D0=BD=D0=B0=D0=B4=20search()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../README.RU.md => README.RU.md | 0 mirzaev/yii2-arangodb/README.md => README.md | 0 composer.json | 2 +- mirzaev/yii2-arangodb/ActiveQuery.php | 4 + mirzaev/yii2-arangodb/ActiveRecord.php | 2 +- mirzaev/yii2-arangodb/Migration.php | 2 + mirzaev/yii2-arangodb/Query.php | 111 ++++++++++++++---- 7 files changed, 95 insertions(+), 26 deletions(-) rename mirzaev/yii2-arangodb/README.RU.md => README.RU.md (100%) rename mirzaev/yii2-arangodb/README.md => README.md (100%) diff --git a/mirzaev/yii2-arangodb/README.RU.md b/README.RU.md similarity index 100% rename from mirzaev/yii2-arangodb/README.RU.md rename to README.RU.md diff --git a/mirzaev/yii2-arangodb/README.md b/README.md similarity index 100% rename from mirzaev/yii2-arangodb/README.md rename to README.md diff --git a/composer.json b/composer.json index 43ad063..83ec625 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ ], "require": { "php": "^8.0.0", - "yiisoft/yii2": "*", + "yiisoft/yii2": "2.*", "triagens/arangodb": "~3.2" }, "require-dev": { diff --git a/mirzaev/yii2-arangodb/ActiveQuery.php b/mirzaev/yii2-arangodb/ActiveQuery.php index 3d27e28..5e21793 100644 --- a/mirzaev/yii2-arangodb/ActiveQuery.php +++ b/mirzaev/yii2-arangodb/ActiveQuery.php @@ -158,4 +158,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface return null; } } + + public function view() { + + } } diff --git a/mirzaev/yii2-arangodb/ActiveRecord.php b/mirzaev/yii2-arangodb/ActiveRecord.php index f0c803b..c1e3a61 100644 --- a/mirzaev/yii2-arangodb/ActiveRecord.php +++ b/mirzaev/yii2-arangodb/ActiveRecord.php @@ -251,7 +251,7 @@ abstract class ActiveRecord extends BaseActiveRecord */ public static function getDb() { - return \Yii::$app->get('arangodb'); + return Yii::$app->get('arangodb'); } protected static function findByCondition($condition) diff --git a/mirzaev/yii2-arangodb/Migration.php b/mirzaev/yii2-arangodb/Migration.php index 8fa1468..c50aae1 100644 --- a/mirzaev/yii2-arangodb/Migration.php +++ b/mirzaev/yii2-arangodb/Migration.php @@ -86,4 +86,6 @@ abstract class Migration extends Component implements MigrationInterface $this->db->getCollectionHandler()->truncate($collection); echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n"; } + + // Разработать создание графов и представлений } diff --git a/mirzaev/yii2-arangodb/Query.php b/mirzaev/yii2-arangodb/Query.php index fa96277..fa0da7c 100644 --- a/mirzaev/yii2-arangodb/Query.php +++ b/mirzaev/yii2-arangodb/Query.php @@ -3,8 +3,6 @@ namespace mirzaev\yii2\arangodb; use Yii; -use ArangoDBClient\Document; -use ArangoDBClient\Statement; use yii\base\Component; use yii\base\InvalidParamException; use yii\base\NotSupportedException; @@ -12,6 +10,12 @@ use yii\db\QueryInterface; use yii\helpers\ArrayHelper; use yii\helpers\Json; +use ArangoDBClient\Document; +use ArangoDBClient\Statement; + +use Exception; + + class Query extends Component implements QueryInterface { const PARAM_PREFIX = 'qp'; @@ -117,7 +121,7 @@ class Query extends Component implements QueryInterface * @param array $bindValues * @param array $params * @return array - * @throws \Exception + * @throws Exception */ public function execute($aql, $bindValues = [], $params = []) { @@ -136,9 +140,9 @@ class Query extends Component implements QueryInterface Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query'); $cursor = $statement->execute(); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); - } catch (\Exception $ex) { + } catch (Exception $ex) { Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); - throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex); + throw new Exception($ex->getMessage(), (int) $ex->getCode(), $ex); } return $this->prepareResult($cursor->getAll()); } @@ -613,7 +617,7 @@ class Query extends Component implements QueryInterface /** * @param null $db * @return array - * @throws \Exception + * @throws Exception */ public function all($db = null) { @@ -624,9 +628,9 @@ class Query extends Component implements QueryInterface Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query'); $cursor = $statement->execute(); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); - } catch (\Exception $ex) { + } catch (Exception $ex) { Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); - throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex); + throw new Exception($ex->getMessage(), (int) $ex->getCode(), $ex); } return $this->prepareResult($cursor->getAll()); } @@ -634,7 +638,7 @@ class Query extends Component implements QueryInterface /** * @param null $db * @return array|bool - * @throws \Exception + * @throws Exception */ public function one($db = null) { @@ -646,9 +650,9 @@ class Query extends Component implements QueryInterface Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query'); $cursor = $statement->execute(); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); - } catch (\Exception $ex) { + } catch (Exception $ex) { Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); - throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex); + throw new Exception($ex->getMessage(), (int) $ex->getCode(), $ex); } $result = $this->prepareResult($cursor->getAll()); return empty($result) ? false : $result[0]; @@ -660,7 +664,7 @@ class Query extends Component implements QueryInterface * @param array $params * @param null $db * @return bool - * @throws \Exception + * @throws Exception */ public function insert($collection, $columns, $params = [], $db = null) { @@ -687,9 +691,9 @@ class Query extends Component implements QueryInterface Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::insert'); $cursor = $statement->execute(); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::insert'); - } catch (\Exception $ex) { + } catch (Exception $ex) { Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::insert'); - throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex); + throw new Exception($ex->getMessage(), (int) $ex->getCode(), $ex); } return true; } @@ -701,7 +705,7 @@ class Query extends Component implements QueryInterface * @param array $params * @param null $db * @return bool - * @throws \Exception + * @throws Exception */ public function update($collection, $columns, $condition = [], $params = [], $db = null) { @@ -730,9 +734,58 @@ class Query extends Component implements QueryInterface Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::update'); $cursor = $statement->execute(); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::update'); - } catch (\Exception $ex) { + } catch (Exception $ex) { Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::update'); - throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex); + throw new Exception($ex->getMessage(), (int) $ex->getCode(), $ex); + } + $meta = $cursor->getMetadata(); + return isset($meta['extra']['operations']['executed']) ? + $meta['extra']['operations']['executed'] : + true; + } + + /** + * @param $collection + * @param $columns + * @param array $condition + * @param array $params + * @param null $db + * @return bool + * @throws Exception + */ + public function search($collection, $expression, $params = [], $db = null) + { + $this->from($collection); + $clauses = [ + $this->buildFrom($collection), + $this->buildSearch($expression), + $this->buildOptions(), + ]; + + $aql = implode($this->separator, array_filter($clauses)); + + $fp = fopen('debug.txt', 'a'); + fwrite($fp, print_r($aql, true) . PHP_EOL); + fclose($fp); + + $params = ArrayHelper::merge( + $params, + [ + 'query' => $aql, + 'bindVars' => $params, + ] + ); + + $statement = $this->getStatement($params, $db); + $token = $this->getRawAql($statement); + Yii::info($token, 'mirzaev\yii2\arangodb\Query::search'); + try { + Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::search'); + $cursor = $statement->execute(); + Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::search'); + } catch (Exception $ex) { + Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::search'); + throw new Exception($ex->getMessage(), (int) $ex->getCode(), $ex); } $meta = $cursor->getMetadata(); return isset($meta['extra']['operations']['executed']) ? @@ -746,7 +799,7 @@ class Query extends Component implements QueryInterface * @param array $params * @param null $db * @return bool - * @throws \Exception + * @throws Exception */ public function remove($collection, $condition = [], $params = [], $db = null) { @@ -775,9 +828,9 @@ class Query extends Component implements QueryInterface Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::remove'); $cursor = $statement->execute(); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::remove'); - } catch (\Exception $ex) { + } catch (Exception $ex) { Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::remove'); - throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex); + throw new Exception($ex->getMessage(), (int) $ex->getCode(), $ex); } $meta = $cursor->getMetadata(); return isset($meta['extra']['operations']['executed']) ? @@ -806,6 +859,16 @@ class Query extends Component implements QueryInterface return 'REMOVE ' . $collection . ' IN ' . $collection; } + /** + * @param $collection + * @param $columns + * @return string + */ + protected function buildSearch(string $expression,): string + { + return 'SEARCH ' . Serializer::encode($expression); + } + /** * @return string */ @@ -846,7 +909,7 @@ class Query extends Component implements QueryInterface * @param string $q * @param null $db * @return int - * @throws \Exception + * @throws Exception * @throws \triagens\ArangoDb\ClientException */ public function count($q = '*', $db = null) @@ -864,9 +927,9 @@ class Query extends Component implements QueryInterface Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query'); $cursor = $statement->execute(); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); - } catch (\Exception $ex) { + } catch (Exception $ex) { Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); - throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex); + throw new Exception($ex->getMessage(), (int) $ex->getCode(), $ex); } return $cursor->getFullCount(); } @@ -874,7 +937,7 @@ class Query extends Component implements QueryInterface /** * @param null $db * @return bool - * @throws \Exception + * @throws Exception */ public function exists($db = null) {