class Connection implements IConnection

Dibi connection.

Properties

$onEvent

function (Event $event); Occurs after query is executed

int read-only $affectedRows
int read-only $insertId

Methods

__construct(array $config, string|null $name = null)

Connection options: (see driver-specific options too)

  • lazy (bool) => if true, connection will be established only when required
  • result (array) => result set options
    • normalize => normalizes result fields (default: true)
    • formatDateTime => date-time format empty for decoding as Dibi\DateTime (default) "..." formatted according to given format, see https://www.php.net/manual/en/datetime.format.php "native" for leaving value as is
    • formatTimeInterval => time-interval format empty for decoding as DateInterval (default) "..." formatted according to given format, see https://www.php.net/manual/en/dateinterval.format.php "native" for leaving value as is
    • formatJson => json format "array" for decoding json as an array (default) "object" for decoding json as \stdClass "native" for leaving value as is
  • profiler (array)
    • run (bool) => enable profiler?
    • file => file to log
    • errorsOnly (bool) => log only errors
  • substitutes (array) => map of driver specific substitutes (under development)
  • onConnect (array) => list of SQL queries to execute (by Connection::query()) after connection is established

__destruct()

Automatically frees the resources allocated for this result set.

void
connect()

Connects to a database.

void
disconnect()

Disconnects from a database.

bool
isConnected()

Returns true when connection was established.

mixed
getConfig(string|null $key = null, $default = null)

Returns configuration variable. If no $key is passed, returns the entire array.

getDriver()

Returns the driver and connects to a database in lazy mode.

query(...$args)

Generates (translates) and executes SQL query.

string
translate(mixed ...$args)

Generates SQL query.

bool
test(mixed ...$args)

Generates and prints SQL query.

dataSource(mixed ...$args)

Generates (translates) and returns SQL query as DataSource.

nativeQuery(string $sql)

Executes the SQL query.

int
getAffectedRows()

Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.

int
getInsertId(string|null $sequence = null)

Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.

void
begin(string|null $savepoint = null)

Begins a transaction (if supported).

void
commit(string|null $savepoint = null)

Commits statements in a transaction.

void
rollback(string|null $savepoint = null)

Rollback changes in a transaction.

mixed
transaction(callable $callback)

No description

createResultSet(ResultDriver $resultDriver)

Result set factory.

command()

No description

select(...$args)

No description

update(string|string[] $table, iterable $args)

No description

insert(string $table, iterable $args)

No description

delete(string $table)

No description

getSubstitutes()

Returns substitution hashmap.

string
substitute(string $value)

Provides substitution.

void
setObjectTranslator(callable $translator)

No description

Expression|null
translateObject(object $object)

No description

Row|null
fetch(mixed ...$args)

Executes SQL query and fetch result - shortcut for query() & fetch().

array
fetchAll(mixed ...$args)

Executes SQL query and fetch results - shortcut for query() & fetchAll().

mixed
fetchSingle(mixed ...$args)

Executes SQL query and fetch first column - shortcut for query() & fetchSingle().

array
fetchPairs(mixed ...$args)

Executes SQL query and fetch pairs - shortcut for query() & fetchPairs().

static Literal
literal(string $value)

No description

static Expression
expression(...$args)

No description

int
loadFile(string $file, callable|null $onProgress = null)

Import SQL dump from file.

getDatabaseInfo()

Gets a information about the current database.

__wakeup()

Prevents unserialization.

__sleep()

Prevents serialization.

void
onEvent($arg)

No description

Details

at line 64
__construct(array $config, string|null $name = null)

Connection options: (see driver-specific options too)

  • lazy (bool) => if true, connection will be established only when required
  • result (array) => result set options
    • normalize => normalizes result fields (default: true)
    • formatDateTime => date-time format empty for decoding as Dibi\DateTime (default) "..." formatted according to given format, see https://www.php.net/manual/en/datetime.format.php "native" for leaving value as is
    • formatTimeInterval => time-interval format empty for decoding as DateInterval (default) "..." formatted according to given format, see https://www.php.net/manual/en/dateinterval.format.php "native" for leaving value as is
    • formatJson => json format "array" for decoding json as an array (default) "object" for decoding json as \stdClass "native" for leaving value as is
  • profiler (array)
    • run (bool) => enable profiler?
    • file => file to log
    • errorsOnly (bool) => log only errors
  • substitutes (array) => map of driver specific substitutes (under development)
  • onConnect (array) => list of SQL queries to execute (by Connection::query()) after connection is established

Parameters

array $config
string|null $name

Exceptions

Exception

at line 109
__destruct()

Automatically frees the resources allocated for this result set.

at line 120
final void connect()

Connects to a database.

Return Value

void

at line 165
final void disconnect()

Disconnects from a database.

Return Value

void

at line 177
final bool isConnected()

Returns true when connection was established.

Return Value

bool

at line 187
final mixed getConfig(string|null $key = null, $default = null)

Returns configuration variable. If no $key is passed, returns the entire array.

Parameters

string|null $key
$default

Return Value

mixed

See also

self::__construct

at line 198
final Driver getDriver()

Returns the driver and connects to a database in lazy mode.

Return Value

Driver

at line 212
final Result query(...$args)

Generates (translates) and executes SQL query.

Parameters

...$args

Return Value

Result

Exceptions

Exception

at line 222
final string translate(mixed ...$args)

Generates SQL query.

Parameters

mixed ...$args

Return Value

string

Exceptions

Exception

at line 235
final bool test(mixed ...$args)

Generates and prints SQL query.

Parameters

mixed ...$args

Return Value

bool

at line 257
final DataSource dataSource(mixed ...$args)

Generates (translates) and returns SQL query as DataSource.

Parameters

mixed ...$args

Return Value

DataSource

Exceptions

Exception

at line 267
final Result nativeQuery(string $sql)

Executes the SQL query.

Parameters

string $sql

Return Value

Result

Exceptions

Exception

at line 299
int getAffectedRows()

Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.

Return Value

int

Exceptions

Exception

at line 318
int getInsertId(string|null $sequence = null)

Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.

Parameters

string|null $sequence

Return Value

int

Exceptions

Exception

at line 336
void begin(string|null $savepoint = null)

Begins a transaction (if supported).

Parameters

string|null $savepoint

Return Value

void

at line 365
void commit(string|null $savepoint = null)

Commits statements in a transaction.

Parameters

string|null $savepoint

Return Value

void

at line 394
void rollback(string|null $savepoint = null)

Rollback changes in a transaction.

Parameters

string|null $savepoint

Return Value

void

at line 420
mixed transaction(callable $callback)

No description

Parameters

callable $callback

Return Value

mixed

at line 450
Result createResultSet(ResultDriver $resultDriver)

Result set factory.

Parameters

ResultDriver $resultDriver

Return Value

Result

at line 460
Fluent command()

No description

Return Value

Fluent

at line 466
Fluent select(...$args)

No description

Parameters

...$args

Return Value

Fluent

at line 475
Fluent update(string|string[] $table, iterable $args)

No description

Parameters

string|string[] $table
iterable $args

Return Value

Fluent

at line 481
Fluent insert(string $table, iterable $args)

No description

Parameters

string $table
iterable $args

Return Value

Fluent

at line 492
Fluent delete(string $table)

No description

Parameters

string $table

Return Value

Fluent

at line 504
HashMap getSubstitutes()

Returns substitution hashmap.

Return Value

HashMap

at line 513
string substitute(string $value)

Provides substitution.

Parameters

string $value

Return Value

string

at line 527
void setObjectTranslator(callable $translator)

No description

Parameters

callable $translator

Return Value

void

at line 551
Expression|null translateObject(object $object)

No description

Parameters

object $object

Return Value

Expression|null

at line 596
Row|null fetch(mixed ...$args)

Executes SQL query and fetch result - shortcut for query() & fetch().

Parameters

mixed ...$args

Return Value

Row|null

Exceptions

Exception

at line 607
array fetchAll(mixed ...$args)

Executes SQL query and fetch results - shortcut for query() & fetchAll().

Parameters

mixed ...$args

Return Value

array

Exceptions

Exception

at line 617
mixed fetchSingle(mixed ...$args)

Executes SQL query and fetch first column - shortcut for query() & fetchSingle().

Parameters

mixed ...$args

Return Value

mixed

Exceptions

Exception

at line 627
array fetchPairs(mixed ...$args)

Executes SQL query and fetch pairs - shortcut for query() & fetchPairs().

Parameters

mixed ...$args

Return Value

array

Exceptions

Exception

at line 633
static Literal literal(string $value)

No description

Parameters

string $value

Return Value

Literal

at line 639
static Expression expression(...$args)

No description

Parameters

...$args

Return Value

Expression

at line 653
int loadFile(string $file, callable|null $onProgress = null)

Import SQL dump from file.

Parameters

string $file
callable|null $onProgress

function (int $count, ?float $percent): void

Return Value

int

count of sql commands

at line 662
Database getDatabaseInfo()

Gets a information about the current database.

Return Value

Database

at line 675
__wakeup()

Prevents unserialization.

at line 684
__sleep()

Prevents serialization.

at line 690
protected void onEvent($arg)

No description

Parameters

$arg

Return Value

void