Connection
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
Create or obtain an instance If no hash is provided, works as classic singleton If hash is provided, creates/returns instance for that hash
Create a new instance of the class and set it as the instance
Clear all instances including the classic singleton instance
Empty constructor since initialization is handled through factory methods and the Strategy pattern implementation
Triggered when invoking inaccessible methods in an object context
Triggered when invoking inaccessible methods in a static context
Get the strategy instance
This method is used to assign the database connection instance
This method is used to get the database connection instance
This method is used to establish a database connection
Pings a server connection, or tries to reconnect if the connection has gone down
Disconnects from a database.
Returns true when connection was established.
This function creates a new transaction, in order to be able to commit or rollback changes made to the database.
This function commits any changes made to the database during this transaction.
This function rolls back any changes made to the database during this transaction and restores the data to its original state.
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.
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.
This function quotes a string for use in an SQL statement and escapes special characters (such as quotes).
Returns an object containing the number of queried rows and the number of affected rows.
Returns the query string.
Sets the query string for the Connection instance.
Returns the parameters associated with this instance.
Sets the query parameters for the Connection instance.
Returns the number of rows affected by an operation.
Sets the number of query rows for the Connection instance.
Returns the number of columns in a statement result.
Sets the number of columns in the query result.
Returns the number of rows affected by an operation.
Sets the number of affected rows for the Connection instance.
Returns the statement for the function.
Set the statement for the function.
This function executes an SQL statement and returns the result set as a statement object.
This function binds the parameters to a prepared query.
This function runs an SQL statement and returns the number of affected rows.
Fetches the next row from the statement and returns it as an array.
Fetches all rows from the statement and returns them as an array.
This function retrieves an attribute from the database.
This function sets an attribute on the database.
This function returns an SQLSTATE code for the last operation executed by the database.
This function returns an array containing error information about the last operation performed by the database.
Sets an engine from the database.
Retrieves an engine from the database.
Sets a driver from the database.
Retrieves a driver from the database.
Sets a host from the database.
Retrieves a host from the database.
Sets a port from the database.
Retrieves a port from the database.
Sets a user from the database.
Retrieves a user from the database.
Sets a password from the database.
Retrieves a password from the database.
Sets a database name from the database.
Retrieves a database name from the database.
Sets an options from the database.
Retrieves an options from the database.
Sets a connected status from the database.
Retrieves a connected status from the database.
Sets a dsn string from the database.
Retrieves a dsn string from the database.
Sets an attributes from the database.
Retrieves an attributes from the database.
Sets a charset from the database.
Retrieves a charset from the database.
Sets an exception from the database.
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
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
IConnection
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.