Various db class improvements, more SQLite tests

This commit is contained in:
Timothy Warren 2012-02-28 12:28:56 -05:00
parent 589dfe01b9
commit d29d433d3c
6 changed files with 59 additions and 6 deletions

View File

@ -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: <pre>{$info[0]}:{$info[1]}\n{$info[2]}</pre>";
}
// -------------------------------------------------------------------------

View File

@ -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;

View File

@ -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;

View File

@ -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 = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$statement =& $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
$statement->execute();
}
function TestPrepareExecute()
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->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']));
}
}

Binary file not shown.

Binary file not shown.