diff --git a/README.md b/README.md index 83d31af..1b15ca9 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Create a connection array or object similar to this: The parameters required depend on the database. ### Running Queries -Query uses the same interface as CodeIgniter's [Active Record class](http://codeigniter.com/user_guide/database/active_record.html). However, it does not implement the `count_all_results`, `distinct`, `having`, `or_having`, `insert_batch`, `update_batch`, or `count_all` methods. +Query uses the same interface as CodeIgniter's [Active Record class](http://codeigniter.com/user_guide/database/active_record.html). However, it does not implement the `count_all_results`, `having`, `or_having`, `insert_batch`, `update_batch`, or `count_all` methods. #### Retrieving Results diff --git a/classes/db_reg.php b/classes/db_reg.php index 8456370..c055563 100644 --- a/classes/db_reg.php +++ b/classes/db_reg.php @@ -54,6 +54,16 @@ class DB_Reg { // Set the current key in the registry self::$instance[$key] = new Query_Builder($db_params); } + + // -------------------------------------------------------------------------- + + /** + * Cleanup method + */ + public function __destruct() + { + unset(self::$instance); + } // -------------------------------------------------------------------------- diff --git a/classes/db_sql.php b/classes/db_sql.php index 45b6a60..30ea1e8 100644 --- a/classes/db_sql.php +++ b/classes/db_sql.php @@ -52,7 +52,7 @@ abstract class DB_SQL { */ public function distinct() { - return ' DISTINCT'; + return ' DISTINCT '; } // -------------------------------------------------------------------------- diff --git a/classes/query_builder.php b/classes/query_builder.php index cb92099..4cbb18a 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -19,7 +19,7 @@ class Query_Builder { // Compiled query component strings - private $select_string, + private $select_string = '', $from_string, $set_string, $order_string, @@ -171,7 +171,7 @@ class Query_Builder { } } - $this->select_string = implode(', ', $safe_array); + $this->select_string .= implode(', ', $safe_array); unset($safe_array); @@ -197,7 +197,7 @@ class Query_Builder { : $field; // Create the select string - $this->select_string = $this->sql->max()."({$field}) AS {$as} "; + $this->select_string .= $this->sql->max()."({$field}) AS {$as} "; return $this; } @@ -221,7 +221,7 @@ class Query_Builder { : $field; // Create the select string - $this->select_string = $this->sql->min()."({$field}) AS {$as} "; + $this->select_string .= $this->sql->min()."({$field}) AS {$as} "; return $this; } @@ -245,7 +245,7 @@ class Query_Builder { : $field; // Create the select string - $this->select_string = $this->sql->avg()."({$field}) AS {$as} "; + $this->select_string .= $this->sql->avg()."({$field}) AS {$as} "; return $this; } @@ -269,11 +269,26 @@ class Query_Builder { : $field; // Create the select string - $this->select_string = $this->sql->sum()."({$field}) AS {$as} "; + $this->select_string .= $this->sql->sum()."({$field}) AS {$as} "; return $this; } + // -------------------------------------------------------------------------- + + /** + * Adds the 'distinct' keyword to a query + * + * @return $this + */ + public function distinct() + { + // Prepend the keyword to the select string + $this->select_string = $this->sql->distinct() . $this->select_string; + + return $this; + } + // -------------------------------------------------------------------------- /** @@ -1119,11 +1134,15 @@ class Query_Builder { // Nothing query-generation related is safe! if ( ! is_callable($this->$name)) { - unset($this->$name); + $this->$name = NULL; } // Set values as an empty array $this->values = array(); + + // Set select string as an empty string, for proper handling + // of the 'distinct' keyword + $this->select_string = ''; } } @@ -1215,6 +1234,8 @@ class Query_Builder { break; } + + // echo $sql . '
'; return $sql; } diff --git a/tests/core/parent.php b/tests/core/db_qb_test.php similarity index 88% rename from tests/core/parent.php rename to tests/core/db_qb_test.php index d0e2f3c..2f8c5e0 100644 --- a/tests/core/parent.php +++ b/tests/core/db_qb_test.php @@ -12,46 +12,6 @@ // -------------------------------------------------------------------------- -/** - * Parent Database Test Class - */ -abstract class DBTest extends UnitTestCase { - - abstract function TestConnection(); - - function tearDown() - { - $this->db = NULL; - } - - function TestGetTables() - { - if (empty($this->db)) return; - - $tables = $this->db->get_tables(); - $this->assertTrue(is_array($tables)); - } - - function TestGetSystemTables() - { - if (empty($this->db)) return; - - $tables = $this->db->get_system_tables(); - - $this->assertTrue(is_array($tables)); - } - - function TestCreateTransaction() - { - if (empty($this->db)) return; - - $res = $this->db->beginTransaction(); - $this->assertTrue($res); - } -} - -// -------------------------------------------------------------------------- - /** * Query builder parent test class */ @@ -147,6 +107,17 @@ abstract class QBTest extends UnitTestCase { $this->assertIsA($query, 'PDOStatement'); } + function TestSelectDistinct() + { + if (empty($this->db)) return; + + $query = $this->db->select_sum('id', 'di') + ->distinct() + ->get('create_test'); + + $this->assertIsA($query, 'PDOStatement'); + } + function TestGetWhere() { if (empty($this->db)) return; @@ -319,4 +290,4 @@ abstract class QBTest extends UnitTestCase { } } -// End of parent.php \ No newline at end of file +// End of db_qb_test.php \ No newline at end of file diff --git a/tests/core/db_test.php b/tests/core/db_test.php new file mode 100644 index 0000000..3e698c2 --- /dev/null +++ b/tests/core/db_test.php @@ -0,0 +1,52 @@ +db = NULL; + } + + function TestGetTables() + { + if (empty($this->db)) return; + + $tables = $this->db->get_tables(); + $this->assertTrue(is_array($tables)); + } + + function TestGetSystemTables() + { + if (empty($this->db)) return; + + $tables = $this->db->get_system_tables(); + + $this->assertTrue(is_array($tables)); + } + + function TestCreateTransaction() + { + if (empty($this->db)) return; + + $res = $this->db->beginTransaction(); + $this->assertTrue($res); + } +} +// End of db_test.php \ No newline at end of file diff --git a/tests/databases/firebird/firebird-qb.php b/tests/databases/firebird/firebird-qb.php index 2c1f2f8..ee3bb86 100644 --- a/tests/databases/firebird/firebird-qb.php +++ b/tests/databases/firebird/firebird-qb.php @@ -116,6 +116,17 @@ class FirebirdQBTest extends QBTest { $this->assertIsA($query, 'Firebird_Result'); } + function TestSelectDistinct() + { + if (empty($this->db)) return; + + $query = $this->db->select_sum('id', 'di') + ->distinct() + ->get('create_test'); + + $this->assertIsA($query, 'Firebird_Result'); + } + function TestGetWhere() { if (empty($this->db)) return; diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index 9ec6109..607f1a4 100755 Binary files a/tests/db_files/FB_TEST_DB.FDB and b/tests/db_files/FB_TEST_DB.FDB differ diff --git a/tests/index.php b/tests/index.php index abd4ff0..c1af0fa 100644 --- a/tests/index.php +++ b/tests/index.php @@ -24,10 +24,13 @@ define('DS', DIRECTORY_SEPARATOR); require_once('simpletest/autorun.php'); // Include db classes -require_once(BASE_DIR.'autoload.php'); +require_once(BASE_DIR . 'autoload.php'); // Require base testing classes -array_map('do_include', glob(TEST_DIR . "/core/*.php")); +require_once(TEST_DIR . '/core/core.php'); +require_once(TEST_DIR . '/core/settings.php'); +require_once(TEST_DIR . '/core/db_test.php'); +require_once(TEST_DIR . '/core/db_qb_test.php'); // Include db tests // Load db classes based on capability