From 893bd6c304d28a864400b687516a6686a9bc9f76 Mon Sep 17 00:00:00 2001 From: Hasnat Ullah Date: Fri, 11 Aug 2017 17:39:44 +0100 Subject: [PATCH 1/2] Add types before brackets --- README.md | 2 +- source/query.class.php | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 04a68f5..daca04f 100644 --- a/README.md +++ b/README.md @@ -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() diff --git a/source/query.class.php b/source/query.class.php index dc128d9..744ba08 100644 --- a/source/query.class.php +++ b/source/query.class.php @@ -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; @@ -620,7 +624,10 @@ private function build_where_string(){ } if(isset($w['bracket'])){ if($w['bracket'] === 'OPEN'){ - $string .= '( '; + if($w['add_type_before'] && !$first){ + $string .= ' ' . $w['type'] . ' '; + } + $string .= ' ( '; $bracket = true; } else { $string .= ' )'; From 46df4f28cfa7cb97f2fe63a097a3255c307e1f05 Mon Sep 17 00:00:00 2001 From: Hasnat Ullah Date: Tue, 15 Aug 2017 10:34:00 +0100 Subject: [PATCH 2/2] Not depend on codeigniter db object, add interface for mysql --- source/db.interface.php | 22 ++++++++++++++++++++++ source/mysql.db.php | 22 ++++++++++++++++++++++ source/query.class.php | 16 ++++++++-------- tests/source/db.mock.php | 20 ++++++++++++++++---- tests/source/queryTest.php | 19 ++++++++++--------- 5 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 source/db.interface.php create mode 100644 source/mysql.db.php diff --git a/source/db.interface.php b/source/db.interface.php new file mode 100644 index 0000000..c9fdefd --- /dev/null +++ b/source/db.interface.php @@ -0,0 +1,22 @@ +db = $db; } /** @@ -614,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'] . ' '; @@ -627,7 +626,7 @@ private function build_where_string(){ if($w['add_type_before'] && !$first){ $string .= ' ' . $w['type'] . ' '; } - $string .= ' ( '; + $string .= '( '; $bracket = true; } else { $string .= ' )'; @@ -635,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; } @@ -662,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; } diff --git a/tests/source/db.mock.php b/tests/source/db.mock.php index f2408fd..8a82c8c 100644 --- a/tests/source/db.mock.php +++ b/tests/source/db.mock.php @@ -1,10 +1,22 @@ q = new query($db); + $db = new mockQueryBuilderDb(); + $this->q = new queryBuilder($db); } /** @@ -231,7 +230,8 @@ public function testBrackets(){ ), array( 'bracket'=>'OPEN', - 'type'=>'AND' + 'type'=>'AND', + 'add_type_before' => true ), array( 'column'=>'TRUE', @@ -242,7 +242,8 @@ public function testBrackets(){ ), array( 'bracket'=>'OPEN', - 'type'=>'OR' + 'type'=>'OR', + 'add_type_before' => true ), array( 'column'=>'NULL', @@ -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() @@ -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()); }