debug("Getting connection", true); $stmt = Connection::getInstance()->prepare($query); foreach ($params as $key => $value) { // menaging LIKE statement in query if (strpos($query, 'SELECT') !== FALSE && in_array($key, ['src', 'dst', 'entrada', 'fila', 'evento', 'nome'])) { $value = "%$value%"; self::$logger->debug("treating value $key", true); } $stmt->bindValue(":$key", $value, (is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR)); } self::$logger->debug("Biding values for $query\nwith params: " . print_r($params, true), true); $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; } if (in_array($key, ['src', 'dst', 'entrada', 'fila', 'evento', 'nome'])) { $query .= " AND $key LIKE :$key"; continue; } $query .= " AND $key = :$key"; } return self::query($query, $params, $fetch); } /** Dinamicaly inserts a record into a table * @param string $table: table name * @param array $columns: associative array containing the fields and its values to be recorded * @return int|bool the number of rows modified by the database operation */ public static function dinamicInsertQuery(string $table, array $columns): int|bool { $query = "INSERT INTO $table ("; $notFirst = false; foreach ($columns as $key => $value) { if ($notFirst) { $query .= ', '; } $notFirst = true; $query .= $key; } $query .= ') VALUES ('; $notFirst = false; foreach ($columns as $key => $value) { if ($notFirst) { $query .= ', '; } $notFirst = true; $query .= ":$key"; } $query .= ')'; return self::query($query, $columns, "count"); } public static function dinamicDeleteQuery(string $table, array $params): int|bool { if (!sizeof($params)) { throw new Exception("Delete statement must have parameters!"); } $query = "DELETE FROM $table WHERE 1 = 1 "; foreach ($params as $key => $value) { if (empty($value)) { continue; } $query .= " AND $key = :$key"; } return self::query($query, $params, 'count'); } }