abstract class AbstractFetch implements IFetchAbstract (View source)

The GenericDatabase\Abstract\AbstractFetch class implements the IFetchAbstract interface and serves as a base class for fetching data in a generic database context. It manages the database connection and the fetch strategy, and provides a set of methods for fetching data in various formats (e.g., as objects, associative arrays, numeric arrays, etc.).

Main functionalities:

  • Manages the database connection and fetch strategy.
  • Provides a base implementation for fetching data from a database using a generic approach.
  • Supports fetching data in various formats (e.g., as objects, associative arrays, numeric arrays, etc.).
  • Acts as a foundation for implementing the IFetchAbstract interface.

Methods:

  • getStrategy(): IFetchStrategy: Returns the current fetch strategy.
  • getInstance(): IConnection: Returns the database connection instance.
  • setStrategy(IFetchStrategy $strategy): void: Sets the fetch strategy.
  • internalFetchAssoc(): array|false|null: Fetches the current row as an associative array.
  • internalFetchNum(): array|false|null: Fetches the current row as a numeric array.
  • internalFetchBoth(): bool|array: Fetches the current row as both an associative and a numeric array.
  • internalFetchColumn(int $columnIndex = 0): false|string: Fetches a single value from the result set, or false if there are no more rows.
  • internalFetchClass(?array $constructorArguments = null, ?string $aClassOrObject = '\stdClass'): object|false: Fetches a single row from the result set as an object.
  • internalFetchAllAssoc(): array: Fetches all rows from the result set as an associative array, or an empty array if there are no more rows.
  • internalFetchAllNum(): array: Fetches all rows from the result set as a numeric array, or an empty array if there are no more rows.
  • internalFetchAllBoth(): array: Fetches all rows from the result set as an array of arrays, where each row is both numerically and associatively indexed.
  • internalFetchAllColumn(int $columnIndex = 0): array: Fetches all values of a single column from the result set as an array of strings.
  • internalFetchAllClass(?array $constructorArguments = [], ?string $aClassOrObject = '\stdClass'): array: Fetches all rows from the result set as an array of objects, each one being an instance of the provided class.

Fields:

  • $instance: The connection instance used for dynamic operations.
  • $fetchStrategy: The options handler for managing configuration.

Concrete implementations of this class should inherit from AbstractFetch and provide their own implementation of the database connection and fetch strategy.

Properties

static protected IConnection $instance
static protected IFetchStrategy $fetchStrategy

Methods

__construct(IConnection $instance, IFetchStrategy $fetchStrategy)

Initialize fetch abstraction with connection and strategy

getStrategy()

Get the current fetch strategy

getInstance()

Get the database connection instance

object|false
internalFetchClass(array|null $constructorArguments = null, string|null $aClassOrObject = '\\stdClass')

Fetches a single row from the result set as an object

bool|array
internalFetchBoth()

Fetches the current row as both an associative and a numeric array.

array|null|false
internalFetchAssoc()

Fetches a single row from the result set as a numerically indexed array, converting all values to strings, or returns false if there are no more rows.

array|false|null
internalFetchNum()

Fetches the current row as a numeric array.

false|string
internalFetchColumn(int $columnIndex = 0)

Fetches a single value from the result set, or false if there are no more rows.

array
internalFetchAllAssoc()

Fetches all rows from the result set as an associative array, or an empty array if there are no more rows.

array
internalFetchAllNum()

Fetches all rows from the result set as a numerically indexed array of arrays, converting all values to strings, or an empty array if there are no more rows.

array
internalFetchAllBoth()

Fetches all rows from the result set as an array of arrays, where each row is both numerically and associatively indexed.

array
internalFetchAllColumn(int $columnIndex = 0)

Fetches all values of a single column from the result set as an array of strings.

array
internalFetchAllClass(array|null $constructorArguments = [], string|null $aClassOrObject = '\\stdClass')

Fetches all rows from the result set as an array of objects, each one being an instance of the provided class.

Details

__construct(IConnection $instance, IFetchStrategy $fetchStrategy)

Initialize fetch abstraction with connection and strategy

Parameters

IConnection $instance

Database connection instance

IFetchStrategy $fetchStrategy

Strategy for fetching results

IFetchStrategy getStrategy()

Get the current fetch strategy

Return Value

IFetchStrategy

Current fetch strategy instance

IConnection getInstance()

Get the database connection instance

Return Value

IConnection

Current database connection instance

object|false internalFetchClass(array|null $constructorArguments = null, string|null $aClassOrObject = '\\stdClass')

Fetches a single row from the result set as an object

Parameters

array|null $constructorArguments

Arguments to pass to the class constructor.

string|null $aClassOrObject

Class name or object to hydrate.

Return Value

object|false

Exceptions

ReflectionException

bool|array internalFetchBoth()

Fetches the current row as both an associative and a numeric array.

Return Value

bool|array

Returns an associative and numeric array if successful, false on failure.

array|null|false internalFetchAssoc()

Fetches a single row from the result set as a numerically indexed array, converting all values to strings, or returns false if there are no more rows.

Return Value

array|null|false

A numerically indexed array of the row values as strings, false if no more rows, or null on error.

array|false|null internalFetchNum()

Fetches the current row as a numeric array.

Return Value

array|false|null

Returns a numeric array if successful, null if no row was found, or false on failure.

false|string internalFetchColumn(int $columnIndex = 0)

Fetches a single value from the result set, or false if there are no more rows.

Parameters

int $columnIndex

Index of the value to fetch. If not provided, fetches the first column.

Return Value

false|string

The value of the row at the specified index, false if no more rows, or null on error.

array internalFetchAllAssoc()

Fetches all rows from the result set as an associative array, or an empty array if there are no more rows.

Return Value

array

An associative array of the row values as strings, or an empty array if no more rows, or null on error.

array internalFetchAllNum()

Fetches all rows from the result set as a numerically indexed array of arrays, converting all values to strings, or an empty array if there are no more rows.

Return Value

array

A numerically indexed array of the row values as strings, or an empty array if no more rows, or null on error.

array internalFetchAllBoth()

Fetches all rows from the result set as an array of arrays, where each row is both numerically and associatively indexed.

All values are converted to strings. If there are no more rows, an empty array is returned.

Return Value

array

An array of the row values as strings, with both numerical and associative indexes, or an empty array if no more rows, or null on error.

array internalFetchAllColumn(int $columnIndex = 0)

Fetches all values of a single column from the result set as an array of strings.

Parameters

int $columnIndex

Index of the value to fetch. If not provided, fetches the first column.

Return Value

array

An array of the column values as strings, or an empty array if no more rows, or null on error.

array internalFetchAllClass(array|null $constructorArguments = [], string|null $aClassOrObject = '\\stdClass')

Fetches all rows from the result set as an array of objects, each one being an instance of the provided class.

The class is created with the provided constructor arguments and properties are set with the values from the row. If there are no more rows, an empty array is returned.

Parameters

array|null $constructorArguments

Arguments to pass to the class constructor.

string|null $aClassOrObject

Class name or object to hydrate.

Return Value

array

An array of the row values as objects, or an empty array if no more rows, or null on error.

Exceptions

ReflectionException