PHP Class LessQL\Database

Show file Open project: morris/lessql

Protected Properties

Property Type Description
$aliases array
$backReferences array
$identifierDelimiter string
$primary array
$queryCallback null | callable
$references array
$required array
$rewrite null | callable
$sequences array

Public Methods

Method Description
__call ( string $name, array $args ) : Result | lessql\Row | null Returns a result for table $name.
__construct ( PDO $pdo ) Constructor. Sets PDO to exception mode.
begin ( ) : boolean Begin a transaction
commit ( ) : boolean Commit changes of transaction
createResult ( Database | Result | lessql\Row $parent, string $name ) : Result Create a result bound to $parent using table or association $name.
createRow ( string $name, array $properties = [], Result | null $result = null ) : lessql\Row Create a row from given properties.
delete ( string $table, array $where = [], array $params = [] ) : PDOStatement Execute delete query and return statement
format ( mixed $value ) : string Format a value for SQL, e.g. DateTime objects
getAlias ( string $alias ) : string Get alias of a table
getBackReference ( string $table, string $name ) : string Get a back reference key for an association on a table
getIdentifierDelimiter ( ) : string Get identifier delimiter
getPrimary ( string $table ) : string | array Get primary key of a table, may be array for compound keys
getReference ( string $table, string $name ) : string Get a reference key for an association on a table
getRequired ( string $table ) : array Get a map of required columns of a table
getSequence ( string $table ) : null | string Get primary sequence name of table (used in INSERT by Postgres)
getSuffix ( array $where, array $orderBy = [], integer | null $limitCount = null, integer | null $limitOffset = null ) : string Return WHERE/LIMIT/ORDER suffix for queries
insert ( string $table, array $rows, string | null $method = null ) : PDOStatement | null Insert one ore more rows into a table
is ( string $column, string | array $value, boolean $not = false ) : string Build an SQL condition expressing that "$column is $value", or "$column is in $value" if $value is an array. Handles null and literals like new Literal( "NOW()" ) correctly.
isNot ( string $column, string | array $value ) : string Build an SQL condition expressing that "$column is not $value" or "$column is not in $value" if $value is an array. Handles null and literals like new Literal( "NOW()" ) correctly.
isRequired ( string $table, string $column ) : boolean Is a column of a table required for saving? Default is no
lastInsertId ( string | null $sequence = null ) : string Return last inserted id
literal ( string $value ) : lessql\Literal Create a SQL Literal
onQuery ( string $query, array $params = [] ) Calls the query callback, if any
prepare ( string $query ) : PDOStatement Prepare an SQL statement
query ( string $query ) : PDOStatement Execute an SQL statement directly
quote ( mixed $value ) : string Quote a value for SQL
quoteIdentifier ( string $identifier ) : string Quote identifier
rewriteTable ( string $table ) : string Get rewritten table name
rollback ( ) : boolean Rollback any changes during transaction
select ( string $table, $options = [] ) : PDOStatement Select rows from a table
setAlias ( string $alias, string $table ) Set alias of a table
setBackReference ( string $table, string $name, string $key ) Set a back reference key for an association on a table
setIdentifierDelimiter ( string | null $d ) Sets delimiter used when quoting identifiers.
setPrimary ( string $table, string | array $key ) Set primary key of a table.
setQueryCallback ( callable $callback ) Set the query callback
setReference ( string $table, string $name, string $key ) Set a reference key for an association on a table
setRequired ( string $table, string $column ) Set a column to be required for saving Any primary key that is not auto-generated should be required Compound primary keys are required by default
setRewrite ( callable $rewrite ) Set table rewrite function For example, it could add a prefix
setSequence ( string $table, string $sequence ) Set primary sequence name of table
table ( $name, integer | null $id = null ) : Result | lessql\Row | null Returns a result for table $name.
update ( string $table, array $data, array $where = [], array $params = [] ) : null | PDOStatement Execute update query and return statement

Protected Methods

Method Description
getColumns ( array $rows ) : array Get list of all columns used in the given rows
insertBatch ( string $table, array $rows ) : PDOStatement | null Insert rows using a single batch query
insertDefault ( string $table, array $rows ) : PDOStatement | null Insert rows using one query per row
insertHead ( string $table, array $columns ) : string Build head of INSERT query (without values)
insertPrepared ( string $table, array $rows ) : PDOStatement | null Insert rows using a prepared query
valueLists ( array $rows, array $columns ) : array Build lists of quoted values for INSERT

Method Details

__call() public method

If $id is given, return the row with that id. Examples: $db->user()->where( ... ) $db->user( 1 )
public __call ( string $name, array $args ) : Result | lessql\Row | null
$name string
$args array
return Result | lessql\Row | null

__construct() public method

Constructor. Sets PDO to exception mode.
public __construct ( PDO $pdo )
$pdo PDO

begin() public method

Begin a transaction
public begin ( ) : boolean
return boolean

commit() public method

Commit changes of transaction
public commit ( ) : boolean
return boolean

createResult() public method

$parent may be the database, a result, or a row
public createResult ( Database | Result | lessql\Row $parent, string $name ) : Result
$parent Database | Result | lessql\Row
$name string
return Result

createRow() public method

Optionally bind it to the given result.
public createRow ( string $name, array $properties = [], Result | null $result = null ) : lessql\Row
$name string
$properties array
$result Result | null
return lessql\Row

delete() public method

DELETE FROM $table [WHERE $where]
public delete ( string $table, array $where = [], array $params = [] ) : PDOStatement
$table string
$where array
$params array
return PDOStatement

format() public method

Format a value for SQL, e.g. DateTime objects
public format ( mixed $value ) : string
$value mixed
return string

getAlias() public method

Get alias of a table
public getAlias ( string $alias ) : string
$alias string
return string

getBackReference() public method

"How would $table be referenced by another table under $name?" Convention is "$table_id"
public getBackReference ( string $table, string $name ) : string
$table string
$name string
return string

getColumns() protected method

Get list of all columns used in the given rows
protected getColumns ( array $rows ) : array
$rows array
return array

getIdentifierDelimiter() public method

Get identifier delimiter
public getIdentifierDelimiter ( ) : string
return string

getPrimary() public method

Convention is "id"
public getPrimary ( string $table ) : string | array
$table string
return string | array

getReference() public method

"How would $table reference another table under $name?" Convention is "$name_id"
public getReference ( string $table, string $name ) : string
$table string
$name string
return string

getRequired() public method

Get a map of required columns of a table
public getRequired ( string $table ) : array
$table string
return array

getSequence() public method

Conventions is "$tableRewritten_$primary_seq"
public getSequence ( string $table ) : null | string
$table string
return null | string

getSuffix() public method

Return WHERE/LIMIT/ORDER suffix for queries
public getSuffix ( array $where, array $orderBy = [], integer | null $limitCount = null, integer | null $limitOffset = null ) : string
$where array
$orderBy array
$limitCount integer | null
$limitOffset integer | null
return string

insert() public method

The $method parameter selects one of the following insert methods: "prepared": Prepare a query and execute it once per row using bound params Does not support Literals in row data (PDO limitation) "batch": Create a single query mit multiple value lists Supports Literals, but not supported everywhere default: Execute one INSERT per row Supports Literals, supported everywhere, slow for many rows
public insert ( string $table, array $rows, string | null $method = null ) : PDOStatement | null
$table string
$rows array
$method string | null
return PDOStatement | null

insertBatch() protected method

Insert rows using a single batch query
protected insertBatch ( string $table, array $rows ) : PDOStatement | null
$table string
$rows array
return PDOStatement | null

insertDefault() protected method

Insert rows using one query per row
protected insertDefault ( string $table, array $rows ) : PDOStatement | null
$table string
$rows array
return PDOStatement | null

insertHead() protected method

Build head of INSERT query (without values)
protected insertHead ( string $table, array $columns ) : string
$table string
$columns array
return string

insertPrepared() protected method

Insert rows using a prepared query
protected insertPrepared ( string $table, array $rows ) : PDOStatement | null
$table string
$rows array
return PDOStatement | null

is() public method

Build an SQL condition expressing that "$column is $value", or "$column is in $value" if $value is an array. Handles null and literals like new Literal( "NOW()" ) correctly.
public is ( string $column, string | array $value, boolean $not = false ) : string
$column string
$value string | array
$not boolean
return string

isNot() public method

Build an SQL condition expressing that "$column is not $value" or "$column is not in $value" if $value is an array. Handles null and literals like new Literal( "NOW()" ) correctly.
public isNot ( string $column, string | array $value ) : string
$column string
$value string | array
return string

isRequired() public method

Is a column of a table required for saving? Default is no
public isRequired ( string $table, string $column ) : boolean
$table string
$column string
return boolean

lastInsertId() public method

Return last inserted id
public lastInsertId ( string | null $sequence = null ) : string
$sequence string | null
return string

literal() public method

Create a SQL Literal
public literal ( string $value ) : lessql\Literal
$value string
return lessql\Literal

onQuery() public method

Calls the query callback, if any
public onQuery ( string $query, array $params = [] )
$query string
$params array

prepare() public method

Prepare an SQL statement
public prepare ( string $query ) : PDOStatement
$query string
return PDOStatement

query() public method

Execute an SQL statement directly
public query ( string $query ) : PDOStatement
$query string
return PDOStatement

quote() public method

Quote a value for SQL
public quote ( mixed $value ) : string
$value mixed
return string

quoteIdentifier() public method

Quote identifier
public quoteIdentifier ( string $identifier ) : string
$identifier string
return string

rewriteTable() public method

Get rewritten table name
public rewriteTable ( string $table ) : string
$table string
return string

rollback() public method

Rollback any changes during transaction
public rollback ( ) : boolean
return boolean

select() public method

Select rows from a table
public select ( string $table, $options = [] ) : PDOStatement
$table string
return PDOStatement

setAlias() public method

Set alias of a table
public setAlias ( string $alias, string $table )
$alias string
$table string

setBackReference() public method

Set a back reference key for an association on a table
public setBackReference ( string $table, string $name, string $key )
$table string
$name string
$key string

setIdentifierDelimiter() public method

Should be backtick or double quote. Set to null to disable quoting.

setPrimary() public method

Compound keys may be passed as an array. Always set compound primary keys explicitly with this method.
public setPrimary ( string $table, string | array $key )
$table string
$key string | array

setQueryCallback() public method

Set the query callback
public setQueryCallback ( callable $callback )
$callback callable

setReference() public method

Set a reference key for an association on a table
public setReference ( string $table, string $name, string $key )
$table string
$name string
$key string

setRequired() public method

Set a column to be required for saving Any primary key that is not auto-generated should be required Compound primary keys are required by default
public setRequired ( string $table, string $column )
$table string
$column string

setRewrite() public method

Set table rewrite function For example, it could add a prefix
public setRewrite ( callable $rewrite )
$rewrite callable

setSequence() public method

Set primary sequence name of table
public setSequence ( string $table, string $sequence )
$table string
$sequence string

table() public method

If $id is given, return the row with that id.
public table ( $name, integer | null $id = null ) : Result | lessql\Row | null
$name
$id integer | null
return Result | lessql\Row | null

update() public method

UPDATE $table SET $data [WHERE $where]
public update ( string $table, array $data, array $where = [], array $params = [] ) : null | PDOStatement
$table string
$data array
$where array
$params array
return null | PDOStatement

valueLists() protected method

Build lists of quoted values for INSERT
protected valueLists ( array $rows, array $columns ) : array
$rows array
$columns array
return array

Property Details

$aliases protected property

protected array $aliases
return array

$backReferences protected property

protected array $backReferences
return array

$identifierDelimiter protected property

protected string $identifierDelimiter
return string

$primary protected property

protected array $primary
return array

$queryCallback protected property

protected null|callable $queryCallback
return null | callable

$references protected property

protected array $references
return array

$required protected property

protected array $required
return array

$rewrite protected property

protected null|callable $rewrite
return null | callable

$sequences protected property

protected array $sequences
return array