Update and Insert tests

This commit is contained in:
Timothy Warren 2012-03-13 12:57:12 -04:00
parent 20555ccc3a
commit 1d30e2be71
4 changed files with 58 additions and 25 deletions

View File

@ -18,8 +18,7 @@
*/
class Query_Builder {
private $table,
$sql,
private $sql,
$select_string,
$from_string,
$where_array,
@ -475,7 +474,7 @@ class Query_Builder {
$this->set($data);
}
$sql = 'UPDATE '.$this->quote_ident($table). ' SET '. $this->set_string;
$sql = $this->_compile('update', $table);
$params = array_values($this->set_array);
@ -485,8 +484,6 @@ class Query_Builder {
// the set string
if ( ! empty($this->where_string))
{
$sql .= $this->where_string;
$where_params = array_values($this->where_array);
foreach($where_params as $w)
@ -513,6 +510,8 @@ class Query_Builder {
// @todo implement delete method
}
// --------------------------------------------------------------------------
// ! Miscellaneous Methods
// --------------------------------------------------------------------------
/**
@ -577,7 +576,7 @@ class Query_Builder {
}
// Set the limit via the class variables
if (is_numeric($this->limit))
if (isset($this->limit) && is_numeric($this->limit))
{
$sql = $this->sql->limit($sql, $this->limit, $this->offset);
}
@ -591,12 +590,21 @@ class Query_Builder {
') VALUES ('.implode(', ', $params).')';
break;
case "update":
$sql = 'UPDATE '.$this->db->quote_ident($table). ' SET '. $this->set_string;
if ( ! empty($this->where_string))
{
$sql .= $this->where_string;
}
break;
case "delete":
// @todo Implement delete statements
break;
}
echo $sql.'<br />';
//echo $sql.'<br />';
return $sql;
}

View File

@ -20,10 +20,7 @@ class FirebirdQBTest extends UnitTestCase {
function __construct()
{
parent::__construct();
}
function setUp()
{
$dbpath = TEST_DIR.DS.'test_dbs'.DS.'FB_TEST_DB.FDB';
// Test the query builder
@ -34,11 +31,8 @@ class FirebirdQBTest extends UnitTestCase {
$params->user = 'sysdba';
$params->pass = 'masterkey';
$this->qb = new Query_Builder($params);
}
function tearDown()
{
unset($this->qb);
//echo '<hr /> Firebird Queries <br />';
}
function TestQBGet()
@ -111,4 +105,25 @@ class FirebirdQBTest extends UnitTestCase {
$this->assertTrue(is_resource($query));
}
function TestInsert()
{
$query = $this->qb->set('id', 4)
->set('key', 4)
->set('val', 5)
->insert('create_test');
$this->assertTrue($query);
}
function TestUpdate()
{
$query = $this->qb->set('id', 4)
->set('key', 'gogle')
->set('val', 'non-word')
->where('id', 4)
->update('create_test');
$this->assertTrue($query);
}
}

View File

@ -17,20 +17,19 @@
*/
class SQLiteQBTest extends UnitTestCase {
function setUp()
{
$path = TEST_DIR.DS.'test_dbs'.DS.'test_sqlite.db';
function __construct()
{
parent::__construct();
$path = TEST_DIR.DS.'test_dbs'.DS.'test_sqlite.db';
$params = new Stdclass();
$params->type = 'sqlite';
$params->file = $path;
$params->host = 'localhost';
$this->qb = new Query_Builder($params);
}
function tearDown()
{
unset($this->qb);
}
//echo '<hr /> SQLite Queries <br />';
}
function TestGet()
{
@ -110,4 +109,15 @@
$this->assertIsA($query, 'PDOStatement');
}
function TestUpdate()
{
$query = $this->qb->set('id', 4)
->set('key', 'gogle')
->set('val', 'non-word')
->where('id', 4)
->update('create_test');
$this->assertIsA($query, 'PDOStatement');
}
}

Binary file not shown.