Welcome Guest, Not a member yet? Register   Sign In
My Adodb Library integration
#1

[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!


Messages In This Thread
My Adodb Library integration - by El Forum - 11-23-2007, 01:52 PM
My Adodb Library integration - by El Forum - 11-24-2007, 07:33 AM
My Adodb Library integration - by El Forum - 01-04-2008, 10:47 PM
My Adodb Library integration - by El Forum - 05-23-2012, 10:16 AM



Theme © iAndrew 2016 - Forum software by © MyBB