Skip to content

Commit 77b46a1

Browse files
committed
more refactoring where clause, removed string passing option and updated the tests
1 parent 0460ce2 commit 77b46a1

File tree

12 files changed

+221
-154
lines changed

12 files changed

+221
-154
lines changed

lib/ezFunctions.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,20 @@ function notLike($x, $y, $and = null, ...$args)
238238
/**
239239
* Creates a IN () comparison expression with the given arguments.
240240
*/
241-
function in($x, $y, $and = null, ...$args)
241+
function in($x, $y, ...$args)
242242
{
243243
$expression = array();
244-
\array_push($expression, $x, \_IN, $y, $and, ...$args);
244+
\array_push($expression, $x, \_IN, $y, ...$args);
245245
return $expression;
246246
}
247247

248248
/**
249249
* Creates a NOT IN () comparison expression with the given arguments.
250250
*/
251-
function notIn($x, $y, $and = null, ...$args)
251+
function notIn($x, $y, ...$args)
252252
{
253253
$expression = array();
254-
\array_push($expression, $x, \_notIN, $y, $and, ...$args);
254+
\array_push($expression, $x, \_notIN, $y, ...$args);
255255
return $expression;
256256
}
257257

@@ -271,7 +271,7 @@ function between($x, $y, $y2, ...$args)
271271
function notBetween($x, $y, $y2, ...$args)
272272
{
273273
$expression = array();
274-
\array_push($expression, $x, \_notBETWEEN, $y, $y2, ...$args);
274+
\array_push($expression, $x, \_notBETWEEN, $y, $y2, \_AND, ...$args);
275275
return $expression;
276276
}
277277

lib/ezQuery.php

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,29 @@ private function conditionIs($key, $condition, $combine)
340340
$this->whereSQL .= "$key $isCondition NULL $combine ";
341341
}
342342

343+
private function retrieveConditions($whereConditions)
344+
{
345+
foreach ($whereConditions as $checkFields) {
346+
$operator[] = (isset($checkFields[1])) ? $checkFields[1]: '';
347+
if (empty($checkFields[1])) {
348+
$this->clearPrepare();
349+
return [[],[],[],[]];
350+
}
351+
352+
if (\strtoupper($checkFields[1]) == 'IN') {
353+
$whereClause[ $checkFields[0] ] = \array_slice((array) $checkFields, 2);
354+
$combiner[] = \_AND;
355+
$extra[] = null;
356+
} else {
357+
$whereClause[ (isset($checkFields[0])) ? $checkFields[0] : '1' ] = (isset($checkFields[2])) ? $checkFields[2] : '' ;
358+
$combiner[] = (isset($checkFields[3])) ? $checkFields[3]: \_AND;
359+
$extra[] = (isset($checkFields[4])) ? $checkFields[4]: null;
360+
}
361+
}
362+
363+
return [$operator, $whereClause, $combiner, $extra];
364+
}
365+
343366
public function where( ...$whereConditions)
344367
{
345368
if (empty($whereConditions))
@@ -348,54 +371,30 @@ public function where( ...$whereConditions)
348371
$whereOrHaving = ($this->isWhere) ? 'WHERE' : 'HAVING';
349372
$this->isWhere = true;
350373

351-
$whereClause = [];
352-
$operator = [];
353-
$extra = [];
354-
$combiner = [];
355374
$this->combineWith = '';
356375

357-
$storeWhereConditions = $whereConditions;
358-
if (\is_string($whereConditions[0])) {
359-
if ((\strpos($whereConditions[0], 'WHERE') !== false)
360-
|| (\strpos($whereConditions[0], 'HAVING') !== false)
361-
)
362-
return $whereConditions[0];
363-
364-
$storeWhereConditions = [];
365-
foreach ($whereConditions as $makeWhereArray)
366-
$storeWhereConditions[] = \explode(' ', $makeWhereArray);
367-
}
368-
369-
foreach ($storeWhereConditions as $checkFields) {
370-
$operator[] = (isset($checkFields[1])) ? $checkFields[1]: '';
371-
if (empty($checkFields[1]))
372-
return $this->clearPrepare();
373-
374-
if (\strtoupper($checkFields[1]) == 'IN') {
375-
$whereClause[ $checkFields[0] ] = \array_slice((array) $checkFields, 2);
376-
$combiner[] = (isset($checkFields[3])) ? $checkFields[3]: \_AND;
377-
$extra[] = (isset($checkFields[4])) ? $checkFields[4]: null;
378-
} else {
379-
$whereClause[ (isset($checkFields[0])) ? $checkFields[0] : '1' ] = (isset($checkFields[2])) ? $checkFields[2] : '' ;
380-
$combiner[] = (isset($checkFields[3])) ? $checkFields[3]: \_AND;
381-
$extra[] = (isset($checkFields[4])) ? $checkFields[4]: null;
382-
}
383-
}
376+
if (\is_string($whereConditions[0])
377+
&& ((\strpos($whereConditions[0], 'WHERE') !== false) || (\strpos($whereConditions[0], 'HAVING') !== false)))
378+
return $whereConditions[0];
384379

380+
list($operator, $whereClause, $combiner, $extra) = $this->retrieveConditions($whereConditions);
381+
if (empty($operator))
382+
return false;
383+
385384
$where = '1';
386385
if (! isset($whereClause['1'])) {
387386
$this->whereSQL = '';
388387
$i = 0;
389388
foreach($whereClause as $key => $val) {
390389
$isCondition = \strtoupper($operator[$i]);
390+
if (! \in_array( $isCondition, \_BOOLEAN_OPERATORS))
391+
return $this->clearPrepare();
392+
391393
$combine = $combiner[$i];
392394
$this->combineWith = \_AND;
393395
if ( \in_array(\strtoupper($combine), \_COMBINERS) || isset($extra[$i]))
394396
$this->combineWith = isset($extra[$i]) ? $combine : \strtoupper($combine);
395397

396-
if (! \in_array( $isCondition, \_BOOLEAN_OPERATORS))
397-
return $this->clearPrepare();
398-
399398
if (($isCondition == \_BETWEEN) || ($isCondition == \_notBETWEEN)) {
400399
$this->conditionBetween($key, $isCondition, $val, $this->combineWith, $extra[$i]);
401400
} elseif ($isCondition == \_IN) {
@@ -418,7 +417,7 @@ public function where( ...$whereConditions)
418417
if (($this->isPrepareOn()) && !empty($this->prepareValues()) && ($where != '1'))
419418
return " $whereOrHaving $where ";
420419

421-
return ($where != '1') ? " $whereOrHaving $where " : ' ' ;
420+
return ($where != '1') ? " $whereOrHaving $where " : ' ';
422421
}
423422

424423
public function selecting(string $table = null, $columnFields = '*', ...$conditions)

lib/ezQueryInterface.php

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -273,29 +273,34 @@ public function orderBy($orderBy, $order);
273273
public function limit($numberOf, $offset = null);
274274

275275
/**
276-
* Helper returns an WHERE sql clause string.
277-
*
278-
* format:
279-
* `where( array(x, =, y, and, extra) )` or
280-
* `where( "x = y and extra" );` // Strings will need to be double spaced
281-
*
282-
* example:
283-
* `where( array(key, operator, value, combine, extra) );` or
284-
* `where( "key operator value combine extra" );` // Strings will need to be double spaced
285-
*
286-
* @param array $whereConditions like
287-
* @param $key, - table column
288-
* @param $operator, - set the operator condition,
289-
* either '<','>', '=', '!=', '>=', '<=', '<>', 'in', 'like',
290-
* 'not like', 'between', 'not between', 'is null', 'is not null'
291-
* @param $value, - will be escaped
292-
* @param $combine, - combine additional where clauses with,
293-
* either 'AND','OR', 'NOT', 'AND NOT'
294-
* or carry over of `value` in the case the `operator` is 'between' or 'not between'
295-
* @param $extra - carry over of `combine` in the case the operator is 'between' or 'not between')"
296-
*
297-
* @return mixed bool/string - WHERE SQL statement, or false on error
298-
*/
276+
* Helper returns an WHERE sql clause string.
277+
*
278+
* format:
279+
* `where( comparison(x, y, and) )`
280+
*
281+
* example:
282+
* `where( eq(key, value ), like('key', '_%?');`
283+
*
284+
* @param array $whereConditions - In the following format:
285+
*
286+
* eq('key/Field/Column', $value, _AND), // combine next expression
287+
* neq('key/Field/Column', $value, _OR), // will combine next expression again
288+
* ne('key/Field/Column', $value), // the default is _AND so will combine next expression
289+
* lt('key/Field/Column', $value)
290+
* lte('key/Field/Column', $value)
291+
* gt('key/Field/Column', $value)
292+
* gte('key/Field/Column', $value)
293+
* isNull('key/Field/Column')
294+
* isNotNull('key/Field/Column')
295+
* like('key/Field/Column', '_%')
296+
* notLike('key/Field/Column', '_%')
297+
* in('key/Field/Column', $values)
298+
* notIn('key/Field/Column', $values)
299+
* between('key/Field/Column', $value, $value2)
300+
* notBetween('key/Field/Column', $value, $value2)
301+
*
302+
* @return mixed bool/string - WHERE SQL statement, or false on error
303+
*/
299304
public function where( ...$whereConditions);
300305

301306
/**
@@ -323,9 +328,9 @@ public function where( ...$whereConditions);
323328
* @param mixed $conditions - of the following parameters:
324329
*
325330
* @param $joins, - join clause (type, left table, right table, left column, right column, condition = EQ)
326-
* @param $whereKey, - where clause ( array(x, =, y, and, extra) ) or ( "x = y and extra" )
331+
* @param $whereKey, - where clause ( comparison(x, y, and) )
327332
* @param $groupBy, - grouping over clause the results
328-
* @param $having, - having clause ( array(x, =, y, and, extra) ) or ( "x = y and extra" )
333+
* @param $having, - having clause ( comparison(x, y, and) )
329334
* @param $orderby, - ordering by clause for the query
330335
* @param $limit, - limit clause the number of records
331336
* @param $union/$unionAll - union clause combine the result sets and removes duplicate rows/does not remove

tests/ezQueryTest.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ public function testClean()
4141

4242
/**
4343
* @covers ezsql\ezQuery::having
44+
* @covers ezsql\ezQuery::retrieveConditions
45+
* @covers \in
4446
*/
4547
public function testHaving()
4648
{
4749
$this->assertFalse($this->object->having(''));
4850
$this->assertEmpty($this->object->having());
4951

50-
$expect = $this->object->having("other_test in testing 1 testing 2 testing 3 testing 4 testing 5");
52+
$expect = $this->object->having(in('other_test', 'testing 1', 'testing 2', 'testing 3', 'testing 4', 'testing 5'));
5153

5254
$this->assertContains('HAVING', $expect);
5355
}
@@ -56,16 +58,19 @@ public function testHaving()
5658
* @covers ezsql\ezQuery::where
5759
* @covers ezsql\ezQuery::conditionIs
5860
* @covers ezsql\ezQuery::conditionBetween
61+
* @covers ezsql\ezQuery::retrieveConditions
5962
* @covers ezsql\ezQuery::conditions
6063
* @covers ezsql\ezQuery::conditionIn
6164
* @covers ezsql\ezQuery::isPrepareOn
65+
* @covers \in
66+
* @covers \like
6267
*/
6368
public function testWhere()
6469
{
6570
$this->assertFalse($this->object->where(''));
6671
$this->assertEmpty($this->object->where());
6772

68-
$expect = $this->object->where("where_test in testing 1 testing 2 testing 3 testing 4 testing 5");
73+
$expect = $this->object->where(in('where_test', 'testing 1', 'testing 2', 'testing 3', 'testing 4', 'testing 5'));
6974

7075
$this->assertContains('WHERE', $expect);
7176
$this->assertContains('IN', $expect);
@@ -89,6 +94,7 @@ public function testWhere()
8994
* @covers ezsql\ezQuery::where
9095
* @covers ezsql\ezQuery::conditionIs
9196
* @covers ezsql\ezQuery::conditionBetween
97+
* @covers ezsql\ezQuery::retrieveConditions
9298
* @covers ezsql\ezQuery::conditions
9399
* @covers ezsql\ezQuery::conditionIn
94100
*/
@@ -138,7 +144,7 @@ public function testAddPrepare()
138144
public function testDelete()
139145
{
140146
$this->assertFalse($this->object->delete(''));
141-
$this->assertFalse($this->object->delete('test_unit_delete',array('good','bad')));
147+
$this->assertFalse($this->object->delete('test_unit_delete', array('good', 'bad')));
142148
}
143149

144150
/**
@@ -147,51 +153,49 @@ public function testDelete()
147153
*/
148154
public function testSelecting()
149155
{
150-
$this->assertFalse($this->object->selecting('',''));
151-
//$this->expectException(\Error::class);
152-
//$this->expectExceptionMessageRegExp('/[Call to undefined method ezsql\ezQuery::get_results()]/');
153-
$this->assertNotNull($this->object->selecting('table','columns','WHERE','GROUP BY','HAVING','ORDER BY','LIMIT'));
156+
$this->assertFalse($this->object->selecting('', ''));
157+
$this->assertNotNull($this->object->selecting('table', 'columns', 'WHERE', 'GROUP BY', 'HAVING', 'ORDER BY', 'LIMIT'));
154158
}
155159

156160
/**
157161
* @covers ezsql\ezQuery::create_select
158162
*/
159163
public function testCreate_select()
160164
{
161-
$this->assertFalse($this->object->create_select('','',''));
165+
$this->assertFalse($this->object->create_select('', '', ''));
162166
}
163167

164168
/**
165169
* @covers ezsql\ezQuery::insert_select
166170
*/
167171
public function testInsert_select()
168172
{
169-
$this->assertFalse($this->object->insert_select('','',''));
173+
$this->assertFalse($this->object->insert_select('', '', ''));
170174
}
171175

172176
/**
173177
* @covers ezsql\ezQuery::insert
174178
*/
175179
public function testInsert()
176180
{
177-
$this->assertFalse($this->object->insert('',''));
181+
$this->assertFalse($this->object->insert('', ''));
178182
}
179183

180184
/**
181185
* @covers ezsql\ezQuery::update
182186
*/
183187
public function testUpdate()
184188
{
185-
$this->assertFalse($this->object->update('',''));
186-
$this->assertFalse($this->object->update('test_unit_delete',array('test_unit_update'=>'date()'),''));
189+
$this->assertFalse($this->object->update('', ''));
190+
$this->assertFalse($this->object->update('test_unit_delete', array('test_unit_update' => 'date()'),''));
187191
}
188192

189193
/**
190194
* @covers ezsql\ezQuery::replace
191195
*/
192196
public function testReplace()
193197
{
194-
$this->assertFalse($this->object->replace('',''));
198+
$this->assertFalse($this->object->replace('', ''));
195199
}
196200

197201
/**

tests/mysqli/mysqliTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,19 +323,19 @@ public function testUpdate()
323323
$this->object->insert('unit_test', array('id '=> 13, 'test_key' => 'testUpdate() 13' ));
324324

325325
$unit_test['test_key'] = 'testing testUpdate()';
326-
$where="id = 11";
326+
$where = ['id', '=', 11];
327327
$this->assertEquals(1, $this->object->update('unit_test', $unit_test, $where));
328328

329329
$this->assertEquals(1,
330330
$this->object->update('unit_test', $unit_test, ['test_key', EQ, 'testUpdate() 13', 'and'], ['id', '=', 13])
331331
);
332332

333333
$this->assertEquals(0,
334-
$this->object->update('unit_test', $unit_test, "id = 14")
334+
$this->object->update('unit_test', $unit_test, eq('id', 14))
335335
);
336336

337337
$this->assertEquals(1,
338-
$this->object->update('unit_test', $unit_test, "test_key = testUpdate() 12 and", "id = 12")
338+
$this->object->update('unit_test', $unit_test, eq('test_key', 'testUpdate() 12'), ['id', '=', 12])
339339
);
340340

341341
$this->assertEquals(0, $this->object->drop('unit_test'));
@@ -367,9 +367,9 @@ public function testDelete()
367367
['test_key', '=', $unit_test['test_key'], 'and'], ['id','=', 3]));
368368

369369
$where = 1;
370-
$this->assertEquals(0, $this->object->delete('unit_test', ['test_key','=',$where]));
370+
$this->assertEquals(0, $this->object->delete('unit_test', ['test_key', '=', $where]));
371371

372-
$where="id = 2";
372+
$where = ['id', '=', 2];
373373
$this->assertEquals(1, $this->object->delete('unit_test', $where));
374374
}
375375

0 commit comments

Comments
 (0)