feat(query-builder): Implement advanced WHERE and new JOIN clauses
This commit is contained in:
@@ -63,6 +63,7 @@ class Model
|
||||
'leftJoin' => '',
|
||||
'rightJoin' => '',
|
||||
'innerJoin' => '',
|
||||
'crossJoin' => '',
|
||||
'orderBy' => '',
|
||||
'groupBy' => '',
|
||||
'limit' => '',
|
||||
@@ -193,6 +194,7 @@ class Model
|
||||
'leftJoin' => '',
|
||||
'rightJoin' => '',
|
||||
'innerJoin' => '',
|
||||
'crossJoin' => '',
|
||||
'orderBy' => '',
|
||||
'groupBy' => '',
|
||||
'limit' => '',
|
||||
@@ -217,6 +219,10 @@ class Model
|
||||
$sql .= ' FROM ' . static::table();
|
||||
}
|
||||
|
||||
if (static::$querySelect['crossJoin'] != '') {
|
||||
$sql .= static::$querySelect['crossJoin'];
|
||||
}
|
||||
|
||||
if (static::$querySelect['innerJoin'] != '') {
|
||||
$sql .= static::$querySelect['innerJoin'];
|
||||
}
|
||||
@@ -645,36 +651,113 @@ class Model
|
||||
* @param array $arr
|
||||
* Array with all values to compare against the column.
|
||||
*
|
||||
* @param bool $in
|
||||
* Defines whether to check positively (IN) or negatively (NOT IN).
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function whereIn(
|
||||
string $column,
|
||||
array $arr,
|
||||
bool $in = true
|
||||
): static {
|
||||
$arrIn = [];
|
||||
foreach ($arr as $value) {
|
||||
$arrIn[] = static::bind($value);
|
||||
}
|
||||
|
||||
if ($in) {
|
||||
$where_in = "$column IN (" . join(', ', $arrIn) . ")";
|
||||
} else {
|
||||
$where_in = "$column NOT IN (" . join(', ', $arrIn) . ")";
|
||||
}
|
||||
|
||||
$where = "$column IN (" . join(', ', $arrIn) . ")";
|
||||
if (static::$querySelect['where'] == '') {
|
||||
static::$querySelect['where'] = $where_in;
|
||||
static::$querySelect['where'] = $where;
|
||||
} else {
|
||||
static::$querySelect['where'] .= " AND $where_in";
|
||||
static::$querySelect['where'] .= " AND $where";
|
||||
}
|
||||
|
||||
return new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines WHERE using NOT IN in the SQL statement.
|
||||
*
|
||||
* @param string $column
|
||||
* The column to compare.
|
||||
*
|
||||
* @param array $arr
|
||||
* Array with all values to compare against the column.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function whereNotIn(
|
||||
string $column,
|
||||
array $arr,
|
||||
): static {
|
||||
$arrIn = [];
|
||||
foreach ($arr as $value) {
|
||||
$arrIn[] = static::bind($value);
|
||||
}
|
||||
|
||||
$where = "$column NOT IN (" . join(', ', $arrIn) . ")";
|
||||
if (static::$querySelect['where'] == '') {
|
||||
static::$querySelect['where'] = $where;
|
||||
} else {
|
||||
static::$querySelect['where'] .= " AND $where";
|
||||
}
|
||||
|
||||
return new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines WHERE using IS NULL in the SQL statement.
|
||||
*
|
||||
* @param string $column
|
||||
* The column to compare.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function whereNull(string $column): static
|
||||
{
|
||||
static::where($column, 'IS', 'NULL', true);
|
||||
return new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines WHERE using IS NOT NULL in the SQL statement.
|
||||
*
|
||||
* @param string $column
|
||||
* The column to compare.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function whereNotNull(string $column): static
|
||||
{
|
||||
static::where($column, 'IS NOT', 'NULL', true);
|
||||
return new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines WHERE using EXIST in the SQL statement.
|
||||
*
|
||||
* @param string $query
|
||||
* SQL query.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function whereExist(string $query): static
|
||||
{
|
||||
static::where($column, 'EXIST', "($query)", true);
|
||||
return new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines WHERE using NOT EXIST in the SQL statement.
|
||||
*
|
||||
* @param string $query
|
||||
* SQL query.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function whereNotExist(string $query): static
|
||||
{
|
||||
static::where($column, 'NOT EXIST', "($query)", true);
|
||||
return new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines LEFT JOIN in the SQL statement.
|
||||
*
|
||||
@@ -738,6 +821,10 @@ class Model
|
||||
$operatorOrColumnB = '=';
|
||||
}
|
||||
|
||||
if (static::db()->getAttribute(PDO::ATTR_DRIVER_NAME) == 'sqlite') {
|
||||
return static::leftJoin($table, $columnB, $operatorOrColumnB, $columnA);
|
||||
}
|
||||
|
||||
static::$querySelect['rightJoin'] .= ' RIGHT JOIN ' . $table . ' ON ' . "$columnA$operatorOrColumnB$columnB";
|
||||
|
||||
return new static();
|
||||
@@ -777,6 +864,40 @@ class Model
|
||||
return new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines CROSS JOIN in the SQL statement.
|
||||
*
|
||||
* @param string $table
|
||||
* Table to join with the current object's table.
|
||||
*
|
||||
* @param string $columnA
|
||||
* Column to compare for the join.
|
||||
*
|
||||
* @param string $operatorOrColumnB
|
||||
* Operator or column to compare as equal for the join
|
||||
* if $columnB is not defined.
|
||||
*
|
||||
* @param string|null $columnB
|
||||
* (Optional) Column to compare for the join.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function crossJoin(
|
||||
string $table,
|
||||
string $columnA,
|
||||
string $operatorOrColumnB,
|
||||
?string $columnB = null
|
||||
): static {
|
||||
if (is_null($columnB)) {
|
||||
$columnB = $operatorOrColumnB;
|
||||
$operatorOrColumnB = '=';
|
||||
}
|
||||
|
||||
static::$querySelect['crossJoin'] .= ' CROSS JOIN ' . $table . ' ON ' . "$columnA$operatorOrColumnB$columnB";
|
||||
|
||||
return new static();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines GROUP BY in the SQL statement.
|
||||
*
|
||||
@@ -794,11 +915,11 @@ class Model
|
||||
/**
|
||||
* Defines LIMIT in the SQL statement.
|
||||
*
|
||||
* @param int $offsetOrQuantity
|
||||
* Defines the rows to skip or the quantity to take
|
||||
* if $quantity is not defined.
|
||||
* @param int $offsetOrQuantity
|
||||
* Defines the rows to skip or the quantity to take
|
||||
* if $quantity is not defined.
|
||||
* @param int|null $quantity
|
||||
* (Optional) Defines the maximum number of rows to take.
|
||||
* (Optional) Defines the maximum number of rows to take.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user