class Connection implements IConnection, IConnectionStrategy (View source)

The GenericDatabase\Connection class is responsible for establishing and managing database connections.

It uses a strategy pattern to support different database engines. The class provides methods for connecting to a database, executing queries, fetching results, and managing the connection state.

Example Usage:

//Create a new connection instance
$connection = Connection::getInstance();

//Set the database engine
$connection->setEngine('pdo');

//Set connection parameters
$connection->setHost('localhost');
$connection->setPort(3306);
$connection->setUser('root');
$connection->setPassword('password');

//Connect to the database
$connection->connect();

//Execute a query
$result = $connection->query('SELECT * FROM users');

//Fetch all rows from the result set
$rows = $connection->fetchAll($result);

//Disconnect from the database
$connection->disconnect();

Dynamic and Static container class for Connection connections.

Traits

This trait provides methods to manage a single instance of a class or multiple instances identified by a unique hash. It includes methods to get, create, set, and clear instances.

Constants

FETCH_LAZY

Fetch mode that starts fetching rows only when they are requested.

FETCH_ASSOC

Constant for the fetch mode representing fetching as an associative array

FETCH_NUM

Constant for the fetch mode representing fetching as a numeric array

FETCH_BOTH

Constant for the fetch mode representing fetching as both a numeric and associative array

FETCH_OBJ

Constant for the fetch mode representing fetching as an object

FETCH_BOUND

Fetch mode that requires explicit binding of PHP variables to fetch values.

FETCH_COLUMN

Constant for the fetch mode representing fetching a single column

FETCH_CLASS

Constant for the fetch mode representing fetching into a new instance of a specified class

FETCH_INTO

Constant for the fetch mode representing fetching into an existing object

Methods

static Singleton
getInstance(string|null $hash = null)

Create or obtain an instance If no hash is provided, works as classic singleton If hash is provided, creates/returns instance for that hash

static Singleton
newInstance(string|null $hash = null)

Create a new instance of the class and set it as the instance

static void
setInstance(Singleton $instance, string|null $hash = null)

Set a specific instance

static void
clearInstance(string|null $hash = null)

Clear instance(s)

static void
clearAllInstances()

Clear all instances including the classic singleton instance

__construct()

Empty constructor since initialization is handled through factory methods and the Strategy pattern implementation

Connection|string|int|bool|array|null
__call(string $name, array $arguments)

Triggered when invoking inaccessible methods in an object context

static Connection
__callStatic(string $name, array $arguments)

Triggered when invoking inaccessible methods in a static context

void
setStrategy(IConnection $strategy)

Defines the strategy instance

getStrategy()

Get the strategy instance

mixed
setConnection(mixed $connection)

This method is used to assign the database connection instance

mixed
getConnection()

This method is used to get the database connection instance

mixed
connect()

This method is used to establish a database connection

bool
ping()

Pings a server connection, or tries to reconnect if the connection has gone down

void
disconnect()

Disconnects from a database.

bool
isConnected()

Returns true when connection was established.

bool
beginTransaction()

This function creates a new transaction, in order to be able to commit or rollback changes made to the database.

bool
commit()

This function commits any changes made to the database during this transaction.

bool
rollback()

This function rolls back any changes made to the database during this transaction and restores the data to its original state.

bool
inTransaction()

This function returns the last ID generated by an auto-increment column, either the last one inserted during the current transaction, or by passing in the optional name parameter.

string|int|false
lastInsertId(string|null $name = null)

This function returns the last ID generated by an auto-increment column, either the last one inserted during the current transaction, or by passing in the optional name parameter.

mixed
quote(mixed ...$params)

This function quotes a string for use in an SQL statement and escapes special characters (such as quotes).

object
getAllMetadata()

Returns an object containing the number of queried rows and the number of affected rows.

string
getQueryString()

Returns the query string.

void
setQueryString(string $params)

Sets the query string for the Connection instance.

array|null
getQueryParameters()

Returns the parameters associated with this instance.

void
setQueryParameters(array|null $params)

Sets the query parameters for the Connection instance.

int|false
getQueryRows()

Returns the number of rows affected by an operation.

void
setQueryRows(callable|int|false $params)

Sets the number of query rows for the Connection instance.

int|false
getQueryColumns()

Returns the number of columns in a statement result.

void
setQueryColumns(int|false $params)

Sets the number of columns in the query result.

int|false
getAffectedRows()

Returns the number of rows affected by an operation.

void
setAffectedRows(int|false $params)

Sets the number of affected rows for the Connection instance.

mixed
getStatement()

Returns the statement for the function.

void
setStatement(mixed $statement)

Set the statement for the function.

mixed
query(mixed ...$params)

This function executes an SQL statement and returns the result set as a statement object.

mixed
prepare(mixed ...$params)

This function binds the parameters to a prepared query.

mixed
exec(mixed ...$params)

This function runs an SQL statement and returns the number of affected rows.

mixed
fetch(int|null $fetchStyle = null, mixed $fetchArgument = null, mixed $optArgs = null)

Fetches the next row from the statement and returns it as an array.

mixed
fetchAll(int|null $fetchStyle = null, mixed $fetchArgument = null, mixed $optArgs = null)

Fetches all rows from the statement and returns them as an array.

mixed
getAttribute(mixed $name)

This function retrieves an attribute from the database.

void
setAttribute(mixed $name, mixed $value)

This function sets an attribute on the database.

int|string|array|bool|null
errorCode(mixed $inst = null)

This function returns an SQLSTATE code for the last operation executed by the database.

string|array|bool|null
errorInfo(mixed $inst = null)

This function returns an array containing error information about the last operation performed by the database.

static Connection|void
setEngine(mixed $value)

Sets an engine from the database.

static Connection|static
getEngine(mixed $value)

Retrieves an engine from the database.

static Connection|void
setDriver(mixed $value)

Sets a driver from the database.

static Connection|string
getDriver(mixed $value)

Retrieves a driver from the database.

static Connection|void
setHost(mixed $value)

Sets a host from the database.

static Connection|string
getHost(mixed $value)

Retrieves a host from the database.

static Connection|void
setPort(mixed $value)

Sets a port from the database.

static Connection|int
getPort(mixed $value)

Retrieves a port from the database.

static Connection|void
setUser(mixed $value)

Sets a user from the database.

static Connection|string
getUser(mixed $value)

Retrieves a user from the database.

static Connection|void
setPassword(mixed $value)

Sets a password from the database.

static Connection|string
getPassword(mixed $value)

Retrieves a password from the database.

static Connection|void
setDatabase(mixed $value)

Sets a database name from the database.

static Connection|string
getDatabase(mixed $value)

Retrieves a database name from the database.

static Connection|void
setOptions(mixed $value)

Sets an options from the database.

static Connection|array|null
getOptions(mixed $value)

Retrieves an options from the database.

static Connection|static
setConnected(mixed $value)

Sets a connected status from the database.

static Connection|mixed
getConnected(mixed $value)

Retrieves a connected status from the database.

static Connection|void
setDsn(mixed $value)

Sets a dsn string from the database.

static Connection|mixed
getDsn(mixed $value)

Retrieves a dsn string from the database.

static Connection|void
setAttributes(mixed $value)

Sets an attributes from the database.

static Connection|mixed
getAttributes(mixed $value)

Retrieves an attributes from the database.

static Connection|void
setCharset(mixed $value)

Sets a charset from the database.

static Connection|string
getCharset(mixed $value)

Retrieves a charset from the database.

static Connection|void
setException(mixed $value)

Sets an exception from the database.

static Connection|mixed
getException(mixed $value)

Retrieves an exception from the database.

Details

static Singleton getInstance(string|null $hash = null)

Create or obtain an instance If no hash is provided, works as classic singleton If hash is provided, creates/returns instance for that hash

Parameters

string|null $hash

Optional unique identifier for multiple instances

Return Value

Singleton

static Singleton newInstance(string|null $hash = null)

Create a new instance of the class and set it as the instance

Parameters

string|null $hash

Optional unique identifier for multiple instances

Return Value

Singleton

The newly created instance

static void setInstance(Singleton $instance, string|null $hash = null)

Set a specific instance

Parameters

Singleton $instance

The instance to set

string|null $hash

Optional unique identifier for multiple instances

Return Value

void

static void clearInstance(string|null $hash = null)

Clear instance(s)

Parameters

string|null $hash

Optional unique identifier for multiple instances

Return Value

void

static void clearAllInstances()

Clear all instances including the classic singleton instance

Return Value

void

__construct()

Empty constructor since initialization is handled through factory methods and the Strategy pattern implementation

Connection|string|int|bool|array|null __call(string $name, array $arguments)

Triggered when invoking inaccessible methods in an object context

Parameters

string $name

Name of the method

array $arguments

Array of arguments

Return Value

Connection|string|int|bool|array|null

static Connection __callStatic(string $name, array $arguments)

Triggered when invoking inaccessible methods in a static context

Parameters

string $name

Name of the method

array $arguments

Array of arguments

Return Value

Connection

Exceptions

ReflectionException

void setStrategy(IConnection $strategy)

Defines the strategy instance

Parameters

IConnection $strategy

Return Value

void

IConnection getStrategy()

Get the strategy instance

Return Value

IConnection

mixed setConnection(mixed $connection)

This method is used to assign the database connection instance

Parameters

mixed $connection

Sets an instance of the connection with the database

Return Value

mixed

mixed getConnection()

This method is used to get the database connection instance

Return Value

mixed

mixed connect()

This method is used to establish a database connection

Return Value

mixed

bool ping()

Pings a server connection, or tries to reconnect if the connection has gone down

Return Value

bool

void disconnect()

Disconnects from a database.

Return Value

void

bool isConnected()

Returns true when connection was established.

Return Value

bool

bool beginTransaction()

This function creates a new transaction, in order to be able to commit or rollback changes made to the database.

Return Value

bool

bool commit()

This function commits any changes made to the database during this transaction.

Return Value

bool

bool rollback()

This function rolls back any changes made to the database during this transaction and restores the data to its original state.

Return Value

bool

bool inTransaction()

This function returns the last ID generated by an auto-increment column, either the last one inserted during the current transaction, or by passing in the optional name parameter.

Return Value

bool

string|int|false lastInsertId(string|null $name = null)

This function returns the last ID generated by an auto-increment column, either the last one inserted during the current transaction, or by passing in the optional name parameter.

Parameters

string|null $name

= null Resource name, table or view

Return Value

string|int|false

mixed quote(mixed ...$params)

This function quotes a string for use in an SQL statement and escapes special characters (such as quotes).

Parameters

mixed ...$params

Content to be quoted

Return Value

mixed

object getAllMetadata()

Returns an object containing the number of queried rows and the number of affected rows.

Return Value

object

An associative object with keys 'queryRows' and 'affectedRows'.

string getQueryString()

Returns the query string.

Return Value

string

The query string associated with this instance.

void setQueryString(string $params)

Sets the query string for the Connection instance.

Parameters

string $params

The query string to set.

Return Value

void

array|null getQueryParameters()

Returns the parameters associated with this instance.

Return Value

array|null

The parameters associated with this instance.

void setQueryParameters(array|null $params)

Sets the query parameters for the Connection instance.

Parameters

array|null $params

The query parameters to set.

Return Value

void

int|false getQueryRows()

Returns the number of rows affected by an operation.

Return Value

int|false

The number of affected rows

void setQueryRows(callable|int|false $params)

Sets the number of query rows for the Connection instance.

Parameters

callable|int|false $params

The number of query rows to set.

Return Value

void

int|false getQueryColumns()

Returns the number of columns in a statement result.

Return Value

int|false

The number of columns in the result or false in case of an error.

void setQueryColumns(int|false $params)

Sets the number of columns in the query result.

Parameters

int|false $params

The number of columns or false if there are no columns.

Return Value

void

int|false getAffectedRows()

Returns the number of rows affected by an operation.

Return Value

int|false

The number of affected rows

void setAffectedRows(int|false $params)

Sets the number of affected rows for the Connection instance.

Parameters

int|false $params

The number of affected rows to set.

Return Value

void

mixed getStatement()

Returns the statement for the function.

Return Value

mixed

void setStatement(mixed $statement)

Set the statement for the function.

Parameters

mixed $statement

The statement to be set.

Return Value

void

mixed query(mixed ...$params)

This function executes an SQL statement and returns the result set as a statement object.

Parameters

mixed ...$params

Statement to be queried

Return Value

mixed

mixed prepare(mixed ...$params)

This function binds the parameters to a prepared query.

Parameters

mixed ...$params

Statement to be prepared

Return Value

mixed

mixed exec(mixed ...$params)

This function runs an SQL statement and returns the number of affected rows.

Parameters

mixed ...$params

Statement to be executed

Return Value

mixed

mixed fetch(int|null $fetchStyle = null, mixed $fetchArgument = null, mixed $optArgs = null)

Fetches the next row from the statement and returns it as an array.

Parameters

int|null $fetchStyle

The fetch style to use.

mixed $fetchArgument

The fetch argument to use.

mixed $optArgs

Additional options for the fetch operation.

Return Value

mixed

The query result.

mixed fetchAll(int|null $fetchStyle = null, mixed $fetchArgument = null, mixed $optArgs = null)

Fetches all rows from the statement and returns them as an array.

Parameters

int|null $fetchStyle

The fetch style to use.

mixed $fetchArgument

The fetch argument to use.

mixed $optArgs

Additional options for the fetch operation.

Return Value

mixed

The query result.

mixed getAttribute(mixed $name)

This function retrieves an attribute from the database.

Parameters

mixed $name

The attribute name

Return Value

mixed

void setAttribute(mixed $name, mixed $value)

This function sets an attribute on the database.

Parameters

mixed $name

The attribute name

mixed $value

The attribute value

Return Value

void

int|string|array|bool|null errorCode(mixed $inst = null)

This function returns an SQLSTATE code for the last operation executed by the database.

Parameters

mixed $inst

= null Resource name, table or view

Return Value

int|string|array|bool|null

string|array|bool|null errorInfo(mixed $inst = null)

This function returns an array containing error information about the last operation performed by the database.

Parameters

mixed $inst

= null Resource name, table or view

Return Value

string|array|bool|null

static Connection|void setEngine(mixed $value)

Sets an engine from the database.

Parameters

mixed $value

Return Value

Connection|void

static Connection|static getEngine(mixed $value)

Retrieves an engine from the database.

Parameters

mixed $value

Return Value

Connection|static

static Connection|void setDriver(mixed $value)

Sets a driver from the database.

Parameters

mixed $value

Return Value

Connection|void

static Connection|string getDriver(mixed $value)

Retrieves a driver from the database.

Parameters

mixed $value

Return Value

Connection|string

static Connection|void setHost(mixed $value)

Sets a host from the database.

Parameters

mixed $value

Return Value

Connection|void

static Connection|string getHost(mixed $value)

Retrieves a host from the database.

Parameters

mixed $value

Return Value

Connection|string

static Connection|void setPort(mixed $value)

Sets a port from the database.

Parameters

mixed $value

Return Value

Connection|void

static Connection|int getPort(mixed $value)

Retrieves a port from the database.

Parameters

mixed $value

Return Value

Connection|int

static Connection|void setUser(mixed $value)

Sets a user from the database.

Parameters

mixed $value

Return Value

Connection|void

static Connection|string getUser(mixed $value)

Retrieves a user from the database.

Parameters

mixed $value

Return Value

Connection|string

static Connection|void setPassword(mixed $value)

Sets a password from the database.

Parameters

mixed $value

Return Value

Connection|void

static Connection|string getPassword(mixed $value)

Retrieves a password from the database.

Parameters

mixed $value

Return Value

Connection|string

static Connection|void setDatabase(mixed $value)

Sets a database name from the database.

Parameters

mixed $value

Return Value

Connection|void

static Connection|string getDatabase(mixed $value)

Retrieves a database name from the database.

Parameters

mixed $value

Return Value

Connection|string

static Connection|void setOptions(mixed $value)

Sets an options from the database.

Parameters

mixed $value

Return Value

Connection|void

static Connection|array|null getOptions(mixed $value)

Retrieves an options from the database.

Parameters

mixed $value

Return Value

Connection|array|null

static Connection|static setConnected(mixed $value)

Sets a connected status from the database.

Parameters

mixed $value

Return Value

Connection|static

static Connection|mixed getConnected(mixed $value)

Retrieves a connected status from the database.

Parameters

mixed $value

Return Value

Connection|mixed

static Connection|void setDsn(mixed $value)

Sets a dsn string from the database.

Parameters

mixed $value

Return Value

Connection|void

static Connection|mixed getDsn(mixed $value)

Retrieves a dsn string from the database.

Parameters

mixed $value

Return Value

Connection|mixed

static Connection|void setAttributes(mixed $value)

Sets an attributes from the database.

Parameters

mixed $value

Return Value

Connection|void

static Connection|mixed getAttributes(mixed $value)

Retrieves an attributes from the database.

Parameters

mixed $value

Return Value

Connection|mixed

static Connection|void setCharset(mixed $value)

Sets a charset from the database.

Parameters

mixed $value

Return Value

Connection|void

static Connection|string getCharset(mixed $value)

Retrieves a charset from the database.

Parameters

mixed $value

Return Value

Connection|string

static Connection|void setException(mixed $value)

Sets an exception from the database.

Parameters

mixed $value

Return Value

Connection|void

static Connection|mixed getException(mixed $value)

Retrieves an exception from the database.

Parameters

mixed $value

Return Value

Connection|mixed