From 62f5d058490ca0a8de9fa73bbf5701ff5b9d9f2d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 29 Feb 2012 14:36:42 -0500 Subject: [PATCH] Started query builder class, renamed _manip classes to _sql --- common/db_pdo.php | 4 +- common/query_builder.php | 73 ++++++++++++++++++ common/settings.php | 14 ++++ databases/firebird.php | 8 +- .../{firebird_manip.php => firebird_sql.php} | 8 +- databases/mysql.php | 4 +- databases/{mysql_manip.php => mysql_sql.php} | 10 +-- databases/odbc.php | 4 +- databases/{odbc_manip.php => odbc_sql.php} | 8 +- databases/pgsql.php | 4 +- databases/{pgsql_manip.php => pgsql_sql.php} | 10 +-- databases/sqlite.php | 4 +- .../{sqlite_manip.php => sqlite_sql.php} | 6 +- index.php | 4 +- tests/databases/firebird.php | 4 +- tests/databases/sqlite.php | 4 +- tests/index.php | 4 +- tests/test_dbs/test_sqlite.db | Bin 2048 -> 2048 bytes 18 files changed, 126 insertions(+), 47 deletions(-) create mode 100644 common/query_builder.php rename databases/{firebird_manip.php => firebird_sql.php} (90%) rename databases/{mysql_manip.php => mysql_sql.php} (86%) rename databases/{odbc_manip.php => odbc_sql.php} (83%) rename databases/{pgsql_manip.php => pgsql_sql.php} (78%) rename databases/{sqlite_manip.php => sqlite_sql.php} (95%) diff --git a/common/db_pdo.php b/common/db_pdo.php index 2cfb54e..d295f78 100644 --- a/common/db_pdo.php +++ b/common/db_pdo.php @@ -204,7 +204,7 @@ abstract class DB_PDO extends PDO { /** * Abstract parent for database manipulation subclasses */ -abstract class db_manip { +abstract class DB_SQL { /** * Get database-specific sql to create a new table @@ -226,5 +226,7 @@ abstract class db_manip { * @return string */ abstract public function delete_table($name); + + } // End of db_pdo.php \ No newline at end of file diff --git a/common/query_builder.php b/common/query_builder.php new file mode 100644 index 0000000..9fa642f --- /dev/null +++ b/common/query_builder.php @@ -0,0 +1,73 @@ +settings =& Settings::get_instance(); + + $params = $this->settings->get_db($conn_name); + + $params->type = strtolower($params->type); + $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; + + // Initiate the constructor for the + switch($dbtype) + { + default: + $this->db = new $dbtype("host={$params->host};port={$params->port};", $params->user, $params->pass); + break; + + case "sqlite": + $this->db = new $dbtype($params->file, $params->user, $params->pass); + break; + + case "firebird": + $this->db = new $dbtype("{$params->host}:{$params->file}", $params->user, $params->pass); + break; + } + } + + // -------------------------------------------------------------------------- + + /** + * Shortcut to directly call database methods + * + * @param string $name + * @param array $params + * @return mixed + */ + public function __call($name, $params) + { + if (is_callable($this->$db->$name)) + { + return call_user_func_array(array(&$this->db, $name), $params); + } + else + { + return NULL; + } + } + +} \ No newline at end of file diff --git a/common/settings.php b/common/settings.php index e94a7d3..a577179 100644 --- a/common/settings.php +++ b/common/settings.php @@ -171,5 +171,19 @@ class Settings { { return $this->current->dbs; } + + // -------------------------------------------------------------------------- + + /** + * Retreive a specific database connection + * + * @param string $name + * @return object + */ + public function get_db($name) + { + return (isset($this->current->dbs->{$name})) ? $this->current->dbs->{$name} : FALSE; + } + } // End of settings.php \ No newline at end of file diff --git a/databases/firebird.php b/databases/firebird.php index 0912936..de3c38e 100644 --- a/databases/firebird.php +++ b/databases/firebird.php @@ -28,12 +28,12 @@ class firebird extends DB_PDO { * @param string $user * @param string $pass */ - public function __construct($dbpath, $user="sysdba", $pass="masterkey") + public function __construct($dbpath, $user='sysdba', $pass='masterkey') { - $this->conn =& ibase_connect($dbpath, $user, $pass); + $this->conn =& ibase_connect($dbpath, $user, $pass, 'utf-8'); - $class = __CLASS__."_manip"; - $this->manip = new $class; + $class = __CLASS__."_sql"; + $this->sql = new $class; } // -------------------------------------------------------------------------- diff --git a/databases/firebird_manip.php b/databases/firebird_sql.php similarity index 90% rename from databases/firebird_manip.php rename to databases/firebird_sql.php index 5247305..800a749 100644 --- a/databases/firebird_manip.php +++ b/databases/firebird_sql.php @@ -13,11 +13,9 @@ // -------------------------------------------------------------------------- /** - * Firebird Database Manipulation class - * - * PDO-firebird isn't stable, so this is a wrapper of the ibase_ public functions. + * Firebird Specific SQL */ -class firebird_manip extends db_manip { +class Firebird_SQL extends DB_SQL { /** * Convienience public function to generate sql for creating a db table @@ -88,4 +86,4 @@ class firebird_manip extends db_manip { return 'DROP TABLE "'.$name.'"'; } } -//End of firebird_manip.php \ No newline at end of file +//End of firebird_sql.php \ No newline at end of file diff --git a/databases/mysql.php b/databases/mysql.php index 6ce88e7..30c409b 100644 --- a/databases/mysql.php +++ b/databases/mysql.php @@ -36,8 +36,8 @@ class MySQL extends DB_PDO { parent::__construct("mysql:$dsn", $username, $password, $options); - $class = __CLASS__.'_manip'; - $this->manip = new $class; + $class = __CLASS__.'_sql'; + $this->sql = new $class; } // -------------------------------------------------------------------------- diff --git a/databases/mysql_manip.php b/databases/mysql_sql.php similarity index 86% rename from databases/mysql_manip.php rename to databases/mysql_sql.php index eef6e0c..cc77203 100644 --- a/databases/mysql_manip.php +++ b/databases/mysql_sql.php @@ -13,9 +13,9 @@ // -------------------------------------------------------------------------- /** - * MySQL Database manipulation class + * MySQL specifc SQL */ - class MySQL_manip extends db_manip{ + class MySQL_SQL extends DB_SQL{ /** * Convienience public function for creating a new MySQL table @@ -40,9 +40,7 @@ */ public function delete_table($name) { - return <<manip = new $class; + $class = __CLASS__.'_sql'; + $this->sql = new $class; } // -------------------------------------------------------------------------- diff --git a/databases/odbc_manip.php b/databases/odbc_sql.php similarity index 83% rename from databases/odbc_manip.php rename to databases/odbc_sql.php index e70c6fd..b6dfa47 100644 --- a/databases/odbc_manip.php +++ b/databases/odbc_sql.php @@ -13,11 +13,9 @@ // -------------------------------------------------------------------------- /** - * ODBC Database Manipulation class - * - * @extends ODBC + * ODBC SQL Class */ -class ODBC_manip extends db_manip { +class ODBC_SQL extends DB_SQL { public function create_table($name, $columns, $constraints=array(), $indexes=array()) { @@ -30,4 +28,4 @@ class ODBC_manip extends db_manip { return "DROP TABLE {$name}"; } } -// End of odbc_manip.php \ No newline at end of file +// End of odbc_sql.php \ No newline at end of file diff --git a/databases/pgsql.php b/databases/pgsql.php index ba5eacc..30722b8 100644 --- a/databases/pgsql.php +++ b/databases/pgsql.php @@ -32,8 +32,8 @@ class pgSQL extends DB_PDO { parent::__construct("pgsql:$dsn", $username, $password, $options); //Get db manip class - $class = __CLASS__.'_manip'; - $this->manip = new $class; + $class = __CLASS__.'_sql'; + $this->sql = new $class; } // -------------------------------------------------------------------------- diff --git a/databases/pgsql_manip.php b/databases/pgsql_sql.php similarity index 78% rename from databases/pgsql_manip.php rename to databases/pgsql_sql.php index 8f689b4..c530d54 100644 --- a/databases/pgsql_manip.php +++ b/databases/pgsql_sql.php @@ -13,11 +13,9 @@ // -------------------------------------------------------------------------- /** - * PostgreSQL DB Structure manipulation class - * - * @extends PgSQL + * PostgreSQL specifc SQL */ -class pgSQL_manip extends db_manip { +class pgSQL_SQL extends DB_SQL { public function create_table($name, $columns, $constraints=array(), $indexes=array()) { @@ -26,9 +24,7 @@ class pgSQL_manip extends db_manip { public function delete_table($name) { - return <<manip = new $class; + $class = __CLASS__."_sql"; + $this->sql = new $class; } // -------------------------------------------------------------------------- diff --git a/databases/sqlite_manip.php b/databases/sqlite_sql.php similarity index 95% rename from databases/sqlite_manip.php rename to databases/sqlite_sql.php index d8f622f..e23fd99 100644 --- a/databases/sqlite_manip.php +++ b/databases/sqlite_sql.php @@ -13,9 +13,9 @@ // -------------------------------------------------------------------------- /** - * SQLite Database manipulation class + * SQLite Specific SQL */ -class SQLite_manip extends db_manip { +class SQLite_SQL extends DB_SQL { /** * Convenience public function to create a new table @@ -99,4 +99,4 @@ class SQLite_manip extends db_manip { } } } -//End of sqlite_manip.php \ No newline at end of file +//End of sqlite_sql.php \ No newline at end of file diff --git a/index.php b/index.php index 6f41f3c..050e17f 100644 --- a/index.php +++ b/index.php @@ -94,7 +94,7 @@ foreach(pdo_drivers() as $d) if(is_file($file)) { require_once("{$path}{$d}.php"); - require_once("{$path}{$d}_manip.php"); + require_once("{$path}{$d}_sql.php"); } } @@ -102,7 +102,7 @@ foreach(pdo_drivers() as $d) if(function_exists('ibase_connect')) { require_once("{$path}firebird.php"); - require_once("{$path}firebird_manip.php"); + require_once("{$path}firebird_sql.php"); } // -------------------------------------------------------------------------- diff --git a/tests/databases/firebird.php b/tests/databases/firebird.php index 060571a..507a6eb 100644 --- a/tests/databases/firebird.php +++ b/tests/databases/firebird.php @@ -72,7 +72,7 @@ class FirebirdTest extends UnitTestCase { function TestCreateTable() { //Attempt to create the table - $sql = $this->db->manip->create_table('create_test', array( + $sql = $this->db->sql->create_table('create_test', array( 'id' => 'SMALLINT', 'key' => 'VARCHAR(64)', 'val' => 'BLOB SUB_TYPE TEXT' @@ -123,7 +123,7 @@ SQL; function TestDeleteTable() { //Attempt to delete the table - $sql = $this->db->manip->delete_table('create_test'); + $sql = $this->db->sql->delete_table('create_test'); $this->db->query($sql); //Reset diff --git a/tests/databases/sqlite.php b/tests/databases/sqlite.php index f683e04..811478d 100644 --- a/tests/databases/sqlite.php +++ b/tests/databases/sqlite.php @@ -47,7 +47,7 @@ class SQLiteTest extends UnitTestCase { function TestCreateTable() { //Attempt to create the table - $sql = $this->db->manip->create_table('create_test', + $sql = $this->db->sql->create_table('create_test', array( 'id' => 'INTEGER', 'key' => 'TEXT', @@ -95,7 +95,7 @@ SQL; $this->assertTrue(isset($dbs['create_test'])); //Attempt to delete the table - $sql = $this->db->manip->delete_table('create_test'); + $sql = $this->db->sql->delete_table('create_test'); $this->db->query($sql); //Check diff --git a/tests/index.php b/tests/index.php index fbb70a6..c5bfff0 100644 --- a/tests/index.php +++ b/tests/index.php @@ -45,7 +45,7 @@ foreach(pdo_drivers() as $d) if(is_file($src_file)) { require_once("{$src_path}{$d}.php"); - require_once("{$src_path}{$d}_manip.php"); + require_once("{$src_path}{$d}_sql.php"); require_once("{$test_path}{$d}.php"); } } @@ -54,6 +54,6 @@ foreach(pdo_drivers() as $d) if(function_exists('ibase_connect')) { require_once("{$src_path}firebird.php"); - require_once("{$src_path}firebird_manip.php"); + require_once("{$src_path}firebird_sql.php"); require_once("{$test_path}firebird.php"); } \ No newline at end of file diff --git a/tests/test_dbs/test_sqlite.db b/tests/test_dbs/test_sqlite.db index 95c9a937efc171b2fed9aa865c4db7ffc63c20ee..d0f6a23fd9312d1fe0f53827ebeffa3443b30fb8 100755 GIT binary patch delta 58 zcmZn=Xb_knEx3V!fq@B%8G&@fL>*)P4Gg-Elz4$W1{MYe=1H3cS=5;s^MKS@=7|k+ E0T+Y|7XSbN literal 2048 zcmWFz^vNtqRY=P(%1ta$FlJz3U}R))P*7lCU|0jhOi;`Sq{Dz1M8g0w8zzQMuVK)A zB+m;HWno}oo`fzv${!7Z(Gb80fjnhp#zy&)#H5_m|T^# GTnqql<~CaZ