Some Query Builder tests

This commit is contained in:
Timothy Warren 2012-03-08 12:37:33 -05:00
parent 1cbfb0a0e3
commit 0856de65fc
6 changed files with 77 additions and 43 deletions

View File

@ -19,7 +19,7 @@
*/
class firebird extends DB_PDO {
protected $statement, $trans, $count, $result;
protected $statement, $trans, $count, $result, $conn;
/**
* Open the link to the database
@ -30,14 +30,10 @@ class firebird extends DB_PDO {
*/
public function __construct($dbpath, $user='sysdba', $pass='masterkey')
{
// More a pain than it is worth to actually
// pass around the resource that this provides.
// Since the resource is not required by the
// functions that would use it, I'm dumping it.
$conn = @ibase_connect($dbpath, $user, $pass, 'utf-8');
$this->conn = @ibase_connect($dbpath, $user, $pass, 'utf-8');
// Throw an exception to make this match other pdo classes
if ( ! is_resource($conn))
if ( ! is_resource($this->conn))
{
throw new PDOException(ibase_errmsg());
die();
@ -87,18 +83,18 @@ class firebird extends DB_PDO {
if (isset($this->trans))
{
$this->statement = @ibase_query($this->trans, $sql);
$this->statement = ibase_query($this->trans, $sql);
}
else
{
$this->statement = @ibase_query($sql);
$this->statement = ibase_query($this->conn, $sql);
}
// Throw the error as a exception
if ($this->statement === FALSE)
/*if ($this->statement === FALSE)
{
throw new PDOException(ibase_errmsg());
}
}*/
return $this->statement;
}
@ -161,13 +157,13 @@ class firebird extends DB_PDO {
*/
public function prepare($query, $options=NULL)
{
$this->statement = @ibase_prepare($query);
$this->statement = ibase_prepare($this->conn, $query);
// Throw the error as an exception
if ($this->statement === FALSE)
/*if ($this->statement === FALSE)
{
throw new PDOException(ibase_errmsg());
}
}*/
return $this->statement;
}
@ -268,7 +264,7 @@ SQL;
*/
public function beginTransaction()
{
if(($this->trans = ibase_trans()) !== NULL)
if(($this->trans = ibase_trans($this->conn)) !== NULL)
{
return TRUE;
}

View File

@ -103,7 +103,7 @@ class Firebird_SQL extends DB_SQL {
if ($offset > 0)
{
$sql .= ' SKIP'. (int) $offset;
$sql .= ' SKIP '. (int) $offset;
}
$sql = preg_replace("`SELECT`i", "SELECT {$sql}", $orig_sql);

View File

@ -18,7 +18,7 @@
*/
class Query_Builder {
private $table, $where_array;
private $table, $where_array, $sql;
/**
* Constructor
@ -52,24 +52,9 @@ class Query_Builder {
$this->db = new $dbtype("{$params->host}:{$params->file}", $params->user, $params->pass);
break;
}
}
// --------------------------------------------------------------------------
/**
* Shortcut to directly access database class properties
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
if (isset($this->db->$key))
{
return $this->db->$key;
}
return NULL;
// Make things just slightly shorter
$this->sql =& $this->db->sql;
}
// --------------------------------------------------------------------------
@ -83,7 +68,7 @@ class Query_Builder {
*/
public function __call($name, $params)
{
if (isset($this->db->$name))
if (method_exists($this->db, $name))
{
if (is_callable($this->db->$name))
{
@ -107,19 +92,24 @@ class Query_Builder {
*/
public function get($table='', $limit=FALSE, $offset=FALSE)
{
$sql = 'SELECT * FROM ' . $this->quote_ident($table);
// @todo Only add in the table name when using the select method
// @tood Only execute combined query when using other query methods and empty parameters
$sql = 'SELECT * FROM ' . $this->db->quote_ident($table);
if ( ! empty($table) && $limit === FALSE && $offset === FALSE)
{
$result = $this->query($sql);
$result = $this->db->query($sql);
}
else
{
$result = $this->query($this->sql->limit($sql, $limit, $offset));
$sql = $this->sql->limit($sql, $limit, $offset);
$result = $this->db->query($sql);
}
//echo $sql."<br />";
// For the firebird class, return $this so you can act on the result
return (is_resource($result)) ? $this : $result;
return $result;
}
// --------------------------------------------------------------------------

View File

@ -100,11 +100,11 @@ class FirebirdTest extends UnitTestCase {
//This test fails for an unknown reason, when clearly the table exists
//Reset
/*$this->tearDown();
$this->tearDown();
$this->setUp();
//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 />';
@ -133,6 +133,27 @@ class FirebirdTest extends UnitTestCase {
$this->assertTrue($res);
}
function TestQBGet()
{
$query = $this->qb->get('create_test');
$this->assertTrue(is_resource($query));
}
function TestQBGetLimit()
{
$query = $this->qb->get('create_test', 2);
$this->assertTrue(is_resource($query));
}
function TestQBGetLimitSkip()
{
$query = $this->qb->get('create_test', 2, 1);
$this->assertTrue(is_resource($query));
}
function TestPreparedStatements()
{
$sql = <<<SQL

View File

@ -28,6 +28,12 @@ class SQLiteTest extends UnitTestCase {
{
$path = dirname(__FILE__)."/../test_dbs/test_sqlite.db";
$this->db = new SQLite($path);
$params = new Stdclass();
$params->type = 'sqlite';
$params->file = $path;
$params->host = 'localhost';
$this->qb = new Query_Builder($params);
}
function tearDown()
@ -125,6 +131,27 @@ SQL;
$this->assertTrue($res);
}
function TestQBGet()
{
$query = $this->qb->get('create_test');
$this->assertIsA($query, 'PDOStatement');
}
function TestQBGetLimit()
{
$query = $this->qb->get('create_test', 2);
$this->assertIsA($query, 'PDOStatement');
}
function TestQBGetLimitSkip()
{
$query = $this->qb->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
function TestDeleteTable()
{
//Make sure the table exists to delete

Binary file not shown.