yii2-arangodb/Migration.php

97 lines
3.4 KiB
PHP
Raw Normal View History

2014-07-29 13:33:21 +08:00
<?php
namespace devgroup\arangodb;
2014-08-11 20:32:03 +08:00
use triagens\ArangoDb\Document;
2014-07-29 13:33:21 +08:00
use yii\base\Component;
use yii\db\MigrationInterface;
use yii\di\Instance;
use yii\helpers\ArrayHelper;
abstract class Migration extends Component implements MigrationInterface
{
/**
* @var Connection|string the DB connection object or the application component ID of the DB connection
* that this migration should work with.
*/
public $db = 'arangodb';
/**
* Initializes the migration.
* This method will set [[db]] to be the 'db' application component, if it is null.
*/
public function init()
{
parent::init();
$this->db = Instance::ensure($this->db, Connection::className());
}
public function execute($aql, $bindValues = [], $params = [])
{
echo " > execute AQL: $aql ...";
$time = microtime(true);
$options = [
'query' => $aql,
'bindValues' => $bindValues,
];
$options = ArrayHelper::merge($params, $options);
$statement = $this->db->getStatement($options);
$statement->execute();
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}
public function insert($collection, $columns)
{
echo " > insert into $collection ...";
$time = microtime(true);
$this->db->getDocumentHandler()->save($collection, $columns);
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}
public function update($collection, $columns, $condition = '', $params = [])
{
echo " > update $collection ...";
$time = microtime(true);
2014-08-11 20:32:03 +08:00
$docs = (new Query())->select($collection)->from($collection)->where($condition, $params)->all();
foreach ($docs as $doc) {
$this->db->getDocumentHandler()->updateById($collection, $doc['_key'], Document::createFromArray($columns));
}
2014-07-29 13:33:21 +08:00
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}
public function delete($collection, $condition = '', $params = [])
{
echo " > delete from $collection ...";
$time = microtime(true);
2014-08-11 20:32:03 +08:00
$docs = (new Query())->select($collection)->from($collection)->where($condition, $params)->all();
foreach ($docs as $doc) {
$this->db->getDocumentHandler()->removeById($collection, $doc['_key']);
}
2014-07-29 13:33:21 +08:00
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}
public function createCollection($collection, $options = [])
{
echo " > create collection $collection ...";
$time = microtime(true);
$this->db->getCollectionHandler()->create($collection, $options);
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}
public function dropCollection($collection)
{
echo " > drop collection $collection ...";
$time = microtime(true);
$this->db->getCollectionHandler()->drop($collection);
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}
public function truncateCollection($collection)
{
echo " > truncate collection $collection ...";
$time = microtime(true);
$this->db->getCollectionHandler()->truncate($collection);
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}
}