Fixed prepared statements in Firebird driver

This commit is contained in:
Timothy Warren 2012-02-23 11:26:08 -05:00
parent c4ff05074f
commit 89a014685d
7 changed files with 79 additions and 20 deletions

4
.gitignore vendored
View File

@ -3,4 +3,6 @@
settings.json settings.json
errors.txt errors.txt
*/simpletest/* */simpletest/*
tests/test_dbs/* tests/test_dbs/*
*.db
*.FDB

View File

@ -42,7 +42,7 @@ abstract class DB_PDO extends PDO {
// Prepare the sql // Prepare the sql
$query = $this->prepare($sql); $query = $this->prepare($sql);
if( ! is_like_array($query)) if( ! (is_object($query) || is_resource($query)))
{ {
$this->get_last_error(); $this->get_last_error();
return FALSE; return FALSE;
@ -52,7 +52,7 @@ abstract class DB_PDO extends PDO {
$this->statement =& $query; $this->statement =& $query;
if( ! is_like_array($data)) if( ! (is_array($data) || is_object($data)))
{ {
trigger_error("Invalid data argument"); trigger_error("Invalid data argument");
return FALSE; return FALSE;

View File

@ -118,9 +118,10 @@ class firebird extends DB_PDO {
/** /**
* Emulate PDO prepare * Emulate PDO prepare
* *
* @param string $query
* @return resource * @return resource
*/ */
public function prepare() public function prepare($query)
{ {
$this->statement = ibase_prepare($this->conn, $query); $this->statement = ibase_prepare($this->conn, $query);
return $this->statement; return $this->statement;
@ -248,8 +249,43 @@ SQL;
*/ */
public function execute($args) public function execute($args)
{ {
// Is there a better way to do this? //Add the prepared statement as the first parameter
return eval('ibase_execute('.$this->statement.','.explode(',', $args).")"); 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);
}
/**
* Prepare and execute a query
*
* @param string $sql
* @param array $args
* @return resource
*/
public function prepare_execute($sql, $args)
{
$query = $this->prepare($sql);
// Set the statement in the class variable for easy later access
$this->statement =& $query;
return $this->execute($args);
}
/**
* Bind a prepared query with arguments for executing
*
* @param string $sql
* @param mixed $args
* @return FALSE
*/
public function prepare_query($sql, $args)
{
// You can't bind query statements before execution with
// the firebird database
return FALSE;
}
} }
// End of firebird.php // End of firebird.php

View File

@ -62,7 +62,7 @@ class firebird_manip extends db_manip {
$columns = array(); $columns = array();
foreach($column_array as $n => $props) foreach($column_array as $n => $props)
{ {
$str = "{$n} "; $str = '"'.$n.'" ';
$str .= (isset($props['type'])) ? "{$props['type']} " : ""; $str .= (isset($props['type'])) ? "{$props['type']} " : "";
$str .= (isset($props['constraint'])) ? "{$props['constraint']} " : ""; $str .= (isset($props['constraint'])) ? "{$props['constraint']} " : "";
@ -70,9 +70,9 @@ class firebird_manip extends db_manip {
} }
// Generate the sql for the creation of the table // Generate the sql for the creation of the table
$sql = "CREATE TABLE \"{$name}\" ("; $sql = 'CREATE TABLE "'.$name.'" (';
$sql .= implode(",", $columns); $sql .= implode(',', $columns);
$sql .= ")"; $sql .= ')';
return $sql; return $sql;
} }

View File

@ -72,7 +72,7 @@ class FirebirdTest extends UnitTestCase {
function TestCreateTable() function TestCreateTable()
{ {
//Attempt to create the table //Attempt to create the table
$sql = $this->db->manip->create_table('create_test', array('id' => 'SMALLINT')); $sql = $this->db->manip->create_table('create_test', array('id' => 'SMALLINT', 'key' => 'VARCHAR(64)', 'val' => 'BLOB SUB_TYPE TEXT'));
$this->db->query($sql); $this->db->query($sql);
//This test fails for an unknown reason, when clearly the table exists //This test fails for an unknown reason, when clearly the table exists
@ -87,6 +87,34 @@ class FirebirdTest extends UnitTestCase {
$this->assertTrue($table_exists);*/ $this->assertTrue($table_exists);*/
} }
function TestPreparedStatements()
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->db->prepare($sql);
$this->db->execute(array(1,"boogers", "Gross"));
}
function TestPrepareExecute()
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->db->prepare_execute($sql, array(
2, "works", 'also?'
));
}
function TestPrepareQuery()
{
$this->assertFalse($this->db->prepare_query('', array()));
}
function TestDeleteTable() function TestDeleteTable()
{ {
@ -103,12 +131,5 @@ class FirebirdTest extends UnitTestCase {
$this->assertFalse($table_exists); $this->assertFalse($table_exists);
} }
function TestPreparedStatements()
{
$sql = 'INSERT INTO "create_test" ("id") VALUES (?),(?),(?)';
$this->db->prepare($sql);
$this->db->execute(array(1,2,3));
}
} }

Binary file not shown.

Binary file not shown.