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', 'file' => '/path/to/db/file',
); );
$db = new Query_Builder($params); $db = Query($params);
The parameters required depend on the database. The parameters required depend on the database.

View File

@ -100,39 +100,12 @@ class Query_Builder {
/** /**
* Constructor * 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 $this->db = $db;
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;
}
// Set the connection name property, if applicable // Set the connection name property, if applicable
if (isset($params->name)) if (isset($params->name))
@ -146,62 +119,6 @@ class Query_Builder {
// Make things just slightly shorter // Make things just slightly shorter
$this->sql =& $this->db->sql; $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 // ! Select Queries
@ -1376,7 +1293,7 @@ class Query_Builder {
$sql .= $h['conjunction'] . $h['string']; $sql .= $h['conjunction'] . $h['string'];
} }
} }
// Set the limit via the class variables // Set the limit via the class variables
if (isset($this->limit) && is_numeric($this->limit)) if (isset($this->limit) && is_numeric($this->limit))
{ {

View File

@ -16,7 +16,7 @@
/** /**
* Global classes/functions that don't really fit anywhere else * Global classes/functions that don't really fit anywhere else
*/ */
/** /**
* Generic exception for bad drivers * Generic exception for bad drivers
* *
@ -89,4 +89,87 @@ function db_filter($array, $index)
return $new_array; 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 // End of common.php

View File

@ -32,7 +32,7 @@ abstract class QBTest extends UnitTestCase {
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestPrefixGet() public function TestPrefixGet()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;
@ -41,7 +41,7 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestGetWNumRows() public function TestGetWNumRows()
@ -528,7 +528,7 @@ abstract class QBTest extends UnitTestCase {
$this->expectException('BadDBDriverException'); $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->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() public function __construct()
{ {
parent::__construct(); parent::__construct();
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB'; $dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
// Test the query builder // Test the query builder
@ -32,20 +32,20 @@ class FirebirdQBTest extends QBTest {
$params->user = 'sysdba'; $params->user = 'sysdba';
$params->pass = 'masterkey'; $params->pass = 'masterkey';
$params->prefix = 'create_'; $params->prefix = 'create_';
$this->db = new Query_Builder($params); $this->db = Query($params);
// echo '<hr /> Firebird Queries <hr />'; // echo '<hr /> Firebird Queries <hr />';
} }
public function TestTypeList() public function TestTypeList()
{ {
$sql = $this->db->sql->type_list(); $sql = $this->db->sql->type_list();
$query = $this->db->query($sql); $query = $this->db->query($sql);
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
$res = $query->fetchAll(PDO::FETCH_ASSOC); $res = $query->fetchAll(PDO::FETCH_ASSOC);
$this->assertTrue(is_array($res)); $this->assertTrue(is_array($res));
} }
} }

View File

@ -37,6 +37,13 @@ class FirebirdTest extends DBTest {
unset($this->tables); unset($this->tables);
} }
// --------------------------------------------------------------------------
public function TestExists()
{
$this->assertTrue(function_exists('ibase_connect'));
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function TestConnection() public function TestConnection()
@ -85,7 +92,7 @@ class FirebirdTest extends DBTest {
/*public function TestCreateTable() /*public function TestCreateTable()
{ {
//Attempt to create the table //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', 'id' => 'SMALLINT',
'key' => 'VARCHAR(64)', 'key' => 'VARCHAR(64)',
'val' => 'BLOB SUB_TYPE TEXT' 'val' => 'BLOB SUB_TYPE TEXT'
@ -100,7 +107,7 @@ class FirebirdTest extends DBTest {
//Check //Check
$table_exists = (bool)in_array('create_test', $this->tables); $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); $this->assertTrue($table_exists);
}*/ }*/
@ -179,7 +186,7 @@ SQL;
/*public function TestDeleteTable() /*public function TestDeleteTable()
{ {
//Attempt to delete the table //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); $this->db->query($sql);
//Reset //Reset

View File

@ -25,11 +25,7 @@ class MySQLQBTest extends QBTest {
$params = json_decode(file_get_contents(QBASE_DIR . "test_config.json")); $params = json_decode(file_get_contents(QBASE_DIR . "test_config.json"));
$params = $params->mysql; $params = $params->mysql;
$params->type = "MySQL"; $params->type = "MySQL";
$params->prefix = "create_"; $params->prefix = "create_";;
$this->db = new Query_Builder($params);
// echo '<hr /> MySQL Queries <hr />';
} }
elseif (($var = getenv('CI'))) elseif (($var = getenv('CI')))
{ {
@ -42,13 +38,9 @@ class MySQLQBTest extends QBTest {
'type' => 'mysql', 'type' => 'mysql',
'prefix' => 'create_' 'prefix' => 'create_'
); );
}
$this->db = new Query_Builder($params); $this->db = Query($params);
}
else
{
die("Error with mysql credentials");
}
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -25,11 +25,6 @@ class PgSQLQBTest extends QBTest {
$params = $params->pgsql; $params = $params->pgsql;
$params->type = "pgsql"; $params->type = "pgsql";
$params->prefix = 'create_'; $params->prefix = 'create_';
$this->db = new Query_Builder($params);
// echo '<hr /> Postgres Queries <hr />';
} }
elseif (($var = getenv('CI'))) elseif (($var = getenv('CI')))
{ {
@ -42,9 +37,9 @@ class PgSQLQBTest extends QBTest {
'type' => 'pgsql', 'type' => 'pgsql',
'prefix' => 'create_' 'prefix' => 'create_'
); );
$this->db = new Query_Builder($params);
} }
$this->db = Query($params);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -7,28 +7,28 @@
* @author Timothy J. Warren * @author Timothy J. Warren
* @copyright Copyright (c) 2012 * @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager * @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 { class SQLiteQBTest extends QBTest {
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'; $path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db';
$params = new Stdclass(); $params = new Stdclass();
$params->type = 'sqlite'; $params->type = 'sqlite';
$params->file = $path; $params->file = $path;
$params->host = 'localhost'; $params->host = 'localhost';
$params->prefix = 'create_'; $params->prefix = 'create_';
$this->db = new Query_Builder($params); $this->db = Query($params);
// echo '<hr /> SQLite Queries <hr />'; // echo '<hr /> SQLite Queries <hr />';
} }
} }

Binary file not shown.