From 56e5e53147af84bc30bafaa0658ec1b9ec40de20 Mon Sep 17 00:00:00 2001 From: MasterZydra Date: Mon, 15 Apr 2024 22:51:59 +0200 Subject: [PATCH] Added function `numRows` to ResultInterface --- .../Controllers/AmountDevelopmentStatsController.php | 2 +- .../FinancialRevenueProfitStatsController.php | 2 +- .../Controllers/OpenVolumeDistributionsController.php | 2 +- app/Models/DeliveryNote.php | 4 ++-- app/Models/Invoice.php | 4 ++-- framework/Database/BaseModel.php | 2 +- framework/Database/Database.php | 8 ++++---- framework/Database/MariaDbResult.php | 9 +++++++++ framework/Database/MigrationRunner.php | 6 ++---- framework/Database/ResultInterface.php | 2 ++ resources/Views/Components/datalistSubdistricts.php | 2 +- resources/Views/Components/invoiceYearSelect.php | 2 +- resources/Views/pdf/volumeDistribution.php | 2 +- 13 files changed, 28 insertions(+), 19 deletions(-) diff --git a/app/Http/Controllers/AmountDevelopmentStatsController.php b/app/Http/Controllers/AmountDevelopmentStatsController.php index 125f35b..9dd764e 100644 --- a/app/Http/Controllers/AmountDevelopmentStatsController.php +++ b/app/Http/Controllers/AmountDevelopmentStatsController.php @@ -16,7 +16,7 @@ public function execute(): void $dataSet = Database::unprepared('SELECT year, SUM(amount) AS amount FROM deliveryNotes GROUP BY year ORDER BY year ASC'); $data = []; if ($dataSet !== false) { - while ($row = $dataSet->fetch_assoc()) { + while ($row = $dataSet->fetch()) { $data[$row['year']] = $row['amount']; } } diff --git a/app/Http/Controllers/FinancialRevenueProfitStatsController.php b/app/Http/Controllers/FinancialRevenueProfitStatsController.php index d4a0c33..4c4eae2 100644 --- a/app/Http/Controllers/FinancialRevenueProfitStatsController.php +++ b/app/Http/Controllers/FinancialRevenueProfitStatsController.php @@ -21,7 +21,7 @@ public function execute(): void $dataSet = Database::executeBuilder(QueryBuilder::new('invoices')->select('DISTINCT year')); $years = []; if ($dataSet !== false) { - while ($row = $dataSet->fetch_assoc()) { + while ($row = $dataSet->fetch()) { $years[] = $row['year']; } } diff --git a/app/Http/Controllers/OpenVolumeDistributionsController.php b/app/Http/Controllers/OpenVolumeDistributionsController.php index ab1d209..18e96e6 100644 --- a/app/Http/Controllers/OpenVolumeDistributionsController.php +++ b/app/Http/Controllers/OpenVolumeDistributionsController.php @@ -29,7 +29,7 @@ public function execute(): void $deliveryNotes = []; if ($dataSet !== false) { - while ($row = $dataSet->fetch_assoc()) { + while ($row = $dataSet->fetch()) { $deliveryNote = DeliveryNote::findById($row['id']); $deliveryNote->setCalcSum($row['calcSum']); $deliveryNotes[] = $deliveryNote; diff --git a/app/Models/DeliveryNote.php b/app/Models/DeliveryNote.php index c1e53ff..b8318af 100644 --- a/app/Models/DeliveryNote.php +++ b/app/Models/DeliveryNote.php @@ -140,10 +140,10 @@ public static function nextDeliveryNoteNr(int $year = null): int ->select('MAX(nr) + 1 AS nextId') ->where(ColType::Int, 'year', Condition::Equal, $year) ); - if ($dataSet === false || $dataSet->num_rows !== 1) { + if ($dataSet === false || $dataSet->numRows() !== 1) { return 1; } - $row = $dataSet->fetch_assoc(); + $row = $dataSet->fetch(); if ($row['nextId'] === null) { return 1; } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 7915ec9..f3bc4de 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -94,10 +94,10 @@ public static function nextInvoiceNr(int $year = null): int ->select('MAX(nr) + 1 AS nextId') ->where(ColType::Int, 'year', Condition::Equal, $year) ); - if ($dataSet === false || $dataSet->num_rows !== 1) { + if ($dataSet === false || $dataSet->numRows() !== 1) { return 1; } - $row = $dataSet->fetch_assoc(); + $row = $dataSet->fetch(); if ($row['nextId'] === null) { return 1; } diff --git a/framework/Database/BaseModel.php b/framework/Database/BaseModel.php index 348cbba..82ae507 100644 --- a/framework/Database/BaseModel.php +++ b/framework/Database/BaseModel.php @@ -75,7 +75,7 @@ public static function all(WhereQueryBuilder $query = null): array } $all = []; - while ($row = $dataSet->fetch_assoc()) { + while ($row = $dataSet->fetch()) { array_push($all, static::new($row)); } return $all; diff --git a/framework/Database/Database.php b/framework/Database/Database.php index 4673bdc..cb16995 100644 --- a/framework/Database/Database.php +++ b/framework/Database/Database.php @@ -9,7 +9,7 @@ class Database private static ?MariaDB $mariaDB = null; /** Execute the SQL generated by the given query build */ - public static function executeBuilder(QueryBuilder|WhereQueryBuilder $queryBuilder): ResultInterface|bool + public static function executeBuilder(QueryBuilder|WhereQueryBuilder $queryBuilder): ResultInterface|false { if ($queryBuilder->isWhereEmpty()) { return self::unprepared($queryBuilder->build()); @@ -22,7 +22,7 @@ public static function executeBuilder(QueryBuilder|WhereQueryBuilder $queryBuild } } - public static function executeBlueprint(BlueprintInterface $blueprint): ResultInterface|bool + public static function executeBlueprint(BlueprintInterface $blueprint): ResultInterface|false { $result = false; foreach ($blueprint->build() as $sql) { @@ -35,7 +35,7 @@ public static function executeBlueprint(BlueprintInterface $blueprint): ResultIn } /** Execute the given unprepared query. You should try to avoid unprepared statements if they contain user input! */ - public static function unprepared(string $query): ResultInterface|bool + public static function unprepared(string $query): ResultInterface|false { self::getMariaDb(); self::$mariaDB->connect(); @@ -49,7 +49,7 @@ public static function unprepared(string $query): ResultInterface|bool * * @param string $colTypes i = int, d = float, s = string */ - public static function prepared(string $query, string $colTypes, ...$values): ResultInterface|bool + public static function prepared(string $query, string $colTypes, ...$values): ResultInterface|false { self::getMariaDb(); self::$mariaDB->connect(); diff --git a/framework/Database/MariaDbResult.php b/framework/Database/MariaDbResult.php index 5a42b81..433adb5 100644 --- a/framework/Database/MariaDbResult.php +++ b/framework/Database/MariaDbResult.php @@ -23,4 +23,13 @@ public function fetch(): array|false } return $row; } + + public function numRows(): int + { + if ($this->result === false) { + return 0; + } + + return $this->result->num_rows; + } } \ No newline at end of file diff --git a/framework/Database/MigrationRunner.php b/framework/Database/MigrationRunner.php index 2c5533c..1a960f7 100644 --- a/framework/Database/MigrationRunner.php +++ b/framework/Database/MigrationRunner.php @@ -94,8 +94,7 @@ private function isMigrationAlreadyApplied(string $migrationName): bool return false; } - /** @var \mysqli_result $result */ - return $result->num_rows === 1; + return $result->numRows() === 1; } /** Extract the migration name out of the complete file path */ @@ -129,7 +128,6 @@ private function doesMigrationsTableExists(): bool return false; } - /** @var \mysqli_result $result */ - return $result->num_rows === 1; + return $result->numRows() === 1; } } diff --git a/framework/Database/ResultInterface.php b/framework/Database/ResultInterface.php index 6a9fb0c..eda2c07 100644 --- a/framework/Database/ResultInterface.php +++ b/framework/Database/ResultInterface.php @@ -5,4 +5,6 @@ interface ResultInterface { public function fetch(): array|false; + + public function numRows(): int; } \ No newline at end of file diff --git a/resources/Views/Components/datalistSubdistricts.php b/resources/Views/Components/datalistSubdistricts.php index db66fe5..832f701 100644 --- a/resources/Views/Components/datalistSubdistricts.php +++ b/resources/Views/Components/datalistSubdistricts.php @@ -7,7 +7,7 @@ $subdistricts = []; if ($dataSet !== false) { - while ($row = $dataSet->fetch_assoc()) { + while ($row = $dataSet->fetch()) { $subdistricts[] = $row['subdistrict']; } } diff --git a/resources/Views/Components/invoiceYearSelect.php b/resources/Views/Components/invoiceYearSelect.php index 2985574..ea42b61 100644 --- a/resources/Views/Components/invoiceYearSelect.php +++ b/resources/Views/Components/invoiceYearSelect.php @@ -12,7 +12,7 @@ $years = []; if ($dataSet !== false) { - while ($row = $dataSet->fetch_assoc()) { + while ($row = $dataSet->fetch()) { $years[] = $row['year']; } } diff --git a/resources/Views/pdf/volumeDistribution.php b/resources/Views/pdf/volumeDistribution.php index dbfc4fe..8829dc1 100644 --- a/resources/Views/pdf/volumeDistribution.php +++ b/resources/Views/pdf/volumeDistribution.php @@ -40,7 +40,7 @@ Menge in - fetch_assoc()) { + fetch()) { $totalAmount += $row['amount']; ?>