improve query builder and connection manager, skip coverage of some soon-to-be-removed methods

This commit is contained in:
Timothy Warren 2014-04-02 10:31:59 -04:00
parent 482f36ea24
commit 02d4e0e52e
8 changed files with 60 additions and 55 deletions

View File

@ -178,11 +178,7 @@ final class Connection_Manager {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// Convert array to object // Convert array to object
if (is_array($params))
{
$params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); $params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS);
}
$params->type = strtolower($params->type); $params->type = strtolower($params->type);
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';

View File

@ -1306,8 +1306,6 @@ class Query_Builder implements Query_Builder_Interface {
$vals = array_merge($this->values, (array) $this->where_values); $vals = array_merge($this->values, (array) $this->where_values);
} }
$evals = (is_array($vals)) ? $vals : array();
$start_time = microtime(TRUE); $start_time = microtime(TRUE);
if (empty($vals)) if (empty($vals))
@ -1320,29 +1318,10 @@ class Query_Builder implements Query_Builder_Interface {
} }
$end_time = microtime(TRUE); $end_time = microtime(TRUE);
$total_time = number_format($end_time - $start_time, 5); $total_time = number_format($end_time - $start_time, 5);
// Add the interpreted query to the list of executed queries
foreach($evals as &$v)
{
$v = ( ! is_numeric($v)) ? htmlentities($this->db->quote($v), ENT_NOQUOTES, 'utf-8', FALSE) : $v;
}
$esql = str_replace('?', "%s", $sql);
array_unshift($vals, $esql);
array_unshift($evals, $esql);
$this->_append_query($vals, $sql, $total_time);
$this->queries[] = array(
'time' => $total_time,
'sql' => call_user_func_array('sprintf', $evals),
);
$this->queries['total_time'] += $total_time;
array_shift($vals);
// Set the last query to get rowcounts properly
$this->db->last_query = $sql;
// Reset class state for next query // Reset class state for next query
$this->reset_query(); $this->reset_query();
@ -1370,6 +1349,41 @@ class Query_Builder implements Query_Builder_Interface {
throw new BadMethodCallException("Method does not exist"); throw new BadMethodCallException("Method does not exist");
} }
/**
* Convert the prepared statement into readable sql
*
* @param array $vals
* @param string $sql
* @param string $total_time
* @return void
*/
protected function _append_query($vals, $sql, $total_time)
{
$evals = (is_array($vals)) ? $vals : array();
// Quote string values
foreach($evals as &$v)
{
$v = ( ! is_numeric($v)) ? htmlentities($this->db->quote($v), ENT_NOQUOTES, 'utf-8', FALSE) : $v;
}
$esql = str_replace('?', "%s", $sql);
// Add the query onto the array of values to pass
// as arguments to sprintf
array_unshift($evals, $esql);
// Add the interpreted query to the list of executed queries
$this->queries[] = array(
'time' => $total_time,
'sql' => call_user_func_array('sprintf', $evals),
);
$this->queries['total_time'] += $total_time;
// Set the last query to get rowcounts properly
$this->db->last_query = $sql;
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**

View File

@ -62,14 +62,12 @@ class Firebird extends Abstract_Driver {
*/ */
public function __construct($dbpath, $user='SYSDBA', $pass='masterkey', array $options = array()) public function __construct($dbpath, $user='SYSDBA', $pass='masterkey', array $options = array())
{ {
if (isset($options[PDO::ATTR_PERSISTENT]) && $options[PDO::ATTR_PERSISTENT] == TRUE)
{ $connect_function = (isset($options[PDO::ATTR_PERSISTENT]) && $options[PDO::ATTR_PERSISTENT] == TRUE)
$this->conn = fbird_pconnect($dbpath, $user, $pass, 'utf-8', 0); ? 'fbird_pconnect'
} : 'fbird_connect';
else
{ $this->conn = $connect_function($dbpath, $user, $pass, 'utf-8', 0);
$this->conn = fbird_connect($dbpath, $user, $pass, 'utf-8', 0);
}
// Throw an exception to make this match other pdo classes // Throw an exception to make this match other pdo classes
if ( ! is_resource($this->conn)) throw new PDOException(fbird_errmsg(), fbird_errcode(), NULL); if ( ! is_resource($this->conn)) throw new PDOException(fbird_errmsg(), fbird_errcode(), NULL);
@ -79,13 +77,11 @@ class Firebird extends Abstract_Driver {
// of DB_PDO, which defines these two // of DB_PDO, which defines these two
// class variables for the other drivers // class variables for the other drivers
// Load the sql class foreach(array('sql', 'util') as $sub)
$class = __CLASS__."_sql"; {
$this->sql = new $class(); $class = __CLASS__ . "_{$sub}";
$this->$sub = new $class($this);
// Load the util class }
$class = __CLASS__."_util";
$this->util = new $class($this);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -26,7 +26,7 @@ class Firebird_Util extends DB_Util {
/** /**
* Create an SQL backup file for the current database's structure * Create an SQL backup file for the current database's structure
* * @codeCoverageIgnore
* @return string * @return string
*/ */
public function backup_structure() public function backup_structure()
@ -40,6 +40,7 @@ class Firebird_Util extends DB_Util {
/** /**
* Create an SQL backup file for the current database's data * Create an SQL backup file for the current database's data
* *
* @codeCoverageIgnore
* @param array $exclude * @param array $exclude
* @param bool $system_tables * @param bool $system_tables
* @return string * @return string

View File

@ -30,11 +30,11 @@ class MySQL_Util extends DB_Util {
/** /**
* Convienience public function for creating a new MySQL table * Convienience public function for creating a new MySQL table
* *
* @codeCoverageIgnore
* @param string $name * @param string $name
* @param array $columns * @param array $columns
* @param array $constraints * @param array $constraints
* @param array $indexes * @param array $indexes
*
* @return string * @return string
*/ */
public function create_table($name, $columns, array $constraints=array(), array $indexes=array()) public function create_table($name, $columns, array $constraints=array(), array $indexes=array())
@ -125,10 +125,7 @@ class MySQL_Util extends DB_Util {
foreach($dbs as &$d) foreach($dbs as &$d)
{ {
// Skip built-in dbs // Skip built-in dbs
if ($d == 'mysql') if ($d == 'mysql') continue;
{
continue;
}
// Get the list of tables // Get the list of tables
$tables = $this->driver_query("SHOW TABLES FROM `{$d}`", TRUE); $tables = $this->driver_query("SHOW TABLES FROM `{$d}`", TRUE);

View File

@ -26,6 +26,7 @@ class SQLite_Util extends DB_Util {
/** /**
* Convenience public function to create a new table * Convenience public function to create a new table
* *
* @codeCoverageIgnore
* @param string $name //Name of the table * @param string $name //Name of the table
* @param array $columns //columns as straight array and/or column => type pairs * @param array $columns //columns as straight array and/or column => type pairs
* @param array $constraints // column => constraint pairs * @param array $constraints // column => constraint pairs
@ -98,6 +99,7 @@ class SQLite_Util extends DB_Util {
/** /**
* Create an SQL backup file for the current database's data * Create an SQL backup file for the current database's data
* *
* @codeCoverageIgnore
* @param array $excluded * @param array $excluded
* @return string * @return string
*/ */

View File

@ -56,7 +56,7 @@ abstract class DBTest extends Query_TestCase {
public function testBackupData() public function testBackupData()
{ {
$this->assertTrue(is_string($this->db->util->backup_data(array('create_delete')))); $this->assertTrue(is_string($this->db->util->backup_data(array('create_delete', TRUE))));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -40,8 +40,6 @@ class FirebirdQBTest extends QBTest {
$this->db = Query($params); $this->db = Query($params);
} }
// --------------------------------------------------------------------------
public function testGetNamedConnectionException() public function testGetNamedConnectionException()
{ {
try try
@ -54,8 +52,6 @@ class FirebirdQBTest extends QBTest {
} }
} }
// --------------------------------------------------------------------------
public function testGetNamedConnection() public function testGetNamedConnection()
{ {
$dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB'; $dbpath = QTEST_DIR.QDS.'db_files'.QDS.'FB_TEST_DB.FDB';
@ -74,8 +70,6 @@ class FirebirdQBTest extends QBTest {
$this->assertReference($f_conn, Query('fire')); $this->assertReference($f_conn, Query('fire'));
} }
// --------------------------------------------------------------------------
public function testGetCompiledSelect() public function testGetCompiledSelect()
{ {
$sql = $this->db->get_compiled_select('create_test'); $sql = $this->db->get_compiled_select('create_test');
@ -170,4 +164,9 @@ class FirebirdQBTest extends QBTest {
$this->assertEqual($expected, $error); $this->assertEqual($expected, $error);
} }
public function testBackupStructure()
{
$this->assertEquals('', $this->db->util->backup_structure());
}
} }