diff --git a/framework/yii/db/redis/Connection.php b/framework/yii/db/redis/Connection.php deleted file mode 100644 index 46db575..0000000 --- a/framework/yii/db/redis/Connection.php +++ /dev/null @@ -1,425 +0,0 @@ -close(); - return array_keys(get_object_vars($this)); - } - - /** - * Returns a value indicating whether the DB connection is established. - * @return boolean whether the DB connection is established - */ - public function getIsActive() - { - return $this->_socket !== null; - } - - /** - * Establishes a DB connection. - * It does nothing if a DB connection has already been established. - * @throws Exception if connection fails - */ - public function open() - { - if ($this->_socket === null) { - if (empty($this->dsn)) { - throw new InvalidConfigException('Connection.dsn cannot be empty.'); - } - $dsn = explode('/', $this->dsn); - $host = $dsn[2]; - if (strpos($host, ':')===false) { - $host .= ':6379'; - } - $db = isset($dsn[3]) ? $dsn[3] : 0; - - \Yii::trace('Opening DB connection: ' . $this->dsn, __CLASS__); - $this->_socket = @stream_socket_client( - $host, - $errorNumber, - $errorDescription, - $this->connectionTimeout ? $this->connectionTimeout : ini_get("default_socket_timeout") - ); - if ($this->_socket) { - if ($this->dataTimeout !== null) { - stream_set_timeout($this->_socket, $timeout=(int)$this->dataTimeout, (int) (($this->dataTimeout - $timeout) * 1000000)); - } - if ($this->password !== null) { - $this->executeCommand('AUTH', array($this->password)); - } - $this->executeCommand('SELECT', array($db)); - $this->initConnection(); - } else { - \Yii::error("Failed to open DB connection ({$this->dsn}): " . $errorNumber . ' - ' . $errorDescription, __CLASS__); - $message = YII_DEBUG ? 'Failed to open DB connection: ' . $errorNumber . ' - ' . $errorDescription : 'Failed to open DB connection.'; - throw new Exception($message, $errorDescription, (int)$errorNumber); - } - } - } - - /** - * Closes the currently active DB connection. - * It does nothing if the connection is already closed. - */ - public function close() - { - if ($this->_socket !== null) { - \Yii::trace('Closing DB connection: ' . $this->dsn, __CLASS__); - $this->executeCommand('QUIT'); - stream_socket_shutdown($this->_socket, STREAM_SHUT_RDWR); - $this->_socket = null; - $this->_transaction = null; - } - } - - /** - * Initializes the DB connection. - * This method is invoked right after the DB connection is established. - * The default implementation triggers an [[EVENT_AFTER_OPEN]] event. - */ - protected function initConnection() - { - $this->trigger(self::EVENT_AFTER_OPEN); - } - - /** - * Returns the currently active transaction. - * @return Transaction the currently active transaction. Null if no active transaction. - */ - public function getTransaction() - { - return $this->_transaction && $this->_transaction->isActive ? $this->_transaction : null; - } - - /** - * Starts a transaction. - * @return Transaction the transaction initiated - */ - public function beginTransaction() - { - $this->open(); - $this->_transaction = new Transaction(array( - 'db' => $this, - )); - $this->_transaction->begin(); - return $this->_transaction; - } - - /** - * Returns the name of the DB driver for the current [[dsn]]. - * @return string name of the DB driver - */ - public function getDriverName() - { - if (($pos = strpos($this->dsn, ':')) !== false) { - return strtolower(substr($this->dsn, 0, $pos)); - } else { - return 'redis'; - } - } - - /** - * - * @param string $name - * @param array $params - * @return mixed - */ - public function __call($name, $params) - { - $redisCommand = strtoupper(Inflector::camel2words($name, false)); - if (in_array($redisCommand, $this->redisCommands)) { - return $this->executeCommand($name, $params); - } else { - return parent::__call($name, $params); - } - } - - /** - * Executes a redis command. - * For a list of available commands and their parameters see http://redis.io/commands. - * - * @param string $name the name of the command - * @param array $params list of parameters for the command - * @return array|bool|null|string Dependend on the executed command this method - * will return different data types: - * - * - `true` for commands that return "status reply". - * - `string` for commands that return "integer reply" - * as the value is in the range of a signed 64 bit integer. - * - `string` or `null` for commands that return "bulk reply". - * - `array` for commands that return "Multi-bulk replies". - * - * See [redis protocol description](http://redis.io/topics/protocol) - * for details on the mentioned reply types. - * @trows Exception for commands that return [error reply](http://redis.io/topics/protocol#error-reply). - */ - public function executeCommand($name, $params=array()) - { - $this->open(); - - array_unshift($params, $name); - $command = '*' . count($params) . "\r\n"; - foreach($params as $arg) { - $command .= '$' . mb_strlen($arg, '8bit') . "\r\n" . $arg . "\r\n"; - } - - \Yii::trace("Executing Redis Command: {$name}", __CLASS__); - fwrite($this->_socket, $command); - - return $this->parseResponse(implode(' ', $params)); - } - - private function parseResponse($command) - { - if(($line = fgets($this->_socket)) === false) { - throw new Exception("Failed to read from socket.\nRedis command was: " . $command); - } - $type = $line[0]; - $line = mb_substr($line, 1, -2, '8bit'); - switch($type) - { - case '+': // Status reply - return true; - case '-': // Error reply - throw new Exception("Redis error: " . $line . "\nRedis command was: " . $command); - case ':': // Integer reply - // no cast to int as it is in the range of a signed 64 bit integer - return $line; - case '$': // Bulk replies - if ($line == '-1') { - return null; - } - $length = $line + 2; - $data = ''; - while ($length > 0) { - if(($block = fread($this->_socket, $line + 2)) === false) { - throw new Exception("Failed to read from socket.\nRedis command was: " . $command); - } - $data .= $block; - $length -= mb_strlen($block, '8bit'); - } - return mb_substr($data, 0, -2, '8bit'); - case '*': // Multi-bulk replies - $count = (int) $line; - $data = array(); - for($i = 0; $i < $count; $i++) { - $data[] = $this->parseResponse($command); - } - return $data; - default: - throw new Exception('Received illegal data from redis: ' . $line . "\nRedis command was: " . $command); - } - } -} diff --git a/framework/yii/db/redis/Transaction.php b/framework/yii/db/redis/Transaction.php deleted file mode 100644 index 721a7be..0000000 --- a/framework/yii/db/redis/Transaction.php +++ /dev/null @@ -1,91 +0,0 @@ -_active; - } - - /** - * Begins a transaction. - * @throws InvalidConfigException if [[connection]] is null - */ - public function begin() - { - if (!$this->_active) { - if ($this->db === null) { - throw new InvalidConfigException('Transaction::db must be set.'); - } - \Yii::trace('Starting transaction', __CLASS__); - $this->db->open(); - $this->db->createCommand('MULTI')->execute(); - $this->_active = true; - } - } - - /** - * Commits a transaction. - * @throws Exception if the transaction or the DB connection is not active. - */ - public function commit() - { - if ($this->_active && $this->db && $this->db->isActive) { - \Yii::trace('Committing transaction', __CLASS__); - $this->db->createCommand('EXEC')->execute(); - // TODO handle result of EXEC - $this->_active = false; - } else { - throw new Exception('Failed to commit transaction: transaction was inactive.'); - } - } - - /** - * Rolls back a transaction. - * @throws Exception if the transaction or the DB connection is not active. - */ - public function rollback() - { - if ($this->_active && $this->db && $this->db->isActive) { - \Yii::trace('Rolling back transaction', __CLASS__); - $this->db->pdo->commit(); - $this->_active = false; - } else { - throw new Exception('Failed to roll back transaction: transaction was inactive.'); - } - } -} diff --git a/framework/yii/db/redis/schema.md b/framework/yii/db/redis/schema.md deleted file mode 100644 index 1bd45b3..0000000 --- a/framework/yii/db/redis/schema.md +++ /dev/null @@ -1,35 +0,0 @@ -To allow AR to be stored in redis we need a special Schema for it. - -HSET prefix:className:primaryKey - - -http://redis.io/commands - -Current Redis connection: -https://github.com/jamm/Memory - - -# Queries - -wrap all these in transactions MULTI - -## insert - -SET all attribute key-value pairs -SET all relation key-value pairs -make sure to create back-relations - -## update - -SET all attribute key-value pairs -SET all relation key-value pairs - - -## delete - -DEL all attribute key-value pairs -DEL all relation key-value pairs -make sure to update back-relations - - -http://redis.io/commands/hmget sounds suiteable! \ No newline at end of file diff --git a/framework/yii/db/redis/ActiveQuery.php b/framework/yii/redis/ActiveQuery.php similarity index 99% rename from framework/yii/db/redis/ActiveQuery.php rename to framework/yii/redis/ActiveQuery.php index 1d44d97..54b7d31 100644 --- a/framework/yii/db/redis/ActiveQuery.php +++ b/framework/yii/redis/ActiveQuery.php @@ -8,7 +8,7 @@ * @license http://www.yiiframework.com/license/ */ -namespace yii\db\redis; +namespace yii\redis; /** * ActiveQuery represents a DB query associated with an Active Record class. diff --git a/framework/yii/db/redis/ActiveRecord.php b/framework/yii/redis/ActiveRecord.php similarity index 99% rename from framework/yii/db/redis/ActiveRecord.php rename to framework/yii/redis/ActiveRecord.php index b043e21..44cd5d7 100644 --- a/framework/yii/db/redis/ActiveRecord.php +++ b/framework/yii/redis/ActiveRecord.php @@ -8,7 +8,7 @@ * @license http://www.yiiframework.com/license/ */ -namespace yii\db\redis; +namespace yii\redis; use yii\base\InvalidCallException; use yii\base\InvalidConfigException; @@ -70,7 +70,7 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord public static function hashPk($pk) { - return (is_array($pk) ? implode('-', $pk) : $pk); // TODO escape PK glue + return is_array($pk) ? implode('-', $pk) : $pk; // TODO escape PK glue } /** diff --git a/framework/yii/db/redis/ActiveRelation.php b/framework/yii/redis/ActiveRelation.php similarity index 98% rename from framework/yii/db/redis/ActiveRelation.php rename to framework/yii/redis/ActiveRelation.php index e01f3a4..aae21fc 100644 --- a/framework/yii/db/redis/ActiveRelation.php +++ b/framework/yii/redis/ActiveRelation.php @@ -8,9 +8,7 @@ * @license http://www.yiiframework.com/license/ */ -namespace yii\db\redis; - -use yii\base\NotSupportedException; +namespace yii\redis; /** * ActiveRecord is the base class for classes representing relational data in terms of objects. @@ -19,7 +17,7 @@ use yii\base\NotSupportedException; * @author Carsten Brandt * @since 2.0 */ -class ActiveRelation extends \yii\db\redis\ActiveQuery +class ActiveRelation extends \yii\redis\ActiveQuery { /** * @var boolean whether this relation should populate all query results into AR instances. diff --git a/framework/yii/db/redis/RecordSchema.php b/framework/yii/redis/RecordSchema.php similarity index 97% rename from framework/yii/db/redis/RecordSchema.php rename to framework/yii/redis/RecordSchema.php index 3bc219d..6c82515 100644 --- a/framework/yii/db/redis/RecordSchema.php +++ b/framework/yii/redis/RecordSchema.php @@ -5,7 +5,7 @@ * @author Carsten Brandt */ -namespace yii\db\redis; +namespace yii\redis; use yii\base\InvalidConfigException; diff --git a/tests/unit/data/ar/redis/ActiveRecord.php b/tests/unit/data/ar/redis/ActiveRecord.php index 7419479..9f6d526 100644 --- a/tests/unit/data/ar/redis/ActiveRecord.php +++ b/tests/unit/data/ar/redis/ActiveRecord.php @@ -7,7 +7,7 @@ namespace yiiunit\data\ar\redis; -use yii\db\redis\Connection; +use yii\redis\Connection; /** * ActiveRecord is ... @@ -15,7 +15,7 @@ use yii\db\redis\Connection; * @author Qiang Xue * @since 2.0 */ -class ActiveRecord extends \yii\db\redis\ActiveRecord +class ActiveRecord extends \yii\redis\ActiveRecord { public static $db; diff --git a/tests/unit/data/ar/redis/Customer.php b/tests/unit/data/ar/redis/Customer.php index 91a75ff..30146b0 100644 --- a/tests/unit/data/ar/redis/Customer.php +++ b/tests/unit/data/ar/redis/Customer.php @@ -2,7 +2,7 @@ namespace yiiunit\data\ar\redis; -use yii\db\redis\RecordSchema; +use yii\redis\RecordSchema; class Customer extends ActiveRecord { @@ -12,7 +12,7 @@ class Customer extends ActiveRecord public $status2; /** - * @return \yii\db\redis\ActiveRelation + * @return \yii\redis\ActiveRelation */ public function getOrders() { diff --git a/tests/unit/data/ar/redis/Item.php b/tests/unit/data/ar/redis/Item.php index 55d1420..3ab0f10 100644 --- a/tests/unit/data/ar/redis/Item.php +++ b/tests/unit/data/ar/redis/Item.php @@ -2,7 +2,7 @@ namespace yiiunit\data\ar\redis; -use yii\db\redis\RecordSchema; +use yii\redis\RecordSchema; class Item extends ActiveRecord { diff --git a/tests/unit/data/ar/redis/Order.php b/tests/unit/data/ar/redis/Order.php index 8ccb12e..39979fe 100644 --- a/tests/unit/data/ar/redis/Order.php +++ b/tests/unit/data/ar/redis/Order.php @@ -2,7 +2,7 @@ namespace yiiunit\data\ar\redis; -use yii\db\redis\RecordSchema; +use yii\redis\RecordSchema; class Order extends ActiveRecord { diff --git a/tests/unit/data/ar/redis/OrderItem.php b/tests/unit/data/ar/redis/OrderItem.php index f0719b9..b77c216 100644 --- a/tests/unit/data/ar/redis/OrderItem.php +++ b/tests/unit/data/ar/redis/OrderItem.php @@ -2,7 +2,7 @@ namespace yiiunit\data\ar\redis; -use yii\db\redis\RecordSchema; +use yii\redis\RecordSchema; class OrderItem extends ActiveRecord { diff --git a/tests/unit/framework/db/redis/ActiveRecordTest.php b/tests/unit/framework/redis/ActiveRecordTest.php similarity index 99% rename from tests/unit/framework/db/redis/ActiveRecordTest.php rename to tests/unit/framework/redis/ActiveRecordTest.php index e9a66e6..2587878 100644 --- a/tests/unit/framework/db/redis/ActiveRecordTest.php +++ b/tests/unit/framework/redis/ActiveRecordTest.php @@ -1,8 +1,8 @@ getParam('databases'); $params = isset($databases['redis']) ? $databases['redis'] : array(); - $db = new \yii\db\redis\Connection; + $db = new Connection; $db->dsn = $params['dsn']; $db->password = $params['password']; if ($reset) { $db->open(); $db->flushall(); -/* $lines = explode(';', file_get_contents($params['fixture'])); - foreach ($lines as $line) { - if (trim($line) !== '') { - $db->pdo->exec($line); - } - }*/ } return $db; }