Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implemented 'having' and 'or_having' methods

  • Loading branch information...
commit bf162323f03aa819a71649aa5758a8255bd3534e 1 parent 052a155
Timothy Warren authored
2  README.md
Source Rendered
@@ -45,7 +45,7 @@ Create a connection array or object similar to this:
45 45 The parameters required depend on the database.
46 46
47 47 ### Running Queries
48   -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.
  48 +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.
49 49
50 50 #### Retrieving Results
51 51
66 classes/query_builder.php
@@ -52,6 +52,9 @@ class Query_Builder {
52 52 // 'string' => 'k=?'
53 53 // )
54 54 private $query_map;
  55 +
  56 + // Map for having clause
  57 + private $having_map;
55 58
56 59 // Convenience property for connection management
57 60 public $conn_name = "";
@@ -493,7 +496,28 @@ public function or_not_like($field, $val, $pos='both')
493 496 */
494 497 public function having($key, $val=array())
495 498 {
496   - // @todo Implement having
  499 + $where = $this->_where($key, $val);
  500 +
  501 + // Create key/value placeholders
  502 + foreach($where as $f => $val)
  503 + {
  504 + // Split each key by spaces, in case there
  505 + // is an operator such as >, <, !=, etc.
  506 + $f_array = explode(' ', trim($f));
  507 +
  508 + $item = $this->quote_ident($f_array[0]);
  509 +
  510 + // Simple key value, or an operator
  511 + $item .= (count($f_array) === 1) ? '= ?' : " {$f_array[1]} ?";
  512 +
  513 + // Put in the query map for select statements
  514 + $this->having_map[] = array(
  515 + 'conjunction' => ( ! empty($this->having_map)) ? ' AND ' : ' HAVING ',
  516 + 'string' => $item
  517 + );
  518 + }
  519 +
  520 + return $this;
497 521 }
498 522
499 523 // --------------------------------------------------------------------------
@@ -507,7 +531,28 @@ public function having($key, $val=array())
507 531 */
508 532 public function or_having($key, $val=array())
509 533 {
510   - // @todo Implement or_having
  534 + $where = $this->_where($key, $val);
  535 +
  536 + // Create key/value placeholders
  537 + foreach($where as $f => $val)
  538 + {
  539 + // Split each key by spaces, in case there
  540 + // is an operator such as >, <, !=, etc.
  541 + $f_array = explode(' ', trim($f));
  542 +
  543 + $item = $this->quote_ident($f_array[0]);
  544 +
  545 + // Simple key value, or an operator
  546 + $item .= (count($f_array) === 1) ? '= ?' : " {$f_array[1]} ?";
  547 +
  548 + // Put in the query map for select statements
  549 + $this->having_map[] = array(
  550 + 'conjunction' => ( ! empty($this->having_map)) ? ' OR ' : ' HAVING ',
  551 + 'string' => $item
  552 + );
  553 + }
  554 +
  555 + return $this;
511 556 }
512 557
513 558 // --------------------------------------------------------------------------
@@ -515,7 +560,7 @@ public function or_having($key, $val=array())
515 560 // --------------------------------------------------------------------------
516 561
517 562 /**
518   - * Do all the repeditive stuff for where type methods
  563 + * Do all the repeditive stuff for where/having type methods
519 564 *
520 565 * @param mixed $key
521 566 * @param mixed $val
@@ -569,7 +614,7 @@ public function where($key, $val=array())
569 614 $item = $this->quote_ident($f_array[0]);
570 615
571 616 // Simple key value, or an operator
572   - $item .= (count($f_array === 1)) ? '= ?' : " {$f_array[1]} ?";
  617 + $item .= (count($f_array) === 1) ? '= ?' : " {$f_array[1]} ?";
573 618
574 619 // Put in the query map for select statements
575 620 $this->query_map[] = array(
@@ -997,7 +1042,7 @@ public function get($table='', $limit=FALSE, $offset=FALSE)
997 1042 $sql = $this->_compile();
998 1043
999 1044 // Do prepared statements for anything involving a "where" clause
1000   - if ( ! empty($this->query_map))
  1045 + if ( ! empty($this->query_map) || ! empty($this->having_map))
1001 1046 {
1002 1047 $result = $this->prepare_execute($sql, $this->values);
1003 1048 }
@@ -1218,7 +1263,7 @@ private function _reset()
1218 1263 $this->$name = NULL;
1219 1264 }
1220 1265
1221   - // Set values as an empty array
  1266 + // Set empty arrays
1222 1267 $this->values = array();
1223 1268
1224 1269 // Set select string as an empty string, for proper handling
@@ -1266,6 +1311,15 @@ private function _compile($type='', $table="")
1266 1311 {
1267 1312 $sql .= $this->group_string;
1268 1313 }
  1314 +
  1315 + // Set the having string
  1316 + if ( ! empty($this->having_map))
  1317 + {
  1318 + foreach($this->having_map as $h)
  1319 + {
  1320 + $sql .= $h['conjunction'] . $h['string'];
  1321 + }
  1322 + }
1269 1323
1270 1324 // Set the order_by string
1271 1325 if ( ! empty($this->order_string))
28 tests/core/db_qb_test.php
@@ -55,6 +55,34 @@ function TestGetWhere()
55 55 $this->assertIsA($query, 'PDOStatement');
56 56 }
57 57
  58 + function TestHaving()
  59 + {
  60 + if (empty($this->db)) return;
  61 +
  62 + $query = $this->db->select('id')
  63 + ->from('create_test')
  64 + ->group_by('id')
  65 + ->having(array('id >' => 1))
  66 + ->having('id !=', 3)
  67 + ->get();
  68 +
  69 + $this->assertIsA($query, 'PDOStatement');
  70 + }
  71 +
  72 + function TestOrHaving()
  73 + {
  74 + if (empty($this->db)) return;
  75 +
  76 + $query = $this->db->select('id')
  77 + ->from('create_test')
  78 + ->group_by('id')
  79 + ->having(array('id >' => 1))
  80 + ->or_having('id !=', 3)
  81 + ->get();
  82 +
  83 + $this->assertIsA($query, 'PDOStatement');
  84 + }
  85 +
58 86 function TestGetViews()
59 87 {
60 88 if (empty($this->db)) return;
28 tests/databases/firebird/firebird-qb.php
@@ -56,6 +56,34 @@ function TestGetLimitSkip()
56 56 $this->assertIsA($query, 'Firebird_Result');
57 57 }
58 58
  59 + function TestHaving()
  60 + {
  61 + if (empty($this->db)) return;
  62 +
  63 + $query = $this->db->select('id')
  64 + ->from('create_test')
  65 + ->group_by('id')
  66 + ->having(array('id >' => 1))
  67 + ->having('id !=', 3)
  68 + ->get();
  69 +
  70 + $this->assertIsA($query, 'Firebird_Result');
  71 + }
  72 +
  73 + function TestOrHaving()
  74 + {
  75 + if (empty($this->db)) return;
  76 +
  77 + $query = $this->db->select('id')
  78 + ->from('create_test')
  79 + ->group_by('id')
  80 + ->having(array('id >' => 1))
  81 + ->or_having('id !=', 3)
  82 + ->get();
  83 +
  84 + $this->assertIsA($query, 'Firebird_Result');
  85 + }
  86 +
59 87 function TestSelectWhereGet()
60 88 {
61 89 $query = $this->db->select('id, key as k, val')
BIN  tests/db_files/FB_TEST_DB.FDB
Binary file not shown

0 comments on commit bf16232

Please sign in to comment.
Something went wrong with that request. Please try again.