TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
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
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:55 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design