Overhaul firebird driver to better match PDO interface.

This commit is contained in:
Timothy Warren 2012-03-15 14:09:45 -04:00
parent a9995ecd6c
commit a304ffd134
4 changed files with 148 additions and 106 deletions

View File

@ -137,9 +137,6 @@ abstract class DB_PDO extends PDO {
return FALSE;
}
// Execute the query
$this->statement->execute();
// Return number of rows affected
return $this->statement->rowCount();
}

View File

@ -19,7 +19,7 @@
*/
class firebird extends DB_PDO {
protected $statement, $trans, $count, $result, $conn;
protected $statement, $statement_link, $trans, $count, $result, $conn;
/**
* Open the link to the database
@ -83,69 +83,23 @@ class firebird extends DB_PDO {
if (isset($this->trans))
{
$this->statement = @ibase_query($this->trans, $sql);
$this->statement_link = @ibase_query($this->trans, $sql);
}
else
{
$this->statement = @ibase_query($this->conn, $sql);
$this->statement_link = @ibase_query($this->conn, $sql);
}
// Throw the error as a exception
if ($this->statement === FALSE)
if ($this->statement_link === FALSE)
{
throw new PDOException(ibase_errmsg());
}
return $this->statement;
return new FireBird_Result($this->statement_link);
}
// --------------------------------------------------------------------------
/**
* Emulate PDO fetch public function
*
* @param int $fetch_style
* @return mixed
*/
public function fetch($fetch_style=PDO::FETCH_ASSOC)
{
switch($fetch_style)
{
case PDO::FETCH_OBJ:
return ibase_fetch_object($this->statement, IBASE_FETCH_BLOBS);
break;
case PDO::FETCH_NUM:
return ibase_fetch_row($this->statement, IBASE_FETCH_BLOBS);
break;
default:
return ibase_fetch_assoc($this->statement, IBASE_FETCH_BLOBS);
break;
}
}
// --------------------------------------------------------------------------
/**
* Emulate PDO fetchAll public function
*
* @param int $fetch_style
* @return mixed
*/
public function fetchAll($fetch_style=PDO::FETCH_ASSOC)
{
$all = array();
while($row = $this->fetch($fetch_style))
{
$all[] = $row;
}
$this->result = $all;
return $all;
}
// --------------------------------------------------------------------------
@ -157,15 +111,15 @@ class firebird extends DB_PDO {
*/
public function prepare($query, $options=NULL)
{
$this->statement = @ibase_prepare($this->conn, $query);
$this->statement_link = @ibase_prepare($this->conn, $query);
// Throw the error as an exception
if ($this->statement === FALSE)
if ($this->statement_link === FALSE)
{
throw new PDOException(ibase_errmsg());
}
return $this->statement;
return new FireBird_Result($this->statement_link);
}
// --------------------------------------------------------------------------
@ -187,7 +141,7 @@ SQL;
$tables = array();
while($row = $this->fetch(PDO::FETCH_ASSOC))
while($row = $this->statement->fetch(PDO::FETCH_ASSOC))
{
$tables[] = $row['RDB$RELATION_NAME'];
}
@ -214,7 +168,7 @@ SQL;
$tables = array();
while($row = $this->fetch(PDO::FETCH_ASSOC))
while($row = $this->statement->fetch(PDO::FETCH_ASSOC))
{
$tables[] = $row['RDB$RELATION_NAME'];
}
@ -224,18 +178,6 @@ SQL;
// --------------------------------------------------------------------------
/**
* Return the number of rows affected by the previous query
*
* @return int
*/
public function affected_rows($statement="")
{
return ibase_affected_rows();
}
// --------------------------------------------------------------------------
/**
* Return the number of rows returned for a SELECT query
*
@ -250,7 +192,7 @@ SQL;
}
//Fetch all the rows for the result
$this->result = $this->fetchAll();
$this->result = $this->statement->fetchAll();
return count($this->result);
}
@ -296,23 +238,7 @@ SQL;
return ibase_rollback($this->trans);
}
// --------------------------------------------------------------------------
/**
* Run a prepared statement query
*
* @param array $args
* @return bool
*/
public function execute($args)
{
//Add the prepared statement as the first parameter
array_unshift($args, $this->statement);
// Let php do all the hard stuff in converting
// the array of arguments into a list of arguments
return call_user_func_array('ibase_execute', $args);
}
// --------------------------------------------------------------------------
@ -330,7 +256,7 @@ SQL;
// Set the statement in the class variable for easy later access
$this->statement =& $query;
return $this->execute($args);
return $query->execute($args);
}
// --------------------------------------------------------------------------
@ -465,4 +391,123 @@ SQL;
return $output_sql;
}
}
// --------------------------------------------------------------------------
/**
* Firebird result class to emulate PDOStatement Class
*/
class Firebird_Result {
private $statement;
/**
* Create the object by passing the resource for
* the query
*
* @param resource $link
*/
public function __construct($link)
{
$this->statement = $link;
}
// --------------------------------------------------------------------------
/**
* Emulate PDO fetch public function
*
* @param int $fetch_style
* @return mixed
*/
public function fetch($fetch_style=PDO::FETCH_ASSOC, $statement=NULL)
{
if ( ! is_null($statement))
{
$this->statement = $statement;
}
switch($fetch_style)
{
case PDO::FETCH_OBJ:
return ibase_fetch_object($this->statement, IBASE_FETCH_BLOBS);
break;
case PDO::FETCH_NUM:
return ibase_fetch_row($this->statement, IBASE_FETCH_BLOBS);
break;
default:
return ibase_fetch_assoc($this->statement, IBASE_FETCH_BLOBS);
break;
}
}
// --------------------------------------------------------------------------
/**
* Emulate PDO fetchAll public function
*
* @param int $fetch_style
* @return mixed
*/
public function fetchAll($fetch_style=PDO::FETCH_ASSOC, $statement=NULL)
{
$all = array();
while($row = $this->fetch($fetch_style, $statement))
{
$all[] = $row;
}
$this->result = $all;
return $all;
}
// --------------------------------------------------------------------------
/**
* Run a prepared statement query
*
* @param array $args
* @return bool
*/
public function execute($args)
{
//Add the prepared statement as the first parameter
array_unshift($args, $this->statement);
// Let php do all the hard stuff in converting
// the array of arguments into a list of arguments
return new Firebird_Result(call_user_func_array('ibase_execute', $args));
}
// --------------------------------------------------------------------------
/**
* Return the number of rows affected by the previous query
*
* @return int
*/
public function rowCount($statement="")
{
return ibase_affected_rows();
}
// --------------------------------------------------------------------------
/**
* Method to emulate PDO->errorInfo / PDOStatement->errorInfo
*
* @return array
*/
public function errorInfo()
{
$code = ibase_errcode();
$msg = ibase_errmsg();
return array(0, $code, $msg);
}
}
// End of firebird-ibase.php

View File

@ -39,21 +39,21 @@ class FirebirdQBTest extends UnitTestCase {
{
$query = $this->db->get('create_test ct');
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestGetLimit()
{
$query = $this->db->get('create_test', 2);
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestGetLimitSkip()
{
$query = $this->db->get('create_test', 2, 1);
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestSelectWhereGet()
@ -63,7 +63,7 @@ class FirebirdQBTest extends UnitTestCase {
->where('id <', 800)
->get('create_test', 2, 1);
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestSelectWhereGet2()
@ -73,7 +73,7 @@ class FirebirdQBTest extends UnitTestCase {
->get('create_test', 2, 1);
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestSelectGet()
@ -81,7 +81,7 @@ class FirebirdQBTest extends UnitTestCase {
$query = $this->db->select('id, key as k, val')
->get('create_test', 2, 1);
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestSelectFromGet()
@ -91,7 +91,7 @@ class FirebirdQBTest extends UnitTestCase {
->where('id >', 1)
->get();
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestSelectFromLimitGet()
@ -102,7 +102,7 @@ class FirebirdQBTest extends UnitTestCase {
->limit(3)
->get();
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestOrderBy()
@ -116,7 +116,7 @@ class FirebirdQBTest extends UnitTestCase {
->limit(5,2)
->get();
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestOrderByRand()
@ -129,7 +129,7 @@ class FirebirdQBTest extends UnitTestCase {
->limit(5,2)
->get();
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestOrWhere()
@ -141,7 +141,7 @@ class FirebirdQBTest extends UnitTestCase {
->limit(2, 1)
->get();
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
/*function TestGroupBy()
@ -157,7 +157,7 @@ class FirebirdQBTest extends UnitTestCase {
->limit(5,2)
->get();
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}*/
function TestLike()
@ -166,7 +166,7 @@ class FirebirdQBTest extends UnitTestCase {
->like('key', 'og')
->get();
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestWhereIn()
@ -175,7 +175,7 @@ class FirebirdQBTest extends UnitTestCase {
->where_in('key', array(12, 96, "works"))
->get();
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestJoin()
@ -184,7 +184,7 @@ class FirebirdQBTest extends UnitTestCase {
->join('create_join cj', 'cj.id = create_test.id')
->get();
$this->assertTrue(is_resource($query));
$this->assertIsA($query, 'Firebird_Result');
}
function TestInsert()

View File

@ -142,8 +142,8 @@ class FirebirdTest extends UnitTestCase {
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->db->prepare($sql);
$this->db->execute(array(1,"booger's", "Gross"));
$query = $this->db->prepare($sql);
$query->execute(array(1,"booger's", "Gross"));
}