Skip to content

Commit

Permalink
Added interface ResultInterface that all database providers use as …
Browse files Browse the repository at this point in the history
…return type for queries
  • Loading branch information
MasterZydra committed Apr 15, 2024
1 parent 6ddbbce commit fc17474
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 20 additions & 6 deletions framework/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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();
Expand All @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions framework/Database/DatabaseInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Framework\Database;

interface DatabaseInterface
{
/** Open connection to the DB */
public function connect(): void;

/** Close the open connection */
public function disconnect(): void;

/** Execute the given query */
public function unprepared(string $query): ResultInterface|false;

/** Execute the given prepared statement */
public function prepared(string $query, string $colTypes, ...$values): ResultInterface|false;
}
12 changes: 6 additions & 6 deletions framework/Database/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use mysqli_result;

/** This class simplifies the connection to MariaDB and executing queries. */
class MariaDB
class MariaDB implements DatabaseInterface
{
private ?mysqli $mysqli;
private ?mysqli $mysqli = null;

public function __construct(
private string $host,
Expand Down Expand Up @@ -43,17 +43,17 @@ public function disconnect(): void
}

/** Execute the given query */
public function unprepared(string $query): mysqli_result|bool
public function unprepared(string $query): ResultInterface|false
{
if ($this->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) {
Expand All @@ -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());
}
}
26 changes: 26 additions & 0 deletions framework/Database/MariaDbResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Framework\Database;

use mysqli_result;

class MariaDbResult implements ResultInterface
{
public function __construct(
private mysqli_result|false $result,
) {
}

public function fetch(): array|false
{
if ($this->result === false) {
return false;
}

$row = $this->result->fetch_assoc();
if ($row === null) {
return false;
}
return $row;
}
}
8 changes: 8 additions & 0 deletions framework/Database/ResultInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Framework\Database;

interface ResultInterface
{
public function fetch(): array|false;
}

0 comments on commit fc17474

Please sign in to comment.