From a39910330587417c14d57986dff79ebfb5ecfe11 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Tue, 18 Feb 2025 16:45:25 +0200 Subject: [PATCH] Adds database execute and prepare (needed for the tests) --- app/classes/database.php | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/app/classes/database.php b/app/classes/database.php index 35c2c7d..7d06545 100644 --- a/app/classes/database.php +++ b/app/classes/database.php @@ -129,6 +129,47 @@ class Database { return $this->pdo; } + /** + * Executes an SQL query with optional parameters. + * + * @param string $query The SQL query to execute + * @param array $params Optional parameters for the query + * @return PDOStatement|false The result of the query execution + * @throws Exception If the query fails + */ + public function execute($query, $params = []) { + if (!$this->pdo) { + throw new Exception('No database connection.'); + } + + try { + $stmt = $this->pdo->prepare($query); + $stmt->execute($params); + return $stmt; + } catch (PDOException $e) { + throw new Exception('Query execution failed: ' . $e->getMessage()); + } + } + + /** + * Prepares an SQL statement for execution. + * + * @param string $query The SQL query to prepare + * @return PDOStatement The prepared statement + * @throws Exception If the preparation fails + */ + public function prepare($query) { + if (!$this->pdo) { + throw new Exception('No database connection.'); + } + + try { + return $this->pdo->prepare($query); + } catch (PDOException $e) { + throw new Exception('Statement preparation failed: ' . $e->getMessage()); + } + } + } ?>