update remove and insert methods
This commit is contained in:
parent
9f1dea1740
commit
14a26fac98
104
Query.php
104
Query.php
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace devgroup\arangodb;
|
||||
|
||||
use triagens\ArangoDb\Cursor;
|
||||
use triagens\ArangoDb\Document;
|
||||
use Yii;
|
||||
use yii\base\Component;
|
||||
|
@ -470,6 +471,109 @@ class Query extends Component implements QueryInterface
|
|||
return empty($result) ? false : $result[0];
|
||||
}
|
||||
|
||||
public function insert($collection, $columns, $params = [], $db = null)
|
||||
{
|
||||
$doc = Json::encode($columns);
|
||||
|
||||
$aql = "INSERT $doc IN $collection";
|
||||
|
||||
$options = ArrayHelper::merge(
|
||||
$params,
|
||||
[
|
||||
'query' => $aql,
|
||||
]
|
||||
);
|
||||
|
||||
$statement = $this->getStatement($options, $db);
|
||||
$token = $this->getRawAql($statement);
|
||||
Yii::info($token, 'devgroup\arangodb\Query::insert');
|
||||
try {
|
||||
Yii::beginProfile($token, 'devgroup\arangodb\Query::insert');
|
||||
$cursor = $statement->execute();
|
||||
Yii::endProfile($token, 'devgroup\arangodb\Query::insert');
|
||||
} catch (\Exception $ex) {
|
||||
Yii::endProfile($token, 'devgroup\arangodb\Query::insert');
|
||||
throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function update($collection, $columns, $condition = [], $params = [], $db = null)
|
||||
{
|
||||
$this->from($collection);
|
||||
$clauses = [
|
||||
$this->buildFrom($collection),
|
||||
$this->buildWhere($condition, $params),
|
||||
$this->buildUpdate($collection, $columns),
|
||||
];
|
||||
|
||||
$aql = implode($this->separator, array_filter($clauses));
|
||||
|
||||
$options = ArrayHelper::merge(
|
||||
$params,
|
||||
[
|
||||
'query' => $aql,
|
||||
'bindVars' => $params,
|
||||
]
|
||||
);
|
||||
|
||||
$statement = $this->getStatement($options, $db);
|
||||
$token = $this->getRawAql($statement);
|
||||
Yii::info($token, 'devgroup\arangodb\Query::update');
|
||||
try {
|
||||
Yii::beginProfile($token, 'devgroup\arangodb\Query::update');
|
||||
$cursor = $statement->execute();
|
||||
Yii::endProfile($token, 'devgroup\arangodb\Query::update');
|
||||
} catch (\Exception $ex) {
|
||||
Yii::endProfile($token, 'devgroup\arangodb\Query::update');
|
||||
throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex);
|
||||
}
|
||||
return $cursor->getMetadata()['extra']['operations']['executed'];
|
||||
}
|
||||
|
||||
public function remove($collection, $condition, $params = [], $db = null)
|
||||
{
|
||||
$this->from($collection);
|
||||
$clauses = [
|
||||
$this->buildFrom($collection),
|
||||
$this->buildWhere($condition, $params),
|
||||
$this->buildRemove($collection),
|
||||
];
|
||||
|
||||
$aql = implode($this->separator, array_filter($clauses));
|
||||
|
||||
$options = ArrayHelper::merge(
|
||||
$params,
|
||||
[
|
||||
'query' => $aql,
|
||||
'bindVars' => $params,
|
||||
]
|
||||
);
|
||||
|
||||
$statement = $this->getStatement($options, $db);
|
||||
$token = $this->getRawAql($statement);
|
||||
Yii::info($token, 'devgroup\arangodb\Query::remove');
|
||||
try {
|
||||
Yii::beginProfile($token, 'devgroup\arangodb\Query::remove');
|
||||
$cursor = $statement->execute();
|
||||
Yii::endProfile($token, 'devgroup\arangodb\Query::remove');
|
||||
} catch (\Exception $ex) {
|
||||
Yii::endProfile($token, 'devgroup\arangodb\Query::remove');
|
||||
throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex);
|
||||
}
|
||||
return $cursor->getMetadata()['extra']['operations']['executed'];
|
||||
}
|
||||
|
||||
protected function buildUpdate($collection, $columns)
|
||||
{
|
||||
return 'UPDATE ' . $collection . ' WITH ' . Json::encode($columns) . ' IN ' . $collection;
|
||||
}
|
||||
|
||||
protected function buildRemove($collection)
|
||||
{
|
||||
return 'REMOVE ' . $collection . ' IN ' . $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Document[] $rows
|
||||
* @return array
|
||||
|
|
Loading…
Reference in New Issue