diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index 73e8884..fa88a4f 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -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.'
'; + //echo $sql.'
'; return $sql; } diff --git a/tests/databases/firebird-qb.php b/tests/databases/firebird-qb.php index 929ad6d..25b17fb 100644 --- a/tests/databases/firebird-qb.php +++ b/tests/databases/firebird-qb.php @@ -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 '
Firebird Queries
'; } 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); + } } \ No newline at end of file diff --git a/tests/databases/sqlite-qb.php b/tests/databases/sqlite-qb.php index 898275f..f080edb 100644 --- a/tests/databases/sqlite-qb.php +++ b/tests/databases/sqlite-qb.php @@ -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 '
SQLite Queries
'; + } 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'); + } } \ 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 7531aad..776d51a 100755 Binary files a/tests/test_dbs/FB_TEST_DB.FDB and b/tests/test_dbs/FB_TEST_DB.FDB differ