|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\core;
|
|
|
|
|
|
|
|
//use app\core\Database;
|
|
|
|
use app\core\Connection;
|
|
|
|
use PDO;
|
|
|
|
use Exception;
|
|
|
|
use PDOException;
|
|
|
|
use stdClass;
|
|
|
|
use app\shared\Logger;
|
|
|
|
|
|
|
|
abstract class Repository
|
|
|
|
{
|
|
|
|
public DataBase $db;
|
|
|
|
//protected static string $table;
|
|
|
|
protected static Logger $logger;
|
|
|
|
|
|
|
|
private static function setLog()
|
|
|
|
{
|
|
|
|
self::$logger = new Logger('api' . date('Ymd'), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds the values to the query, executed and returns the information selected in @param string $fetch.
|
|
|
|
*/
|
|
|
|
public static function query(string $query, array $params = [], string $fetch = 'all'): array|int|bool
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
self::setLog();
|
|
|
|
self::$logger->debug("Getting connection", true);
|
|
|
|
$stmt = Connection::getInstance()->prepare($query);
|
|
|
|
self::$logger->debug("Biding values for $query\nwith params: " . print_r($params, true), true);
|
|
|
|
foreach ($params as $key => $value) {
|
|
|
|
|
|
|
|
// menaging LIKE statement in query
|
|
|
|
if (in_array($key, ['src', 'dst', 'entrada', 'fila', 'evento'])) {
|
|
|
|
$value = "%$value%";
|
|
|
|
}
|
|
|
|
$stmt->bindValue(":$key", $value, (is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR));
|
|
|
|
}
|
|
|
|
$stmt->execute();
|
|
|
|
|
|
|
|
//selects the type of return
|
|
|
|
switch ($fetch) {
|
|
|
|
case 'all':
|
|
|
|
return $stmt->fetchAll();
|
|
|
|
case 'one':
|
|
|
|
return $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
case 'count':
|
|
|
|
return $stmt->rowCount();
|
|
|
|
default:
|
|
|
|
return $stmt->fetchAll();
|
|
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
self::$logger->error(print_r(['error' => $e->getMessage(), 'query' => $query, 'params' => $params], true));
|
|
|
|
return ['status' => false, 'data' => ['message' => 'Nenhum resultado encontrado!']];
|
|
|
|
} catch (Exception $general) {
|
|
|
|
self::$logger->error(print_r(['error' => $general->getMessage(), 'query' => $query, 'params' => $params], true));
|
|
|
|
return ['status' => false, 'data' => ['message' => 'Nenhum resultado encontrado!']];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Dinamicaly creates a SELECT query based on passed parameters
|
|
|
|
*
|
|
|
|
* @param string $table: table name
|
|
|
|
* @param array $columns: list of columns to be returned
|
|
|
|
* @param array $params: list of parameters to be used in 'WHERE' statement
|
|
|
|
* @param string fetch: type of return desired
|
|
|
|
*/
|
|
|
|
public static function dinamicSelectQuery(string $table, array $columns, array $params, string $fetch): array|int|bool
|
|
|
|
{
|
|
|
|
self::setLog();
|
|
|
|
$query = "SELECT " . implode(', ', $columns) . ' FROM ' . $table . " WHERE 1 = 1 ";
|
|
|
|
|
|
|
|
foreach ($params as $key => $value) {
|
|
|
|
if (empty($value)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$query .= " AND $key = :$key";
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::query($query, $params, $fetch);
|
|
|
|
}
|
|
|
|
}
|