Location: PHPKode > projects > Moc10 PHP Library > library/Moc10/Db.php
<?php
/**
 * Moc10 Library
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.TXT.
 * It is also available through the world-wide-web at this URL:
 * http://www.moc10phplibrary.com/LICENSE.TXT
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to hide@address.com so we can send you a copy immediately.
 *
 * @category   Moc10
 * @package    Moc10_Db
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 */

/**
 * Moc10_Db
 *
 * @category   Moc10
 * @package    Moc10_Db
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 * @version    1.9.7
 */

class Moc10_Db
{

    /**
     * Default database interface object
     * @var Moc10_Db_MySQL|Moc10_Db_MySQLi|Moc10_Db_PostgreSQL|Moc10_Db_SQLite
     */
    public $interface = null;

    /**
     * Instance of the database connection
     * @var Moc10_Db
     */
    static private $_instance = null;

    /**
     * Default database type
     * @var string ('MySQL', 'MySQLi', 'PostgreSQL' or 'SQLite')
     */
    static private $_type = null;

    /**
     * Default database name
     * @var string
     */
    static private $_dbname = null;

    /**
     * Default database hostname
     * @var string
     */
    static private $_hostname = null;

    /**
     * Default database username
     * @var string
     */
    static private $_username = null;

    /**
     * Default database password
     * @var string
     */
    static private $_password = null;

    /**
     * Constructor
     *
     * Instantiate the database connection object.
     *
     * @param  string $typ
     * @param  string $dbase
     * @param  string $host
     * @param  string $user
     * @param  string $pass
     * @throws Exception
     * @return void
     */
    private function __construct($typ = null, $dbase = null, $host = null, $user = null, $pass = null)
    {

        // Open connection to the DB server, or throw an exception if there's an error.
        if (is_null($typ) && is_null($dbase) && is_null($host) && is_null($user) && is_null($pass)) {

            // Create connection with pre-defined parameters.
            $typ = self::$_type;
            $dbase = self::$_dbname;
            $host = self::$_hostname;
            $user = self::$_username;
            $pass = self::$_password;

        }

        if (is_null($typ)) {
            $lang = new Moc10_Language();
            throw new Exception($lang->__('Error: The proper database credentials were not passed.'));
        } else {

            switch ($typ) {

                // MySQL.
                case 'MySQL':
                    $this->interface = new Moc10_Db_MySQL($dbase, $host, $user, $pass);

                    break;

                // MySQLi.
                case 'MySQLi':
                    $this->interface = new Moc10_Db_MySQLi($dbase, $host, $user, $pass);

                    break;

                // PostgreSQL.
                case 'PostgreSQL':
                    $this->interface = new Moc10_Db_PostgreSQL($dbase, $host, $user, $pass);

                    break;

                // SQLite.
                case 'SQLite':
                    $this->interface = new Moc10_Db_SQLite($dbase);

                    break;

            }

        }

    }

    /**
     * Determine whether or not an instance of the DB object exists already,
     * and instantiate the object if it doesn't exist.
     *
     * @param  string $typ
     * @param  string $dbase
     * @param  string $host
     * @param  string $user
     * @param  string $pass
     * @return Moc10_Db
     */
    public static function getInstance($typ = null, $dbase = null, $host = null, $user = null, $pass = null)
    {

        if (empty(self::$_instance)) {
            self::$_instance = new Moc10_Db($typ, $dbase, $host, $user, $pass);
        }

        return self::$_instance;

    }

}
Return current item: Moc10 PHP Library