2014-06-16 19:03:26 +08:00
|
|
|
<?php
|
|
|
|
|
2017-04-14 04:44:05 +07:00
|
|
|
namespace explosivebit\arangodb;
|
2014-06-16 19:03:26 +08:00
|
|
|
|
2017-12-06 10:12:19 +07:00
|
|
|
use ArangoDBClient\EdgeHandler;
|
|
|
|
use ArangoDBClient\Export;
|
2014-08-04 19:11:29 +08:00
|
|
|
use Yii;
|
2017-10-09 11:19:40 +07:00
|
|
|
use ArangoDBClient\CollectionHandler;
|
2017-10-03 06:20:15 +07:00
|
|
|
use ArangoDBClient\ConnectionOptions;
|
2017-10-09 11:19:40 +07:00
|
|
|
use ArangoDBClient\Document;
|
|
|
|
use ArangoDBClient\DocumentHandler;
|
|
|
|
use ArangoDBClient\Statement;
|
|
|
|
use ArangoDBClient\UpdatePolicy;
|
|
|
|
|
2017-11-10 15:33:59 +07:00
|
|
|
use yii\base\BaseObject;
|
2014-06-16 19:03:26 +08:00
|
|
|
|
2017-11-10 15:33:59 +07:00
|
|
|
class Connection extends BaseObject
|
2014-07-29 13:33:21 +08:00
|
|
|
{
|
|
|
|
private $connection = null;
|
|
|
|
|
|
|
|
public $connectionOptions = [
|
|
|
|
// server endpoint to connect to
|
|
|
|
ConnectionOptions::OPTION_ENDPOINT => 'tcp://127.0.0.1:8529',
|
|
|
|
// authorization type to use (currently supported: 'Basic')
|
|
|
|
ConnectionOptions::OPTION_AUTH_TYPE => 'Basic',
|
|
|
|
// user for basic authorization
|
|
|
|
ConnectionOptions::OPTION_AUTH_USER => 'root',
|
|
|
|
// password for basic authorization
|
|
|
|
ConnectionOptions::OPTION_AUTH_PASSWD => '',
|
|
|
|
// connection persistence on server. can use either 'Close'
|
|
|
|
// (one-time connections) or 'Keep-Alive' (re-used connections)
|
|
|
|
ConnectionOptions::OPTION_CONNECTION => 'Close',
|
|
|
|
// connect timeout in seconds
|
|
|
|
ConnectionOptions::OPTION_TIMEOUT => 3,
|
|
|
|
// whether or not to reconnect when a keep-alive connection has timed out on server
|
|
|
|
ConnectionOptions::OPTION_RECONNECT => true,
|
|
|
|
// optionally create new collections when inserting documents
|
|
|
|
ConnectionOptions::OPTION_CREATE => true,
|
|
|
|
// optionally create new collections when inserting documents
|
|
|
|
ConnectionOptions::OPTION_UPDATE_POLICY => UpdatePolicy::LAST,
|
|
|
|
];
|
|
|
|
|
|
|
|
/** @var null|CollectionHandler $collectionHandler */
|
|
|
|
private $collectionHandler = null;
|
|
|
|
/** @var null|DocumentHandler $documentHandler */
|
|
|
|
private $documentHandler = null;
|
2017-12-06 10:12:19 +07:00
|
|
|
/** @var null|EdgeHandler $documentHandler */
|
|
|
|
private $edgeHandler = null;
|
2014-07-29 13:33:21 +08:00
|
|
|
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
parent::init();
|
|
|
|
|
2014-08-04 19:11:29 +08:00
|
|
|
$token = 'Opening ArangoDB connection: ' . $this->connectionOptions[ConnectionOptions::OPTION_ENDPOINT];
|
|
|
|
try {
|
2017-04-14 04:44:05 +07:00
|
|
|
Yii::info($token, 'explosivebit\arangodb\Connection::open');
|
|
|
|
Yii::beginProfile($token, 'explosivebit\arangodb\Connection::open');
|
2017-12-06 10:12:19 +07:00
|
|
|
$this->connection = new \ArangoDBClient\Connection($this->connectionOptions);
|
2017-10-09 11:19:40 +07:00
|
|
|
$this->collectionHandler = new CollectionHandler($this->connection);
|
2017-12-06 10:12:19 +07:00
|
|
|
$this->documentHandler = new DocumentHandler($this->connection);
|
|
|
|
$this->edgeHandler = new EdgeHandler($this->connection);
|
2017-04-14 04:44:05 +07:00
|
|
|
Yii::endProfile($token, 'explosivebit\arangodb\Connection::open');
|
2014-08-04 19:11:29 +08:00
|
|
|
} catch (\Exception $ex) {
|
2017-04-14 04:44:05 +07:00
|
|
|
Yii::endProfile($token, 'explosivebit\arangodb\Connection::open');
|
2017-12-06 10:12:19 +07:00
|
|
|
throw new \Exception($ex->getMessage(), (int)$ex->getCode(), $ex);
|
2014-08-04 19:11:29 +08:00
|
|
|
}
|
2014-07-29 13:33:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|CollectionHandler
|
|
|
|
*/
|
|
|
|
public function getCollectionHandler()
|
|
|
|
{
|
|
|
|
return $this->collectionHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $collectionId
|
|
|
|
* @return \triagens\ArangoDb\Collection
|
|
|
|
*/
|
|
|
|
public function getCollection($collectionId)
|
|
|
|
{
|
|
|
|
return $this->getCollectionHandler()->get($collectionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|DocumentHandler
|
|
|
|
*/
|
|
|
|
public function getDocumentHandler()
|
|
|
|
{
|
|
|
|
return $this->documentHandler;
|
|
|
|
}
|
|
|
|
|
2017-12-06 10:12:19 +07:00
|
|
|
/**
|
|
|
|
* @return null|EdgeHandler
|
|
|
|
*/
|
|
|
|
public function getEdgeHandler()
|
|
|
|
{
|
|
|
|
return $this->edgeHandler;
|
|
|
|
}
|
|
|
|
|
2014-07-29 13:33:21 +08:00
|
|
|
/**
|
|
|
|
* @param $collectionId
|
|
|
|
* @param $documentId
|
|
|
|
* @return Document
|
|
|
|
*/
|
|
|
|
public function getDocument($collectionId, $documentId)
|
|
|
|
{
|
|
|
|
return $this->getDocumentHandler()->get($collectionId, $documentId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $options
|
|
|
|
* @return Statement
|
|
|
|
*/
|
|
|
|
public function getStatement($options = [])
|
2014-06-16 19:03:26 +08:00
|
|
|
{
|
2017-10-09 11:19:40 +07:00
|
|
|
return new Statement($this->connection, $options);
|
2014-06-16 19:03:26 +08:00
|
|
|
}
|
2017-11-10 15:33:59 +07:00
|
|
|
|
2017-12-06 10:12:19 +07:00
|
|
|
/**
|
|
|
|
* @param array $options
|
|
|
|
* @return Export
|
|
|
|
*/
|
|
|
|
public function getExport($options = []) {
|
|
|
|
return new Export($this->connection, $options);
|
|
|
|
}
|
|
|
|
}
|