Orion PHP  0.11.12
The PHP5.3 framework
sql.php
Go to the documentation of this file.
00001 <?php
00002 
00003 namespace Orion\Core\DB;
00004 
00005 
00006 /**
00007  * \Orion\Core\DB\Sql
00008  * 
00009  * Orion sql connector class.
00010  *
00011  * Generates a singelton PDO instance.
00012  *
00013  * @author Thibaut Despoulain
00014  * @license BSD 4-clauses
00015  * @version 0.11.12
00016  *
00017  * @static
00018  */
00019 class Sql implements Base
00020 {
00021     /**
00022      * PDO instance
00023      * @var Object (PDO)
00024      */
00025     private static $instance = null;
00026 
00027     /**
00028      * Default SQL driver for PDO connector
00029      * @var string Must be a valid SQL driver
00030      * @see OrionSql::$SQL_DRIVERS
00031      */
00032     private static $DEFAULT_DRIVER = 'mysql';
00033 
00034     /**
00035      * List of valid SQL drivers
00036      * @var array<string> PDO drivers list
00037      * @see PDO
00038      */
00039     private static $SQL_DRIVERS = array( 'mysql', 'pgsql' );
00040 
00041     /**
00042      * the constructor is set to private
00043      * so nobody can create a new instance using new
00044      */
00045     private function __construct()
00046     {
00047         
00048     }
00049 
00050     /**
00051      * Return PDO instance or create intitial connection
00052      * @return PDO
00053      * @access public
00054      */
00055     public static function getConnection()
00056     {
00057         if ( !self::$instance )
00058         {
00059             try
00060             {
00061                 $config = \Orion::config();
00062 
00063                 if ( in_array( $config->get( 'SQL_DRIVER' ), self::$SQL_DRIVERS ) )
00064                     $driver = $config->get( 'SQL_DRIVER' );
00065                 else
00066                     $driver = self::$DEFAULT_DRIVER;
00067 
00068                 if ( $config->defined( 'SQL_PORT' ) )
00069                     $port = ';port=' . $config->get( 'SQL_PORT' );
00070                 else
00071                     $port = '';
00072 
00073                 self::$instance = new \PDO( $driver . ":host=" . $config->get( 'SQL_HOST' ) . $port . ";dbname=" . $config->get( 'SQL_DBNAME' ), $config->get( 'SQL_USER' ), $config->get( 'SQL_PASSWORD' ) );
00074                 self::$instance->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
00075             }
00076             catch ( \PDOException $e )
00077             {
00078                 throw new Exception( $e->getMessage(), $e->getCode(), get_class() );
00079             }
00080         }
00081         return self::$instance;
00082     }
00083 
00084     /**
00085      * Manually close the PDO connection to database
00086      * @return boolean success
00087      */
00088     public static function disconnect()
00089     {
00090         if ( self::$instance != null )
00091             self::$instance = null;
00092 
00093         return (self::$instance == null);
00094     }
00095 
00096     /**
00097      * __clone is set to private, so nobody can clone the instance
00098      */
00099     private function __clone()
00100     {
00101         
00102     }
00103 
00104 }
00105 
00106 ?>