Доработка удаления, обновления и генерации запроса

This commit is contained in:
Arsen Mirzaev Tatyano-Muradovich 2021-03-22 05:01:43 +10:00
parent 1592238e5f
commit a7abed56d6
15 changed files with 43 additions and 22 deletions

View File

@ -34,12 +34,12 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"mirzaev\\yii2\\arangodb\\": "mirzaev/yii2-arangodb" "mirzaev\\yii2\\arangodb\\": "mirzaev/yii2/arangodb"
} }
}, },
"autoload-dev": { "autoload-dev": {
"psr-4": { "psr-4": {
"mirzaev\\yii2\\arangodb\\tests\\": "mirzaev/yii2-arangodb/tests" "mirzaev\\yii2\\arangodb\\tests\\": "mirzaev/yii2/arangodb/tests"
} }
} }
} }

View File

@ -22,7 +22,8 @@ abstract class Migration extends Component implements MigrationInterface
public function init() public function init()
{ {
parent::init(); parent::init();
$this->db = Instance::ensure($this->db, Connection::className());
$this->db = Instance::ensure($this->db, Connection::class;
} }
public function execute($aql, $bindValues = [], $params = []) public function execute($aql, $bindValues = [], $params = [])

View File

@ -48,7 +48,7 @@ class Query extends Component implements QueryInterface
public string $collection; public string $collection;
public array $vars = []; public array $lets = [];
/** /**
* Массив коллекций вершин и направлений для их обхода * Массив коллекций вершин и направлений для их обхода
@ -818,14 +818,18 @@ class Query extends Component implements QueryInterface
*/ */
protected function genQuery($query = null, array $params = []) protected function genQuery($query = null, array $params = [])
{ {
// Инициализация
isset($query) ? $query : $query = $this; isset($query) ? $query : $query = $this;
$this->in ?? (isset($this->collection) ? $this->in = $this->collection : throw new Exception('Не найдена коллекция'));
$this->for ?? $this->for = $this->in;
$this->collection ?? $this->collection = $this->in;
$params = array_merge($params, $query->params); $params = array_merge($params, $query->params);
$clauses = [ $clauses = [
static::genFor($query->for ?? $query->collection), static::genFor($query->for ?? $query->collection),
static::genIn($query->in ?? $query->collection, $query->traversals), static::genIn($query->in ?? $query->collection, $query->traversals),
static::genLet($query->vars), static::genLet($query->lets),
$this->genForeach($query->foreach), $this->genForeach($query->foreach),
$this->genWhere($query->where, $params), $this->genWhere($query->where, $params),
isset($this->search) ? $this->genSearch($this->search, $this->searchType) : null, isset($this->search) ? $this->genSearch($this->search, $this->searchType) : null,
@ -906,19 +910,24 @@ class Query extends Component implements QueryInterface
} }
/** /**
* @param $collection
* @param $columns * @param $columns
* @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($columns, $params = [], $db = null)
{ {
$doc = Serializer::encode($columns); // Инициализация
$this->in ?? (isset($this->collection) ? $this->in = $this->collection : throw new Exception('Не найдена коллекция'));
$this->collection ?? $this->collection = $this->in;
$data = Serializer::encode($columns);
$clauses = [ $clauses = [
"INSERT $doc IN {$this->quoteCollectionName($collection)}", "INSERT $data IN {$this->quoteCollectionName($this->in ?? $this->collection)}",
$this->genOptions(), $this->genOptions(),
]; ];
@ -946,22 +955,26 @@ class Query extends Component implements QueryInterface
} }
/** /**
* @param $collection
* @param $columns * @param $columns
* @param array $condition
* @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($columns, $params = [], $db = null)
{ {
$this->collection = $collection; // Инициализация
$this->in ?? (isset($this->collection) ? $this->in = $this->collection : throw new Exception('Не найдена коллекция'));
$this->for ?? $this->for = $this->in;
$this->collection ?? $this->collection = $this->in;
$clauses = [ $clauses = [
$this->genFor($collection), static::genFor($this->for ?? $this->collection),
$this->genIn($collection), static::genIn($this->in ?? $this->collection, $this->traversals),
$this->genWhere($condition, $params), $this->genWhere($this->where, $params),
$this->genUpdate($collection, $columns), $this->genUpdate($this->collection, $columns),
$this->genOptions(), $this->genOptions(),
]; ];
@ -977,7 +990,9 @@ class Query extends Component implements QueryInterface
$statement = $this->getStatement($params, $db); $statement = $this->getStatement($params, $db);
$token = $this->getRawAql($statement); $token = $this->getRawAql($statement);
Yii::info($token, 'mirzaev\yii2\arangodb\Query::update'); Yii::info($token, 'mirzaev\yii2\arangodb\Query::update');
try { try {
Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::update'); Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::update');
$cursor = $statement->execute(); $cursor = $statement->execute();
@ -1000,12 +1015,17 @@ class Query extends Component implements QueryInterface
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function remove($condition = [], $params = [], $db = null) public function remove($params = [], $db = null)
{ {
// Инициализация
$this->in ?? (isset($this->collection) ? $this->in = $this->collection : throw new Exception('Не найдена коллекция'));
$this->for ?? $this->for = $this->in;
$this->collection ?? $this->collection = $this->in;
$clauses = [ $clauses = [
static::genFor($this->for ?? $this->collection), static::genFor($this->for ?? $this->collection),
static::genIn($this->in ?? $this->collection, $this->traversals), static::genIn($this->in ?? $this->collection, $this->traversals),
$this->genWhere($condition, $params), $this->genWhere($this->where, $params),
$this->genRemove($this->in ?? $this->collection), $this->genRemove($this->in ?? $this->collection),
$this->genOptions(), $this->genOptions(),
]; ];
@ -1266,11 +1286,11 @@ class Query extends Component implements QueryInterface
/** /**
* @param array $expression * @param array $expression
*/ */
public function where($expression) public function where($expression, $operator = 'AND')
{ {
$this->where = match (true) { $this->where = match (true) {
is_null($this->where) => $expression, is_null($this->where) => $expression,
default => ['AND', $this->where, $expression] default => [$operator, $this->where, $expression]
}; };
return $this; return $this;
@ -1280,7 +1300,7 @@ class Query extends Component implements QueryInterface
*/ */
public function let(string $name, mixed $value): static public function let(string $name, mixed $value): static
{ {
$this->vars[$name] = $value; $this->lets[$name] = $value;
return $this; return $this;
} }