View Single Post
Old 12-11-2007, 03:14 PM   #1 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default Comment on my database wrapper.

I decided to write a wrapper class because we use MySQL and ODBC at work in most applications we write. So I thought it wouldn't be a bad idea if done right. So here's what I've got so far and it works, but I do believe it's missing flexibility.

Here is my hierarchy:
Code:
lib/
    config/
        databases.php
    Db/
        Adapter/
            Abstract,php
            Mysql.php
            Odbc.php
        DatabaseFactory.php
lib/config/databases.php
PHP Code:
$database = array(
    
'mysql_host' => array(
        
'host' => 'host',
        
'username' => 'username',
        
'password' => 'password',
        
'defaultDb'=> 'default_db'
    
),
    
'odbc_host' => array(
        
'dsn' => 'dsn',
        
'username' => 'username',
        
'password' => 'password'
    
)
); 
lib/Db/DatabaseFactory.php
PHP Code:
class DatabaseFactory {
    public static function 
factory($adapter$config) {
        if(!
is_string($adapter) || empty($adapter)) {
            throw new 
Exception('adapter needs to be a string');
        }

        if(!
is_array($config)) {
            throw new 
Exception('config needs to be an array');
        }
        
        
$adapterNamespace 'Db_Adapter';


        require_once 
'Adapter/' ucwords($adapter) . '.php';

        
$adapterName $adapterNamespace '_' $adapter;
        
$dbAdapter = new $adapterName($config);
        
        return 
$dbAdapter;
    }

lib/Db/Adapter/Abstract.php
PHP Code:
abstract class Db_Adapter_Abstract {
    protected 
$_connection null;
    protected 
$_results null;
    protected 
$_searchResults = array();
    
    public function 
__construct($config) {
        
$this->connect($config);
    }
    
    abstract protected function 
connect($config);


lib/Db/Adapter/Mysql.php
PHP Code:
include_once 'Abstract.php';

class 
Db_Adapter_Mysql extends Db_Adapter_Abstract {
    protected function 
connect($config) {
        try {
            
$this->connection mysql_connect(
                
$config['host'], 
                
$config['username'], 
                
$config['password']
            );
    
            
mysql_select_db($config['defaultDb']);
        } catch(
Exception $e) {
            echo 
$e->getMessage();
        }
    }

    public function 
query($query) {
        
$this->_results mysql_query($query);

        return 
$this;
    }
    
    public function 
fetchAll() {
        while(
$row mysql_fetch_assoc($this->_results)) {
            
array_push($this->_searchResults$row);
        }

        return 
$this->_searchResults;
    }

    public function 
getNumRows() {
        return (int) 
mysql_num_rows($this->_results);
    }

lib/Db/Adapter/Odbc.php
PHP Code:
include_once 'Abstract.php';

class 
Db_Adapter_Odbc extends Db_Adapter_Abstract {
    protected function 
connect($config) {
        try {
            
$this->_connection odbc_connect(
                
$config['dsn'], 
                
$config['username'], 
                
$config['password']
            );    
        } catch(
Exception $e) {
            echo 
'e: ' $e->getMessage();
        }
    }

    public function 
query($query$params = array()) {
        if(!
is_array($params) || empty($params)) {
            
$this->_results odbc_exec($this->_connection$query);
        }
        
        if(
is_array($params) && sizeof($params) > 0) {
            
$stmt odbc_prepare($this->_connection$query
                or die(
odbc_error());
            
$this->_results odbc_execute($stmt$params)
                or die(
odbc_error());
        }

        return 
$this;
    }
    
    public function 
fetchAll() {
        while(
$row odbc_fetch_array($this->_results)) {
            
array_push($this->_searchResults$row);
        }

        return 
$this->_searchResults;
    }

    public function 
getNumRows() {
        return (int) 
odbc_num_rows($this->_results);
    }

Here is currently how you would use it:
PHP Code:
ini_set('display_errors''on');
error_reporting(E_ALL|E_STRICT);

include 
'./config/databases.php';
include 
'Db/DatabaseFactory.php';

$odbc DatabaseFactory::factory('odbc'$database['odbc_host']);

echo 
'<pre>';
$q $odbc->query('select * from foo');
print_r($q->fetchAll());
echo 
'</pre>';

$mysql DatabaseFactory::factory('mysql'$database['mysql_host']);

echo 
'<pre>';
print_r($mysql->query('select * from bar')->fetchAll());
echo 
'</pre>'
One thing I was thinking about was making my results returned as an iterator list. Good idea?

Constructive criticism is more than welcomed.

Thank you.
bdm is offline  
Reply With Quote