DB
+ Inheritance diagram for DB:

Description

Extends PDO and acts as a central access point for the schema.

Protected Properties

Junction[] $junctions = []
 
callable $logger
 Notified whenever a query is executed or a statement is prepared. More...
 
string $migrations = 'migrations/default'
 The migrations directory for this connection. More...
 
Record[] $records = []
 
Schema $schema
 
int $transactions = 0
 The count of open transactions/savepoints. More...
 

Public Static Methods

static self fromConfig (string $connection='default', string $file='db.config.php')
 Returns a new connection using a configuration file. More...
 
static ExpressionInterface[] marks (int $count)
 Returns an array of ? placeholder expressions. More...
 
static string[] slots (array $columns)
 Converts an array of columns to :named placeholders for prepared queries. More...
 

Public Methods

 __construct ($dsn, $username=null, $password=null, array $options=[])
 Sets various attributes to streamline operations. More...
 
true beginTransaction ()
 Allows nested transactions by using SAVEPOINT More...
 
true commit ()
 Allows nested transactions by using RELEASE SAVEPOINT More...
 
int exec ($sql)
 Notifies the logger and executes. More...
 
mixed factory (string $class,... $args)
 Central point of object creation. More...
 
string getDriver ()
 The driver's name. More...
 
Junction getJunction (string $interface)
 Returns a Junction access object based on an annotated interface. More...
 
callable getLogger ()
 
Migrator getMigrator ()
 
Record getRecord ($class)
 Returns a Record access object based on an annotated class. More...
 
Schema getSchema ()
 
bool isMySQL ()
 
bool isPostgreSQL ()
 
bool isSQLite ()
 
void log (string $sql)
 
Transaction newTransaction ()
 Returns a scoped transaction. More...
 
bool offsetExists ($table)
 Whether a table exists. More...
 
null Table offsetGet ($table)
 Returns a table by name. More...
 
 offsetSet ($offset, $value)
 Throws. More...
 
 offsetUnset ($offset)
 Throws. More...
 
Statement prepare ($sql, $options=[])
 Notifies the logger and prepares a statement. More...
 
Statement query ($sql, $mode=PDO::ATTR_DEFAULT_FETCH_MODE, $arg3=null, array $ctorargs=[])
 Notifies the logger and queries. More...
 
string ExpressionInterface quote ($value, $type=self::PARAM_STR)
 Quotes a value, with special considerations. More...
 
string[] quoteArray (array $values)
 Quotes an array of values. More...
 
string quoteList (array $values)
 Returns a quoted, comma-separated list. More...
 
true rollBack ()
 Allows nested transactions by using ROLLBACK TO SAVEPOINT More...
 
$this setLogger (callable $logger)
 
$this setRecord (string $class, Record $record)
 Installs a Record for a third-party class that cannot be annotated. More...
 
void sqliteCreateFunctions (array $callables, bool $deterministic=true)
 Create multiple SQLite functions at a time. More...
 
mixed transact (callable $work)
 Performs work within a scoped transaction. More...
 

Protected Methods

void __construct_mysql ()
 MySQL construction hook. More...
 
void __construct_sqlite ()
 SQLite construction hook. More...
 

Properties

◆ $logger

callable $logger
protected

Notified whenever a query is executed or a statement is prepared.

This is a stub-closure by default.

fn($sql): void

◆ $migrations

string $migrations = 'migrations/default'
protected

The migrations directory for this connection.

This can be set via class override, or configuration file (recommended).

When using DB::fromConfig(), this defaults to migrations/<CONNECTION NAME>

◆ $transactions

int $transactions = 0
protected

The count of open transactions/savepoints.

Constructor

◆ __construct()

__construct (   $dsn,
  $username = null,
  $password = null,
array  $options = [] 
)

Sets various attributes to streamline operations.

Registers missing SQLite functions.

Parameters
string$dsn
string$username
string$password
array$options

Methods

◆ __construct_mysql()

void __construct_mysql ( )
protected

MySQL construction hook.

◆ __construct_sqlite()

void __construct_sqlite ( )
protected

SQLite construction hook.

◆ beginTransaction()

true beginTransaction ( )

Allows nested transactions by using SAVEPOINT

Use DB::newTransaction() to work with Transaction instead.

Returns
true

◆ commit()

true commit ( )

Allows nested transactions by using RELEASE SAVEPOINT

Use DB::newTransaction() to work with Transaction instead.

Returns
true

◆ exec()

int exec (   $sql)

Notifies the logger and executes.

Parameters
string$sql
Returns
int

◆ factory()

mixed factory ( string  $class,
  $args 
)

Central point of object creation.

Override this to override classes.

The only thing that calls this should be \Helix\DB\FactoryTrait

Parameters
string$class
mixed...$args
Returns
mixed

◆ fromConfig()

static self fromConfig ( string  $connection = 'default',
string  $file = 'db.config.php' 
)
static

Returns a new connection using a configuration file.

See db.config.php in the test directory for an example.

Parameters
string$connection
string$file
Returns
self

◆ getDriver()

string getDriver ( )
final

The driver's name.

  • mysql
  • pgsql
  • sqlite
Returns
string

◆ getJunction()

Junction getJunction ( string  $interface)

Returns a Junction access object based on an annotated interface.

Parameters
string$interface
Returns
Junction

◆ getLogger()

callable getLogger ( )
final
Returns
callable

◆ getMigrator()

Migrator getMigrator ( )
Parameters
string$dir
Returns
Migrator

◆ getRecord()

Record getRecord (   $class)

Returns a Record access object based on an annotated class.

Parameters
string | EntityInterface$class
Returns
Record

◆ getSchema()

Schema getSchema ( )
Returns
Schema

◆ isMySQL()

bool isMySQL ( )
final
Returns
bool

◆ isPostgreSQL()

bool isPostgreSQL ( )
final
Returns
bool

◆ isSQLite()

bool isSQLite ( )
final
Returns
bool

◆ log()

void log ( string  $sql)
Parameters
string$sql

◆ marks()

static ExpressionInterface [] marks ( int  $count)
static

Returns an array of ? placeholder expressions.

Parameters
int$count
Returns
ExpressionInterface[]

◆ newTransaction()

Transaction newTransaction ( )

Returns a scoped transaction.

Returns
Transaction

◆ offsetExists()

bool offsetExists (   $table)
final

Whether a table exists.

Parameters
string$table
Returns
bool

◆ offsetGet()

null Table offsetGet (   $table)

Returns a table by name.

Parameters
string$table
Returns
null|Table

◆ offsetSet()

offsetSet (   $offset,
  $value 
)
final

Throws.

Parameters
$offset
$value
Exceptions
LogicException

◆ offsetUnset()

offsetUnset (   $offset)
final

Throws.

Parameters
$offset
Exceptions
LogicException

◆ prepare()

Statement prepare (   $sql,
  $options = [] 
)

Notifies the logger and prepares a statement.

Parameters
string$sql
array$options
Returns
Statement

◆ query()

Statement query (   $sql,
  $mode = PDO::ATTR_DEFAULT_FETCH_MODE,
  $arg3 = null,
array  $ctorargs = [] 
)

Notifies the logger and queries.

Parameters
string$sql
int$mode
mixed$arg3
array$ctorargs
Returns
Statement

◆ quote()

string ExpressionInterface quote (   $value,
  $type = self::PARAM_STR 
)

Quotes a value, with special considerations.

  • ExpressionInterface instances are returned as-is.
  • Booleans and integers are returned as unquoted integer-string.
  • Everything else is returned as a quoted string.
Parameters
bool | number | string | object$value
int$typeIgnored.
Returns
string|ExpressionInterface

◆ quoteArray()

string [] quoteArray ( array  $values)

Quotes an array of values.

Keys are preserved.

Parameters
array$values
Returns
string[]

◆ quoteList()

string quoteList ( array  $values)

Returns a quoted, comma-separated list.

Parameters
array$values
Returns
string

◆ rollBack()

true rollBack ( )

Allows nested transactions by using ROLLBACK TO SAVEPOINT

Use DB::newTransaction() to work with Transaction instead.

Returns
true

◆ setLogger()

$this setLogger ( callable  $logger)
Parameters
callable$logger
Returns
$this

◆ setRecord()

$this setRecord ( string  $class,
Record  $record 
)

Installs a Record for a third-party class that cannot be annotated.

Parameters
string$class
Record$record
Returns
$this

◆ slots()

static string [] slots ( array  $columns)
static

Converts an array of columns to :named placeholders for prepared queries.

Qualified columns are slotted as qualifier__column (two underscores).

Parameters
string[]$columns
Returns
string[] ["column" => ":column"]

◆ sqliteCreateFunctions()

void sqliteCreateFunctions ( array  $callables,
bool  $deterministic = true 
)

Create multiple SQLite functions at a time.

Parameters
callable[]$callablesKeyed by function name.
bool$deterministicWhether the callables aren't random / are without side-effects.

◆ transact()

mixed transact ( callable  $work)

Performs work within a scoped transaction.

The work is rolled back if an exception is thrown.

Parameters
callable$workfn(DB $this): mixed
Returns
mixed The return value of $work