- Add option for reset query or not at get, getFirts, buildQuery and count

- Add sql_calc_found_rows method.
- Add found_rows method.
- Improve count method usind sql_calc_found_rows and found_rows.
This commit is contained in:
kj 2021-08-14 17:30:03 -04:00
parent 9785954870
commit e3b97601e2

View File

@ -26,16 +26,17 @@ class ModelMySQL {
static protected $tableSufix = 's';
static protected $db;
static protected $querySelect = [
'select' => ['*'],
'where' => '',
'from' => '',
'leftJoin' => '',
'rightJoin' => '',
'innerJoin' => '',
'AndOr' => '',
'orderBy' => '',
'groupBy' => '',
'limit' => '',
'select' => ['*'],
'where' => '',
'from' => '',
'leftJoin' => '',
'rightJoin' => '',
'innerJoin' => '',
'AndOr' => '',
'orderBy' => '',
'groupBy' => '',
'limit' => '',
'sql_calc_found_rows' => false
];
/*
@ -84,16 +85,17 @@ class ModelMySQL {
*/
protected static function resetQuery() {
static::$querySelect = [
'select' => ['*'],
'where' => '',
'from' => '',
'leftJoin' => '',
'rightJoin' => '',
'innerJoin' => '',
'AndOr' => '',
'orderBy' => '',
'groupBy' => '',
'limit' => '',
'select' => ['*'],
'where' => '',
'from' => '',
'leftJoin' => '',
'rightJoin' => '',
'innerJoin' => '',
'AndOr' => '',
'orderBy' => '',
'groupBy' => '',
'limit' => '',
'sql_calc_found_rows' => false
];
}
@ -101,11 +103,17 @@ class ModelMySQL {
* Construye la sentencia SQL a partir static::$querySelect y una vez
* construída, llama a resetQuery.
*
* @param boolean $resetQuery
* Indica si el query debe reiniciarse o no (por defecto es true).
*
* @return string
* Contiene la sentencia SQL.
*/
protected static function buildQuery() {
$sql = 'SELECT '.join(', ', static::$querySelect['select']);
protected static function buildQuery($resetQuery = true) {
if (static::$querySelect['sql_calc_found_rows'])
$sql = 'SELECT SQL_CALC_FOUND_ROWS '.join(', ', static::$querySelect['select']);
else
$sql = 'SELECT '.join(', ', static::$querySelect['select']);
if (static::$querySelect['from'] != '') {
$sql .= ' FROM '.static::$querySelect['from'];
@ -145,7 +153,8 @@ class ModelMySQL {
$sql .= ' LIMIT '.static::$querySelect['limit'];
}
static::resetQuery();
if ($resetQuery)
static::resetQuery();
return $sql;
}
@ -616,22 +625,44 @@ class ModelMySQL {
/*
* Retorna la cantidad de filas que hay en un query.
*
* @param boolean $resetQuery
* Indica si el query debe reiniciarse o no (por defecto es true).
*
* @return int
*/
public static function count() {
if (static::$querySelect['select'] == ['*'])
static::$querySelect['select'] = ['count(*) as quantity'];
else
static::$querySelect['select'][] = 'count(*) as quantity';
$sql = static::buildQuery();
public static function count($resetQuery = true) {
static::$querySelect['select'] = ['1'];
static::$querySelect['sql_calc_found_rows'] = true;
static::$querySelect['limit'] = '1';
$sql = static::buildQuery($resetQuery);
$result = static::query($sql)->fetch_assoc();
return static::found_row();
}
/*
* Retorna las filas contadas en el último query.
*
* @return int
*/
public static function found_row() {
$result = static::query('SELECT FOUND_ROWS() AS quantity')->fetch_assoc();
return $result['quantity'];
}
/*
* Obtiene una instancia según su id.
* Habilita el conteo de todos las coincidencias posibles incluso usando limit.
*
* @return ModelMySQL
*/
public static function sql_calc_found_rows() {
static::$querySelect['sql_calc_found_rows'] = true;
return new static();
}
/*
* Obtiene una instancia según su primary key (generalmente id).
*
* @param int $id
* @param mixed $id
* @return ModelMySQL
*/
public static function getById($id) {
@ -674,10 +705,13 @@ class ModelMySQL {
/*
* Obtener los resultados de la consulta SQL.
*
* @param boolean $resetQuery
* Indica si el query debe reiniciarse o no (por defecto es true).
*
* @return ModelMySQL[]
*/
public static function get() { // Devuelve array vacío si no encuentra nada
$sql = static::buildQuery();
public static function get($resetQuery = true) { // Devuelve array vacío si no encuentra nada
$sql = static::buildQuery($resetQuery);
$result = static::query($sql);
$instances = [];
@ -692,12 +726,15 @@ class ModelMySQL {
/*
* El primer elemento de la consulta SQL.
*
* @param boolean $resetQuery
* Indica si el query debe reiniciarse o no (por defecto es true).
*
* @return mixed
* Puede retornar un objeto ModelMySQL o null.
*/
public static function getFirst() { // Devuelve null si no encuentra nada
public static function getFirst($resetQuery = true) { // Devuelve null si no encuentra nada
static::limit(1);
$instances = static::get();
$instances = static::get($resetQuery);
return empty($instances) ? null : $instances[0];
}