Skip to content

Commit

Permalink
feat(database): Add replacements for deprecated fetch and fetchAll
Browse files Browse the repository at this point in the history
DBAL deprecated these and will remove them it in the future.

Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Sep 27, 2023
1 parent f134244 commit 35968f2
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/private/DB/ResultAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*/
namespace OC\DB;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Result;
use OC\DB\Exceptions\DbalException;
use OCP\DB\IResult;
use PDO;

Expand All @@ -50,6 +52,22 @@ public function fetch(int $fetchMode = PDO::FETCH_ASSOC) {
return $this->inner->fetch($fetchMode);
}

public function fetchAssociative(): array|false {
try {
return $this->inner->fetchAssociative();
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function fetchAllAssociative(): array {
try {
return $this->inner->fetchAllAssociative();
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array {
if ($fetchMode !== PDO::FETCH_ASSOC && $fetchMode !== PDO::FETCH_NUM && $fetchMode !== PDO::FETCH_COLUMN) {
throw new \Exception('Fetch mode needs to be assoc, num or column.');
Expand All @@ -61,10 +79,34 @@ public function fetchColumn($columnIndex = 0) {
return $this->inner->fetchOne();
}

public function fetchNumeric(): array|false {
try {
return $this->inner->fetchNumeric();
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function fetchAllNumeric(): array {
try {
return $this->inner->fetchAllNumeric();
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function fetchOne() {
return $this->inner->fetchOne();
}

public function fetchFirstColumn(): array {
try {
return $this->inner->fetchAllNumeric();
} catch (Exception $e) {
throw DbalException::wrap($e);
}
}

public function rowCount(): int {
return $this->inner->rowCount();
}
Expand Down
52 changes: 52 additions & 0 deletions lib/public/DB/IResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,37 @@ public function closeCursor(): bool;
* @return mixed
*
* @since 21.0.0
* @deprecated 28.0.0 use fetchAssociative, fetchNumeric or fetchOne
*/
public function fetch(int $fetchMode = PDO::FETCH_ASSOC);

/**
* Returns the next row of the result as an associative array or FALSE if there are no more rows.
*
* @return array<string,mixed>|false
* @throws Exception
*
* @since 28.0.0
*/
public function fetchAssociative(): array|false;

/**
* Returns an array containing all of the result rows represented as associative arrays
*
* @return list<array<string,mixed>>
* @throws Exception
*
* @since 28.0.0
*/
public function fetchAllAssociative(): array;

/**
* @param int $fetchMode (one of PDO::FETCH_ASSOC, PDO::FETCH_NUM or PDO::FETCH_COLUMN (2, 3 or 7)
*
* @return mixed[]
*
* @since 21.0.0
* @deprecated 28.0.0 use fetchAllAssociative, fetchAllNumeric or fetchOne
*/
public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array;

Expand All @@ -77,6 +99,26 @@ public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array;
*/
public function fetchColumn();

/**
* Returns the next row of the result as a numeric array or FALSE if there are no more rows
*
* @return list<mixed>|false
* @throws Exception
*
* @since 28.0.0
*/
public function fetchNumeric(): array|false;

/**
* Returns an array containing all of the result rows represented as numeric arrays
*
* @return list<list<mixed>>
* @throws Exception
*
* @since 28.0.0
*/
public function fetchAllNumeric(): array;

/**
* Returns the first value of the next row of the result or FALSE if there are no more rows.
*
Expand All @@ -86,6 +128,16 @@ public function fetchColumn();
*/
public function fetchOne();

/**
* Returns an array containing all of the result rows represented as numeric arrays
*
* @return list<list<mixed>>
* @throws Exception
*
* @since 21.0.0
*/
public function fetchFirstColumn(): array;

/**
* @return int
*
Expand Down

0 comments on commit 35968f2

Please sign in to comment.