Skip to content

Commit

Permalink
Added 'distinct' method to query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
timw4mail committed Apr 13, 2012
1 parent ebe1b1a commit 9b9ea5a
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions classes/db_reg.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ private function __construct($key)
// Set the current key in the registry
self::$instance[$key] = new Query_Builder($db_params);
}

// --------------------------------------------------------------------------

/**
* Cleanup method
*/
public function __destruct()
{
unset(self::$instance);
}

// --------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion classes/db_sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function min()
*/
public function distinct()
{
return ' DISTINCT';
return ' DISTINCT ';
}

// --------------------------------------------------------------------------
Expand Down
35 changes: 28 additions & 7 deletions classes/query_builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class Query_Builder {

// Compiled query component strings
private $select_string,
private $select_string = '',
$from_string,
$set_string,
$order_string,
Expand Down Expand Up @@ -171,7 +171,7 @@ public function select($fields)
}
}

$this->select_string = implode(', ', $safe_array);
$this->select_string .= implode(', ', $safe_array);

unset($safe_array);

Expand All @@ -197,7 +197,7 @@ public function select_max($field, $as=FALSE)
: $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;
}
Expand All @@ -221,7 +221,7 @@ public function select_min($field, $as=FALSE)
: $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;
}
Expand All @@ -245,7 +245,7 @@ public function select_avg($field, $as=FALSE)
: $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;
}
Expand All @@ -269,11 +269,26 @@ public function select_sum($field, $as=FALSE)
: $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;
}

// --------------------------------------------------------------------------

/**
Expand Down Expand Up @@ -1119,11 +1134,15 @@ private function _reset()
// 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 = '';
}
}

Expand Down Expand Up @@ -1215,6 +1234,8 @@ private function _compile($type='', $table="")

break;
}

// echo $sql . '<br />';

return $sql;
}
Expand Down
53 changes: 12 additions & 41 deletions tests/core/parent.php → tests/core/db_qb_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -147,6 +107,17 @@ function TestSelectSum()
$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;
Expand Down Expand Up @@ -319,4 +290,4 @@ function TestGetViews()
}
}

// End of parent.php
// End of db_qb_test.php
52 changes: 52 additions & 0 deletions tests/core/db_test.php
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions tests/databases/firebird/firebird-qb.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ function TestSelectSum()
$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;
Expand Down
Binary file modified tests/db_files/FB_TEST_DB.FDB
Binary file not shown.
7 changes: 5 additions & 2 deletions tests/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
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
Expand Down

0 comments on commit 9b9ea5a

Please sign in to comment.