Create query() method to handle database connections instead of Query_Builder class

This commit is contained in:
Timothy Warren 2012-11-08 14:28:49 -05:00
parent 47d493537e
commit 6875de3e7c
10 changed files with 122 additions and 128 deletions

View File

@ -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.

View File

@ -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))
{

View File

@ -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

View File

@ -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);
}
}

View File

@ -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 '<hr /> Firebird Queries <hr />';
}
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));
}
}

View File

@ -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.'<br />';
//echo "create_test exists :".(int)$table_exists.'<br />';
$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

View File

@ -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 '<hr /> MySQL Queries <hr />';
$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);
}
// --------------------------------------------------------------------------

View File

@ -25,11 +25,6 @@ class PgSQLQBTest extends QBTest {
$params = $params->pgsql;
$params->type = "pgsql";
$params->prefix = 'create_';
$this->db = new Query_Builder($params);
// echo '<hr /> Postgres Queries <hr />';
}
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);
}
// --------------------------------------------------------------------------

View File

@ -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 '<hr /> SQLite Queries <hr />';
}
}

Binary file not shown.