PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/pear/PDB/tags/0.0.3/PDB.php

http://digg.googlecode.com/
PHP | 139 lines | 39 code | 10 blank | 90 comment | 2 complexity | b23a1f92659fd4fbe96394be80412e60 MD5 | raw file
  1<?php
  2
  3/**
  4 * A simplistic wrapper for PDO
  5 *
  6 * PDB is a simplistic wrapper that adds helper functions to PDO. It was
  7 * creatd in the vain of DB and MDB2, but a pure PHP5/PDO implementation.
  8 *
  9 * PHP version 5
 10 *
 11 * Copyright (c) 2007, Digg, Inc.
 12 * 
 13 * All rights reserved.
 14 * 
 15 * Redistribution and use in source and binary forms, with or without 
 16 * modification, are permitted provided that the following conditions are met:
 17 *
 18 *  - Redistributions of source code must retain the above copyright notice,
 19 *    this list of conditions and the following disclaimer.
 20 *  - Redistributions in binary form must reproduce the above copyright notice,
 21 *    this list of conditions and the following disclaimer in the documentation
 22 *    and/or other materials provided with the distribution.
 23 *  - Neither the name of the Digg, Inc. nor the names of its contributors 
 24 *    may be used to endorse or promote products derived from this software 
 25 *    without specific prior written permission.
 26 *
 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 37 * POSSIBILITY OF SUCH DAMAGE.
 38 *
 39 * @category   DB
 40 * @package    PDB
 41 * @author     Joe Stump <joe@joestump.net> 
 42 * @copyright  2007-2008 (c) Digg.com 
 43 * @license    http://tinyurl.com/42zef New BSD License
 44 * @version    CVS: $Id:$
 45 * @link       http://www.php.net/pdo
 46 * @link       http://pear.php.net/package/PDB
 47 * @filesource
 48 */
 49
 50/**
 51 * Base PDB class
 52 *
 53 * @category   DB
 54 * @package    PDB
 55 * @author     Joe Stump <joe@joestump.net> 
 56 * @copyright  2007-2008 (c) Digg.com 
 57 * @license    http://tinyurl.com/42zef New BSD License
 58 * @version    Release: @package_version@
 59 * @link       http://pear.php.net/package/PDB
 60 */
 61abstract class PDB
 62{
 63    /**
 64     * Singleton connections
 65     *
 66     * @see PDB::singleton()
 67     * @var array $singletons
 68     */
 69    static protected $singletons = array();
 70
 71    /**
 72     * Connect to a database
 73     *
 74     * @param string $dsn      PDO DSN (e.g. mysql:host=127.0.0.1:dbname=foo)
 75     * @param string $username The DB username 
 76     * @param string $password The DB password
 77     * @param array  $options  PDO options
 78     *
 79     * @access public
 80     * @throws {@link PDB_Exception} when unable to connect
 81     * @link http://us.php.net/manual/en/pdo.constants.php
 82     * @link http://us.php.net/manual/en/pdo.construct.php
 83     * @return object Instance of PDB driver
 84     */
 85    public static function connect($dsn, 
 86                                   $username = null,
 87                                   $password = null,
 88                                   array $options = array()) 
 89    {
 90        list($type,) = explode(':', $dsn);
 91
 92        $file = 'PDB/' . $type . '.php';
 93        include_once $file;
 94
 95        $class = 'PDB_' . $type;
 96        if (!class_exists($class)) {
 97            throw new PDB_Exception('PDB class not found: ' . $class);
 98        }
 99
100        try {
101            $instance = new $class($dsn, $username, $password, $options);
102        } catch (PDOException $error) {
103            throw new PDB_Exception($error);
104        }
105
106        return $instance;
107    }
108
109    /**
110     * Create a singleton DB connection
111     *
112     * @param string $dsn      PDO DSN (e.g. mysql:host=127.0.0.1:dbname=foo)
113     * @param string $username The DB username 
114     * @param string $password The DB password
115     * @param array  $options  PDO options
116     *
117     * @access public
118     * @return object Instance of PDB driver
119     * @throws {@link PDB_Exception} when unable to connect
120     * @link http://us.php.net/manual/en/pdo.construct.php
121     */
122    static public function singleton($dsn,
123                                     $username = null,
124                                     $password = null,
125                                     array $options = array()) 
126    {
127        $key = md5($dsn . $username . $password . serialize($options));
128        if (!isset(self::$singletons[$key])) {
129            self::$singletons[$key] = self::connect($dsn, 
130                                                    $username,
131                                                    $password,
132                                                    $options);
133        }
134
135        return self::$singletons[$key];
136    }
137}
138
139?>