Added 'distinct' method to query builder
This commit is contained in:
parent
ebe1b1a647
commit
9b9ea5aeed
@ -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
|
||||
|
||||
|
@ -57,6 +57,16 @@ class DB_Reg {
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Cleanup method
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
unset(self::$instance);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Return exiting connections
|
||||
*
|
||||
|
@ -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,7 +269,22 @@ 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 = '';
|
||||
}
|
||||
}
|
||||
|
||||
@ -1216,6 +1235,8 @@ class Query_Builder {
|
||||
break;
|
||||
}
|
||||
|
||||
// echo $sql . '<br />';
|
||||
|
||||
return $sql;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
// End of db_qb_test.php
|
52
tests/core/db_test.php
Normal file
52
tests/core/db_test.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Query
|
||||
*
|
||||
* Free Query Builder / Database Abstraction Layer
|
||||
*
|
||||
* @author Timothy J. Warren
|
||||
* @copyright Copyright (c) 2012
|
||||
* @link https://github.com/aviat4ion/Query
|
||||
* @license http://philsturgeon.co.uk/code/dbad-license
|
||||
*/
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
// End of db_test.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;
|
||||
|
Binary file not shown.
@ -27,7 +27,10 @@ require_once('simpletest/autorun.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
|
||||
|
Loading…
Reference in New Issue
Block a user