feat(query-builder): Implement advanced WHERE and new JOIN clauses
This commit is contained in:
154
readme.org
154
readme.org
@@ -28,79 +28,89 @@ En la siguiente tabla se encuentra la lista de estados de los gestores de bases
|
||||
+ *En blanco* como que no ha sido probado aún.
|
||||
+ *error* como que fue probado, no funciona y no ha sido aún arreglado.
|
||||
+ *not supported* como no soportado por el gestor de bases de datos.
|
||||
+ *fixed* para aquello que no existe en el gestor de DB, pero la librería lo traduce a un equivalente.
|
||||
+ *fixed* para aquello que no existe en el gestor de BD, pero la librería lo traduce a un equivalente.
|
||||
|
||||
|------------------+---------------+---------------+------------|
|
||||
| method | MySQL/MariaDB | sqlite3 | postgreSQL |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| db | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| query | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| resetQuery | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| buildQuery | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| getInstance | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| getVars | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| className | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| table | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| update | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| beginTransaction | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| rollBack | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| commit | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| add | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| save | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| delete | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| select | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| from | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| where | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| where_in | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| leftJoin | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| rightJoin | ok | not supported | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| innerJoin | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| and | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| or | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| groupBy | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| limit | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| orderBy | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| count | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| getById | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| search | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| get | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| getFirst | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| all | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
| setNull | ok | ok | |
|
||||
|------------------+---------------+---------------+------------|
|
||||
|------------------+---------------+---------+------------|
|
||||
| method | MySQL/MariaDB | sqlite3 | postgreSQL |
|
||||
|------------------+---------------+---------+------------|
|
||||
| db | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| query | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| resetQuery | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| buildQuery | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| getInstance | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| getVars | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| className | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| table | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| update | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| beginTransaction | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| rollBack | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| commit | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| add | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| save | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| delete | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| select | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| from | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| where | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| whereIn | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| whereNotIn | | | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| whereNull | | | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| whereNotNull | | | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| whereExists | | | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| whereNotExists | | | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| leftJoin | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| rightJoin | ok | fixed | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| innerJoin | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| and | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| or | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| groupBy | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| limit | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| orderBy | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| count | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| getById | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| search | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| get | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| getFirst | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| all | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
| setNull | ok | ok | |
|
||||
|------------------+---------------+---------+------------|
|
||||
|
||||
* Contacto
|
||||
|
||||
|
||||
@@ -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