Skip to content

Commit

Permalink
added null wheres
Browse files Browse the repository at this point in the history
  • Loading branch information
Desmond Chin authored and Desmond Chin committed Jan 3, 2021
1 parent 4f2fa92 commit d8ed3a8
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ protected function startLog()
$this->log = true;
}

/**
* Executes the prepared query.
*/
public function execute()
{
$query = $this->queryBuilder();
Expand Down Expand Up @@ -337,7 +340,7 @@ public function delete()
* @param $type This is either OR or AND
* @return $this
*/
private function whereProcessor($args, $type)
private function whereProcessor($args, $type, ?bool $null = false)
{
// Check if this where clause is the first
$first = false;
Expand Down Expand Up @@ -370,22 +373,91 @@ private function whereProcessor($args, $type)
}
$index++;
}

return $this;
}

private function nullWhereProcessor(string $column, string $type, bool $null){
// Check if this where clause is the first
$first = false;
if ($this->_where == "") {
$first = true;
// If it is, append WHERE in the query
$this->_where = " WHERE ";
}

if ($first === false) {
$this->_where .= " $type ";
}

if($null == false){
$nullString = "IS NOT NULL";
}else{
$nullString = "IS NULL";
}

$this->_where .= " $column $nullString ";

return $this;
}

/**
* Generates the where query
*
*/
public function where()
{
return $this->whereProcessor(func_get_args(), 'AND');
}

/**
* Generates a where with the OR Operator
*/
public function orWhere()
{
return $this->whereProcessor(func_get_args(), 'OR');
}

/**
* Generates a where for a null column
*
* @param string $column The column where the value should be null.
*/
public function whereNull(string $column)
{
return $this->nullWhereProcessor($column, 'AND', true);
}

/**
* Generates a where for a null column with the OR operator
*
* @param string $column The column where the value should be null.
*/
public function orWhereNull(string $column)
{
return $this->nullWhereProcessor($column, 'AND', true);
}

/**
* Generates a where for not null column with the OR operator
*
* @param string $column The column where the value should not be null.
*/
public function whereNotNull(string $column)
{
return $this->nullWhereProcessor($column, 'AND', false);
}

/**
* Generates a where for not null column with the OR operator
*
* @param string $column The column where the value should not be null.
*/
public function orWhereNotNull(string $column)
{
return $this->nullWhereProcessor($column, 'AND', false);
}

/**
* whereRaw
*
Expand Down

0 comments on commit d8ed3a8

Please sign in to comment.