feat(query-builder): Implement advanced WHERE and new JOIN clauses

This commit is contained in:
kj
2025-10-17 13:13:01 -03:00
parent 892b3614ec
commit 6e433b4d06
2 changed files with 219 additions and 88 deletions

View File

@@ -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. + *En blanco* como que no ha sido probado aún.
+ *error* como que fue probado, no funciona y no ha sido aún arreglado. + *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. + *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 | | method | MySQL/MariaDB | sqlite3 | postgreSQL |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| db | ok | ok | | | db | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| query | ok | ok | | | query | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| resetQuery | ok | ok | | | resetQuery | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| buildQuery | ok | ok | | | buildQuery | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| getInstance | ok | ok | | | getInstance | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| getVars | ok | ok | | | getVars | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| className | ok | ok | | | className | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| table | ok | ok | | | table | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| update | ok | ok | | | update | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| beginTransaction | ok | ok | | | beginTransaction | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| rollBack | ok | ok | | | rollBack | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| commit | ok | ok | | | commit | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| add | ok | ok | | | add | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| save | ok | ok | | | save | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| delete | ok | ok | | | delete | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| select | ok | ok | | | select | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| from | ok | ok | | | from | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| where | ok | ok | | | where | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| where_in | ok | ok | | | whereIn | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| leftJoin | ok | ok | | | whereNotIn | | | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| rightJoin | ok | not supported | | | whereNull | | | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| innerJoin | ok | ok | | | whereNotNull | | | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| and | ok | ok | | | whereExists | | | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| or | ok | ok | | | whereNotExists | | | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| groupBy | ok | ok | | | leftJoin | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| limit | ok | ok | | | rightJoin | ok | fixed | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| orderBy | ok | ok | | | innerJoin | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| count | ok | ok | | | and | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| getById | ok | ok | | | or | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| search | ok | ok | | | groupBy | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| get | ok | ok | | | limit | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| getFirst | ok | ok | | | orderBy | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| all | ok | ok | | | count | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| setNull | ok | ok | | | getById | ok | ok | |
|------------------+---------------+---------------+------------| |------------------+---------------+---------+------------|
| search | ok | ok | |
|------------------+---------------+---------+------------|
| get | ok | ok | |
|------------------+---------------+---------+------------|
| getFirst | ok | ok | |
|------------------+---------------+---------+------------|
| all | ok | ok | |
|------------------+---------------+---------+------------|
| setNull | ok | ok | |
|------------------+---------------+---------+------------|
* Contacto * Contacto

View File

@@ -63,6 +63,7 @@ class Model
'leftJoin' => '', 'leftJoin' => '',
'rightJoin' => '', 'rightJoin' => '',
'innerJoin' => '', 'innerJoin' => '',
'crossJoin' => '',
'orderBy' => '', 'orderBy' => '',
'groupBy' => '', 'groupBy' => '',
'limit' => '', 'limit' => '',
@@ -193,6 +194,7 @@ class Model
'leftJoin' => '', 'leftJoin' => '',
'rightJoin' => '', 'rightJoin' => '',
'innerJoin' => '', 'innerJoin' => '',
'crossJoin' => '',
'orderBy' => '', 'orderBy' => '',
'groupBy' => '', 'groupBy' => '',
'limit' => '', 'limit' => '',
@@ -217,6 +219,10 @@ class Model
$sql .= ' FROM ' . static::table(); $sql .= ' FROM ' . static::table();
} }
if (static::$querySelect['crossJoin'] != '') {
$sql .= static::$querySelect['crossJoin'];
}
if (static::$querySelect['innerJoin'] != '') { if (static::$querySelect['innerJoin'] != '') {
$sql .= static::$querySelect['innerJoin']; $sql .= static::$querySelect['innerJoin'];
} }
@@ -645,36 +651,113 @@ class Model
* @param array $arr * @param array $arr
* Array with all values to compare against the column. * Array with all values to compare against the column.
* *
* @param bool $in
* Defines whether to check positively (IN) or negatively (NOT IN).
*
* @return static * @return static
*/ */
public static function whereIn( public static function whereIn(
string $column, string $column,
array $arr, array $arr,
bool $in = true
): static { ): static {
$arrIn = []; $arrIn = [];
foreach ($arr as $value) { foreach ($arr as $value) {
$arrIn[] = static::bind($value); $arrIn[] = static::bind($value);
} }
if ($in) { $where = "$column IN (" . join(', ', $arrIn) . ")";
$where_in = "$column IN (" . join(', ', $arrIn) . ")";
} else {
$where_in = "$column NOT IN (" . join(', ', $arrIn) . ")";
}
if (static::$querySelect['where'] == '') { if (static::$querySelect['where'] == '') {
static::$querySelect['where'] = $where_in; static::$querySelect['where'] = $where;
} else { } else {
static::$querySelect['where'] .= " AND $where_in"; static::$querySelect['where'] .= " AND $where";
} }
return new static(); 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. * Defines LEFT JOIN in the SQL statement.
* *
@@ -738,6 +821,10 @@ class Model
$operatorOrColumnB = '='; $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"; static::$querySelect['rightJoin'] .= ' RIGHT JOIN ' . $table . ' ON ' . "$columnA$operatorOrColumnB$columnB";
return new static(); return new static();
@@ -777,6 +864,40 @@ class Model
return new static(); 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. * Defines GROUP BY in the SQL statement.
* *
@@ -794,11 +915,11 @@ class Model
/** /**
* Defines LIMIT in the SQL statement. * Defines LIMIT in the SQL statement.
* *
* @param int $offsetOrQuantity * @param int $offsetOrQuantity
* Defines the rows to skip or the quantity to take * Defines the rows to skip or the quantity to take
* if $quantity is not defined. * if $quantity is not defined.
* @param int|null $quantity * @param int|null $quantity
* (Optional) Defines the maximum number of rows to take. * (Optional) Defines the maximum number of rows to take.
* *
* @return static * @return static
*/ */