From eaa60c71082c1e49f8a48d633347c98b68a387c0 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 6 Nov 2012 01:11:22 +0200 Subject: Added possibility to pass custom database objects to DB Forge and DB Utilities Also, their property is no longer public and the utility class no longer extends CI_DB_forge. --- system/core/Loader.php | 61 +++++++++++++--------- system/database/DB_forge.php | 9 ++-- system/database/DB_utility.php | 11 ++-- system/database/drivers/mysql/mysql_forge.php | 5 +- system/database/drivers/mysqli/mysqli_forge.php | 5 +- .../drivers/pdo/subdrivers/pdo_mysql_forge.php | 5 +- .../drivers/pdo/subdrivers/pdo_pgsql_forge.php | 5 +- .../drivers/pdo/subdrivers/pdo_sqlite_forge.php | 5 +- system/database/drivers/postgre/postgre_forge.php | 5 +- system/database/drivers/sqlite3/sqlite3_forge.php | 5 +- 10 files changed, 66 insertions(+), 50 deletions(-) (limited to 'system') diff --git a/system/core/Loader.php b/system/core/Loader.php index 808fa80df..9525f35d0 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -352,26 +352,30 @@ class CI_Loader { /** * Load the Database Utilities Class * - * @return void + * @param object $db Database object + * @param bool $return Whether to return the DB Forge class object or not + * @return void|object */ - public function dbutil() + public function dbutil($db = NULL, $return = FALSE) { - if ( ! class_exists('CI_DB')) - { - $this->database(); - } - $CI =& get_instance(); - // for backwards compatibility, load dbforge so we can extend dbutils off it - // this use is deprecated and strongly discouraged - $CI->load->dbforge(); + if ( ! is_object($db) OR ! ($db instanceof CI_DB)) + { + class_exists('CI_DB', FALSE) OR $this->database(); + $db =& $CI->db; + } require_once(BASEPATH.'database/DB_utility.php'); - require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_utility.php'); - $class = 'CI_DB_'.$CI->db->dbdriver.'_utility'; + require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_utility.php'); + $class = 'CI_DB_'.$db->dbdriver.'_utility'; + + if ($return === TRUE) + { + return new $class($db); + } - $CI->dbutil = new $class(); + $CI->dbutil = new $class($db); } // -------------------------------------------------------------------- @@ -379,35 +383,42 @@ class CI_Loader { /** * Load the Database Forge Class * - * @return void + * @param object $db Database object + * @param bool $return Whether to return the DB Forge class object or not + * @return void|object */ - public function dbforge() + public function dbforge($db = NULL, $return = FALSE) { - if ( ! class_exists('CI_DB')) + $CI =& get_instance(); + if ( ! is_object($db) OR ! ($db instanceof CI_DB)) { - $this->database(); + class_exists('CI_DB', FALSE) OR $this->database(); + $db =& $CI->db; } - $CI =& get_instance(); - require_once(BASEPATH.'database/DB_forge.php'); - require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php'); + require_once(BASEPATH.'database/drivers/'.$db->dbdriver.'/'.$db->dbdriver.'_forge.php'); - if ( ! empty($CI->db->subdriver)) + if ( ! empty($db->subdriver)) { - $driver_path = BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/subdrivers/'.$CI->db->dbdriver.'_'.$CI->db->subdriver.'_forge.php'; + $driver_path = BASEPATH.'database/drivers/'.$db->dbdriver.'/subdrivers/'.$db->dbdriver.'_'.$db->subdriver.'_forge.php'; if (file_exists($driver_path)) { require_once($driver_path); - $class = 'CI_DB_'.$CI->db->dbdriver.'_'.$CI->db->subdriver.'_forge'; + $class = 'CI_DB_'.$db->dbdriver.'_'.$db->subdriver.'_forge'; } } else { - $class = 'CI_DB_'.$CI->db->dbdriver.'_forge'; + $class = 'CI_DB_'.$db->dbdriver.'_forge'; + } + + if ($return === TRUE) + { + return new $class($db); } - $CI->dbforge = new $class(); + $CI->dbforge = new $class($db); } // -------------------------------------------------------------------- diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 2b9fb169a..34140c6e0 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -40,7 +40,7 @@ abstract class CI_DB_forge { * * @var object */ - public $db; + protected $db; /** * Fields data @@ -150,13 +150,12 @@ abstract class CI_DB_forge { /** * Class constructor * + * @param object &$db Database object * @return void */ - public function __construct() + public function __construct(&$db) { - // Assign the main database object to $this->db - $CI =& get_instance(); - $this->db =& $CI->db; + $this->db =& $db; log_message('debug', 'Database Forge Class Initialized'); } diff --git a/system/database/DB_utility.php b/system/database/DB_utility.php index f7bef6a2c..a8e34c92d 100644 --- a/system/database/DB_utility.php +++ b/system/database/DB_utility.php @@ -33,14 +33,14 @@ defined('BASEPATH') OR exit('No direct script access allowed'); * @author EllisLab Dev Team * @link http://codeigniter.com/user_guide/database/ */ -abstract class CI_DB_utility extends CI_DB_forge { +abstract class CI_DB_utility { /** * Database object * * @var object */ - public $db; + protected $db; // -------------------------------------------------------------------- @@ -70,13 +70,12 @@ abstract class CI_DB_utility extends CI_DB_forge { /** * Class constructor * + * @param object &$db Database object * @return void */ - public function __construct() + public function __construct(&$db) { - // Assign the main database object to $this->db - $CI =& get_instance(); - $this->db =& $CI->db; + $this->db =& $db; log_message('debug', 'Database Utility Class Initialized'); } diff --git a/system/database/drivers/mysql/mysql_forge.php b/system/database/drivers/mysql/mysql_forge.php index ea3207760..019f6d353 100644 --- a/system/database/drivers/mysql/mysql_forge.php +++ b/system/database/drivers/mysql/mysql_forge.php @@ -74,11 +74,12 @@ class CI_DB_mysql_forge extends CI_DB_forge { /** * Class constructor * + * @param object &$db Database object * @return void */ - public function __construct() + public function __construct(&$db) { - parent::__construct(); + parent::__construct($db); $this->_create_table .= ' DEFAULT CHARSET '.$this->db->char_set.' COLLATE '.$this->db->dbcollat; } diff --git a/system/database/drivers/mysqli/mysqli_forge.php b/system/database/drivers/mysqli/mysqli_forge.php index 914d6a268..088a6a09a 100644 --- a/system/database/drivers/mysqli/mysqli_forge.php +++ b/system/database/drivers/mysqli/mysqli_forge.php @@ -74,11 +74,12 @@ class CI_DB_mysqli_forge extends CI_DB_forge { /** * Class constructor * + * @param object &$db Database object * @return void */ - public function __construct() + public function __construct(&$db) { - parent::__construct(); + parent::__construct($db); $this->_create_table .= ' DEFAULT CHARSET '.$this->db->char_set.' COLLATE '.$this->db->dbcollat; } diff --git a/system/database/drivers/pdo/subdrivers/pdo_mysql_forge.php b/system/database/drivers/pdo/subdrivers/pdo_mysql_forge.php index dc856ed2c..2add7d204 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_mysql_forge.php +++ b/system/database/drivers/pdo/subdrivers/pdo_mysql_forge.php @@ -88,11 +88,12 @@ class CI_DB_pdo_mysql_forge extends CI_DB_pdo_forge { /** * Class constructor * + * @param object &$db Database object * @return void */ - public function __construct() + public function __construct(&$db) { - parent::__construct(); + parent::__construct($db); $this->_create_table .= ' DEFAULT CHARSET '.$this->db->char_set.' COLLATE '.$this->db->dbcollat; } diff --git a/system/database/drivers/pdo/subdrivers/pdo_pgsql_forge.php b/system/database/drivers/pdo/subdrivers/pdo_pgsql_forge.php index b5235d24b..1e4d2ac59 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_pgsql_forge.php +++ b/system/database/drivers/pdo/subdrivers/pdo_pgsql_forge.php @@ -71,11 +71,12 @@ class CI_DB_pdo_pgsql_forge extends CI_DB_pdo_forge { /** * Class constructor * + * @param object &$db Database object * @return void */ - public function __construct() + public function __construct(&$db) { - parent::__construct(); + parent::__construct($db); if (version_compare($this->db->version(), '9.0', '>')) { diff --git a/system/database/drivers/pdo/subdrivers/pdo_sqlite_forge.php b/system/database/drivers/pdo/subdrivers/pdo_sqlite_forge.php index 414826212..2e5f12379 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_sqlite_forge.php +++ b/system/database/drivers/pdo/subdrivers/pdo_sqlite_forge.php @@ -68,11 +68,12 @@ class CI_DB_pdo_sqlite_forge extends CI_DB_pdo_forge { /** * Class constructor * + * @param object &$db Database object * @return void */ - public function __construct() + public function __construct(&$db) { - parent::__construct(); + parent::__construct($db); if (version_compare($this->db->version(), '3.3', '<')) { diff --git a/system/database/drivers/postgre/postgre_forge.php b/system/database/drivers/postgre/postgre_forge.php index ad26763a4..425622fac 100644 --- a/system/database/drivers/postgre/postgre_forge.php +++ b/system/database/drivers/postgre/postgre_forge.php @@ -64,11 +64,12 @@ class CI_DB_postgre_forge extends CI_DB_forge { /** * Class constructor * + * @param object &$db Database object * @return void */ - public function __construct() + public function __construct(&$db) { - parent::__construct(); + parent::__construct($db); if (version_compare($this->db->version(), '9.0', '>')) { diff --git a/system/database/drivers/sqlite3/sqlite3_forge.php b/system/database/drivers/sqlite3/sqlite3_forge.php index 7ba0f7bab..e9b91e972 100644 --- a/system/database/drivers/sqlite3/sqlite3_forge.php +++ b/system/database/drivers/sqlite3/sqlite3_forge.php @@ -54,11 +54,12 @@ class CI_DB_sqlite3_forge extends CI_DB_forge { /** * Class constructor * + * @param object &$db Database object * @return void */ - public function __construct() + public function __construct(&$db) { - parent::__construct(); + parent::__construct($db); if (version_compare($this->db->version(), '3.3', '<')) { -- cgit v1.2.3-24-g4f1b