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

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": {
"psr-4": {
"mirzaev\\yii2\\arangodb\\": "mirzaev/yii2-arangodb"
"mirzaev\\yii2\\arangodb\\": "mirzaev/yii2/arangodb"
}
},
"autoload-dev": {
"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()
{
parent::init();
$this->db = Instance::ensure($this->db, Connection::className());
$this->db = Instance::ensure($this->db, Connection::class;
}
public function execute($aql, $bindValues = [], $params = [])

View File

@ -48,7 +48,7 @@ class Query extends Component implements QueryInterface
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 = [])
{
// Инициализация
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);
$clauses = [
static::genFor($query->for ?? $query->collection),
static::genIn($query->in ?? $query->collection, $query->traversals),
static::genLet($query->vars),
static::genLet($query->lets),
$this->genForeach($query->foreach),
$this->genWhere($query->where, $params),
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 array $params
* @param null $db
*
* @return bool
*
* @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 = [
"INSERT $doc IN {$this->quoteCollectionName($collection)}",
"INSERT $data IN {$this->quoteCollectionName($this->in ?? $this->collection)}",
$this->genOptions(),
];
@ -946,22 +955,26 @@ class Query extends Component implements QueryInterface
}
/**
* @param $collection
* @param $columns
* @param array $condition
* @param array $params
* @param null $db
*
* @return bool
*
* @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 = [
$this->genFor($collection),
$this->genIn($collection),
$this->genWhere($condition, $params),
$this->genUpdate($collection, $columns),
static::genFor($this->for ?? $this->collection),
static::genIn($this->in ?? $this->collection, $this->traversals),
$this->genWhere($this->where, $params),
$this->genUpdate($this->collection, $columns),
$this->genOptions(),
];
@ -977,7 +990,9 @@ class Query extends Component implements QueryInterface
$statement = $this->getStatement($params, $db);
$token = $this->getRawAql($statement);
Yii::info($token, 'mirzaev\yii2\arangodb\Query::update');
try {
Yii::beginProfile($token, 'mirzaev\yii2\arangodb\Query::update');
$cursor = $statement->execute();
@ -1000,12 +1015,17 @@ class Query extends Component implements QueryInterface
* @return bool
* @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 = [
static::genFor($this->for ?? $this->collection),
static::genIn($this->in ?? $this->collection, $this->traversals),
$this->genWhere($condition, $params),
$this->genWhere($this->where, $params),
$this->genRemove($this->in ?? $this->collection),
$this->genOptions(),
];
@ -1266,11 +1286,11 @@ class Query extends Component implements QueryInterface
/**
* @param array $expression
*/
public function where($expression)
public function where($expression, $operator = 'AND')
{
$this->where = match (true) {
is_null($this->where) => $expression,
default => ['AND', $this->where, $expression]
default => [$operator, $this->where, $expression]
};
return $this;
@ -1280,7 +1300,7 @@ class Query extends Component implements QueryInterface
*/
public function let(string $name, mixed $value): static
{
$this->vars[$name] = $value;
$this->lets[$name] = $value;
return $this;
}