Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Examples

$q->table('table')
->begin_and()
->begin_and()
->begin_and(false) // false so we don't add and before opening bracket
->and_where('col_1', 1)
->or_where('col_2', 2)
->end_and()
Expand Down
22 changes: 22 additions & 0 deletions source/db.interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

interface queryBuilderDbInterface{
/**
* escapes value
* @param $value
* @return mixed
*/
public function escape($value);

/**
* return quotes to be used in query
* @return mixed
*/
public function quote();

/**
* return field quotes to be used in query
* @return mixed
*/
public function field_quote();
}
22 changes: 22 additions & 0 deletions source/mysql.db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
require_once dirname(__FILE__) . '/db.interface.php';

class mysqlQueryBuilderDb implements queryBuilderDbInterface{

/** {@inheritDoc} */
public function escape($value){
return mysql_real_escape_string($value);
}

/** {@inheritDoc} */
public function quote()
{
return "'";
}

/** {@inheritDoc} */
public function field_quote()
{
return "`";
}
}
25 changes: 16 additions & 9 deletions source/query.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @property-read int $limit The LIMIT condition that is part of the query
* @property-read int $offset The OFFSET condition that is part of the query
*/
class query{
class queryBuilder{
/**
* Less than operator
* @var string
Expand Down Expand Up @@ -124,9 +124,9 @@ class query{
private $db = null;
/**
*
* @param db database class
* @param queryBuilderDbInterface database class
*/
public function __construct(&$db){
public function __construct($db){
$this->db = $db;
}
/**
Expand Down Expand Up @@ -264,22 +264,26 @@ public function cross_join($table, $conditions){
}
/**
* Push an open bracket into the where stack to group OR conditions
* @param bool $add_type_before
* @return query
*/
public function begin_or(){
public function begin_or($add_type_before = true){
$this->wheres[] = array(
'bracket'=>'OPEN',
'add_type_before'=>$add_type_before,
'type'=>'OR'
);
return $this;
}
/**
* Push an open bracket into the where stack to group AND conditions
* @param bool $add_type_before
* @return query
*/
public function begin_and(){
public function begin_and($add_type_before = true){
$this->wheres[] = array(
'bracket'=>'OPEN',
'add_type_before'=>$add_type_before,
'type'=>'AND'
);
return $this;
Expand Down Expand Up @@ -610,7 +614,6 @@ private function build_where_string(){
foreach($this->wheres as $w){
if($first){
$string = ' WHERE ';
$first = false;
} else {
if(!$bracket && !isset($w['bracket'])){
$string .= ' ' . $w['type'] . ' ';
Expand All @@ -620,6 +623,9 @@ private function build_where_string(){
}
if(isset($w['bracket'])){
if($w['bracket'] === 'OPEN'){
if($w['add_type_before'] && !$first){
$string .= ' ' . $w['type'] . ' ';
}
$string .= '( ';
$bracket = true;
} else {
Expand All @@ -628,11 +634,12 @@ private function build_where_string(){
} else {
$string .= $w['column'] . ' ' . $w['comparison']. ' ';
if($w['escape']){
$string .= db::QUOTE . $this->db->escape($w['value']) . db::QUOTE;
$string .= $this->db->quote() . $this->db->escape($w['value']) . $this->db->quote();
} else {
$string .= $w['value'];
}
}
$first = false;
}
return $string;
}
Expand All @@ -655,10 +662,10 @@ private function build_having_string(){
$string = '';
if(!empty($this->havings)){
$tmp = array_shift($this->havings);
$string .= ' HAVING ' . $tmp['column'] . ' ' . $tmp['comparison'] . ' ' . db::QUOTE . $this->db->escape($tmp['having']) . db::QUOTE;
$string .= ' HAVING ' . $tmp['column'] . ' ' . $tmp['comparison'] . ' ' . $this->db->quote() . $this->db->escape($tmp['having']) . $this->db->quote();
}
foreach($this->havings as $h){
$string .= ' '.$h['comparison_type'] . ' ' . $h['column'] . ' ' . $h['comparison'] . ' ' . db::QUOTE . $this->db->escape($h['having']) . db::QUOTE;
$string .= ' '.$h['comparison_type'] . ' ' . $h['column'] . ' ' . $h['comparison'] . ' ' . $this->db->quote() . $this->db->escape($h['having']) . $this->db->quote();
}
return $string;
}
Expand Down
20 changes: 16 additions & 4 deletions tests/source/db.mock.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
<?php
require_once dirname(__FILE__) . '/../../source/db.interface.php';

class db{
const QUOTE = "'";
const FIELD_QUOTE = "`";
class mockQueryBuilderDb implements queryBuilderDbInterface{

function escape($value){
/** {@inheritDoc} */
public function escape($value){
return $value;
}

/** {@inheritDoc} */
public function quote()
{
return "'";
}

/** {@inheritDoc} */
public function field_quote()
{
return "`";
}
}
19 changes: 10 additions & 9 deletions tests/source/queryTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__) . '/db.mock.php';
require_once dirname(__FILE__) . '/../../source/query.class.php';

Expand All @@ -20,8 +19,8 @@ class queryTest extends PHPUnit_Framework_TestCase {
* This method is called before a test is executed.
*/
protected function setUp() {
$db = new db();
$this->q = new query($db);
$db = new mockQueryBuilderDb();
$this->q = new queryBuilder($db);
}

/**
Expand Down Expand Up @@ -231,7 +230,8 @@ public function testBrackets(){
),
array(
'bracket'=>'OPEN',
'type'=>'AND'
'type'=>'AND',
'add_type_before' => true
),
array(
'column'=>'TRUE',
Expand All @@ -242,7 +242,8 @@ public function testBrackets(){
),
array(
'bracket'=>'OPEN',
'type'=>'OR'
'type'=>'OR',
'add_type_before' => true
),
array(
'column'=>'NULL',
Expand Down Expand Up @@ -321,7 +322,7 @@ public function testMultiWhere(){
public function testMultiWhereGrouping(){
$this->q->table('table')
->begin_and()
->begin_and()
->begin_and(false)
->and_where('col_1', 1)
->or_where('col_2', 2)
->end_and()
Expand Down Expand Up @@ -626,9 +627,9 @@ public function testDeleteDuplicateEntries(){
$this->q->delete_from('t1')
->table('tbl_name', 't1')
->table('tbl_name', 't2')
->where('t1.userID', 't2.userID', query::EQUAL, false)
->and_where('t1.eventID', 't2.eventID', query::EQUAL, false)
->and_where('t1.ueventID', 't2.ueventID',query::LESS_THAN, false);
->where('t1.userID', 't2.userID', queryBuilder::EQUAL, false)
->and_where('t1.eventID', 't2.eventID', queryBuilder::EQUAL, false)
->and_where('t1.ueventID', 't2.ueventID',queryBuilder::LESS_THAN, false);
$this->assertEquals($expected, $this->q->build_delete());
}

Expand Down