From 6efc5ef008f8bf228238fbf40ebf2285a5e883e3 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 15 Mar 2012 14:04:45 -0400 Subject: [PATCH] Fix issue #7 --- sys/db/db_pdo.php | 2 +- sys/db/drivers/firebird-ibase.php | 217 ++++++++++++++++++------------ tests/databases/firebird-qb.php | 32 +++-- tests/databases/firebird.php | 4 +- tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 5 files changed, 149 insertions(+), 106 deletions(-) diff --git a/sys/db/db_pdo.php b/sys/db/db_pdo.php index 9da0819..9afac89 100644 --- a/sys/db/db_pdo.php +++ b/sys/db/db_pdo.php @@ -133,7 +133,7 @@ abstract class DB_PDO extends PDO { } // Execute the query - $this->statement->execute(); + //$this->statement->execute(); // Return number of rows affected return $this->statement->rowCount(); diff --git a/sys/db/drivers/firebird-ibase.php b/sys/db/drivers/firebird-ibase.php index e631b5e..4f83df1 100644 --- a/sys/db/drivers/firebird-ibase.php +++ b/sys/db/drivers/firebird-ibase.php @@ -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 \ No newline at end of file diff --git a/tests/databases/firebird-qb.php b/tests/databases/firebird-qb.php index bf9ce1a..f0345b3 100644 --- a/tests/databases/firebird-qb.php +++ b/tests/databases/firebird-qb.php @@ -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,9 +141,7 @@ class FirebirdQBTest extends UnitTestCase { ->limit(2, 1) ->get(); - $this->assertTrue(is_resource($query)); - -
db->fetchAll(), TRUE) ?>
+ $this->assertIsA($query, 'Firebird_Result'); } /*function TestGroupBy() @@ -159,7 +157,7 @@ class FirebirdQBTest extends UnitTestCase { ->limit(5,2) ->get(); - $this->assertTrue(is_resource($query)); + $this->assertIsA($query, 'Firebird_Result'); }*/ function TestLike() @@ -168,7 +166,7 @@ class FirebirdQBTest extends UnitTestCase { ->like('key', 'og') ->get(); - $this->assertTrue(is_resource($query)); + $this->assertIsA($query, 'Firebird_Result'); } function TestWhereIn() @@ -177,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() @@ -186,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() diff --git a/tests/databases/firebird.php b/tests/databases/firebird.php index 80aa806..8b0c166 100644 --- a/tests/databases/firebird.php +++ b/tests/databases/firebird.php @@ -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")); } diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB index 3b0c4d0dbfb855d218f710484519ada510fea403..7094814028d2bffb317d551b1a3916460df249fb 100755 GIT binary patch delta 875 zcma)4F=!M)6#X;18}Ih-hW(gnfh@A>Xd~?pl$DqTLs`OU}b-|1z_dBQ@xRIc$!ZL`Ti3gxs>mf=SrP2l`2owyBxx3 zJaa`J^qt{~sa(TVC4&*@B9Y_dnwWUKaA>cN~&WzheK{|41F$ zA!WFcGTcZV&{F3u*iVNJ3G@rOxv^Jc-VQaxjhf*`&HmSzl^FwVEQnV0*wvz^Nqf&U hDHcYXgXuq#PBS|Gb5MP8X^c16@2%ops~`Ws*}rLOfjj^J delta 2001 zcmb7_O=uHA6vy9Anl{-nWQnd7Y&Er{wV2vOT1r8w9z>`h4c4e98~mt;kYK9Tix@4a z;0MOb927wh9*SU9up-z~5sPB4;>Ekkwmo>Q_-1w=$!;obH@t5ClkEQHH=DjG7L&zd z@|Xq#iAXb44}%wdPLk(&6W}`*0MnlQ1Q_XG==4YIFBrh^HL8M!f9K72ZKH3)kBx@V zlyi?c9G)wswok^5J8?t9Vpu+`1}r~TBUTeu04q2XH}oH2wqVXQM)jM1ZSYO%ntnT> zzfbUnVJ%onZJyaypOOkB!}J9fTsrC#>3BYSDxvUv}bGC?Vm z?3hdTLaprCs_fc}obhle6PGe^PuI(ya%4}eTlTmIN|~UP33}8eJ5(!sWFxYNJzUDf zrA*ud^|JdN*@1P-_Isd|2}+ruJucb3wX%CQBHQKRQYJ2C;BB~Jp1k^iu&gFC66Zyh9bycp+4{geR!`nHB__fHJ{lOJT zYof>EI|G^Qcz&?(RW+-2(QV;B@fW*eiE)cZcn3=EC0&=nTe|w53lAtiK1|Vi`a3rE#;srrp}Rq0PdXgM6N`eRN}IA_ZE?|E;96 Vg9LD8@US