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.
diff --git a/system/core/Loader.php b/system/core/Loader.php
index 808fa80..9525f35 100644
--- a/system/core/Loader.php
+++ b/system/core/Loader.php
@@ -352,26 +352,30 @@
/**
* 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';
- $CI->dbutil = new $class();
+ if ($return === TRUE)
+ {
+ return new $class($db);
+ }
+
+ $CI->dbutil = new $class($db);
}
// --------------------------------------------------------------------
@@ -379,35 +383,42 @@
/**
* 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';
}
- $CI->dbforge = new $class();
+ if ($return === TRUE)
+ {
+ return new $class($db);
+ }
+
+ $CI->dbforge = new $class($db);
}
// --------------------------------------------------------------------
diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php
index 2b9fb16..34140c6 100644
--- a/system/database/DB_forge.php
+++ b/system/database/DB_forge.php
@@ -40,7 +40,7 @@
*
* @var object
*/
- public $db;
+ protected $db;
/**
* Fields data
@@ -150,13 +150,12 @@
/**
* 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 f7bef6a..a8e34c9 100644
--- a/system/database/DB_utility.php
+++ b/system/database/DB_utility.php
@@ -33,14 +33,14 @@
* @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 @@
/**
* 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 ea32077..019f6d3 100644
--- a/system/database/drivers/mysql/mysql_forge.php
+++ b/system/database/drivers/mysql/mysql_forge.php
@@ -74,11 +74,12 @@
/**
* 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 914d6a2..088a6a0 100644
--- a/system/database/drivers/mysqli/mysqli_forge.php
+++ b/system/database/drivers/mysqli/mysqli_forge.php
@@ -74,11 +74,12 @@
/**
* 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 dc856ed..2add7d2 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 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 b5235d2..1e4d2ac 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 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 4148262..2e5f123 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 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 ad26763..425622f 100644
--- a/system/database/drivers/postgre/postgre_forge.php
+++ b/system/database/drivers/postgre/postgre_forge.php
@@ -64,11 +64,12 @@
/**
* 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 7ba0f7b..e9b91e9 100644
--- a/system/database/drivers/sqlite3/sqlite3_forge.php
+++ b/system/database/drivers/sqlite3/sqlite3_forge.php
@@ -54,11 +54,12 @@
/**
* 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', '<'))
{