class Connection

Database connection.

Properties

protected string $name Connection name.
protected string. $queryCompiler Query compiler.
protected string $queryBuilderHelper Query builder helper.
protected string $dsn Connection DSN.
protected string $username Database username.
protected string $password Database password.
protected bool $enableLog Enable the query log?
protected bool $reconnect Should we reconnect?
protected bool $usePersistentConnection Should we use a persistent connection?
protected array $options PDO options.
protected array $onConnectQueries Queries that should be executed upon connecting.
protected PDO $pdo PDO object.
protected int $transactionNestingLevel Transaction nesting level.
protected array $log Query log.
protected bool $supportsTransactionalDDL Does the connection support transactional DDL?

Methods

__construct(string $name, string $queryCompiler, string $queryBuilderHelper, array $config)

Constructor.

close()

Closes the database connection.

bool
supportsTransactionalDDL()

Does the connection support transactional DDL?

string
getName()

Returns the connection name.

getQueryBuilderHelper()

Returns a query builder helper instance.

getQueryCompiler(Query $query)

Returns a query compiler instance.

PDO
getPDO()

Returns the PDO instance.

enableLog()

Enables the query log.

disableLog()

Disables the query log.

array
getConnectionOptions()

Returns the connection options.

PDO
connect()

Creates a PDO instance.

reconnect()

Creates a new PDO instance.

bool
isAlive()

Checks if the connection is alive.

string
prepareQueryForLog(string $query, array $params)

Prepares query for logging.

log(string $query, array $params, float $start)

Adds a query to the query log.

clearLog()

Clears the query log.

array
getLog()

Returns the query log for the connection.

array
prepareQueryAndParams(string $query, array $params)

Prepare query and params.

bool
isConnectionLostAndShouldItBeReestablished()

Should we try to reestablish the connection?

bindParameter(PDOStatement $statement, int $key, mixed $value)

Binds parameter to the prepared statement.

prepareAndExecute(string $query, array $params, bool $success = null)

Prepares and executes a query.

bool
query(string $query, array $params = [])

Executes the query and returns TRUE on success or FALSE on failure.

int
queryAndCount(string $query, array $params = [])

Executes the query and return number of affected rows.

mixed|bool
first(string $query, array $params = [], mixed ...$fetchMode)

Returns the first row of the result set.

array
all(string $query, array $params = [], mixed ...$fetchMode)

Returns an array containing all of the result set rows.

mixed|bool
column(string $query, array $params = [])

Returns the value of the first column of the first row of the result set.

array
columns(string $query, array $params = [])

Returns an array containing the values of the indicated 0-indexed column.

array
pairs(string $query, array $params = [])

Returns an array where the first column is used as keys and the second as values.

yield(string $query, array $params = [], mixed ...$fetchMode)

Returns a generator that lets you iterate over the results.

builder()

Returns a query builder instance.

table(string|array|Closure|Subquery|Raw|null $table)

Returns a query builder instance where we have already chosen the table we want to query.

bool
createSavepoint()

Creates a new savepoint.

bool
rollBackSavepoint()

Rolls back to the previously created savepoint.

bool
beginTransaction()

Begin a transaction.

bool
commitTransaction()

Commits a transaction.

bool
rollBackTransaction()

Roll back a transaction.

int
getTransactionNestingLevel()

Returns the transaction nesting level.

bool
inTransaction()

Returns TRUE if we're in a transaction and FALSE if not.

mixed
transaction(Closure $queries)

Executes queries and rolls back the transaction if any of them fail.

Details

at line 160
__construct(string $name, string $queryCompiler, string $queryBuilderHelper, array $config)

Constructor.

Parameters

string $name Connection name
string $queryCompiler Query compiler
string $queryBuilderHelper Query builder helper
array $config Connection configuration

at line 196
close()

Closes the database connection.

at line 206
bool supportsTransactionalDDL()

Does the connection support transactional DDL?

Return Value

bool

at line 216
string getName()

Returns the connection name.

Return Value

string

at line 226
HelperInterface getQueryBuilderHelper()

Returns a query builder helper instance.

Return Value

HelperInterface

at line 239
Compiler getQueryCompiler(Query $query)

Returns a query compiler instance.

Parameters

Query $query Query

Return Value

Compiler

at line 251
PDO getPDO()

Returns the PDO instance.

Return Value

PDO

at line 259
enableLog()

Enables the query log.

at line 267
disableLog()

Disables the query log.

at line 277
protected array getConnectionOptions()

Returns the connection options.

Return Value

array

at line 294
protected PDO connect()

Creates a PDO instance.

Return Value

PDO

at line 322
reconnect()

Creates a new PDO instance.

at line 332
bool isAlive()

Checks if the connection is alive.

Return Value

bool

at line 353
protected string prepareQueryForLog(string $query, array $params)

Prepares query for logging.

Parameters

string $query SQL query
array $params Query paramaters

Return Value

string

at line 389
protected log(string $query, array $params, float $start)

Adds a query to the query log.

Parameters

string $query SQL query
array $params Query parameters
float $start Start time in microseconds

at line 401
clearLog()

Clears the query log.

at line 411
array getLog()

Returns the query log for the connection.

Return Value

array

at line 423
protected array prepareQueryAndParams(string $query, array $params)

Prepare query and params.

Parameters

string $query SQL Query
array $params Query parameters

Return Value

array

at line 454
protected bool isConnectionLostAndShouldItBeReestablished()

Should we try to reestablish the connection?

Return Value

bool

at line 466
protected bindParameter(PDOStatement $statement, int $key, mixed $value)

Binds parameter to the prepared statement.

Parameters

PDOStatement $statement PDO statement
int $key Parameter key
mixed $value Parameter value

at line 506
protected PDOStatement prepareAndExecute(string $query, array $params, bool $success = null)

Prepares and executes a query.

Parameters

string $query SQL query
array $params Query parameters
bool $success &$success Was the query executed successfully?

Return Value

PDOStatement

at line 563
bool query(string $query, array $params = [])

Executes the query and returns TRUE on success or FALSE on failure.

Parameters

string $query SQL query
array $params Query parameters

Return Value

bool

at line 577
int queryAndCount(string $query, array $params = [])

Executes the query and return number of affected rows.

Parameters

string $query SQL query
array $params Query parameters

Return Value

int

at line 590
mixed|bool first(string $query, array $params = [], mixed ...$fetchMode)

Returns the first row of the result set.

Parameters

string $query SQL query
array $params Query params
mixed ...$fetchMode Fetch mode

Return Value

mixed|bool

at line 610
array all(string $query, array $params = [], mixed ...$fetchMode)

Returns an array containing all of the result set rows.

Parameters

string $query SQL query
array $params Query parameters
mixed ...$fetchMode Fetch mode

Return Value

array

at line 622
mixed|bool column(string $query, array $params = [])

Returns the value of the first column of the first row of the result set.

Parameters

string $query SQL query
array $params Query parameters

Return Value

mixed|bool

at line 634
array columns(string $query, array $params = [])

Returns an array containing the values of the indicated 0-indexed column.

Parameters

string $query SQL query
array $params Query parameters

Return Value

array

at line 646
array pairs(string $query, array $params = [])

Returns an array where the first column is used as keys and the second as values.

Parameters

string $query SQL query
array $params Query parameters

Return Value

array

at line 659
Generator yield(string $query, array $params = [], mixed ...$fetchMode)

Returns a generator that lets you iterate over the results.

Parameters

string $query SQL query
array $params Query params
mixed ...$fetchMode Fetch mode

Return Value

Generator

at line 686
Query builder()

Returns a query builder instance.

Return Value

Query

at line 697
Query table(string|array|Closure|Subquery|Raw|null $table)

Returns a query builder instance where we have already chosen the table we want to query.

Parameters

string|array|Closure|Subquery|Raw|null $table Database table or subquery

Return Value

Query

at line 707
protected bool createSavepoint()

Creates a new savepoint.

Return Value

bool

at line 717
protected bool rollBackSavepoint()

Rolls back to the previously created savepoint.

Return Value

bool

at line 727
bool beginTransaction()

Begin a transaction.

Return Value

bool

at line 742
bool commitTransaction()

Commits a transaction.

Return Value

bool

at line 757
bool rollBackTransaction()

Roll back a transaction.

Return Value

bool

at line 783
int getTransactionNestingLevel()

Returns the transaction nesting level.

Return Value

int

at line 793
bool inTransaction()

Returns TRUE if we're in a transaction and FALSE if not.

Return Value

bool

at line 804
mixed transaction(Closure $queries)

Executes queries and rolls back the transaction if any of them fail.

Parameters

Closure $queries Queries

Return Value

mixed