From a3e4fa3c6810cadadf785fc204d880b7bb0bf31c Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 30 Jan 2012 07:57:17 -0500 Subject: [PATCH] Updated Database classes --- src/databases/db_pdo.php | 84 ++++++++++++++++++++++++++++++++++++++++ src/databases/mysql.php | 14 ++++++- src/databases/odbc.php | 7 ++++ src/databases/pgsql.php | 15 ++++++- src/databases/sqlite.php | 14 ++++++- src/windows/add_db.php | 26 +++++++++++++ 6 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 src/windows/add_db.php diff --git a/src/databases/db_pdo.php b/src/databases/db_pdo.php index 3b57aa8..0b72e97 100644 --- a/src/databases/db_pdo.php +++ b/src/databases/db_pdo.php @@ -19,11 +19,95 @@ */ class DB_PDO extends PDO { + protected $statement; + function __construct($dsn, $username=NULL, $password=NULL, $driver_options=array()) { parent::__construct($dsn, $username, $password, $driver_options); } + // -------------------------------------------------------------------------- + + /** + * PHP magic method to facilitate dynamic methods + * + * @param string $name + * @param array $args + */ + function __call($name, $args) + { + if(is_callable($this->$name)) + { + //Add $this to the beginning of the args array + array_unshift($args, $this); + + //Call the dynamic function + return call_user_func_array($this->$name, $args); + } + } + + // -------------------------------------------------------------------------- + + /** + * PHP magic methods to call non-static methods statically + * + * @param string $name + * @param array $args + */ + public static function __callStatic($name, $args) + { + if(is_callable(parent::$name)) + { + return call_user_func_array(parent::$name, $args); + } + } + + // -------------------------------------------------------------------------- + + /** + * Simplifies prepared statements for database queries + * + * @param string $sql + * @param array $data + * @return mixed PDOStatement / FALSE + */ + function prepare_query($sql, $data) + { + // Prepare the sql + $query = $this->prepare($sql); + + if( ! is_like_array($query)) + { + $this->get_last_error(); + return FALSE; + } + + // Set the statement in the class variable for easy later access + $this->statement =& $query; + + + if( ! is_like_array($data)) + { + trigger_error("Invalid data argument"); + return FALSE; + } + + // Bind the parameters + foreach($data as $k => $value) + { + $res = $query->bindValue($k, $value); + + if( ! $res) + { + trigger_error("Parameter not successfully bound"); + return FALSE; + } + } + + return $query; + + } + } // End of db_pdo.php \ No newline at end of file diff --git a/src/databases/mysql.php b/src/databases/mysql.php index 703997f..397e62c 100644 --- a/src/databases/mysql.php +++ b/src/databases/mysql.php @@ -9,7 +9,19 @@ * @link https://github.com/aviat4ion/OpenSQLManager * @license http://philsturgeon.co.uk/code/dbad-license */ -class MySQL { + // -------------------------------------------------------------------------- + + /** + * MySQL specific class + * + * @extends DB_PDO + */ +class MySQL extends DB_PDO { + + function __construct($dsn, $username=null, $password=null, $options=array()) + { + parent::__construct($dsn, $username, $password, $options); + } } \ No newline at end of file diff --git a/src/databases/odbc.php b/src/databases/odbc.php index 9e2975f..8236ef5 100644 --- a/src/databases/odbc.php +++ b/src/databases/odbc.php @@ -16,9 +16,16 @@ * ODBC Database Driver * * For general database access for databases not specified by the main drivers + * + * @extends DB_PDO */ class ODBC extends DB_PDO { + function __construct($dsn, $username=null, $password=null, $options=array()) + { + parent::__construct($dsn, $username, $password, $options); + } + } // End of odbc.php \ No newline at end of file diff --git a/src/databases/pgsql.php b/src/databases/pgsql.php index 20569a5..7e34d0c 100644 --- a/src/databases/pgsql.php +++ b/src/databases/pgsql.php @@ -9,6 +9,19 @@ * @link https://github.com/aviat4ion/OpenSQLManager * @license http://philsturgeon.co.uk/code/dbad-license */ -class pgSQL { + +// -------------------------------------------------------------------------- + +/** + * PostgreSQL specifc class + * + * @extends DB_PDO + */ +class pgSQL extends DB_PDO { + + function __construct($dsn, $username=null, $password=null, $options=array()) + { + parent::__construct($dsn, $username, $password, $options); + } } \ No newline at end of file diff --git a/src/databases/sqlite.php b/src/databases/sqlite.php index 5bc493e..845de28 100644 --- a/src/databases/sqlite.php +++ b/src/databases/sqlite.php @@ -9,7 +9,19 @@ * @link https://github.com/aviat4ion/OpenSQLManager * @license http://philsturgeon.co.uk/code/dbad-license */ -class SQLite { +// -------------------------------------------------------------------------- + +/** + * SQLite specific class + * + * @extends DB_PDO + */ +class SQLite extends DB_PDO { + + function __construct($dsn) + { + parent::__construct($dsn); + } } \ No newline at end of file diff --git a/src/windows/add_db.php b/src/windows/add_db.php new file mode 100644 index 0000000..761e545 --- /dev/null +++ b/src/windows/add_db.php @@ -0,0 +1,26 @@ +