Created
December 9, 2010 17:12
-
-
Save markjames/734991 to your computer and use it in GitHub Desktop.
Database class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Database class | |
* | |
* @author Mark James | |
* @package | |
*/ | |
/** | |
To setup: | |
$config = array(); | |
$config['database_host'] = 'localhost'; | |
$config['database_name'] = 'mydatabase'; | |
$config['database_user'] = 'local'; | |
$config['database_password'] = ''; | |
Database::GetInstance( $config ); | |
To use in your controller: | |
$db = Database::GetInstance(); | |
$db->run('SELECT * FROM `table` WHERE fieldname=:fieldname', array('fieldname'=>'Hello World')); | |
*/ | |
class Database extends PDO { | |
/** | |
* Number of queries executed | |
* | |
* @var int | |
*/ | |
public $queryCount; | |
/** | |
* Statements | |
* | |
* @var array | |
*/ | |
protected $_statements = array(); | |
/** | |
* Fetch mode | |
* | |
* @var mixed | |
*/ | |
protected $_fetchMode; | |
/** | |
* Instances | |
* | |
* @var array | |
*/ | |
public static $Instances = array(); | |
/** | |
* Get database instance | |
* | |
* @param Config $config Config | |
* @return Database | |
*/ | |
public static function GetInstance( $config=null ) { | |
if( $config==null ) { | |
return current(self::$Instances); | |
} else { | |
if( isset($config['database_socket']) && $config['database_socket'] ) { | |
$dsn = 'mysql:unix_socket='.$config['database_socket'].';dbname='.$config['database_name']; | |
} else { | |
$dsn = 'mysql:host='.$config['database_host'].';dbname='.$config['database_name']; | |
} | |
$username = $config['database_user']; | |
$password = $config['database_password']; | |
$id = "$dsn;$username;$password;"; | |
if( !array_key_exists( $id, self::$Instances ) ) { | |
self::$Instances[$id] = new Database( $config ); | |
} | |
return self::$Instances[$id]; | |
} | |
} | |
/** | |
* Constructor for Database | |
* | |
*/ | |
public function __construct( $config, $configOverrides=null ) { | |
// Get DB info from config | |
$this->config = $config; | |
$this->_fetchMode = PDO::FETCH_ASSOC; | |
$this->queryCount = 0; | |
if( $configOverrides === null ) { | |
if( isset($config['database_socket']) && $config['database_socket'] ) { | |
$dsn = 'mysql:unix_socket='.$config['database_socket'].';dbname='.$config['database_name']; | |
} else { | |
$dsn = 'mysql:host='.$config['database_host'].';dbname='.$config['database_name']; | |
} | |
$username = $config['database_user']; | |
$password = $config['database_password']; | |
} else { | |
if( isset($config['database_socket']) && $config['database_socket'] ) { | |
$dsn = 'mysql:unix_socket='.array_get('database_socket',$configOverrides,$config['database_socket']).';dbname='.array_get('database_name',$configOverrides,$config['database_name']); | |
} else { | |
$dsn = 'mysql:host='.array_get('database_host',$configOverrides,$config['database_host']).';dbname='.array_get('database_name',$configOverrides,$config['database_name']); | |
} | |
$username = array_get('database_user',$configOverrides,$config['database_user']); | |
$password = array_get('database_password',$configOverrides,$config['database_password']); | |
} | |
// Create PDO instance | |
PDO::__construct( $dsn, $username, $password ); | |
$this->setAttribute( PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true ); | |
$this->setAttribute( PDO::ATTR_STATEMENT_CLASS, array(array_get('database_statement_class',$config,'Database_Statement'), array(null,null))); | |
$this->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); | |
$this->setAttribute( PDO::ATTR_EMULATE_PREPARES, true ); | |
} | |
/** | |
* Run | |
* | |
* @param String $query Query | |
* @param String $params Params | |
* @return Database_Statement | |
*/ | |
public function run( $query, $params=null ) { | |
// If an array has been passed in, then perform them all in | |
// a transaction with rollback | |
if( is_array( $query ) ) { | |
$statements = array(); | |
foreach( $query as $q ) { | |
// We already have this statement prepared | |
if( isset( $this->_statements[$q] ) ) { | |
$statements[] = $this->_statements[$q]; | |
continue; | |
} | |
// Prepare statement | |
$statement = $this->prepare( $q ); | |
$statement->setFetchMode( $this->_fetch_mode ); | |
$statements[] = $statement; | |
$this->_statements[$q] = $statement; | |
} | |
// Statements are prepared, try running them with the params | |
$stmt = null; | |
try { | |
$this->beginTransaction(); | |
foreach( $this->_statements[$query_key] as $stmt ) { | |
$stmt->execute( $params ); | |
$this->queryCount++; | |
} | |
$this->commit(); | |
return $this->_statements[$query_key]; | |
} catch( PDOException $e ) { | |
$this->rollBack(); | |
if( null !== $stmt ) { | |
$error = $stmt->errorInfo(); | |
throw new Exception( implode(', ', $error) ); | |
} | |
} | |
// Query is just a single query | |
} else { | |
// Prepare query | |
if( !isset($this->_statements[$query]) ) { | |
$this->_statements[$query] = $this->prepare( $query ); | |
} | |
// Run query. Return statement if successful, FALSE otherwise. | |
if( $this->_statements[$query]->execute( $params ) || '00000' === $this->_statements[$query]->errorCode() ) { | |
$this->queryCount++; | |
return $this->_statements[$query]; | |
} else { | |
$error = $this->_statements[$query]->errorInfo(); | |
throw new Exception( implode(', ', $error) ); | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment