Skip to content

Commit

Permalink
Adding master/slave database support (Issue #6).
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew A Mattson committed Jan 29, 2013
1 parent e2d4890 commit 4d0cdc5
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 35 deletions.
2 changes: 1 addition & 1 deletion ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Change Log for LEAP ORM

2013-01-28
* Changed Base_DB_ORM_Model::data_source() to Base_DB_ORM_Model::data_source($context = 0).
* Changed Base_DB_ORM_Model::data_source() to Base_DB_ORM_Model::data_source($instance = 0).
* Changed all instance variables $source to $data_source.

2013-01-01
Expand Down
18 changes: 17 additions & 1 deletion classes/Base/DB/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,28 @@
*
* @package Leap
* @category Connection
* @version 2013-01-26
* @version 2013-01-28
*
* @abstract
*/
abstract class Base_DB_DataSource extends Core_Object {

/**
* This constant represents a master instance of a database.
*
* @access public
* @const integer
*/
const MASTER_INSTANCE = 0;

/**
* This constant represents a slave instance of a database.
*
* @access public
* @const integer
*/
const SLAVE_INSTANCE = 1;

/**
* This variable stores the settings for the data source.
*
Expand Down
4 changes: 2 additions & 2 deletions classes/Base/DB/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @package Leap
* @category ORM
* @version 2013-01-09
* @version 2013-01-28
*
* @abstract
*/
Expand Down Expand Up @@ -107,7 +107,7 @@ public static function model($model, $primary_key = array()) {
* @return DB_SQL_Precompiler an instance of the pre-compiler
*/
public static function precompiler($model) {
$data_source = $model::data_source();
$data_source = $model::data_source(DB_DataSource::MASTER_INSTANCE);
$precompiler = 'DB_' . $data_source->dialect . '_Precompiler';
$object = new $precompiler($data_source);
return $object;
Expand Down
2 changes: 1 addition & 1 deletion classes/Base/DB/ORM/Delete/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function __call($function, $arguments) {
public function __construct($model) {
$name = $model;
$model = DB_ORM_Model::model_name($name);
$this->data_source = new DB_DataSource($model::data_source(1));
$this->data_source = new DB_DataSource($model::data_source(DB_DataSource::SLAVE_INSTANCE));
$builder = 'DB_' . $this->data_source->dialect . '_Delete_Builder';
$this->builder = new $builder($this->data_source);
$extension = DB_ORM_Model::builder_name($name);
Expand Down
2 changes: 1 addition & 1 deletion classes/Base/DB/ORM/Insert/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ abstract class Base_DB_ORM_Insert_Proxy extends Core_Object implements DB_SQL_St
public function __construct($model) {
$name = $model;
$model = DB_ORM_Model::model_name($name);
$this->data_source = new DB_DataSource($model::data_source());
$this->data_source = new DB_DataSource($model::data_source(DB_DataSource::MASTER_INSTANCE));
$builder = 'DB_' . $this->data_source->dialect . '_Insert_Builder';
$this->builder = new $builder($this->data_source);
$extension = DB_ORM_Model::builder_name($name);
Expand Down
12 changes: 6 additions & 6 deletions classes/Base/DB/ORM/MPTT.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function add_child($name, Array $fields = NULL) {
throw new Throwable_Marshalling_Exception('Message: Failed to insert record to database. Reason: Model is not insertable.', array(':class' => get_called_class()));
}

$data_source = static::data_source();
$data_source = static::data_source(DB_DataSource::MASTER_INSTANCE);
$table = static::table();

$connection = DB_Connection_Pool::instance()->get_connection($data_source);
Expand Down Expand Up @@ -243,7 +243,7 @@ public function add_child($name, Array $fields = NULL) {
* node
*/
public function ancestors($ordering = 'ASC', $limit = 0, $root = TRUE) {
$data_source = static::data_source();
$data_source = static::data_source(DB_DataSource::SLAVE_INSTANCE);

$builder = DB_SQL::select($data_source)
->all('t1.*')
Expand Down Expand Up @@ -301,7 +301,7 @@ public function delete($reset = FALSE) {
throw new Throwable_Marshalling_Exception('Message: Failed to delete record from database. Reason: Model is not savable.', array(':class' => get_called_class()));
}

$data_source = static::data_source();
$data_source = static::data_source(DB_DataSource::MASTER_INSTANCE);
$table = static::table();

$connection = DB_Connection_Pool::instance()->get_connection($data_source);
Expand Down Expand Up @@ -496,7 +496,7 @@ public function leaves($ordering = 'ASC', $limit = 0) {
* @see http://stackoverflow.com/questions/7661913/modified-preorder-tree-traversal-selecting-nodes-1-level-deep
*/
public function level() {
$record = DB_SQL::select(static::data_source())
$record = DB_SQL::select(static::data_source(DB_DataSource::SLAVE_INSTANCE))
->column(DB_SQL::expr('COUNT(parent_id) - 1'), 'level')
->from(static::table())
->where('scope', DB_SQL_Operator::_EQUAL_TO_, $this->fields['scope']->value)
Expand Down Expand Up @@ -576,7 +576,7 @@ public function save($reload = FALSE, $mode = NULL) {
$columns = array_keys($this->fields);

if ( ! empty($columns)) {
$builder = DB_SQL::update(static::data_source())
$builder = DB_SQL::update(static::data_source(DB_DataSource::MASTER_INSTANCE))
->table(static::table())
->where('id', DB_SQL_Operator::_EQUAL_TO_, $this->fields['id']->value);

Expand Down Expand Up @@ -683,7 +683,7 @@ public function tree() {
* @return DB_ORM_MPTT the newly created root node
**/
public static function add_root($scope, $name, Array $fields = NULL) {
$data_source = static::data_source();
$data_source = static::data_source(DB_DataSource::MASTER_INSTANCE);
$table = static::table();

$connection = DB_Connection_Pool::instance()->get_connection($data_source);
Expand Down
12 changes: 6 additions & 6 deletions classes/Base/DB/ORM/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function delete($reset = FALSE) {
if (empty($primary_key) OR ! is_array($primary_key)) {
throw new Throwable_Marshalling_Exception('Message: Failed to delete record from database. Reason: No primary key has been declared.');
}
$builder = DB_SQL::delete(static::data_source())->from(static::table());
$builder = DB_SQL::delete(static::data_source(DB_DataSource::MASTER_INSTANCE))->from(static::table());
foreach ($primary_key as $column) {
$builder->where($column, DB_SQL_Operator::_EQUAL_TO_, $this->fields[$column]->value);
}
Expand Down Expand Up @@ -328,7 +328,7 @@ public function is_relation($name) {
* table
*/
public function is_saved() {
$builder = DB_SQL::select(static::data_source())
$builder = DB_SQL::select(static::data_source(DB_DataSource::MASTER_INSTANCE)) // done on master instead of slave
->from(static::table())
->limit(1);
foreach (static::primary_key() as $column) {
Expand Down Expand Up @@ -367,7 +367,7 @@ public function load(Array $columns = array()) {
if (empty($primary_key) OR ! is_array($primary_key)) {
throw new Throwable_Marshalling_Exception('Message: Failed to load record from database. Reason: No primary key has been declared.');
}
$builder = DB_SQL::select(static::data_source())->from(static::table())->limit(1);
$builder = DB_SQL::select(static::data_source(DB_DataSource::SLAVE_INSTANCE))->from(static::table())->limit(1);
foreach ($primary_key as $column) {
$builder->where($column, DB_SQL_Operator::_EQUAL_TO_, $this->fields[$column]->value);
}
Expand Down Expand Up @@ -450,7 +450,7 @@ public function save($reload = FALSE, $mode = NULL) {
throw new Throwable_Marshalling_Exception('Message: Failed to save record to database. Reason: No primary key has been declared.');
}

$data_source = static::data_source();
$data_source = static::data_source(DB_DataSource::MASTER_INSTANCE);
$table = static::table();
$columns = array_keys($this->fields);
$hash_code = $this->hash_code();
Expand Down Expand Up @@ -671,11 +671,11 @@ public static function columns() {
*
* @access public
* @static
* @param integer $context the data source context to be used (e.g.
* @param integer $instance the data source instance to be used (e.g.
* 0 = master, 1 = slave, 2 = slave, etc.)
* @return string the data source name
*/
public static function data_source($context = 0) {
public static function data_source($instance = 0) {
return 'default'; // the key used in config/database.php
}

Expand Down
2 changes: 1 addition & 1 deletion classes/Base/DB/ORM/Relation/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function load() {
$parent_model = $this->metadata['parent_model'];
$parent_table = $parent_model::table();
$parent_key = $this->metadata['parent_key'];
$parent_source = $parent_model::data_source();
$parent_source = $parent_model::data_source(DB_DataSource::SLAVE_INSTANCE);

$child_key = $this->metadata['child_key'];

Expand Down
4 changes: 2 additions & 2 deletions classes/Base/DB/ORM/Relation/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ protected function load() {
$child_model = $this->metadata['child_model'];
$child_table = $child_model::table();
$child_key = $this->metadata['child_key'];
$child_source = $child_model::data_source();
$child_source = $child_model::data_source(DB_DataSource::SLAVE_INSTANCE);

if (isset($this->metadata['through_model']) AND isset($this->metadata['through_keys'])) {
$through_model = $this->metadata['through_model'];
$through_table = $through_model::table();
$through_keys = $this->metadata['through_keys'];
$through_source = $through_model::data_source();
$through_source = $through_model::data_source(DB_DataSource::SLAVE_INSTANCE);

if ($through_source != $child_source) {
$builder = DB_SQL::select($through_source)
Expand Down
2 changes: 1 addition & 1 deletion classes/Base/DB/ORM/Relation/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function load() {
$child_model = $this->metadata['child_model'];
$child_table = $child_model::table();
$child_key = $this->metadata['child_key'];
$child_source = $child_model::data_source();
$child_source = $child_model::data_source(DB_DataSource::SLAVE_INSTANCE);

$builder = DB_SQL::select($child_source)
->all("{$child_table}.*")
Expand Down
2 changes: 1 addition & 1 deletion classes/Base/DB/ORM/Select/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function __call($function, $arguments) {
public function __construct($model, Array $columns = array()) {
$name = $model;
$model = DB_ORM_Model::model_name($name);
$this->data_source = new DB_DataSource($model::data_source());
$this->data_source = new DB_DataSource($model::data_source(DB_DataSource::SLAVE_INSTANCE));
$builder = 'DB_' . $this->data_source->dialect . '_Select_Builder';
$this->table = $model::table();
$this->builder = new $builder($this->data_source, $columns);
Expand Down
2 changes: 1 addition & 1 deletion classes/Base/DB/ORM/Update/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function __call($function, $arguments) {
public function __construct($model) {
$name = $model;
$model = DB_ORM_Model::model_name($name);
$this->data_source = new DB_DataSource($model::data_source());
$this->data_source = new DB_DataSource($model::data_source(DB_DataSource::MASTER_INSTANCE));
$builder = 'DB_' . $this->data_source->dialect . '_Update_Builder';
$this->builder = new $builder($this->data_source);
$extension = DB_ORM_Model::builder_name($name);
Expand Down
4 changes: 2 additions & 2 deletions classes/Base/Model/Leap/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public function __construct() {
* @access public
* @override
* @static
* @param integer $context the data source context to be used (e.g.
* @param integer $instance the data source instance to be used (e.g.
* 0 = master, 1 = slave, 2 = slave, etc.)
* @return string the data source name
*/
public static function data_source($context = 0) {
public static function data_source($instance = 0) {
return 'default';
}

Expand Down
4 changes: 2 additions & 2 deletions classes/Base/Model/Leap/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public function __construct() {
* @access public
* @override
* @static
* @param integer $context the data source context to be used (e.g.
* @param integer $instance the data source instance to be used (e.g.
* 0 = master, 1 = slave, 2 = slave, etc.)
* @return string the data source name
*/
public static function data_source($context = 0) {
public static function data_source($instance = 0) {
return 'default';
}

Expand Down
4 changes: 2 additions & 2 deletions classes/Base/Model/Leap/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ public function complete_login() {
* @access public
* @override
* @static
* @param integer $context the data source context to be used (e.g.
* @param integer $instance the data source instance to be used (e.g.
* 0 = master, 1 = slave, 2 = slave, etc.)
* @return string the data source name
*/
public static function data_source($context = 0) {
public static function data_source($instance = 0) {
return 'default';
}

Expand Down
4 changes: 2 additions & 2 deletions classes/Base/Model/Leap/User/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public function __construct() {
* @access public
* @override
* @static
* @param integer $context the data source context to be used (e.g.
* @param integer $instance the data source instance to be used (e.g.
* 0 = master, 1 = slave, 2 = slave, etc.)
* @return string the data source name
*/
public static function data_source($context = 0) {
public static function data_source($instance = 0) {
return 'default';
}

Expand Down
6 changes: 3 additions & 3 deletions classes/Base/Model/Leap/User/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function create_token() {
do {
$token = sha1(uniqid(Text::random('alnum', 32), TRUE));
}
while(DB_SQL::select($this->data_source())->from($this->table())->where('token', DB_SQL_Operator::_EQUAL_TO_, $token)->query()->is_loaded());
while(DB_SQL::select($this->data_source(DB_DataSource::SLAVE_INSTANCE))->from($this->table())->where('token', DB_SQL_Operator::_EQUAL_TO_, $token)->query()->is_loaded());
return $token;
}

Expand All @@ -101,11 +101,11 @@ public function create_token() {
* @access public
* @override
* @static
* @param integer $context the data source context to be used (e.g.
* @param integer $instance the data source instance to be used (e.g.
* 0 = master, 1 = slave, 2 = slave, etc.)
* @return string the data source name
*/
public static function data_source($context = 0) {
public static function data_source($instance = 0) {
return 'default';
}

Expand Down

0 comments on commit 4d0cdc5

Please sign in to comment.