Skip to content

Commit d8ed3a8

Browse files
Desmond ChinDesmond Chin
authored andcommitted
added null wheres
1 parent 4f2fa92 commit d8ed3a8

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

src/Database/Model.php

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ protected function startLog()
141141
$this->log = true;
142142
}
143143

144+
/**
145+
* Executes the prepared query.
146+
*/
144147
public function execute()
145148
{
146149
$query = $this->queryBuilder();
@@ -337,7 +340,7 @@ public function delete()
337340
* @param $type This is either OR or AND
338341
* @return $this
339342
*/
340-
private function whereProcessor($args, $type)
343+
private function whereProcessor($args, $type, ?bool $null = false)
341344
{
342345
// Check if this where clause is the first
343346
$first = false;
@@ -370,22 +373,91 @@ private function whereProcessor($args, $type)
370373
}
371374
$index++;
372375
}
376+
377+
return $this;
378+
}
379+
380+
private function nullWhereProcessor(string $column, string $type, bool $null){
381+
// Check if this where clause is the first
382+
$first = false;
383+
if ($this->_where == "") {
384+
$first = true;
385+
// If it is, append WHERE in the query
386+
$this->_where = " WHERE ";
387+
}
388+
389+
if ($first === false) {
390+
$this->_where .= " $type ";
391+
}
392+
393+
if($null == false){
394+
$nullString = "IS NOT NULL";
395+
}else{
396+
$nullString = "IS NULL";
397+
}
398+
399+
$this->_where .= " $column $nullString ";
400+
373401
return $this;
374402
}
375403

376404
/**
377405
* Generates the where query
406+
*
378407
*/
379408
public function where()
380409
{
381410
return $this->whereProcessor(func_get_args(), 'AND');
382411
}
383412

413+
/**
414+
* Generates a where with the OR Operator
415+
*/
384416
public function orWhere()
385417
{
386418
return $this->whereProcessor(func_get_args(), 'OR');
387419
}
388420

421+
/**
422+
* Generates a where for a null column
423+
*
424+
* @param string $column The column where the value should be null.
425+
*/
426+
public function whereNull(string $column)
427+
{
428+
return $this->nullWhereProcessor($column, 'AND', true);
429+
}
430+
431+
/**
432+
* Generates a where for a null column with the OR operator
433+
*
434+
* @param string $column The column where the value should be null.
435+
*/
436+
public function orWhereNull(string $column)
437+
{
438+
return $this->nullWhereProcessor($column, 'AND', true);
439+
}
440+
441+
/**
442+
* Generates a where for not null column with the OR operator
443+
*
444+
* @param string $column The column where the value should not be null.
445+
*/
446+
public function whereNotNull(string $column)
447+
{
448+
return $this->nullWhereProcessor($column, 'AND', false);
449+
}
450+
451+
/**
452+
* Generates a where for not null column with the OR operator
453+
*
454+
* @param string $column The column where the value should not be null.
455+
*/
456+
public function orWhereNotNull(string $column)
457+
{
458+
return $this->nullWhereProcessor($column, 'AND', false);
459+
}
460+
389461
/**
390462
* whereRaw
391463
*

0 commit comments

Comments
 (0)