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

View File

@ -20,10 +20,7 @@ class FirebirdQBTest extends UnitTestCase {
function __construct() function __construct()
{ {
parent::__construct(); parent::__construct();
}
function setUp()
{
$dbpath = TEST_DIR.DS.'test_dbs'.DS.'FB_TEST_DB.FDB'; $dbpath = TEST_DIR.DS.'test_dbs'.DS.'FB_TEST_DB.FDB';
// Test the query builder // Test the query builder
@ -34,11 +31,8 @@ class FirebirdQBTest extends UnitTestCase {
$params->user = 'sysdba'; $params->user = 'sysdba';
$params->pass = 'masterkey'; $params->pass = 'masterkey';
$this->qb = new Query_Builder($params); $this->qb = new Query_Builder($params);
}
//echo '<hr /> Firebird Queries <br />';
function tearDown()
{
unset($this->qb);
} }
function TestQBGet() function TestQBGet()
@ -111,4 +105,25 @@ class FirebirdQBTest extends UnitTestCase {
$this->assertTrue(is_resource($query)); $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 { class SQLiteQBTest extends UnitTestCase {
function setUp() function __construct()
{ {
$path = TEST_DIR.DS.'test_dbs'.DS.'test_sqlite.db'; parent::__construct();
$path = TEST_DIR.DS.'test_dbs'.DS.'test_sqlite.db';
$params = new Stdclass(); $params = new Stdclass();
$params->type = 'sqlite'; $params->type = 'sqlite';
$params->file = $path; $params->file = $path;
$params->host = 'localhost'; $params->host = 'localhost';
$this->qb = new Query_Builder($params); $this->qb = new Query_Builder($params);
}
//echo '<hr /> SQLite Queries <br />';
function tearDown() }
{
unset($this->qb);
}
function TestGet() function TestGet()
{ {
@ -110,4 +109,15 @@
$this->assertIsA($query, 'PDOStatement'); $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.