Skip to content

Commit b524075

Browse files
committed
[FEATURE] Added better overwrite of where/null etc if enabled
1 parent a61bfa0 commit b524075

4 files changed

Lines changed: 92 additions & 24 deletions

File tree

src/Pecee/Pixie/QueryBuilder/Adapters/BaseAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected function buildCriteria(array $statements, bool $bindValues = true): ar
118118
$criteria = [];
119119
$bindings = [[]];
120120

121-
foreach ($statements as $i => $statement) {
121+
foreach (array_values($statements) as $i => $statement) {
122122

123123
if ($i === 0 && isset($statement['condition'])) {
124124
$criteria[] = $statement['condition'];

src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,50 @@ public function setFetchMode($parameters = null): self
139139
return $this;
140140
}
141141

142+
/**
143+
* Removes existing statement if overwrite is set to enabled.
144+
*
145+
* @param string $type Statement type
146+
* @param string $key Key to search for
147+
* @param mixed $value Value to match
148+
*/
149+
protected function removeExistingStatement(string $type, string $key, $value): void
150+
{
151+
if ($this->overwriteEnabled === false || isset($this->statements[$type]) === false) {
152+
return;
153+
}
154+
155+
foreach ($this->statements[$type] as $index => $statement) {
156+
if ($statement[$key] instanceof \Closure) {
157+
$nestedCriteria = new QueryBuilderHandler($this->connection);
158+
159+
$statement[$key]($nestedCriteria);
160+
161+
if (isset($nestedCriteria->getStatements()[$type])) {
162+
foreach ($nestedCriteria->getStatements()[$type] as $subStatement) {
163+
if ($subStatement[$key] === $value) {
164+
unset($this->statements[$type][$index]);
165+
166+
return;
167+
}
168+
}
169+
}
170+
171+
}
172+
if ($statement[$key] === $value) {
173+
unset($this->statements[$type][$index]);
174+
return;
175+
}
176+
}
177+
}
178+
142179
/**
143180
* Get count of all the rows for the current query
144181
*
145182
* @param string $field
146183
*
147-
* @throws Exception
148184
* @return integer
185+
* @throws Exception
149186
*/
150187
public function count(string $field = '*'): int
151188
{
@@ -157,8 +194,8 @@ public function count(string $field = '*'): int
157194
*
158195
* @param string $type
159196
* @param string $field
160-
* @throws Exception
161197
* @return float
198+
* @throws Exception
162199
*/
163200
protected function aggregate(string $type, string $field = '*'): float
164201
{
@@ -209,8 +246,8 @@ public function getTable(): ?string
209246
/**
210247
* Returns the first row
211248
*
212-
* @throws Exception
213249
* @return \stdClass|string|null
250+
* @throws Exception
214251
*/
215252
public function first()
216253
{
@@ -222,8 +259,8 @@ public function first()
222259
/**
223260
* Get all rows
224261
*
225-
* @throws Exception
226262
* @return array
263+
* @throws Exception
227264
*/
228265
public function get(): array
229266
{
@@ -639,8 +676,8 @@ public function alias(string $alias, ?string $table = null): self
639676
/**
640677
* Creates and returns new query.
641678
*
642-
* @throws Exception
643679
* @return static
680+
* @throws Exception
644681
*/
645682
public function newQuery(): self
646683
{
@@ -654,8 +691,8 @@ public function newQuery(): self
654691
* @param QueryBuilderHandler $queryBuilder
655692
* @param string|null $alias
656693
*
657-
* @throws Exception
658694
* @return Raw
695+
* @throws Exception
659696
*/
660697
public function subQuery(QueryBuilderHandler $queryBuilder, $alias = null): Raw
661698
{
@@ -694,8 +731,8 @@ public function raw(string $value, $bindings = null): Raw
694731
*
695732
* @param string $field
696733
*
697-
* @throws Exception
698734
* @return float
735+
* @throws Exception
699736
*/
700737
public function sum(string $field): float
701738
{
@@ -707,8 +744,8 @@ public function sum(string $field): float
707744
*
708745
* @param string $field
709746
*
710-
* @throws Exception
711747
* @return float
748+
* @throws Exception
712749
*/
713750
public function average(string $field): float
714751
{
@@ -720,8 +757,8 @@ public function average(string $field): float
720757
*
721758
* @param string $field
722759
*
723-
* @throws Exception
724760
* @return float
761+
* @throws Exception
725762
*/
726763
public function min(string $field): float
727764
{
@@ -733,8 +770,8 @@ public function min(string $field): float
733770
*
734771
* @param string $field
735772
*
736-
* @throws Exception
737773
* @return float
774+
* @throws Exception
738775
*/
739776
public function max(string $field): float
740777
{
@@ -744,9 +781,9 @@ public function max(string $field): float
744781
/**
745782
* Forms delete on the current query.
746783
*
747-
* @var array|null $columns
748784
* @return \PDOStatement
749785
* @throws Exception
786+
* @var array|null $columns
750787
*/
751788
public function delete(array $columns = null): \PDOStatement
752789
{
@@ -771,8 +808,8 @@ public function delete(array $columns = null): \PDOStatement
771808
* @param string|int|float $value
772809
* @param string $fieldName
773810
*
774-
* @throws Exception
775811
* @return \stdClass|string|null
812+
* @throws Exception
776813
*/
777814
public function find($value, string $fieldName = 'id')
778815
{
@@ -816,6 +853,7 @@ public function where($key, $operator = null, $value = null): self
816853
protected function whereHandler($key, ?string $operator = null, $value = null, $joiner = 'AND'): self
817854
{
818855
$key = $this->addTablePrefix($key);
856+
$this->removeExistingStatement('wheres', 'key', $key);
819857
$this->statements['wheres'][] = compact('key', 'operator', 'value', 'joiner');
820858

821859
return $this;
@@ -827,8 +865,8 @@ protected function whereHandler($key, ?string $operator = null, $value = null, $
827865
* @param string $fieldName
828866
* @param string|int|float $value
829867
*
830-
* @throws Exception
831868
* @return array
869+
* @throws Exception
832870
*/
833871
public function findAll(string $fieldName, $value): array
834872
{
@@ -861,6 +899,10 @@ public function groupBy($field): self
861899
$field = $this->addTablePrefix($field);
862900
}
863901

902+
if ($this->overwriteEnabled === true) {
903+
$this->statements['groupBys'] = [];
904+
}
905+
864906
if (\is_array($field) === true) {
865907
$this->statements['groupBys'] = array_merge($this->statements['groupBys'], $field);
866908
} else {
@@ -938,6 +980,8 @@ public function join($table, $key = null, $operator = null, $value = null, $type
938980

939981
$table = $this->addTablePrefix($table, false);
940982

983+
$this->removeExistingStatement('joins', 'table', $table);
984+
941985
// Get the criteria only query from the joinBuilder object
942986
$this->statements['joins'][] = [
943987
'type' => $type,
@@ -953,8 +997,8 @@ public function join($table, $key = null, $operator = null, $value = null, $type
953997
*
954998
* @param array $data
955999
*
956-
* @throws Exception
9571000
* @return array|string
1001+
* @throws Exception
9581002
*/
9591003
public function insertIgnore(array $data)
9601004
{
@@ -967,8 +1011,8 @@ public function insertIgnore(array $data)
9671011
* @param array $data
9681012
* @param string $type
9691013
*
970-
* @throws Exception
9711014
* @return array|string|null
1015+
* @throws Exception
9721016
*/
9731017
private function doInsert(array $data, string $type)
9741018
{
@@ -1023,8 +1067,8 @@ private function doInsert(array $data, string $type)
10231067
*
10241068
* @param \Closure $callback
10251069
*
1026-
* @throws Exception
10271070
* @return Transaction
1071+
* @throws Exception
10281072
*/
10291073
public function transaction(\Closure $callback): Transaction
10301074
{
@@ -1079,6 +1123,8 @@ public function joinUsing($table, $fields, $joinType = ''): self
10791123

10801124
$table = $this->addTablePrefix($table, false);
10811125

1126+
$this->removeExistingStatement('joins', 'table', $table);
1127+
10821128
$this->statements['joins'][] = [
10831129
'type' => $joinType,
10841130
'table' => $table,
@@ -1159,6 +1205,7 @@ public function orHaving($key, $operator, $value): self
11591205
public function having($key, $operator, $value, $joiner = 'AND'): self
11601206
{
11611207
$key = $this->addTablePrefix($key);
1208+
$this->removeExistingStatement('havings', 'key', $key);
11621209
$this->statements['havings'][] = compact('key', 'operator', 'value', 'joiner');
11631210

11641211
return $this;
@@ -1267,10 +1314,11 @@ public function orWhereNotNull($key): self
12671314
*/
12681315
protected function whereNullHandler($key, string $prefix = '', string $operator = ''): self
12691316
{
1270-
$key = $this->adapterInstance->wrapSanitizer($this->addTablePrefix($key));
1271-
$prefix = ($prefix !== '') ? $prefix . ' ' : $prefix;
1317+
$this->removeExistingStatement('wheres', 'key', $key);
12721318

1273-
return $this->{$operator . 'Where'}($this->raw("$key IS {$prefix}NULL"));
1319+
$prefix = 'IS' . (($prefix !== '') ? ' ' . $prefix : '');
1320+
1321+
return $this->{$operator . 'Where'}($key, $prefix, $this->raw('NULL'));
12741322
}
12751323

12761324
/**
@@ -1312,6 +1360,7 @@ public function orderBy($fields, string $direction = 'ASC'): self
13121360
$field = $this->addTablePrefix($field);
13131361
}
13141362

1363+
$this->removeExistingStatement('orderBys', 'field', $field);
13151364
$this->statements['orderBys'][] = compact('field', 'type');
13161365
}
13171366

@@ -1377,8 +1426,8 @@ public function removeEvent(string $name, ?string $table = null): void
13771426
*
13781427
* @param array $data
13791428
*
1380-
* @throws Exception
13811429
* @return array|string
1430+
* @throws Exception
13821431
*/
13831432
public function replace(array $data)
13841433
{
@@ -1489,8 +1538,8 @@ public function updateOrInsert(array $data)
14891538
*
14901539
* @param array $data
14911540
*
1492-
* @throws Exception
14931541
* @return \PDOStatement
1542+
* @throws Exception
14941543
*/
14951544
public function update(array $data): \PDOStatement
14961545
{
@@ -1517,8 +1566,8 @@ public function update(array $data): \PDOStatement
15171566
*
15181567
* @param array $data
15191568
*
1520-
* @throws Exception
15211569
* @return array|string
1570+
* @throws Exception
15221571
*/
15231572
public function insert(array $data)
15241573
{
@@ -1617,6 +1666,7 @@ public function whereNull($key): self
16171666
public function for(string $statement): self
16181667
{
16191668
$this->addStatement('for', $statement);
1669+
16201670
return $this;
16211671
}
16221672

@@ -1641,6 +1691,7 @@ public function getColumns(): array
16411691
}
16421692
}
16431693
}
1694+
16441695
return $tColumns;
16451696
}
16461697

tests/Pecee/Pixie/QueryBuilderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,21 @@ public function testQueryPartFor()
188188
$this->assertEquals('SELECT * FROM `cb_users` FOR UPDATE', $query->getQuery()->getRawSql());
189189
}
190190

191+
public function testRemoveQuery() {
192+
193+
$builder = $this->builder->newQuery()->table('test')->setOverwriteEnabled(true);
194+
195+
$query = $builder
196+
->where('parent_id', '=', 10)
197+
->where('parent_id', '=', 5)
198+
->limit(5);
199+
200+
$this->assertEquals('SELECT * FROM `cb_test` WHERE `parent_id` = 5 LIMIT 5', $query->getQuery()->getRawSql());
201+
202+
$query = $builder->whereNull('parent_id');
203+
204+
$this->assertEquals('SELECT * FROM `cb_test` WHERE `parent_id` IS NULL LIMIT 5', $query->getQuery()->getRawSql());
205+
206+
}
207+
191208
}

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function getLiveConnection() {
118118
'host' => '127.0.0.1',
119119
'database' => 'test',
120120
'username' => 'root',
121-
'password' => '',
121+
'password' => 'root',
122122
'charset' => 'utf8mb4', // Optional
123123
'collation' => 'utf8mb4_unicode_ci', // Optional
124124
'prefix' => '', // Table prefix, optional

0 commit comments

Comments
 (0)