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
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
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
Old 12-11-2007, 03:44 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

It's a nice start, you may want to do some research into the Table Data Gateway and Row Data Gateway patterns as these provide a really flexible solution to this problem.

If you are looking for a pre-built solution, [url=http://framework.zend.com/]Zend Framework[/a] provides a set of related classes to facilitate a SQL database interface.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-11-2007, 04:01 PM   #3 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Thanks for your reply, I'll look into the first two link you gave me.

I've used the Zend Framework a bit. But since I've got some free time before my co-op ends. I figured I'd try writing my own for the learning experience.
bdm is offline  
Reply With Quote
Old 12-11-2007, 04:45 PM   #4 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

That seems to look good, for a basic portable wrapper. You may want to take the wrapping to another level though, and start to wrap functions for specific tasks.

Ie.:
users.db.php->odbc.db.php->db.abstract.php
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 12-11-2007, 04:47 PM   #5 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

bluesaga, I don't follow.
bdm is offline  
Reply With Quote
Old 12-11-2007, 04:52 PM   #6 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

Well ie, you make a wrapper to do functions that you would use for user functions, like i dunno, check the passwords etc :)
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 12-12-2007, 03:31 PM   #7 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

@Karl

Do you perhaps have any PHP snippets using the Table Data Gateway and Row Data Gateway pattern? I can't seem to find any examples that could fit with what I currently have.

Thank you.
bdm is offline  
Reply With Quote
Old 03-30-2013, 07:50 AM   #8 (permalink)
The Acquainted
 
Join Date: Mar 2013
Posts: 131
Thanks: 1
caiyanfang is on a distinguished road
Default caiyanfang

The Lime green G Shock from Casio typically watch for almost any outdoor and online game enthusiast. These watches are designed to withstand strong vibrations possibly hard knocks and are also known and sought for their high durability and high quality. They are designed by sport christian louboutin outlet but are utilized casually as well and christian louboutin outlet online come in other colors apart from lime green. They not only tell period but also have a stop watch that has made water resistant capital suitable for exercising and different varieties of sports.
caiyanfang is offline  
Reply With Quote
Reply



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 06:59 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