From 404bd5956961c5d600b5f8a0e4712753a35ace3e Mon Sep 17 00:00:00 2001 From: kj Date: Mon, 22 Aug 2022 15:26:32 -0400 Subject: [PATCH] Improve where, and, or and search method. - static::$querySelect['AndOr'] is REMOVED. - where, and, or and search now use only the static::$querySelect['where'] index. - removed unnecesary brackets on buildQuery. --- src/Libs/Model.php | 160 ++++++++++++++++++++------------------------- 1 file changed, 71 insertions(+), 89 deletions(-) diff --git a/src/Libs/Model.php b/src/Libs/Model.php index f5fb9d0..ddb9a65 100644 --- a/src/Libs/Model.php +++ b/src/Libs/Model.php @@ -37,7 +37,6 @@ class Model { 'leftJoin' => '', 'rightJoin' => '', 'innerJoin' => '', - 'AndOr' => '', 'orderBy' => '', 'groupBy' => '', 'limit' => '' @@ -142,7 +141,6 @@ class Model { 'leftJoin' => '', 'rightJoin' => '', 'innerJoin' => '', - 'AndOr' => '', 'orderBy' => '', 'groupBy' => '', 'limit' => '' @@ -160,43 +158,31 @@ class Model { protected static function buildQuery() : string { $sql = 'SELECT '.join(', ', static::$querySelect['select']); - if (static::$querySelect['from'] != '') { + if (static::$querySelect['from'] != '') $sql .= ' FROM '.static::$querySelect['from']; - } else { + else $sql .= ' FROM '.static::table(); - } - if(static::$querySelect['innerJoin'] != '') { + if(static::$querySelect['innerJoin'] != '') $sql .= static::$querySelect['innerJoin']; - } - if (static::$querySelect['leftJoin'] != '') { + if (static::$querySelect['leftJoin'] != '') $sql .= static::$querySelect['leftJoin']; - } - if(static::$querySelect['rightJoin'] != '') { + if(static::$querySelect['rightJoin'] != '') $sql .= static::$querySelect['rightJoin']; - } - if (static::$querySelect['where'] != '') { + if (static::$querySelect['where'] != '') $sql .= ' WHERE '.static::$querySelect['where']; - if (static::$querySelect['AndOr'] != '') { - $sql .= static::$querySelect['AndOr']; - } - } - - if (static::$querySelect['groupBy'] != '') { + if (static::$querySelect['groupBy'] != '') $sql .= ' GROUP BY '.static::$querySelect['groupBy']; - } - if (static::$querySelect['orderBy'] != '') { + if (static::$querySelect['orderBy'] != '') $sql .= ' ORDER BY '.static::$querySelect['orderBy']; - } - if (static::$querySelect['limit'] != '') { + if (static::$querySelect['limit'] != '') $sql .= ' LIMIT '.static::$querySelect['limit']; - } return $sql; } @@ -416,6 +402,28 @@ class Model { * @return Model */ public static function where(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : Model { + return static::and($column, $operatorOrValue, $value, $no_filter); + } + + /** + * Define AND en la sentencia SQL (se puede anidar). + * + * @param string $column + * La columna a comparar. + * + * @param string $operatorOrValue + * El operador o el valor a comparar como igual en caso de que $value no se defina. + * + * @param string $value + * (opcional) El valor el valor a comparar en la columna. + * + * @param bool $no_filter + * (opcional) Se usa cuando $value es una columna o un valor que no requiere filtros + * contra ataques SQLI (por defecto es false). + * + * @return Model + */ + public static function and(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : Model { if (is_null($value)) { $value = $operatorOrValue; $operatorOrValue = '='; @@ -424,7 +432,45 @@ class Model { if (!$no_filter) $value = static::bindValue($value); - static::$querySelect['where'] = "$column $operatorOrValue $value"; + if (static::$querySelect['where'] == '') + static::$querySelect['where'] = "$column $operatorOrValue $value"; + else + static::$querySelect['where'] .= " AND $column $operatorOrValue $value"; + + return new static(); + } + + /** + * Define OR en la sentencia SQL (se puede anidar). + * + * @param string $column + * La columna a comparar. + * + * @param string $operatorOrValue + * El operador o el valor a comparar como igual en caso de que $value no se defina. + * + * @param string $value + * (opcional) El valor el valor a comparar en la columna. + * + * @param bool $no_filter + * (opcional) Se usa cuando $value es una columna o un valor que no requiere filtros + * contra ataques SQLI (por defecto es false). + * + * @return Model + */ + public static function or(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : Model { + if (is_null($value)) { + $value = $operatorOrValue; + $operatorOrValue = '='; + } + + if (!$no_filter) + $value = static::bindValue($value); + + if (static::$querySelect['where'] == '') + static::$querySelect['where'] = "$column $operatorOrValue $value"; + else + static::$querySelect['where'] .= " OR $column $operatorOrValue $value"; return new static(); } @@ -541,70 +587,6 @@ class Model { return new static(); } - /** - * Define AND en la sentencia SQL (se puede anidar). - * - * @param string $column - * La columna a comparar. - * - * @param string $operatorOrValue - * El operador o el valor a comparar como igual en caso de que $value no se defina. - * - * @param string $value - * (opcional) El valor el valor a comparar en la columna. - * - * @param bool $no_filter - * (opcional) Se usa cuando $value es una columna o un valor que no requiere filtros - * contra ataques SQLI (por defecto es false). - * - * @return Model - */ - public static function and(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : Model { - if (is_null($value)) { - $value = $operatorOrValue; - $operatorOrValue = '='; - } - - if (!$no_filter) - $value = static::bindValue($value); - - static::$querySelect['AndOr'] .= " AND $column $operatorOrValue $value"; - - return new static(); - } - - /** - * Define OR en la sentencia SQL (se puede anidar). - * - * @param string $column - * La columna a comparar. - * - * @param string $operatorOrValue - * El operador o el valor a comparar como igual en caso de que $value no se defina. - * - * @param string $value - * (opcional) El valor el valor a comparar en la columna. - * - * @param bool $no_filter - * (opcional) Se usa cuando $value es una columna o un valor que no requiere filtros - * contra ataques SQLI (por defecto es false). - * - * @return Model - */ - public static function or(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : Model { - if (is_null($value)) { - $value = $operatorOrValue; - $operatorOrValue = '='; - } - - if (!$no_filter) - $value = static::bindValue($value); - - static::$querySelect['AndOr'] .= " OR $column $operatorOrValue $value"; - - return new static(); - } - /** * Define GROUP BY en la sentencia SQL. * @@ -741,7 +723,7 @@ class Model { $db = static::db(); $search = static::bindValue($search); - $where = []; + $where = []; if (DB_TYPE == 'sqlite') foreach($in as $row)