From 6875de3e7c5c00b954b7d3fb044ce76739b62f97 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 8 Nov 2012 14:28:49 -0500 Subject: [PATCH] Create query() method to handle database connections instead of Query_Builder class --- README.md | 2 +- classes/query_builder.php | 93 ++--------------------- common.php | 85 ++++++++++++++++++++- tests/core/db_qb_test.php | 8 +- tests/databases/firebird/firebird-qb.php | 14 ++-- tests/databases/firebird/firebird.php | 13 +++- tests/databases/mysql/mysql-qb.php | 14 +--- tests/databases/pgsql/pgsql-qb.php | 9 +-- tests/databases/sqlite/sqlite-qb.php | 12 +-- tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 10 files changed, 122 insertions(+), 128 deletions(-) diff --git a/README.md b/README.md index 6201a91..304b6d4 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Create a connection array or object similar to this: 'file' => '/path/to/db/file', ); - $db = new Query_Builder($params); + $db = Query($params); The parameters required depend on the database. diff --git a/classes/query_builder.php b/classes/query_builder.php index 29709ba..0ce808d 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -100,39 +100,12 @@ class Query_Builder { /** * Constructor * - * @param object $params - the connection parametere + * @param DB_PDO $db + * @param object $params - the connection parameters */ - public function __construct($params) + public function __construct(&$db, &$params) { - // Convert array to object - if (is_array($params)) - { - $params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); - } - - $params->type = strtolower($params->type); - $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; - - // Generate dsn - $dsn = $this->_connect($dbtype, $params); - - try - { - // Create the database connection - $this->db = ( ! empty($params->user)) - ? new $dbtype($dsn, $params->user, $params->pass) - : new $dbtype($dsn); - } - catch(Exception $e) - { - throw new BadConnectionException('Connection failed, invalid arguments', 2); - } - - // Set the table prefix, if it exists - if (isset($params->prefix)) - { - $this->db->table_prefix = $params->prefix; - } + $this->db = $db; // Set the connection name property, if applicable if (isset($params->name)) @@ -146,62 +119,6 @@ class Query_Builder { // Make things just slightly shorter $this->sql =& $this->db->sql; } - - /** - * Create the dsn for connection to the database - * - * @param string $dbtype - * @param object $params - * @return string - */ - private function _connect($dbtype, &$params) - { - // Let the connection work with 'conn_db' or 'database' - if (isset($params->database)) - { - $params->conn_db = $params->database; - } - - // Add the driver type to the dsn - $dsn = ($dbtype !== 'firebird' && $dbtype !== 'sqlite') - ? strtolower($dbtype).':' - : ''; - - // Make sure the class exists - if ( ! class_exists($dbtype)) - { - throw new BadDBDriverException('Database driver does not exist, or is not supported'); - } - - // Create the dsn for the database to connect to - switch($dbtype) - { - default: - $dsn .= "dbname={$params->conn_db}"; - - if ( ! empty($params->host)) - { - $dsn .= ";host={$params->host}"; - } - - if ( ! empty($params->port)) - { - $dsn .= ";port={$params->port}"; - } - - break; - - case "sqlite": - $dsn .= $params->file; - break; - - case "firebird": - $dsn = "{$params->host}:{$params->file}"; - break; - } - - return $dsn; - } // -------------------------------------------------------------------------- // ! Select Queries @@ -1376,7 +1293,7 @@ class Query_Builder { $sql .= $h['conjunction'] . $h['string']; } } - + // Set the limit via the class variables if (isset($this->limit) && is_numeric($this->limit)) { diff --git a/common.php b/common.php index f54b3c2..e5d684a 100644 --- a/common.php +++ b/common.php @@ -16,7 +16,7 @@ /** * Global classes/functions that don't really fit anywhere else */ - + /** * Generic exception for bad drivers * @@ -89,4 +89,87 @@ function db_filter($array, $index) return $new_array; } +/** + * Connection function + * + * @param mixed $params + * @return Query_Builder + */ +function Query($params) +{ + // Convert array to object + if (is_array($params)) + { + $params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); + } + + $params->type = strtolower($params->type); + $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; + + // Let the connection work with 'conn_db' or 'database' + if (isset($params->database)) + { + $params->conn_db = $params->database; + } + + // Add the driver type to the dsn + $dsn = ($dbtype !== 'firebird' && $dbtype !== 'sqlite') + ? strtolower($dbtype).':' + : ''; + + // Make sure the class exists + if ( ! class_exists($dbtype)) + { + throw new BadDBDriverException('Database driver does not exist, or is not supported'); + } + + // Create the dsn for the database to connect to + switch($dbtype) + { + default: + $dsn .= "dbname={$params->conn_db}"; + + if ( ! empty($params->host)) + { + $dsn .= ";host={$params->host}"; + } + + if ( ! empty($params->port)) + { + $dsn .= ";port={$params->port}"; + } + + break; + + case "sqlite": + $dsn .= $params->file; + break; + + case "firebird": + $dsn = "{$params->host}:{$params->file}"; + break; + } + + try + { + // Create the database connection + $db = ( ! empty($params->user)) + ? new $dbtype($dsn, $params->user, $params->pass) + : new $dbtype($dsn); + } + catch(Exception $e) + { + throw new BadConnectionException('Connection failed, invalid arguments', 2); + } + + // Set the table prefix, if it exists + if (isset($params->prefix)) + { + $db->table_prefix = $params->prefix; + } + + // Return the Query Builder object + return new Query_Builder($db, $params); +} + // End of common.php \ No newline at end of file diff --git a/tests/core/db_qb_test.php b/tests/core/db_qb_test.php index 015f398..96551a9 100644 --- a/tests/core/db_qb_test.php +++ b/tests/core/db_qb_test.php @@ -32,7 +32,7 @@ abstract class QBTest extends UnitTestCase { } // -------------------------------------------------------------------------- - + public function TestPrefixGet() { if (empty($this->db)) return; @@ -41,7 +41,7 @@ abstract class QBTest extends UnitTestCase { $this->assertIsA($query, 'PDOStatement'); } - + // -------------------------------------------------------------------------- public function TestGetWNumRows() @@ -528,7 +528,7 @@ abstract class QBTest extends UnitTestCase { $this->expectException('BadDBDriverException'); - $this->db = new Query_Builder($params); + $this->db = Query($params); } // -------------------------------------------------------------------------- @@ -546,7 +546,7 @@ abstract class QBTest extends UnitTestCase { $this->expectException('BadConnectionException'); - $this->db = new Query_Builder($params); + $this->db = Query($params); } } diff --git a/tests/databases/firebird/firebird-qb.php b/tests/databases/firebird/firebird-qb.php index 4f5f6ab..cee0905 100644 --- a/tests/databases/firebird/firebird-qb.php +++ b/tests/databases/firebird/firebird-qb.php @@ -21,7 +21,7 @@ class FirebirdQBTest extends QBTest { public function __construct() { parent::__construct(); - + $dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB'; // Test the query builder @@ -32,20 +32,20 @@ class FirebirdQBTest extends QBTest { $params->user = 'sysdba'; $params->pass = 'masterkey'; $params->prefix = 'create_'; - $this->db = new Query_Builder($params); - + $this->db = Query($params); + // echo '
Firebird Queries
'; } - + public function TestTypeList() { $sql = $this->db->sql->type_list(); $query = $this->db->query($sql); - + $this->assertIsA($query, 'PDOStatement'); - + $res = $query->fetchAll(PDO::FETCH_ASSOC); - + $this->assertTrue(is_array($res)); } } \ No newline at end of file diff --git a/tests/databases/firebird/firebird.php b/tests/databases/firebird/firebird.php index 5247389..333d5a1 100644 --- a/tests/databases/firebird/firebird.php +++ b/tests/databases/firebird/firebird.php @@ -37,6 +37,13 @@ class FirebirdTest extends DBTest { unset($this->tables); } + // -------------------------------------------------------------------------- + + public function TestExists() + { + $this->assertTrue(function_exists('ibase_connect')); + } + // -------------------------------------------------------------------------- public function TestConnection() @@ -85,7 +92,7 @@ class FirebirdTest extends DBTest { /*public function TestCreateTable() { //Attempt to create the table - $sql = $this->db->sql->create_table('create_join', array( + $sql = $this->db->util->create_table('create_test', array( 'id' => 'SMALLINT', 'key' => 'VARCHAR(64)', 'val' => 'BLOB SUB_TYPE TEXT' @@ -100,7 +107,7 @@ class FirebirdTest extends DBTest { //Check $table_exists = (bool)in_array('create_test', $this->tables); - echo "create_test exists :".(int)$table_exists.'
'; + //echo "create_test exists :".(int)$table_exists.'
'; $this->assertTrue($table_exists); }*/ @@ -179,7 +186,7 @@ SQL; /*public function TestDeleteTable() { //Attempt to delete the table - $sql = $this->db->sql->delete_table('create_test'); + $sql = $this->db->util->delete_table('create_test'); $this->db->query($sql); //Reset diff --git a/tests/databases/mysql/mysql-qb.php b/tests/databases/mysql/mysql-qb.php index 815fd9d..4437ab7 100644 --- a/tests/databases/mysql/mysql-qb.php +++ b/tests/databases/mysql/mysql-qb.php @@ -25,11 +25,7 @@ class MySQLQBTest extends QBTest { $params = json_decode(file_get_contents(QBASE_DIR . "test_config.json")); $params = $params->mysql; $params->type = "MySQL"; - $params->prefix = "create_"; - - $this->db = new Query_Builder($params); - - // echo '
MySQL Queries
'; + $params->prefix = "create_";; } elseif (($var = getenv('CI'))) { @@ -42,13 +38,9 @@ class MySQLQBTest extends QBTest { 'type' => 'mysql', 'prefix' => 'create_' ); + } - $this->db = new Query_Builder($params); - } - else - { - die("Error with mysql credentials"); - } + $this->db = Query($params); } // -------------------------------------------------------------------------- diff --git a/tests/databases/pgsql/pgsql-qb.php b/tests/databases/pgsql/pgsql-qb.php index 8a73d7b..830ffa7 100644 --- a/tests/databases/pgsql/pgsql-qb.php +++ b/tests/databases/pgsql/pgsql-qb.php @@ -25,11 +25,6 @@ class PgSQLQBTest extends QBTest { $params = $params->pgsql; $params->type = "pgsql"; $params->prefix = 'create_'; - - $this->db = new Query_Builder($params); - - // echo '
Postgres Queries
'; - } elseif (($var = getenv('CI'))) { @@ -42,9 +37,9 @@ class PgSQLQBTest extends QBTest { 'type' => 'pgsql', 'prefix' => 'create_' ); - - $this->db = new Query_Builder($params); } + + $this->db = Query($params); } // -------------------------------------------------------------------------- diff --git a/tests/databases/sqlite/sqlite-qb.php b/tests/databases/sqlite/sqlite-qb.php index 0dac9b7..8c1dde1 100644 --- a/tests/databases/sqlite/sqlite-qb.php +++ b/tests/databases/sqlite/sqlite-qb.php @@ -7,28 +7,28 @@ * @author Timothy J. Warren * @copyright Copyright (c) 2012 * @link https://github.com/aviat4ion/OpenSQLManager - * @license http://philsturgeon.co.uk/code/dbad-license + * @license http://philsturgeon.co.uk/code/dbad-license */ // -------------------------------------------------------------------------- /** - * Class for testing Query Builder with SQLite + * Class for testing Query Builder with SQLite */ class SQLiteQBTest extends QBTest { - + public function __construct() { parent::__construct(); - + $path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'; $params = new Stdclass(); $params->type = 'sqlite'; $params->file = $path; $params->host = 'localhost'; $params->prefix = 'create_'; - $this->db = new Query_Builder($params); - + $this->db = Query($params); + // echo '
SQLite Queries
'; } } \ No newline at end of file diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index 045d94bdccecc653d102a0a77d69237d82f39136..fffeff1aa6b6f88f82d66fb896231f5e8112c7c0 100644 GIT binary patch delta 5119 zcmb_gdvH|M8UN1Rd(ZCPv#-3ffh7xhL3t%15Wx=}$$vII3OViIh|N2vsjGyYM_ zc630Lj-gRc8LeGMicCeD7|a!QFiy2~tco3((dk$qN;`})lK3j4M*E$6@7b5&^pBq1 zv*&*2`_6ZMzwbQn#^W{dcugCDrt0A;NTmRaJ8VgmeU$(=Vg^vi!~X!-*l@h6t;8}2 zfP$YMQy}=;S2;Sn=NbQR0;+e!_VEfS!)sbI z3~Kezfn@-*NWWF;p-X9oV$A93!56i1rM{B?SkXd!v#NRgO|@skad-)cvIhV)i!^PX zzp2KqBzvF{E1y=hnGc@SJgli&DB|!}*v}^vZ5H3GXodVJ(Ylp(^r~v}ikcdw!_UA$ zK-mws_VuFjm!`5oIyRMLKiqW7IaD4~wYh<&dZ8QvLO7fY3x6q;NvC@tf*K2D(%Bx^ zhLsX!h2=<~yb@NaFtd{cD5%YAQWFrfwGxQfZEpH*7~n7%JoUT0-#Qk z*vpoiRK08$%V@X1z_P=2XSUm9w@cNV?F?hJwtf+Li{~U*Udg9eN8u4{Q41Xk0q1?n z^*CDw`nrv(e(f#)SX@&BwlOar)Q$#bI*D?7|1qBuf8MDi!U=jnMkVdOPGeI-yUSI6Fh;ANh|!uu zF|1{o5KHk^7r=V#%>ll@ zn7v)LrgdGUyKVLQR+W@!9V)xQ*}186W9$0v%hs*F+z|n%z_hfn?m&bt{X>M-AB@n3 zCnB`*P=q$=5lv~Te-djBV~ugguy(=8hWvrBodw&wEEHcOW21Onn@5ZEh*KTtyKUgU z2-_QB;kEWx7$0(vy{)NX@=vrbdDIW!h7Kpd3Ph}c`i{~WU01HU%Z%DdZIj}9v6t3A zwiP?R?^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