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

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

View File

@ -19,7 +19,7 @@
*/ */
class firebird extends DB_PDO { 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 * Open the link to the database
@ -83,69 +83,23 @@ class firebird extends DB_PDO {
if (isset($this->trans)) if (isset($this->trans))
{ {
$this->statement = @ibase_query($this->trans, $sql); $this->statement_link = @ibase_query($this->trans, $sql);
} }
else else
{ {
$this->statement = @ibase_query($this->conn, $sql); $this->statement_link = @ibase_query($this->conn, $sql);
} }
// Throw the error as a exception // Throw the error as a exception
if ($this->statement === FALSE) if ($this->statement_link === FALSE)
{ {
throw new PDOException(ibase_errmsg()); 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) 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 // Throw the error as an exception
if ($this->statement === FALSE) if ($this->statement_link === FALSE)
{ {
throw new PDOException(ibase_errmsg()); throw new PDOException(ibase_errmsg());
} }
return $this->statement; return new FireBird_Result($this->statement_link);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -187,7 +141,7 @@ SQL;
$tables = array(); $tables = array();
while($row = $this->fetch(PDO::FETCH_ASSOC)) while($row = $this->statement->fetch(PDO::FETCH_ASSOC))
{ {
$tables[] = $row['RDB$RELATION_NAME']; $tables[] = $row['RDB$RELATION_NAME'];
} }
@ -214,7 +168,7 @@ SQL;
$tables = array(); $tables = array();
while($row = $this->fetch(PDO::FETCH_ASSOC)) while($row = $this->statement->fetch(PDO::FETCH_ASSOC))
{ {
$tables[] = $row['RDB$RELATION_NAME']; $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 * Return the number of rows returned for a SELECT query
* *
@ -250,7 +192,7 @@ SQL;
} }
//Fetch all the rows for the result //Fetch all the rows for the result
$this->result = $this->fetchAll(); $this->result = $this->statement->fetchAll();
return count($this->result); return count($this->result);
} }
@ -296,23 +238,7 @@ SQL;
return ibase_rollback($this->trans); 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 // Set the statement in the class variable for easy later access
$this->statement =& $query; $this->statement =& $query;
return $this->execute($args); return $query->execute($args);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -465,4 +391,123 @@ SQL;
return $output_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 // End of firebird-ibase.php

View File

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

View File

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