Added 'distinct' method to query builder

This commit is contained in:
Timothy Warren 2012-04-13 16:50:05 -04:00
parent ebe1b1a647
commit 9b9ea5aeed
9 changed files with 120 additions and 52 deletions

View File

@ -45,7 +45,7 @@ Create a connection array or object similar to this:
The parameters required depend on the database. The parameters required depend on the database.
### Running Queries ### 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 #### Retrieving Results

View File

@ -54,6 +54,16 @@ class DB_Reg {
// Set the current key in the registry // Set the current key in the registry
self::$instance[$key] = new Query_Builder($db_params); self::$instance[$key] = new Query_Builder($db_params);
} }
// --------------------------------------------------------------------------
/**
* Cleanup method
*/
public function __destruct()
{
unset(self::$instance);
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -52,7 +52,7 @@ abstract class DB_SQL {
*/ */
public function distinct() public function distinct()
{ {
return ' DISTINCT'; return ' DISTINCT ';
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -19,7 +19,7 @@
class Query_Builder { class Query_Builder {
// Compiled query component strings // Compiled query component strings
private $select_string, private $select_string = '',
$from_string, $from_string,
$set_string, $set_string,
$order_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); unset($safe_array);
@ -197,7 +197,7 @@ class Query_Builder {
: $field; : $field;
// Create the select string // Create the select string
$this->select_string = $this->sql->max()."({$field}) AS {$as} "; $this->select_string .= $this->sql->max()."({$field}) AS {$as} ";
return $this; return $this;
} }
@ -221,7 +221,7 @@ class Query_Builder {
: $field; : $field;
// Create the select string // Create the select string
$this->select_string = $this->sql->min()."({$field}) AS {$as} "; $this->select_string .= $this->sql->min()."({$field}) AS {$as} ";
return $this; return $this;
} }
@ -245,7 +245,7 @@ class Query_Builder {
: $field; : $field;
// Create the select string // Create the select string
$this->select_string = $this->sql->avg()."({$field}) AS {$as} "; $this->select_string .= $this->sql->avg()."({$field}) AS {$as} ";
return $this; return $this;
} }
@ -269,11 +269,26 @@ class Query_Builder {
: $field; : $field;
// Create the select string // Create the select string
$this->select_string = $this->sql->sum()."({$field}) AS {$as} "; $this->select_string .= $this->sql->sum()."({$field}) AS {$as} ";
return $this; 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! // Nothing query-generation related is safe!
if ( ! is_callable($this->$name)) if ( ! is_callable($this->$name))
{ {
unset($this->$name); $this->$name = NULL;
} }
// Set values as an empty array // Set values as an empty array
$this->values = 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; break;
} }
// echo $sql . '<br />';
return $sql; return $sql;
} }

View File

@ -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 * Query builder parent test class
*/ */
@ -147,6 +107,17 @@ abstract class QBTest extends UnitTestCase {
$this->assertIsA($query, 'PDOStatement'); $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() function TestGetWhere()
{ {
if (empty($this->db)) return; 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
View 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

View File

@ -116,6 +116,17 @@ class FirebirdQBTest extends QBTest {
$this->assertIsA($query, 'Firebird_Result'); $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() function TestGetWhere()
{ {
if (empty($this->db)) return; if (empty($this->db)) return;

Binary file not shown.

View File

@ -24,10 +24,13 @@ define('DS', DIRECTORY_SEPARATOR);
require_once('simpletest/autorun.php'); require_once('simpletest/autorun.php');
// Include db classes // Include db classes
require_once(BASE_DIR.'autoload.php'); require_once(BASE_DIR . 'autoload.php');
// Require base testing classes // 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 // Include db tests
// Load db classes based on capability // Load db classes based on capability