add params & trace & profile
This commit is contained in:
parent
39f2888f6b
commit
30c38fa3a4
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace devgroup\arangodb;
|
namespace devgroup\arangodb;
|
||||||
|
|
||||||
|
use Yii;
|
||||||
use yii\base\InvalidConfigException;
|
use yii\base\InvalidConfigException;
|
||||||
use yii\db\ActiveQueryInterface;
|
use yii\db\ActiveQueryInterface;
|
||||||
use yii\db\ActiveQueryTrait;
|
use yii\db\ActiveQueryTrait;
|
||||||
|
@ -117,8 +118,17 @@ class ActiveQuery extends Query implements ActiveQueryInterface
|
||||||
public function all($db = null)
|
public function all($db = null)
|
||||||
{
|
{
|
||||||
$statement = $this->createCommand();
|
$statement = $this->createCommand();
|
||||||
|
$token = $this->getRawAql($statement);
|
||||||
|
Yii::info($token, __METHOD__);
|
||||||
|
try {
|
||||||
|
Yii::beginProfile($token, __METHOD__);
|
||||||
$cursor = $statement->execute();
|
$cursor = $statement->execute();
|
||||||
$rows = $cursor->getAll();
|
$rows = $cursor->getAll();
|
||||||
|
Yii::endProfile($token, __METHOD__);
|
||||||
|
} catch (\Exception $ex) {
|
||||||
|
Yii::endProfile($token, __METHOD__);
|
||||||
|
throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex);
|
||||||
|
}
|
||||||
if (!empty($rows)) {
|
if (!empty($rows)) {
|
||||||
$models = $this->createModels($rows);
|
$models = $this->createModels($rows);
|
||||||
if (!empty($this->with)) {
|
if (!empty($this->with)) {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace devgroup\arangodb;
|
namespace devgroup\arangodb;
|
||||||
|
|
||||||
|
use Yii;
|
||||||
use triagens\ArangoDb\CollectionHandler;
|
use triagens\ArangoDb\CollectionHandler;
|
||||||
use triagens\ArangoDb\ConnectionOptions;
|
use triagens\ArangoDb\ConnectionOptions;
|
||||||
use triagens\ArangoDb\Document;
|
use triagens\ArangoDb\Document;
|
||||||
|
@ -46,9 +47,18 @@ class Connection extends Object
|
||||||
{
|
{
|
||||||
parent::init();
|
parent::init();
|
||||||
|
|
||||||
|
$token = 'Opening ArangoDB connection: ' . $this->connectionOptions[ConnectionOptions::OPTION_ENDPOINT];
|
||||||
|
try {
|
||||||
|
Yii::info($token, __METHOD__);
|
||||||
|
Yii::beginProfile($token, __METHOD__);
|
||||||
$this->connection = new ArangoDbConnection($this->connectionOptions);
|
$this->connection = new ArangoDbConnection($this->connectionOptions);
|
||||||
$this->collectionHandler = new CollectionHandler($this->connection);
|
$this->collectionHandler = new CollectionHandler($this->connection);
|
||||||
$this->documentHandler = new DocumentHandler($this->connection);
|
$this->documentHandler = new DocumentHandler($this->connection);
|
||||||
|
Yii::endProfile($token, __METHOD__);
|
||||||
|
} catch (\Exception $ex) {
|
||||||
|
Yii::endProfile($token, __METHOD__);
|
||||||
|
throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
98
Query.php
98
Query.php
|
@ -11,6 +11,7 @@ use yii\helpers\ArrayHelper;
|
||||||
use yii\helpers\Json;
|
use yii\helpers\Json;
|
||||||
|
|
||||||
use triagens\ArangoDb\Statement;
|
use triagens\ArangoDb\Statement;
|
||||||
|
use yii\helpers\VarDumper;
|
||||||
|
|
||||||
class Query extends Component implements QueryInterface
|
class Query extends Component implements QueryInterface
|
||||||
{
|
{
|
||||||
|
@ -463,10 +464,42 @@ class Query extends Component implements QueryInterface
|
||||||
return [$aql, $params];
|
return [$aql, $params];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Statement $statement
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function getRawAql($statement)
|
||||||
|
{
|
||||||
|
$query = $statement->getQuery();
|
||||||
|
$values = $statement->getBindVars();
|
||||||
|
|
||||||
|
$search = [];
|
||||||
|
$replace = [];
|
||||||
|
foreach ($values as $key => $value) {
|
||||||
|
$search[] = "@$key";
|
||||||
|
$replace[] = is_string($value) ? "\"$value\"" : $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($search)) {
|
||||||
|
$query = str_replace($search, $replace, $query);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
public function all($db = null)
|
public function all($db = null)
|
||||||
{
|
{
|
||||||
$statement = $this->createCommand();
|
$statement = $this->createCommand();
|
||||||
|
$token = $this->getRawAql($statement);
|
||||||
|
Yii::info($token, __METHOD__);
|
||||||
|
try {
|
||||||
|
Yii::beginProfile($token, __METHOD__);
|
||||||
$cursor = $statement->execute();
|
$cursor = $statement->execute();
|
||||||
|
Yii::endProfile($token, __METHOD__);
|
||||||
|
} catch (\Exception $ex) {
|
||||||
|
Yii::endProfile($token, __METHOD__);
|
||||||
|
throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex);
|
||||||
|
}
|
||||||
return $cursor->getAll();
|
return $cursor->getAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -474,7 +507,16 @@ class Query extends Component implements QueryInterface
|
||||||
{
|
{
|
||||||
$this->limit(1);
|
$this->limit(1);
|
||||||
$statement = $this->createCommand();
|
$statement = $this->createCommand();
|
||||||
|
$token = $this->getRawAql($statement);
|
||||||
|
Yii::info($token, __METHOD__);
|
||||||
|
try {
|
||||||
|
Yii::beginProfile($token, __METHOD__);
|
||||||
$cursor = $statement->execute();
|
$cursor = $statement->execute();
|
||||||
|
Yii::endProfile($token, __METHOD__);
|
||||||
|
} catch (\Exception $ex) {
|
||||||
|
Yii::endProfile($token, __METHOD__);
|
||||||
|
throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex);
|
||||||
|
}
|
||||||
$result = $cursor->getAll();
|
$result = $cursor->getAll();
|
||||||
return empty($result) ? false : $result[0];
|
return empty($result) ? false : $result[0];
|
||||||
}
|
}
|
||||||
|
@ -500,7 +542,16 @@ class Query extends Component implements QueryInterface
|
||||||
{
|
{
|
||||||
$statement = $this->createCommand();
|
$statement = $this->createCommand();
|
||||||
$statement->setCount(true);
|
$statement->setCount(true);
|
||||||
|
$token = $this->getRawAql($statement);
|
||||||
|
Yii::info($token, __METHOD__);
|
||||||
|
try {
|
||||||
|
Yii::beginProfile($token, __METHOD__);
|
||||||
$cursor = $statement->execute();
|
$cursor = $statement->execute();
|
||||||
|
Yii::endProfile($token, __METHOD__);
|
||||||
|
} catch (\Exception $ex) {
|
||||||
|
Yii::endProfile($token, __METHOD__);
|
||||||
|
throw new \Exception($ex->getMessage(), (int) $ex->getCode(), $ex);
|
||||||
|
}
|
||||||
return $cursor->getCount();
|
return $cursor->getCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -516,29 +567,32 @@ class Query extends Component implements QueryInterface
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function where($condition)
|
public function where($condition, $params = [])
|
||||||
{
|
{
|
||||||
$this->where = $condition;
|
$this->where = $condition;
|
||||||
|
$this->addParams($params);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function andWhere($condition)
|
public function andWhere($condition, $params = [])
|
||||||
{
|
{
|
||||||
if ($this->where === null) {
|
if ($this->where === null) {
|
||||||
$this->where = $condition;
|
$this->where = $condition;
|
||||||
} else {
|
} else {
|
||||||
$this->where = ['AND', $this->where, $condition];
|
$this->where = ['AND', $this->where, $condition];
|
||||||
}
|
}
|
||||||
|
$this->addParams($params);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function orWhere($condition)
|
public function orWhere($condition, $params = [])
|
||||||
{
|
{
|
||||||
if ($this->where === null) {
|
if ($this->where === null) {
|
||||||
$this->where = $condition;
|
$this->where = $condition;
|
||||||
} else {
|
} else {
|
||||||
$this->where = ['OR', $this->where, $condition];
|
$this->where = ['OR', $this->where, $condition];
|
||||||
}
|
}
|
||||||
|
$this->addParams($params);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -747,4 +801,42 @@ class Query extends Component implements QueryInterface
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the parameters to be bound to the query.
|
||||||
|
* @param array $params list of query parameter values indexed by parameter placeholders.
|
||||||
|
* For example, `[':name' => 'Dan', ':age' => 31]`.
|
||||||
|
* @return static the query object itself
|
||||||
|
* @see addParams()
|
||||||
|
*/
|
||||||
|
public function params($params)
|
||||||
|
{
|
||||||
|
$this->params = $params;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds additional parameters to be bound to the query.
|
||||||
|
* @param array $params list of query parameter values indexed by parameter placeholders.
|
||||||
|
* For example, `[':name' => 'Dan', ':age' => 31]`.
|
||||||
|
* @return static the query object itself
|
||||||
|
* @see params()
|
||||||
|
*/
|
||||||
|
public function addParams($params)
|
||||||
|
{
|
||||||
|
if (!empty($params)) {
|
||||||
|
if (empty($this->params)) {
|
||||||
|
$this->params = $params;
|
||||||
|
} else {
|
||||||
|
foreach ($params as $name => $value) {
|
||||||
|
if (is_integer($name)) {
|
||||||
|
$this->params[] = $value;
|
||||||
|
} else {
|
||||||
|
$this->params[$name] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue