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

View File

@ -18,7 +18,7 @@
*/ */
class Query_Builder { class Query_Builder {
private $table, $where_array; private $table, $where_array, $sql;
/** /**
* Constructor * Constructor
@ -52,24 +52,9 @@ class Query_Builder {
$this->db = new $dbtype("{$params->host}:{$params->file}", $params->user, $params->pass); $this->db = new $dbtype("{$params->host}:{$params->file}", $params->user, $params->pass);
break; break;
} }
}
// -------------------------------------------------------------------------- // Make things just slightly shorter
$this->sql =& $this->db->sql;
/**
* 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;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -83,7 +68,7 @@ class Query_Builder {
*/ */
public function __call($name, $params) public function __call($name, $params)
{ {
if (isset($this->db->$name)) if (method_exists($this->db, $name))
{ {
if (is_callable($this->db->$name)) if (is_callable($this->db->$name))
{ {
@ -107,19 +92,24 @@ class Query_Builder {
*/ */
public function get($table='', $limit=FALSE, $offset=FALSE) 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) if ( ! empty($table) && $limit === FALSE && $offset === FALSE)
{ {
$result = $this->query($sql); $result = $this->db->query($sql);
} }
else else
{ {
$result = $this->query($this->sql->limit($sql, $limit, $offset)); $sql = $this->sql->limit($sql, $limit, $offset);
$result = $this->db->query($sql);
} }
// For the firebird class, return $this so you can act on the result //echo $sql."<br />";
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 //This test fails for an unknown reason, when clearly the table exists
//Reset //Reset
/*$this->tearDown(); $this->tearDown();
$this->setUp(); $this->setUp();
//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 />';
@ -133,6 +133,27 @@ class FirebirdTest extends UnitTestCase {
$this->assertTrue($res); $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() function TestPreparedStatements()
{ {
$sql = <<<SQL $sql = <<<SQL

View File

@ -28,6 +28,12 @@ class SQLiteTest extends UnitTestCase {
{ {
$path = dirname(__FILE__)."/../test_dbs/test_sqlite.db"; $path = dirname(__FILE__)."/../test_dbs/test_sqlite.db";
$this->db = new SQLite($path); $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() function tearDown()
@ -125,6 +131,27 @@ SQL;
$this->assertTrue($res); $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() function TestDeleteTable()
{ {
//Make sure the table exists to delete //Make sure the table exists to delete

Binary file not shown.