Начало работы над search()

This commit is contained in:
RedHood 2021-01-20 13:59:35 +10:00
parent a4be42cc82
commit 3355611e20
7 changed files with 95 additions and 26 deletions

View File

@ -26,7 +26,7 @@
], ],
"require": { "require": {
"php": "^8.0.0", "php": "^8.0.0",
"yiisoft/yii2": "*", "yiisoft/yii2": "2.*",
"triagens/arangodb": "~3.2" "triagens/arangodb": "~3.2"
}, },
"require-dev": { "require-dev": {

View File

@ -158,4 +158,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface
return null; return null;
} }
} }
public function view() {
}
} }

View File

@ -251,7 +251,7 @@ abstract class ActiveRecord extends BaseActiveRecord
*/ */
public static function getDb() public static function getDb()
{ {
return \Yii::$app->get('arangodb'); return Yii::$app->get('arangodb');
} }
protected static function findByCondition($condition) protected static function findByCondition($condition)

View File

@ -86,4 +86,6 @@ abstract class Migration extends Component implements MigrationInterface
$this->db->getCollectionHandler()->truncate($collection); $this->db->getCollectionHandler()->truncate($collection);
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n"; echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
} }
// Разработать создание графов и представлений
} }

View File

@ -3,8 +3,6 @@
namespace mirzaev\yii2\arangodb; namespace mirzaev\yii2\arangodb;
use Yii; use Yii;
use ArangoDBClient\Document;
use ArangoDBClient\Statement;
use yii\base\Component; use yii\base\Component;
use yii\base\InvalidParamException; use yii\base\InvalidParamException;
use yii\base\NotSupportedException; use yii\base\NotSupportedException;
@ -12,6 +10,12 @@ use yii\db\QueryInterface;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\helpers\Json; use yii\helpers\Json;
use ArangoDBClient\Document;
use ArangoDBClient\Statement;
use Exception;
class Query extends Component implements QueryInterface class Query extends Component implements QueryInterface
{ {
const PARAM_PREFIX = 'qp'; const PARAM_PREFIX = 'qp';
@ -117,7 +121,7 @@ class Query extends Component implements QueryInterface
* @param array $bindValues * @param array $bindValues
* @param array $params * @param array $params
* @return array * @return array
* @throws \Exception * @throws Exception
*/ */
public function execute($aql, $bindValues = [], $params = []) public function execute($aql, $bindValues = [], $params = [])
{ {
@ -136,9 +140,9 @@ class Query extends Component implements QueryInterface
Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query'); Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query');
$cursor = $statement->execute(); $cursor = $statement->execute();
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query');
} catch (\Exception $ex) { } catch (Exception $ex) {
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); 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()); return $this->prepareResult($cursor->getAll());
} }
@ -613,7 +617,7 @@ class Query extends Component implements QueryInterface
/** /**
* @param null $db * @param null $db
* @return array * @return array
* @throws \Exception * @throws Exception
*/ */
public function all($db = null) public function all($db = null)
{ {
@ -624,9 +628,9 @@ class Query extends Component implements QueryInterface
Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query'); Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query');
$cursor = $statement->execute(); $cursor = $statement->execute();
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query');
} catch (\Exception $ex) { } catch (Exception $ex) {
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); 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()); return $this->prepareResult($cursor->getAll());
} }
@ -634,7 +638,7 @@ class Query extends Component implements QueryInterface
/** /**
* @param null $db * @param null $db
* @return array|bool * @return array|bool
* @throws \Exception * @throws Exception
*/ */
public function one($db = null) public function one($db = null)
{ {
@ -646,9 +650,9 @@ class Query extends Component implements QueryInterface
Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query'); Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query');
$cursor = $statement->execute(); $cursor = $statement->execute();
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query');
} catch (\Exception $ex) { } catch (Exception $ex) {
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); 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()); $result = $this->prepareResult($cursor->getAll());
return empty($result) ? false : $result[0]; return empty($result) ? false : $result[0];
@ -660,7 +664,7 @@ class Query extends Component implements QueryInterface
* @param array $params * @param array $params
* @param null $db * @param null $db
* @return bool * @return bool
* @throws \Exception * @throws Exception
*/ */
public function insert($collection, $columns, $params = [], $db = null) 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'); Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::insert');
$cursor = $statement->execute(); $cursor = $statement->execute();
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::insert'); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::insert');
} catch (\Exception $ex) { } catch (Exception $ex) {
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::insert'); 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; return true;
} }
@ -701,7 +705,7 @@ class Query extends Component implements QueryInterface
* @param array $params * @param array $params
* @param null $db * @param null $db
* @return bool * @return bool
* @throws \Exception * @throws Exception
*/ */
public function update($collection, $columns, $condition = [], $params = [], $db = null) 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'); Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::update');
$cursor = $statement->execute(); $cursor = $statement->execute();
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::update'); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::update');
} catch (\Exception $ex) { } catch (Exception $ex) {
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::update'); 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(); $meta = $cursor->getMetadata();
return isset($meta['extra']['operations']['executed']) ? return isset($meta['extra']['operations']['executed']) ?
@ -746,7 +799,7 @@ class Query extends Component implements QueryInterface
* @param array $params * @param array $params
* @param null $db * @param null $db
* @return bool * @return bool
* @throws \Exception * @throws Exception
*/ */
public function remove($collection, $condition = [], $params = [], $db = null) 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'); Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::remove');
$cursor = $statement->execute(); $cursor = $statement->execute();
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::remove'); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::remove');
} catch (\Exception $ex) { } catch (Exception $ex) {
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::remove'); 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(); $meta = $cursor->getMetadata();
return isset($meta['extra']['operations']['executed']) ? return isset($meta['extra']['operations']['executed']) ?
@ -806,6 +859,16 @@ class Query extends Component implements QueryInterface
return 'REMOVE ' . $collection . ' IN ' . $collection; return 'REMOVE ' . $collection . ' IN ' . $collection;
} }
/**
* @param $collection
* @param $columns
* @return string
*/
protected function buildSearch(string $expression,): string
{
return 'SEARCH ' . Serializer::encode($expression);
}
/** /**
* @return string * @return string
*/ */
@ -846,7 +909,7 @@ class Query extends Component implements QueryInterface
* @param string $q * @param string $q
* @param null $db * @param null $db
* @return int * @return int
* @throws \Exception * @throws Exception
* @throws \triagens\ArangoDb\ClientException * @throws \triagens\ArangoDb\ClientException
*/ */
public function count($q = '*', $db = null) public function count($q = '*', $db = null)
@ -864,9 +927,9 @@ class Query extends Component implements QueryInterface
Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query'); Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::query');
$cursor = $statement->execute(); $cursor = $statement->execute();
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query');
} catch (\Exception $ex) { } catch (Exception $ex) {
Yii::endProfile($token, 'mirzaev\yii2\arangodb\Query::query'); 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(); return $cursor->getFullCount();
} }
@ -874,7 +937,7 @@ class Query extends Component implements QueryInterface
/** /**
* @param null $db * @param null $db
* @return bool * @return bool
* @throws \Exception * @throws Exception
*/ */
public function exists($db = null) public function exists($db = null)
{ {