22 public static function add( $options , $name =
'default' )
24 if( array_key_exists( $name , static::$_connections ) )
26 if( array_key_exists( $name , static::$_connections ) )
28 trigger_error(
'Connection name "' . $name .
29 '" already exists, use some other name!' , E_USER_ERROR );
33 foreach( $options as $k => $v )
35 if( !array_key_exists( $k , static::$_connectionOptions ) )
37 trigger_error(
'Unknown option "' . $k .
'" passed as argument to PtcDb!',
41 $options[
'name' ] = $name;
42 if( array_key_exists(
'pdo_attributes' , $options ) )
44 $options[
'pdo_attributes' ] = $options[
'pdo_attributes' ] +
45 static::$_connectionOptions[
'pdo_attributes' ];
47 $options = array_merge( static::$_connectionOptions , $options );
48 static::$_connectionsDetails[ $name ] = $options;
49 static::_debug( static::$_connectionsDetails[ $name ] ,
50 'added connection <b>"' . $name .
'"</b>' ,
'Connection Manager' );
51 return static::$_connectionsDetails[ $name ] = $options;
60 if( !$name ){
return static::$_connectionsDetails; }
61 if( !static::_checkConnectionName( $name ) ){
return false; }
62 return static::$_connectionsDetails[ $name ];
69 public static function getPdo( $name )
71 if ( !static::_checkConnectionName( $name ) ){
return false; }
72 static::_initializeConnection( $name );
73 return static::$_connections[ $name ][
'pdo_object' ];
80 public static function getQB( $name )
82 if( !static::_checkConnectionName( $name ) ){
return false; }
83 static::_initializeConnection( $name );
84 if( !static::$_connectionsDetails[ $name ][
'query_builder' ] )
86 trigger_error(
'QueryBuilder was not set for connection "' . $name .
90 return $connection = static::$_connections[ $name ][
'query_builder' ];
102 if ( !static::_initializeConnection( $name ) ){
return false; }
103 if ( $qb =@static::$_connections[ $name ][
'query_builder' ] )
105 if( in_array( $method , get_class_methods( $qb ) ) )
107 return call_user_func_array( array( $qb , $method ) , $args );
112 $pdo =static::$_connections[ $name ][
'pdo_object' ];
113 return call_user_func_array( array( $pdo , $method ) , $args );
115 trigger_error(
'Call to undefined method "' .$method .
'"!' , E_USER_ERROR );
123 'name' =>
'default' ,
124 'driver' =>
'mysql' ,
127 'host' =>
'localhost' ,
129 'charset' =>
'utf8' ,
130 'query_builder' =>
false ,
131 'query_builder_class' =>
'PtcQueryBuilder' ,
132 'pdo_attributes' => array
134 PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING ,
135 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
152 if ( !array_key_exists( $name , static::$_connections ) )
154 $options = static::$_connectionsDetails[ $name ];
155 static::$_connections[ $name ][
'pdo_object' ] = new \PDO(
156 static::_pdoDriver( $options[
'driver' ] , $options[
'host' ] ) .
157 ';dbname=' . $options[
'db' ] .
';charset:' . $options[
'charset' ] .
';' ,
158 $options[
'user' ] , $options [
'pass' ] );
159 if ( !static::$_connections[ $name ][
'pdo_object' ]){
return false; }
160 foreach ( $options[
'pdo_attributes' ] as $k => $v )
162 static::$_connections[ $name ][
'pdo_object' ]->setAttribute( $k , $v );
164 if ( $options[
'query_builder' ] )
167 if ( class_exists( $options[
'query_builder_class' ] ) ||
168 file_exists( dirname(__FILE__) . $options[
'query_builder_class' ] .
'.php' ) ){ $qb =
true; }
171 trigger_error(
'Class "' . $options[
'query_builder_class' ] .
172 '" not found , please include the class file manually!' , E_USER_ERROR );
175 static::$_connections[ $name ][
'query_builder' ] =
176 new $options[
'query_builder_class' ]( static::$_connections[ $name ][
'pdo_object' ] );
178 static::_debug( array(
'details' => static::$_connectionsDetails[ $name ] ,
179 'connection' => static::$_connections[ $name ] ) ,
'connection <b>"' .
180 $name .
'"</b> initialized' ,
'Connection Manager' );
191 if ( !array_key_exists( $name , static::$_connectionsDetails ) )
193 trigger_error(
'Could not find connection details with name "' .
194 $name .
'"!' , E_USER_ERROR );
210 default :
return 'mysql:host=' . $host;
219 protected static function _debug( $string , $statement = null , $category = null )
221 if ( !defined(
'_PTCDEBUG_NAMESPACE_' ) ) {
return false; }
222 return @call_user_func_array( array(
'\\' . _PTCDEBUG_NAMESPACE_ ,
'bufferSql' ) ,
223 array( $string , $statement , $category ) );
static __callStatic($method, $args=null)
Calls Pdo or query builder methods from the default connection directly.
static getPdo($name)
Retrieves the Pdo object.
static getQB($name)
Retreives the query builder object if present.
static add($options, $name= 'default')
Adds a connection to the manager, will trigger an error if connection name is already present...
static $_connectionOptions
Default connection options property, see Connection Options.
static _debug($string, $statement=null, $category=null)
Sends messsages to the PtcDebug class if present.
static $_connections
Pdo and query builder objects property.
static _initializeConnection($name)
Initializes the pdo and query builder obejcts.
static _pdoDriver($driver, $host)
Builds the pdo driver.
static $_connectionsDetails
Connection details property.
static _checkConnectionName($name)
Checks if a given connection exists.
static getConnection($name=null)
Retrieves connection details previously configured.