Create query() method to handle database connections instead of Query_Builder class
This commit is contained in:
parent
47d493537e
commit
6875de3e7c
@ -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.
|
||||
|
||||
|
@ -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))
|
||||
@ -147,62 +120,6 @@ class Query_Builder {
|
||||
$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
|
||||
// --------------------------------------------------------------------------
|
||||
|
83
common.php
83
common.php
@ -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
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ 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 />';
|
||||
}
|
||||
|
@ -39,6 +39,13 @@ class FirebirdTest extends DBTest {
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
public function TestExists()
|
||||
{
|
||||
$this->assertTrue(function_exists('ibase_connect'));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
public function TestConnection()
|
||||
{
|
||||
$this->assertIsA($this->db, 'Firebird');
|
||||
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
@ -27,7 +27,7 @@
|
||||
$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.
Loading…
Reference in New Issue
Block a user