Implemented 'having' and 'or_having' methods
This commit is contained in:
parent
052a155df5
commit
bf162323f0
@ -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 `having`, `or_having`, `insert_batch` or `update_batch` 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 `insert_batch` or `update_batch` methods.
|
||||||
|
|
||||||
#### Retrieving Results
|
#### Retrieving Results
|
||||||
|
|
||||||
|
@ -52,6 +52,9 @@ class Query_Builder {
|
|||||||
// 'string' => 'k=?'
|
// 'string' => 'k=?'
|
||||||
// )
|
// )
|
||||||
private $query_map;
|
private $query_map;
|
||||||
|
|
||||||
|
// Map for having clause
|
||||||
|
private $having_map;
|
||||||
|
|
||||||
// Convenience property for connection management
|
// Convenience property for connection management
|
||||||
public $conn_name = "";
|
public $conn_name = "";
|
||||||
@ -493,7 +496,28 @@ class Query_Builder {
|
|||||||
*/
|
*/
|
||||||
public function having($key, $val=array())
|
public function having($key, $val=array())
|
||||||
{
|
{
|
||||||
// @todo Implement having
|
$where = $this->_where($key, $val);
|
||||||
|
|
||||||
|
// Create key/value placeholders
|
||||||
|
foreach($where as $f => $val)
|
||||||
|
{
|
||||||
|
// Split each key by spaces, in case there
|
||||||
|
// is an operator such as >, <, !=, etc.
|
||||||
|
$f_array = explode(' ', trim($f));
|
||||||
|
|
||||||
|
$item = $this->quote_ident($f_array[0]);
|
||||||
|
|
||||||
|
// Simple key value, or an operator
|
||||||
|
$item .= (count($f_array) === 1) ? '= ?' : " {$f_array[1]} ?";
|
||||||
|
|
||||||
|
// Put in the query map for select statements
|
||||||
|
$this->having_map[] = array(
|
||||||
|
'conjunction' => ( ! empty($this->having_map)) ? ' AND ' : ' HAVING ',
|
||||||
|
'string' => $item
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
@ -507,7 +531,28 @@ class Query_Builder {
|
|||||||
*/
|
*/
|
||||||
public function or_having($key, $val=array())
|
public function or_having($key, $val=array())
|
||||||
{
|
{
|
||||||
// @todo Implement or_having
|
$where = $this->_where($key, $val);
|
||||||
|
|
||||||
|
// Create key/value placeholders
|
||||||
|
foreach($where as $f => $val)
|
||||||
|
{
|
||||||
|
// Split each key by spaces, in case there
|
||||||
|
// is an operator such as >, <, !=, etc.
|
||||||
|
$f_array = explode(' ', trim($f));
|
||||||
|
|
||||||
|
$item = $this->quote_ident($f_array[0]);
|
||||||
|
|
||||||
|
// Simple key value, or an operator
|
||||||
|
$item .= (count($f_array) === 1) ? '= ?' : " {$f_array[1]} ?";
|
||||||
|
|
||||||
|
// Put in the query map for select statements
|
||||||
|
$this->having_map[] = array(
|
||||||
|
'conjunction' => ( ! empty($this->having_map)) ? ' OR ' : ' HAVING ',
|
||||||
|
'string' => $item
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
@ -515,7 +560,7 @@ class Query_Builder {
|
|||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Do all the repeditive stuff for where type methods
|
* Do all the repeditive stuff for where/having type methods
|
||||||
*
|
*
|
||||||
* @param mixed $key
|
* @param mixed $key
|
||||||
* @param mixed $val
|
* @param mixed $val
|
||||||
@ -569,7 +614,7 @@ class Query_Builder {
|
|||||||
$item = $this->quote_ident($f_array[0]);
|
$item = $this->quote_ident($f_array[0]);
|
||||||
|
|
||||||
// Simple key value, or an operator
|
// Simple key value, or an operator
|
||||||
$item .= (count($f_array === 1)) ? '= ?' : " {$f_array[1]} ?";
|
$item .= (count($f_array) === 1) ? '= ?' : " {$f_array[1]} ?";
|
||||||
|
|
||||||
// Put in the query map for select statements
|
// Put in the query map for select statements
|
||||||
$this->query_map[] = array(
|
$this->query_map[] = array(
|
||||||
@ -997,7 +1042,7 @@ class Query_Builder {
|
|||||||
$sql = $this->_compile();
|
$sql = $this->_compile();
|
||||||
|
|
||||||
// Do prepared statements for anything involving a "where" clause
|
// Do prepared statements for anything involving a "where" clause
|
||||||
if ( ! empty($this->query_map))
|
if ( ! empty($this->query_map) || ! empty($this->having_map))
|
||||||
{
|
{
|
||||||
$result = $this->prepare_execute($sql, $this->values);
|
$result = $this->prepare_execute($sql, $this->values);
|
||||||
}
|
}
|
||||||
@ -1218,7 +1263,7 @@ class Query_Builder {
|
|||||||
$this->$name = NULL;
|
$this->$name = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set values as an empty array
|
// Set empty arrays
|
||||||
$this->values = array();
|
$this->values = array();
|
||||||
|
|
||||||
// Set select string as an empty string, for proper handling
|
// Set select string as an empty string, for proper handling
|
||||||
@ -1266,6 +1311,15 @@ class Query_Builder {
|
|||||||
{
|
{
|
||||||
$sql .= $this->group_string;
|
$sql .= $this->group_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the having string
|
||||||
|
if ( ! empty($this->having_map))
|
||||||
|
{
|
||||||
|
foreach($this->having_map as $h)
|
||||||
|
{
|
||||||
|
$sql .= $h['conjunction'] . $h['string'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set the order_by string
|
// Set the order_by string
|
||||||
if ( ! empty($this->order_string))
|
if ( ! empty($this->order_string))
|
||||||
|
@ -55,6 +55,34 @@ abstract class QBTest extends UnitTestCase {
|
|||||||
$this->assertIsA($query, 'PDOStatement');
|
$this->assertIsA($query, 'PDOStatement');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function TestHaving()
|
||||||
|
{
|
||||||
|
if (empty($this->db)) return;
|
||||||
|
|
||||||
|
$query = $this->db->select('id')
|
||||||
|
->from('create_test')
|
||||||
|
->group_by('id')
|
||||||
|
->having(array('id >' => 1))
|
||||||
|
->having('id !=', 3)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$this->assertIsA($query, 'PDOStatement');
|
||||||
|
}
|
||||||
|
|
||||||
|
function TestOrHaving()
|
||||||
|
{
|
||||||
|
if (empty($this->db)) return;
|
||||||
|
|
||||||
|
$query = $this->db->select('id')
|
||||||
|
->from('create_test')
|
||||||
|
->group_by('id')
|
||||||
|
->having(array('id >' => 1))
|
||||||
|
->or_having('id !=', 3)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$this->assertIsA($query, 'PDOStatement');
|
||||||
|
}
|
||||||
|
|
||||||
function TestGetViews()
|
function TestGetViews()
|
||||||
{
|
{
|
||||||
if (empty($this->db)) return;
|
if (empty($this->db)) return;
|
||||||
|
@ -56,6 +56,34 @@ class FirebirdQBTest extends QBTest {
|
|||||||
$this->assertIsA($query, 'Firebird_Result');
|
$this->assertIsA($query, 'Firebird_Result');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function TestHaving()
|
||||||
|
{
|
||||||
|
if (empty($this->db)) return;
|
||||||
|
|
||||||
|
$query = $this->db->select('id')
|
||||||
|
->from('create_test')
|
||||||
|
->group_by('id')
|
||||||
|
->having(array('id >' => 1))
|
||||||
|
->having('id !=', 3)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$this->assertIsA($query, 'Firebird_Result');
|
||||||
|
}
|
||||||
|
|
||||||
|
function TestOrHaving()
|
||||||
|
{
|
||||||
|
if (empty($this->db)) return;
|
||||||
|
|
||||||
|
$query = $this->db->select('id')
|
||||||
|
->from('create_test')
|
||||||
|
->group_by('id')
|
||||||
|
->having(array('id >' => 1))
|
||||||
|
->or_having('id !=', 3)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$this->assertIsA($query, 'Firebird_Result');
|
||||||
|
}
|
||||||
|
|
||||||
function TestSelectWhereGet()
|
function TestSelectWhereGet()
|
||||||
{
|
{
|
||||||
$query = $this->db->select('id, key as k, val')
|
$query = $this->db->select('id, key as k, val')
|
||||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user