diff --git a/CHANGELOG.md b/CHANGELOG.md index daad6e4..e824b46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Types of changes: `Added`, `Changed`, `Deprecate`, `Removed`, `Fixed`, `Secruity ### Added - Added class `CreateTableBlueprint` to create statements for generating tables in MySQL/MariaDB and SQLite - Added test mode to class `Config` so env values can be changed in test runs +- Added interface `ResultInterface` that all database providers use as return type for queries ## v2.5.0 - 17.03.2024 - Added statistic for amount and price development diff --git a/framework/Database/Database.php b/framework/Database/Database.php index 7fe31a5..4673bdc 100644 --- a/framework/Database/Database.php +++ b/framework/Database/Database.php @@ -3,15 +3,13 @@ namespace Framework\Database; use Framework\Config\Config; -use Framework\Config\ConfigReader; -use mysqli_result; class Database { private static ?MariaDB $mariaDB = null; /** Execute the SQL generated by the given query build */ - public static function executeBuilder(QueryBuilder|WhereQueryBuilder $queryBuilder): mysqli_result|bool + public static function executeBuilder(QueryBuilder|WhereQueryBuilder $queryBuilder): ResultInterface|bool { if ($queryBuilder->isWhereEmpty()) { return self::unprepared($queryBuilder->build()); @@ -24,8 +22,20 @@ public static function executeBuilder(QueryBuilder|WhereQueryBuilder $queryBuild } } + public static function executeBlueprint(BlueprintInterface $blueprint): ResultInterface|bool + { + $result = false; + foreach ($blueprint->build() as $sql) { + $result = self::unprepared($sql); + if ($result === false) { + return false; + } + } + return $result; + } + /** Execute the given unprepared query. You should try to avoid unprepared statements if they contain user input! */ - public static function unprepared(string $query): mysqli_result|bool + public static function unprepared(string $query): ResultInterface|bool { self::getMariaDb(); self::$mariaDB->connect(); @@ -34,8 +44,12 @@ public static function unprepared(string $query): mysqli_result|bool return $result; } - /** Execute the given prepared statement */ - public static function prepared(string $query, string $colTypes, ...$values): mysqli_result|bool + /** + * Execute the given prepared statement + * + * @param string $colTypes i = int, d = float, s = string + */ + public static function prepared(string $query, string $colTypes, ...$values): ResultInterface|bool { self::getMariaDb(); self::$mariaDB->connect(); diff --git a/framework/Database/DatabaseInterface.php b/framework/Database/DatabaseInterface.php new file mode 100644 index 0000000..cc004b7 --- /dev/null +++ b/framework/Database/DatabaseInterface.php @@ -0,0 +1,18 @@ +mysqli === null) { return false; } - return $this->mysqli->query($query); + return new MariaDbResult($this->mysqli->query($query)); } /** Execute the given prepared statement */ - public function prepared(string $query, string $colTypes, ...$values): mysqli_result|bool + public function prepared(string $query, string $colTypes, ...$values): ResultInterface|false { $stmt = $this->mysqli->prepare($query); if ($stmt === false) { @@ -65,6 +65,6 @@ public function prepared(string $query, string $colTypes, ...$values): mysqli_re return false; } - return $stmt->get_result(); + return new MariaDbResult($stmt->get_result()); } } \ No newline at end of file diff --git a/framework/Database/MariaDbResult.php b/framework/Database/MariaDbResult.php new file mode 100644 index 0000000..5a42b81 --- /dev/null +++ b/framework/Database/MariaDbResult.php @@ -0,0 +1,26 @@ +result === false) { + return false; + } + + $row = $this->result->fetch_assoc(); + if ($row === null) { + return false; + } + return $row; + } +} \ No newline at end of file diff --git a/framework/Database/ResultInterface.php b/framework/Database/ResultInterface.php new file mode 100644 index 0000000..6a9fb0c --- /dev/null +++ b/framework/Database/ResultInterface.php @@ -0,0 +1,8 @@ +