Luminova Framework

PHP Luminova: Database Driver Interface Specification

Last updated: 2024-08-28 21:45:59

Database Driver Interface: Facilitating Database Connectivity, Query Execution, and Result Handling. Explore our Database Example to Kickstart Your Integration.

Database drivers Interface, provides methods for executing database queries, statement preparation, and result fetching. To get started, see Database Example.

  • Class namespace: \Luminova\Interface\DriversInterface

constructor

Initialize database driver with your database connection configurations.

\Luminova\Database\Drivers\MySqliDriver - Mysqli connection driver.

new MySqliDriver(\Luminova\Base\CoreDatabase $config): mixed

\Luminova\Database\Drivers\PdoDriver - PDO connection driver.

new PdoDriver(\Luminova\Base\CoreDatabase $config): mixed

Parameters:

ParameterTypeDescription
$config\Luminova\Base\CoreDatabaseDatabase configuration.

Throws:


Methods

isConnected

Checks if the database is connected.

public isConnected(): bool

Return Value:

bool - True if connected, false otherwise.


getDriver

Get database connection driver name in use (e.g sqlite, mysql, cubrid etc..).

public getDriver(): ?string

Return Value:

string|null - Return driver name if connection is open, otherwise null.


raw

Get the actual database connection object of PDO or mysqli database.

public raw(): \Luminova\Interface\ConnInterface|null

Return Value:

\Luminova\Interface\ConnInterface|null - Connection instance if connected, null otherwise.


statement

Get prepared statement of a query result.

public statement(): PDOStatement|mysqli_stmt|mysqli_result|bool|null

Return Value:

PDOStatement|mysqli_stmt|mysqli_result|bool|null - Statement instance.


setDebug

Sets the debug mode.

public setDebug(bool $debug): self

Return Value:

self - The current instance.

Parameters:

ParameterTypeDescription
$debugboolThe debug mode.

error

Returns the error information for the last statement execution.

public error(): string

Return Value:

string - The error information.


errors

Returns all error information.

public errors(): array

Return Value:

array - The error information.


dumpDebug

Debug dumps statement information for the last statement execution.

public dumpDebug(): bool|null

Return Value:

bool|null - True on success, false on failure, or null if debug mode is disabled.


info

Returns information about the last statement execution.

public info(): array

Return Value:

array - The statement execution information.


prepare

Prepares a statement for execution.

public prepare(string $query): self

Return Value:

self - The current instance.

Parameters:

ParameterTypeDescription
$querystringThe SQL query.

query

Executes an sql query string.

public query(string $query): self

Return Value:

self - The current instance.

Parameters:

ParameterTypeDescription
$querystringThe SQL query.

exec

Execute an SQL statement and return the number of affected rows.

public exec(string $query): int

Return Value:

int - The number of affected rows.

Parameters:

ParameterTypeDescription
$querystringThe SQL statement to execute.

beginTransaction

Begins a transaction with optional read-only isolation level and SAVEPOINT for PDO.

public beginTransaction(int $flags = 0, ?string $name = null): bool

Parameters:

ParameterTypeDescription
$flagsintOptional flags to set transaction properties. (Default: 0).
No predefined flags for PDO, specify 4 to create read-only isolation.
$namestring|nullOptional name.
If provided in PDO, SAVEPOINT will be created with name instead.

Return Value:

bool - Return true if transaction started successfully, otherwise false.

Throws:

Note:

  • If $flags is set to 4 in PDO, which is equivalent to MYSQLI_TRANS_START_READ_ONLY, a read-only isolation level will be established. If the transaction starts successfully, it will return true.

  • If $name is specified in PDO, a SAVEPOINT will be created. If the savepoint creation is successful, the transaction will return true.


commit

Commits a transaction.

public commit(int $flags = 0, ?string $name = null): bool

Parameters:

ParameterTypeDescription
$flagsintOptional flags to set transaction properties. (Default: 0).
Only supported in MySQLi.
$namestring|nullOptional name.
Only supported in MySQLi.

Return Value:

bool - Returns true if the transaction was successfully committed, otherwise false.


rollback

Rolls back the current transaction or to a specific name while in PDO uses SAVEPOINT.

public rollback(int $flags = 0, ?string $name = null): bool

Parameters:

ParameterTypeDescription
$flagsintOptional flags to set transaction properties. (Default: 0).
Only supported in MySQLi.
$namestring|nullOptional name.
If provided in PDO, rolls back to the SAVEPOINT named

Return Value:

bool - Return true if rolled back was successful, otherwise false.

Throws:


inTransaction

Determine if there is any active transaction.

public inTransaction(): bool

Return Value:

bool - Return true if any active transaction, otherwise false.


getType

Returns the appropriate parameter type based on the value.

public static getType(mixed $value): string|int|null

Parameters:

ParameterTypeDescription
$valuemixedThe parameter value.

Return Value:

string|int|null - The parameter type.


bind

Binds a value to a parameter.

public bind(string $param, mixed $value, int|null $type = null): self

Parameters:

ParameterTypeDescription
$paramstringThe parameter identifier.
$valuemixedThe parameter value.
$typeint|nullThe parameter type.

Return Value:

self - The current instance.


param

Binds a variable to a parameter.

public param(string $param, mixed &$value, int|null $type = null): self

Parameters:

ParameterTypeDescription
$paramstringThe parameter identifier.
&$valuemixedThe parameter value passed by reference.
$typeint|nullThe parameter type.

Return Value:

self - The current instance.


execute

Executes the prepared statement.

public execute(array|null $params = null): bool

Parameters:

ParameterTypeDescription
$paramsarray|nullAn optional list array to bound parameters while executing statement. Each value is treated as a string.

Return Value:

bool - True on success, false on failure.

Throws:


ok

Check if query execution is completed successfully.

public function ok(): bool;

Return Value:

bool - True on success, false on failure.


rowCount

Returns the number of rows affected by the last statement execution.

public rowCount(): int|bool

Return Value:

int|bool - The number of rows, otherwise return false on failure.


getNext

Fetches a next single row as an object or array.

public getNext(string $type = 'object'): array|object|bool

Parameters:

ParameterTypeDescription
$typestringThe type of result to return ('object' or 'array').

Return Value:

array|object|bool - The result object based on mode, otherwise return false on failure.


getAll

Fetches all rows as an array or objects.

public getAll(string $type = 'object'): array|object|bool

Parameters:

ParameterTypeDescription
$typestringThe type of result to return ('object' or 'array').

Return Value:

array|object|bool - The result object based on mode, otherwise return false on failure.


getInt

Fetches selected rows as a 2D array of integers.

public getInt(): array|bool

Return Value:

array|bool - 2D array of integers else return false on failure.


getCount

Fetches total count of selected rows as integer.

public getCount(): int|bool

Return Value:

int|bool - Return integers else return false on failure.


getColumns

Get columns

public getColumns(int $mode = FETCH_COLUMN): array

Parameters:

ParameterTypeDescription
$modeintFetch column mode [FETCH_COLUMN or FETCH_COLUMN_ASSOC]

Return Value:

array - Return and associative array or indexed array of columns depending on mode.


fetch

Fetch result with a specific type and mode

public fetch(string $type = 'all', int $mode = FETCH_OBJ): mixed

Parameters:

ParameterTypeDescription
$typestringThe type of fetch method ('all' or 'next').
$modeintControls the contents of the returned

Return Value:

mixed - Return selected row item false on failure.


fetchObject

Fetches the result set as an object of the specified class or stdClass.

public fetchObject(string|null $class = 'stdClass', array $arguments = []): object|false

Parameters:

ParameterTypeDescription
$classstring|nullThe name of the class to instantiate, defaults to stdClass.
$argumentsarrayAdditional arguments to pass to the class constructor.

Return Value:

object|false - Returns the fetched object or false if no more rows are available or an error occurs.


getStatment

Get prepared statement depending on database connection specified in .env file.

public getStatment(): PDOStatement|mysqli_stmt|mysqli_result|bool|null

Return Value:

object|false - Returns prepared statement else false or null if not statement.


getLastInsertId

Returns the ID of the last inserted record or sequence value.

public getLastInsertId(): string

Return Value:

string - The last insert ID.


getItem

More fancy way to return record from statement (context: all, next, 2dint, total, lastId, count or column).

public getItem(int $mode = RETURN_ALL string $return = 'object'): mixed

Parameters:

ParameterTypeDescription
$modeintReturn mode RETURN_*
See database return modes .
$returnstring[The return data type (array or object)].

Return Value:

mixed - Return selected rows or false on failure.


free

Frees up the statement cursor and sets the statement object to null.

public free(): void

close

Frees up the statement cursor and close database connection.

public close(): void