[eluser]edwardj[/eluser]
I wanted to integrate ADODB with the codeigniter, and I noticed that the implementation in the wiki looked like it was designed for the older versions of CI. I made my own implementation with modifications, based off some of the other posts I saw here on the boards. This version lets you load multiple database connections and it goes off your database.php config.
For this to work, unzip the adodb files to applications/libraries/adodb.
And have the below file: adodb_loader.php in your applications/libraries folder.
Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Adodb_loader
{
function Adodb_loader($params=null)
{
// check if adodb already loaded
if (!class_exists('ADONewConnection'))
{
require_once(APPPATH.'libraries/adodb/adodb.inc'.EXT);
}
// database handler's name, defaults to 'adodb'
$dbh = (isset($params['name'])) ? $params['name'] : 'adodb';
// the db settings group from the database.php config
$db_group = (isset($params['group'])) ? $params['group'] : '';
$this->_init_adodb_library($dbh,$db_group);
}
function _init_adodb_library($dbh,$db_group)
{
// get CI instance
$CI =& get_instance();
// get database config
include(APPPATH.'config/database'.EXT);
// check which database group settings to use
// default to database setting default
$db_group = (!empty($db_group)) ? $db_group : $active_group;
$cfg = $db[$db_group];
// check that driver is set
if (isset($cfg['dbdriver']))
{
$CI->$dbh =& ADONewConnection($cfg['dbdriver']);
// set debug
$CI->$dbh->debug = $cfg['db_debug'];
// check for persistent connection
if ($cfg['pconnect'])
{
// persistent
$CI->$dbh->PConnect($cfg['hostname'],$cfg['username'],$cfg['password'],$cfg['database']) or die("can't do it: " . $CI->$dbh->ErrorMsg());
}
else
{
// normal
$CI->$dbh->Connect($cfg['hostname'],$cfg['username'],$cfg['password'],$cfg['database']) or die("can't do it: " . $CI->$dbh->ErrorMsg());
}
// use associated array as default format
$CI->$dbh->SetFetchMode(ADODB_FETCH_ASSOC);
}
else
{
die("database settings not set");
}
}
}
?>
Here some examples:
Code: // will create $this->adodb and use the default settings group in config/database.php
$this->load->library('adodb_loader');
// will create $this->db1 and use the default settings group
$this->load->library('adodb_loader',array('name'=>'db1'));
// will create $this->db2 and use the settings group 'setting1' from the config
$this->load->library('adodb_loader',array('name'=>'db2','group'=>'settings1'));
I was able to implement persistent connections and debug settings which are set in config/database.php, but I couldn't get caching working (it's not a flag in adodb, but a separate execute cached sql command) and active records isn't implemented.
Feedback/improvements are more than welcome. Enjoy!
[eluser]Thoer[/eluser]
Thank you edwardj!
I'm only planning to use ADOdb, but I'll definietly use your code as it's quite pretty. Thanks again!
[eluser]Kepler[/eluser]
I used a slightly different approach. I have pass through functions from my ADODB class. I place my class directly in system/libraries so those who want to put the class in system/application/libraries will have to change the CI_AdoDB to MY_AdoDB.
Here is the version that you can place in system/libraries/Adodb.php (Case is important!)
Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* AdoDB Class
*
* @package CodeIgniter
* @subpackage Libraries
* @category AdoDB
* @author Kepler Gelotte
*/
require_once( BASEPATH.'/libs/adodb/adodb.inc.php' );
class CI_AdoDB {
var $conn = false;
function CI_AdoDB()
{
log_message('debug', "AdoDB Class Initialized");
}
function connect( $name_space = '' )
{
include(APPPATH.'config/database'.EXT);
$group = ($name_space == '') ? $active_group : $name_space;
if ( ! isset($db[$group]))
{
show_error('You have specified an invalid database connection group: '.$group);
}
$params = $db[$group];
$this->conn = &ADONewConnection;( $params['dbdriver'].'://'.$params['username'].':'.$params['password'].'@'.$params['hostname'].'/'.$params['database'] );
if ( ! $this->conn ) die( "Connection failed to database " . $db );
}
function execute( $statement )
{
$recordSet = $this->conn->Execute( $statement );
return $recordSet;
}
function replace( $table, $fields, $keys, $autoQuote = false )
{
$rc = $this->conn->Replace( $table, $fields, $keys, $autoQuote );
return $rc;
}
function startTrans( )
{
$rc = $this->conn->StartTrans( );
return $rc;
}
function failTrans( )
{
$rc = $this->conn->FailTrans( );
return $rc;
}
function completeTrans( )
{
$rc = $this->conn->CompleteTrans( );
return $rc;
}
function getErrorMsg()
{
return $this->conn->ErrorMsg();
}
function disconnect()
{
// $recordSet->Close(); # optional
$this->conn->Close();
}
}
// END AdoDB Class
?>
I load the library automatically from system/config/autoload.php
Code: $autoload['libraries'] = array('adodb',...
Here is an example of a model class using this code
Code: function _get_data()
{
$pages = false;
// Get the pages from the database using adodb if needed
$this->adodb->connect();
$recordSet = $this->adodb->execute( 'select page_id, page_name, page_url, page_title, page_author, page_keywords, page_description, page_language_id, page_is_active, page_order from pages' );
if ( ! $recordSet )
{
log_message( 'error', 'Error connecting to the database' );
log_message( 'debug', $this->adodb->getErrorMsg());
}
else
{
unset( $this->pages );
while (!$recordSet->EOF)
{
$pages[] = array(
'page' => $recordSet->fields[0],
'name' => $recordSet->fields[1],
'url' => $recordSet->fields[2],
'title' => $recordSet->fields[3],
'author' => $recordSet->fields[4],
'keywords' => $recordSet->fields[5],
'description' => $recordSet->fields[6],
'lang' => $recordSet->fields[7],
'is_active' => $recordSet->fields[8],
'order' => $recordSet->fields[9]
);
$recordSet->MoveNext();
}
$this->pages = $pages;
}
$this->adodb->disconnect();
}
Regards,
Kepler
[eluser]Juan Ignacio Borda[/eluser]
I've created a similar loader for the tiny adodb-lite layer ( http://sourceforge.net/projects/adodblite/ ) wich consume much less memory and it's faster.
put your adodb_lite library under: application/vendor/adodb_lite
Enjoy!
Code: <?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Adodb_lite_loader {
function Adodb_lite_loader($params = null)
{
// check if adodb already loaded
if (!class_exists('ADONewConnection'))
{
require_once(APPPATH . 'vendor/adodb_lite/adodb.inc' . EXT);
}
// database handler's name, defaults to 'adodb'
$dbh = (isset($params['name'])) ? $params['name'] : 'adodb_lite';
// the db settings group from the database.php config
$db_group = (isset($params['group'])) ? $params['group'] : '';
$this->_init_adodb_lite_library($dbh, $db_group);
}
function _init_adodb_lite_library($dbh, $db_group)
{
// get CI instance
$CI = & get_instance();
// get database config
include(APPPATH . 'config/database' . EXT);
// check which database group settings to use
// default to database setting default
$db_group = (!empty($db_group)) ? $db_group : $active_group;
$cfg = $db[$db_group];
// check that driver is set
if (isset($cfg['dbdriver']))
{
$CI->$dbh = & ADONewConnection($cfg['dbdriver'], 'object');
// set debug
$CI->$dbh->debug = $cfg['db_debug'];
// check for persistent connection
if ($cfg['pconnect'])
{
// persistent
$CI->$dbh->PConnect($cfg['hostname'], $cfg['username'], $cfg['password'], $cfg['database']) or die("can't do it: " . $CI->$dbh->ErrorMsg());
} else
{
// normal
$CI->$dbh->Connect($cfg['hostname'], $cfg['username'], $cfg['password'], $cfg['database']) or die("can't do it: " . $CI->$dbh->ErrorMsg());
}
// use associated array as default format
//$CI->$dbh->SetFetchMode(ADODB_FETCH_ASSOC);
} else
{
die("database settings not set");
}
}
}
|