Documentation

StaticPdb
in package

AbstractYes

Class for doing database queries via PDO (PDO Database => Pdb).

This a compatibility class, all methods are static variants.

Extend this class and implement the getConfig() method.

Table of Contents

Properties

$connections  : array<string|int, Pdb>

Methods

__callStatic()  : mixed
Wrap everything.
buildClause()  : string
Builds a clause string by combining conditions, e.g. for a WHERE or ON clause.
clearLogHandler()  : void
Clear the log handler
clearOverrideConnection()  : void
Clear any overridden connection, reverting behaviour back to the default connection logic.
commit()  : void
connect()  : PDO
Loads a config and creates a new PDO connection with it.
convertEnumArr()  : array<string|int, mixed>
delete()  : int
execute()  : array<string|int, mixed>|string|int|null|PDOStatement
extractEnumArr()  : array<string|int, mixed>
fieldList()  : array<string|int, PdbColumn>
find()  : PdbQuery
formatRs()  : array<string|int, mixed>|string|int|null
generateUid()  : string
get()  : array<string|int, mixed>
getBindSubset()  : array<string|int, mixed>
getConfig()  : PdbConfig
Create a PDB configuration.
getConnection()  : PDO
Gets a PDO connection, creating a new one if necessary
getDependentKeys()  : array<string|int, PdbForeignKey>
getForeignKeys()  : array<string|int, PdbForeignKey>
getInstance()  : Pdb
Get the pdb instance.
getLastInsertId()  : int
getPrefix()  : string
getSchema()  : PdbSchema
getServerVersion()  : string
getTable()  : PdbTable|null
getTableNames()  : array<string|int, string>
indexList()  : array<string|int, PdbIndex>
insert()  : int
insertPrefixes()  : string
Replaces tilde placeholders with table prefixes, and quotes tables according to the rules of the underlying DBMS.
inTransaction()  : bool
likeEscape()  : string
lookup()  : array<string|int, mixed>
now()  : string
prefix()  : string
Gets the prefix to prepend to table names.
prepare()  : PDOStatement
prettyQuery()  : string
prettyQueryIndentation()  : string
q()  : array<string|int, mixed>|string|int|object|PDOStatement|null
Alias for {@see Pdb::query}
query()  : array<string|int, mixed>|string|int|object|PDOStatement|null
Executes a PDO query
quote()  : string
quoteAll()  : array<string|int, string>
quoteField()  : string
quoteIdentifiers()  : string
quoteValue()  : string
recordExists()  : bool
removeFormatter()  : void
rollback()  : void
savepoint()  : string
setFormatter()  : void
setLogHandler()  : void
Set a logging handler function
setOverrideConnection()  : void
Set a PDO connection to use instead of the internal connection logic The specified connection may even be for a different database
setPrefix()  : void
Sets the prefix to prepend to table names.
setTablePrefixOverride()  : void
stripStrings()  : string
tableExists()  : bool
tableList()  : array<string|int, PdbTable>
transact()  : PdbTransaction
update()  : int
upsert()  : int
validateAlias()  : void
validateDirection()  : void
validateEnum()  : bool
validateIdentifier()  : void
validateIdentifierExtended()  : void
validateReturnType()  : void
withTransaction()  : mixed

Properties

$connections

protected static array<string|int, Pdb> $connections = []

Methods

__callStatic()

Wrap everything.

public static __callStatic(string $name, array<string|int, mixed> $arguments) : mixed
Parameters
$name : string
$arguments : array<string|int, mixed>

buildClause()

Builds a clause string by combining conditions, e.g. for a WHERE or ON clause.

public static buildClause(array<string|int, mixed> $conditions, array<string|int, mixed> &$values[, string $combine = 'AND' ]) : string

The resultant clause will contain ? markers for safe use in a prepared SQL statement. The statement and the generated $values can then be run via Pdb::query.

Each condition (see $conditions) is one of:

  • The scalar value 1 or 0 (to match either all or no records)
  • A column => value pair
  • An array with three elements: [column, operator, value(s)].
  • An array like: [operator, column => value].

Conditions are usually combined using AND, but can also be OR or XOR.

Parameters
$conditions : array<string|int, mixed>
$values : array<string|int, mixed>
$combine : string = 'AND'
Tags
throws
InvalidArgumentException
PDOException
Return values
string

clearLogHandler()

Clear the log handler

public static clearLogHandler() : void

clearOverrideConnection()

Clear any overridden connection, reverting behaviour back to the default connection logic.

public static clearOverrideConnection() : void

commit()

public static commit([string $name = = 'null' ]) : void
Parameters
$name : string = = 'null'

connect()

Loads a config and creates a new PDO connection with it.

public static connect(string|PdbConfig|array<string|int, mixed> $config[, array<string|int, mixed> $options = [] ]) : PDO

You probably want getConnection() or getInstance().

Parameters
$config : string|PdbConfig|array<string|int, mixed>
$options : array<string|int, mixed> = []

PDO attributes

Tags
throws
PDOException

If connection fails

Return values
PDO

convertEnumArr()

public static convertEnumArr(string $enum_defn) : array<string|int, mixed>
Parameters
$enum_defn : string
Return values
array<string|int, mixed>

delete()

public static delete(string $table, array<string|int, mixed> $conditions) : int
Parameters
$table : string
$conditions : array<string|int, mixed>
Return values
int

execute()

public static execute(PDOStatement $st, array<string|int, mixed> $params, string|array<string|int, mixed>|PdbReturn $config) : array<string|int, mixed>|string|int|null|PDOStatement
Parameters
$st : PDOStatement
$params : array<string|int, mixed>
$config : string|array<string|int, mixed>|PdbReturn
Return values
array<string|int, mixed>|string|int|null|PDOStatement

extractEnumArr()

public static extractEnumArr(string $table, string $column) : array<string|int, mixed>
Parameters
$table : string
$column : string
Return values
array<string|int, mixed>

fieldList()

public static fieldList(string $table) : array<string|int, PdbColumn>
Parameters
$table : string
Return values
array<string|int, PdbColumn>

formatRs()

public static formatRs(PDOStatement $rs, string $type) : array<string|int, mixed>|string|int|null
Parameters
$rs : PDOStatement
$type : string
Return values
array<string|int, mixed>|string|int|null

generateUid()

public static generateUid(string $table, int $id) : string
Parameters
$table : string
$id : int
Return values
string

get()

public static get(string $table, int $id[, bool $throw = = 'true' ]) : array<string|int, mixed>
Parameters
$table : string
$id : int
$throw : bool = = 'true'
Return values
array<string|int, mixed>

getBindSubset()

public static getBindSubset(string $q, array<string|int, mixed> $binds) : array<string|int, mixed>
Parameters
$q : string
$binds : array<string|int, mixed>
Return values
array<string|int, mixed>

getConfig()

Create a PDB configuration.

public abstract static getConfig([string|null $name = null ]) : PdbConfig
Parameters
$name : string|null = null
Return values
PdbConfig

getConnection()

Gets a PDO connection, creating a new one if necessary

public static getConnection([string $type = 'RW' ]) : PDO
Parameters
$type : string = 'RW'

'RW': read-write, or 'RO': read-only. If replication isn't being used, then only 'RW' is ever necessary.

Tags
throws
PDOException

If connection fails

Return values
PDO

getInstance()

Get the pdb instance.

public static getInstance([string $type = 'RW' ]) : Pdb

Override this if you wish to store the instance elsewhere.

Note however this contains logic for the read/write/only/override connections.

Parameters
$type : string = 'RW'
Return values
Pdb

getLastInsertId()

public static getLastInsertId() : int
Return values
int

getPrefix()

public static getPrefix([string $table = = '\'*\'' ]) : string
Parameters
$table : string = = '\'*\''
Return values
string

getServerVersion()

public static getServerVersion() : string
Return values
string

getTableNames()

public static getTableNames([string $filter = = '\'*\'' ][, bool $strip = = 'true' ]) : array<string|int, string>
Parameters
$filter : string = = '\'*\''
$strip : bool = = 'true'
Return values
array<string|int, string>

indexList()

public static indexList(string $table) : array<string|int, PdbIndex>
Parameters
$table : string
Return values
array<string|int, PdbIndex>

insert()

public static insert(string $table, array<string|int, mixed> $data) : int
Parameters
$table : string
$data : array<string|int, mixed>
Return values
int

insertPrefixes()

Replaces tilde placeholders with table prefixes, and quotes tables according to the rules of the underlying DBMS.

public static insertPrefixes(string $query[, null $compat = null ]) : string

COMPATIBILITY NOTE: the two parameter signature insertPrefixes(PDO, string) is maintained only for backwards compatibility. For all new code please use the standard single parameter insertPrefixes(string).

Parameters
$query : string

Query which contains tilde placeholders, e.g. 'SELECT * FROM ~pages WHERE id = 1'

$compat : null = null

do not use

Return values
string —

Query with tildes replaced by prefixes, e.g. 'SELECT * FROM sprout_pages WHERE id = 1'

inTransaction()

public static inTransaction() : bool
Return values
bool

likeEscape()

public static likeEscape(string $str[, string $escape = = '\'\\\\\'' ]) : string
Parameters
$str : string
$escape : string = = '\'\\\\\''
Return values
string

lookup()

public static lookup(string $table[, array<string|int, mixed> $conditions = = '[]' ][, array<string|int, mixed> $order = = '[\'name\']' ][, string $name = = '\'name\'' ]) : array<string|int, mixed>
Parameters
$table : string
$conditions : array<string|int, mixed> = = '[]'
$order : array<string|int, mixed> = = '[\'name\']'
$name : string = = '\'name\''
Return values
array<string|int, mixed>

now()

public static now() : string
Return values
string

prefix()

Gets the prefix to prepend to table names.

public static prefix() : string
Return values
string

prepare()

public static prepare(string $query) : PDOStatement
Parameters
$query : string
Return values
PDOStatement

prettyQuery()

public static prettyQuery(string $query, array<string|int, mixed> $values) : string
Parameters
$query : string
$values : array<string|int, mixed>
Return values
string

prettyQueryIndentation()

public static prettyQueryIndentation(string $query) : string
Parameters
$query : string
Return values
string

q()

Alias for {@see Pdb::query}

public static q(string $query, array<string|int, mixed> $params, string|array<string|int, mixed>|PdbReturn $config) : array<string|int, mixed>|string|int|object|PDOStatement|null
Parameters
$query : string

The query to execute. Prefix a table name with a tilde (~) to automatically include the table prefix, e.g. ~pages will be converted to fwc_pages

$params : array<string|int, mixed>

Parameters to bind to the query

$config : string|array<string|int, mixed>|PdbReturn

a return type or config PdbReturn

Tags
throws
InvalidArgumentException

If the return type isn't valid

QueryException

If the query execution or formatting failed

ConnectionException

If the connection fails

Return values
array<string|int, mixed>|string|int|object|PDOStatement|null

query()

Executes a PDO query

public static query(string $query, array<string|int, mixed> $params, string|array<string|int, mixed>|PdbReturn $config) : array<string|int, mixed>|string|int|object|PDOStatement|null

For the return type 'pdo', a PDOStatement is returned. You need to close it using $res->closeCursor() once you're finished. For the return type 'null', nothing is returned. For the return type 'count', a count of rows is returned. Additional return types are available; Pdb::formatRs for a full list.

When working with datasets larger than about 50 rows, you may run out of ram when using return types other than 'pdo', 'null', 'count' or 'val' because the other types all return the values as arrays

Return types:

  • PDOStatement For type 'pdo'
  • int For type 'count'
  • null For type 'null'
  • mixed For all other types; see Pdb::formatRs
Parameters
$query : string

The query to execute. Prefix a table name with a tilde (~) to automatically include the table prefix, e.g. ~pages will be converted to fwc_pages

$params : array<string|int, mixed>

Parameters to bind to the query

$config : string|array<string|int, mixed>|PdbReturn

a return type or config PdbReturn

Tags
throws
InvalidArgumentException

If the return type isn't valid

QueryException

If the query execution or formatting failed

ConnectionException

If the connection fails

Return values
array<string|int, mixed>|string|int|object|PDOStatement|null

quote()

public static quote(string $field, string $type) : string
Parameters
$field : string
$type : string
Return values
string

quoteAll()

public static quoteAll(string $field, string $type) : array<string|int, string>
Parameters
$field : string
$type : string
Return values
array<string|int, string>

quoteField()

public static quoteField(string $field[, bool $prefixes = = 'false' ]) : string
Parameters
$field : string
$prefixes : bool = = 'false'
Return values
string

quoteIdentifiers()

public static quoteIdentifiers(string $query[, array<string|int, mixed>|null $tables = = 'null' ][, bool $prefixes = = 'false' ]) : string
Parameters
$query : string
$tables : array<string|int, mixed>|null = = 'null'
$prefixes : bool = = 'false'
Return values
string

quoteValue()

public static quoteValue(string $value) : string
Parameters
$value : string
Return values
string

recordExists()

public static recordExists(string $table, array<string|int, mixed> $conditions) : bool
Parameters
$table : string
$conditions : array<string|int, mixed>
Return values
bool

removeFormatter()

public static removeFormatter(string $class_name) : void
Parameters
$class_name : string

rollback()

public static rollback([string $name = = 'null' ]) : void
Parameters
$name : string = = 'null'

savepoint()

public static savepoint([string $name = = 'null' ]) : string
Parameters
$name : string = = 'null'
Return values
string

setFormatter()

public static setFormatter(string $class_name, callable $func) : void
Parameters
$class_name : string
$func : callable

setLogHandler()

Set a logging handler function

public static setLogHandler(callable $func) : void

This function will be called after each query to allow calling code to do extra debugging or logging

The function signature should be: function(string $query, array $params, PDOStatement|QueryException $result)

Parameters
$func : callable

The logging function to use

setOverrideConnection()

Set a PDO connection to use instead of the internal connection logic The specified connection may even be for a different database

public static setOverrideConnection(PDO $connection[, PdbConfig|array<string|int, mixed> $config = [] ]) : void

NOTE: The flag indicating if the driver is currently in a transaction (inTransaction) does not get changed by calls to this function, so it may not produce correct results in that case.

Parameters
$connection : PDO

Any PDO connection.

$config : PdbConfig|array<string|int, mixed> = []
Tags
example

$mssql = new PDO('obdc:ms-sql-server'); Pdb::setOverrideConnection($mssql); $res = Pdb::query('SELECT TOP 3 FROM [my table]', [], 'row');

setPrefix()

Sets the prefix to prepend to table names.

public static setPrefix(string $prefix) : void

Only use this to override the existing prefix; e.g. in the Preview helper

Parameters
$prefix : string

The overriding prefix

setTablePrefixOverride()

public static setTablePrefixOverride(string $table, string $prefix) : void
Parameters
$table : string
$prefix : string

stripStrings()

public static stripStrings(string $q) : string
Parameters
$q : string
Return values
string

tableExists()

public static tableExists(string $table) : bool
Parameters
$table : string
Return values
bool

update()

public static update(string $table, array<string|int, mixed> $data, array<string|int, mixed> $conditions) : int
Parameters
$table : string
$data : array<string|int, mixed>
$conditions : array<string|int, mixed>
Return values
int

upsert()

public static upsert(string $table, array<string|int, mixed> $data, array<string|int, mixed> $conditions) : int
Parameters
$table : string
$data : array<string|int, mixed>
$conditions : array<string|int, mixed>
Return values
int

validateAlias()

public static validateAlias(string $name[, bool $loose = = 'false' ]) : void
Parameters
$name : string
$loose : bool = = 'false'

validateDirection()

public static validateDirection(string $name) : void
Parameters
$name : string

validateEnum()

public static validateEnum(string $table, array<string|int, mixed> $field) : bool
Parameters
$table : string
$field : array<string|int, mixed>
Return values
bool

validateIdentifier()

public static validateIdentifier(string $name[, bool $loose = = 'false' ]) : void
Parameters
$name : string
$loose : bool = = 'false'

validateIdentifierExtended()

public static validateIdentifierExtended(string $name[, bool $loose = = 'false' ]) : void
Parameters
$name : string
$loose : bool = = 'false'

validateReturnType()

public static validateReturnType(string $name) : void
Parameters
$name : string

withTransaction()

public static withTransaction(callable $callback) : mixed
Parameters
$callback : callable
On this page

Search results