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 045d94b..fffeff1 100644 Binary files a/tests/db_files/FB_TEST_DB.FDB and b/tests/db_files/FB_TEST_DB.FDB differ