Skip to content

Commit

Permalink
Added function numRows to ResultInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterZydra committed Apr 15, 2024
1 parent 2da8445 commit 56e5e53
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/AmountDevelopmentStatsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/OpenVolumeDistributionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions app/Models/DeliveryNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion framework/Database/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions framework/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions framework/Database/MariaDbResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 2 additions & 4 deletions framework/Database/MigrationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -129,7 +128,6 @@ private function doesMigrationsTableExists(): bool
return false;
}

/** @var \mysqli_result $result */
return $result->num_rows === 1;
return $result->numRows() === 1;
}
}
2 changes: 2 additions & 0 deletions framework/Database/ResultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
interface ResultInterface
{
public function fetch(): array|false;

public function numRows(): int;
}
2 changes: 1 addition & 1 deletion resources/Views/Components/datalistSubdistricts.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

$subdistricts = [];
if ($dataSet !== false) {
while ($row = $dataSet->fetch_assoc()) {
while ($row = $dataSet->fetch()) {
$subdistricts[] = $row['subdistrict'];
}
}
Expand Down
2 changes: 1 addition & 1 deletion resources/Views/Components/invoiceYearSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

$years = [];
if ($dataSet !== false) {
while ($row = $dataSet->fetch_assoc()) {
while ($row = $dataSet->fetch()) {
$years[] = $row['year'];
}
}
Expand Down
2 changes: 1 addition & 1 deletion resources/Views/pdf/volumeDistribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<td><b>Menge in <?= setting('massUnit') ?></b></td>
</tr>

<?php while ($row = $dataSet->fetch_assoc()) {
<?php while ($row = $dataSet->fetch()) {
$totalAmount += $row['amount'];
?>
<tr>
Expand Down

0 comments on commit 56e5e53

Please sign in to comment.