From 14a26fac98869bbf8a29a03b0a8536dadd66d7c6 Mon Sep 17 00:00:00 2001 From: evgen-d Date: Fri, 8 Aug 2014 11:58:42 +0400 Subject: [PATCH] update remove and insert methods --- Query.php | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/Query.php b/Query.php index ba9de9e..70c1cee 100644 --- a/Query.php +++ b/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