From d29d433d3c9772bed8b35ab8ecd16297629f0224 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 28 Feb 2012 12:28:56 -0500 Subject: [PATCH] Various db class improvements, more SQLite tests --- src/common/db_pdo.php | 21 +++++++++++++++++- src/databases/sqlite.php | 4 ++-- src/databases/sqlite_manip.php | 2 +- tests/databases/sqlite.php | 38 +++++++++++++++++++++++++++++++-- tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes tests/test_dbs/test_sqlite.db | Bin 4096 -> 4096 bytes 6 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/common/db_pdo.php b/src/common/db_pdo.php index bf2c8b6..100c505 100644 --- a/src/common/db_pdo.php +++ b/src/common/db_pdo.php @@ -61,6 +61,11 @@ abstract class DB_PDO extends PDO { // Bind the parameters foreach($data as $k => $value) { + if(is_numeric($k)) + { + $k++; + } + $res = $query->bindValue($k, $value); if( ! $res) @@ -83,7 +88,7 @@ abstract class DB_PDO extends PDO { */ public function prepare_execute($sql, $params) { - $this->prepare_query($sql, $params); + $this->statement =& $this->prepare_query($sql, $params); $this->statement->execute(); return $this->statement; @@ -126,6 +131,20 @@ abstract class DB_PDO extends PDO { // Return number of rows affected return $this->statement->rowCount; } + + // -------------------------------------------------------------------------- + + /** + * Return the last error for the current database connection + * + * @return string + */ + public function get_last_error() + { + $info = $this->errorInfo(); + + echo "Error:
{$info[0]}:{$info[1]}\n{$info[2]}
"; + } // ------------------------------------------------------------------------- diff --git a/src/databases/sqlite.php b/src/databases/sqlite.php index 2b56ed0..49cb114 100644 --- a/src/databases/sqlite.php +++ b/src/databases/sqlite.php @@ -26,10 +26,10 @@ class SQLite extends DB_PDO { * * @param string $dsn */ - public function __construct($dsn) + public function __construct($dsn, $user=NULL, $pass=NULL) { // DSN is simply `sqlite:/path/to/db` - parent::__construct("sqlite:{$dsn}"); + parent::__construct("sqlite:{$dsn}", $user, $pass); $class = __CLASS__."_manip"; $this->manip = new $class; diff --git a/src/databases/sqlite_manip.php b/src/databases/sqlite_manip.php index 8048c9c..b3cedd3 100644 --- a/src/databases/sqlite_manip.php +++ b/src/databases/sqlite_manip.php @@ -68,7 +68,7 @@ class SQLite_manip extends db_manip { // Generate the sql for the creation of the table $sql = "CREATE TABLE \"{$name}\" ("; - $sql .= implode(",", $columns); + $sql .= implode(", ", $columns); $sql .= ")"; return $sql; diff --git a/tests/databases/sqlite.php b/tests/databases/sqlite.php index f4c5a53..4c417d6 100644 --- a/tests/databases/sqlite.php +++ b/tests/databases/sqlite.php @@ -53,14 +53,47 @@ class SQLiteTest extends UnitTestCase { function TestCreateTable() { //Attempt to create the table - $sql = $this->db->manip->create_table('create_test', array('id' => 'INTEGER'), array('id' => 'PRIMARY KEY')); + $sql = $this->db->manip->create_table('create_test', + array( + 'id' => 'INTEGER', + 'key' => 'TEXT', + 'val' => 'TEXT', + ), + array( + 'id' => 'PRIMARY KEY' + ) + ); $this->db->query($sql); //Check $dbs = $this->db->get_tables(); - $this->assertEqual($dbs['create_test'], 'CREATE TABLE "create_test" (id INTEGER PRIMARY KEY)'); + $this->assertEqual($dbs['create_test'], 'CREATE TABLE "create_test" (id INTEGER PRIMARY KEY, key TEXT , val TEXT )'); } + + function TestPreparedStatements() + { + $sql = <<db->prepare_query($sql, array(1,"boogers", "Gross")); + + $statement->execute(); + } + + function TestPrepareExecute() + { + $sql = <<db->prepare_execute($sql, array( + 2, "works", 'also?' + )); + + } + function TestDeleteTable() { //Make sure the table exists to delete @@ -75,4 +108,5 @@ class SQLiteTest extends UnitTestCase { $dbs = $this->db->get_tables(); $this->assertTrue(empty($dbs['create_test'])); } + } \ No newline at end of file diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB index 8687efd4c3a7cc0b0b39d10b86bbff9cc668a6e2..111bfce6e91db904e6d787ccd44d7d7ffe8d6650 100755 GIT binary patch delta 5443 zcmdT|ZEO_B8J^kO+4Jq4y>~v_=QFP&ip#G#XU!D%kU2)<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l?oxQ5g`gX3T25o5a#9;%rP7sOw9Kf xnBOtq+bqa(iCKw{iG@L2yga`syErkYINx4?k%d88JSjgvJ+-LVy(qu97yyl*DLViF delta 108 zcmZorXi%6SEx4P3fq?~xnSq!Ih<8uaF$N0iK9c7J@)(&{GBB@X4%t|6j%nfqN!Czh zWybu;F3g(B$wjG&C8_Zxsl_D<8ks2yo_-;&?yf-!0YRR=jzN(M-ma0GS1`wLY-VBk G!wdjOxf~+^