Skip to content

Commit

Permalink
Added HAVING clause to query builder
Browse files Browse the repository at this point in the history
Also whitespace fixes

Issue #20
  • Loading branch information
jeroenrnl committed Jun 29, 2015
1 parent 274c930 commit 8065610
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
42 changes: 42 additions & 0 deletions UnitTests/PDOdatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,48 @@ public function testQueryWithClause() {

}

/**
* Test a SELECT query with a GROUP BY and HAVING clause
*/
public function testQueryWithGroupBy() {

$qry=new select("photos");
$qry->addGroupBy("album");
$having=new clause("album_id > :albid");
$qry->addParam(new param(":albid", 5, PDO::PARAM_INT));

$qry->having($having);

$sql=(string) $qry;
$exp_sql="SELECT * FROM zoph_photos GROUP BY album HAVING (album_id > :albid);";

$this->assertEquals($exp_sql, $sql);

unset($qry);
unset($clause);

$qry=new select("photos");
$qry->addGroupBy("album");
$where=new clause("photo_id > :photoid");
$having=new clause("album_id > :albid");
$qry->addParams(array(
new param(":photoid", 10, PDO::PARAM_INT),
new param(":albid", 5, PDO::PARAM_INT)
));

$qry->where($where);
$qry->having($having);

$sql=(string) $qry;
$exp_sql="SELECT * FROM zoph_photos WHERE (photo_id > :photoid) GROUP BY album HAVING (album_id > :albid);";

$this->assertEquals($exp_sql, $sql);

unset($qry);
unset($clause);

}

/**
* Test a SELECT query with a JOIN clause
*/
Expand Down
12 changes: 12 additions & 0 deletions php/classes/query.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ abstract class query {
protected $params=null;
/** @var string WHERE clause */
protected $clause=null;
/** @var string HAVING clause */
protected $having=null;
/** @var array ORDER clause */
protected $order=array();
/** @var array count for LIMIT clause */
Expand Down Expand Up @@ -164,6 +166,16 @@ public function where(clause $clause) {
return $this;
}

/**
* Add a HAVING clause to the query
* @param clause HAVING clause
* @return query return the query to enable chaining
*/
public function having(clause $clause) {
$this->having=$clause;
return $this;
}

/**
* Add ORDER BY clause to query
* @param string order to add
Expand Down
16 changes: 10 additions & 6 deletions php/classes/select.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Expand Down Expand Up @@ -52,15 +52,15 @@ public function join($table, $on, $jointype="INNER") {
}

$table=db::getPrefix() . $table;

if (!in_array($jointype, array("INNER", "LEFT", "RIGHT"))) {
throw new DatabaseException("Unknown JOIN type");
}
$this->joins[]=$jointype . " JOIN " . $table . " ON " . $on;
return $this;
}

/**
/**
* Add GROUP BY clause to query
* @param string GRPUP BY to add
* @return query return the query to enable chaining
Expand All @@ -70,7 +70,7 @@ public function addGroupBy($group) {
return $this;
}

/**
/**
* Get GROUP BY for query
* @return string GROUP clause
*/
Expand All @@ -86,7 +86,7 @@ private function getGroupBy() {
/**
* Execute query
*/
public function execute() {
public function execute() {
return db::query($this);
}

Expand Down Expand Up @@ -116,12 +116,16 @@ public function __toString() {
if($this->clause instanceof clause) {
$sql .= " WHERE " . $this->clause;
}

$groupby=trim($this->getGroupBy());
if(!empty($groupby)) {
$sql .= " " . $groupby;
}

if($this->having instanceof clause) {
$sql .= " HAVING " . $this->having;
}

$order=trim($this->getOrder());
if(!empty($order)) {
$sql .= " " . $order;
Expand Down

0 comments on commit 8065610

Please sign in to comment.