FRED™
3.0
FRED™: Framework for Rapid and Easy Development
|
Database access layer (PDO based). More...
Public Member Functions | |
createPdo ($attributes=null) | |
date ($time=null) | |
Convert a Unix timestamp to database date format. More... | |
dateTime ($time=null) | |
Convert a Unix timestamp to database date+time format. More... | |
isSelection ($sql) | |
Check if an SQL statement is a select statement. More... | |
keyColumn ($table) | |
Primary key column. More... | |
begin () | |
Begin a transaction. More... | |
rollBack () | |
Roll a transaction back. More... | |
commit () | |
Commit a transaction. More... | |
transaction ($callback, $throw=true) | |
Wrap a callback function in a transaction. More... | |
lastInsertId () | |
Get the last auto-incremented ID. More... | |
statement ($sql, $args=null, $pdo=null) | |
Create a PDO statement. More... | |
execute ($sql, $args=null, $pdo=null) | |
Execute an SQL statement. More... | |
query ($sql, $args=null, $pdo=null) | |
Execute an SQL statement. More... | |
fetch ($statement) | |
Fetch a row from an SQL statement. More... | |
all ($sql, $args=null) | |
Return al rows from an SQL statement. More... | |
single ($sql, $args=null, $auto=true) | |
Return a single row from an SQL statement. More... | |
record ($sql, $args=null) | |
Returns an array from an SQL statement. More... | |
each ($callback, $sql, $args=null) | |
Run a callback function for every row in an SQL resultset. More... | |
multiColumns ($columns) | |
Expand an array with scalar values and arrays to multiple arrays. More... | |
prepareWhere (&$where, &$args=null, $glue='and') | |
Prepare a where statement. More... | |
limit ($sql, $limit, $offset=null) | |
Limit an SQL statement. More... | |
scout ($table, $columns=null, $where=null, $args=null) | |
Pre-select records (will be used by select()). More... | |
insert ($table, $columns, $replace_if_exists=false) | |
Insert a record. More... | |
replace ($table, $columns) | |
Replace a record (insert if not exists). More... | |
select ($table, $columns=' *', $where=null, $args=null, $limit=null, $offset=null) | |
Select records. More... | |
update ($table, $columns, $where=null, $args=null, $insert_if_not_exists=false) | |
Update one or more record(s). More... | |
delete ($table, $where=null, $args=null) | |
Delete one or more record(s). More... | |
exists ($table, $where=null, $args=null) | |
Check if a record exists that meets the requirement. More... | |
count ($table, $where=null, $args=null) | |
The number of records that meet the requirements. More... | |
min ($table, $column, $where=null, $args=null) | |
Smallest value from records that meet the requirements. More... | |
max ($table, $column, $where=null, $args=null) | |
Biggest value from records that meet the requirements. More... | |
average ($table, $column, $where=null, $args=null) | |
Average value from records that meet the requirements. More... | |
sum ($table, $column, $where=null, $args=null) | |
Total value from records that meet the requirements. More... | |
database () | |
Name of the current database. More... | |
tables () | |
All tables in current database. More... | |
columns ($table, $database=null) | |
Column properties for a table. More... | |
optimize ($table=null) | |
Optimize table(s). More... | |
explain ($sql, $args=null) | |
Explain plan. More... | |
processes () | |
Running processes. More... | |
kill ($id) | |
Kill a process. More... | |
__call ($func_name, $params) | |
__invoke ($sql, $args=null) | |
__sleep () | |
Public Member Functions inherited from Component | |
__construct ($fred, $config=null) | |
__destruct () | |
clientConfig () | |
Public configuration. More... | |
config ($key, $default=null) | |
Retrieve a config value. More... | |
ping () | |
Ping function. More... | |
filemtime ($filename) | |
Filemtime with session cache. More... | |
component ($name) | |
Get a component (local or default). More... | |
components (... $names) | |
Get multiple components in an array. More... | |
Public Member Functions inherited from Thing | |
propertyExists ($property) | |
Check if a property exists (public or published). More... | |
constants ($prefix=null) | |
Return all constants. More... | |
get ($key) | |
Get one or more properties. More... | |
set ($key, $value=null) | |
Set one or more properties. More... | |
__get ($key) | |
__set ($key, $value) | |
Data Fields | |
const | ACTION_INSERT = 'insert' |
const | ACTION_REPLACE = 'replace' |
const | ACTION_UPDATE = 'update' |
const | ACTION_DELETE = 'delete' |
const | EVENT_OPEN = 'db:open' |
const | EVENT_LOG_CHANGE = 'db:logChange' |
const | EVENT_LOG_ADD_ID = 'db:logAddId' |
$multiOperators = ['^' => 'or' | |
$statTime = 3600 | |
Operators (key) and glue (value) for multi column where conditions. More... | |
$statPrefix = 'db' | |
$logTimes | |
Query time above which to add a note to the log (key = prio, value = edge; higher times first). More... | |
$logTimeSignificant = 3 | |
Number of significant figures (rounding). More... | |
$logTables = [] | |
Tables (key) and key columns (value) to log changes for. More... | |
$defTables = null | |
Table definition to use (table name). More... | |
$migrateClassName = __CLASS__ . '\\Migrate' | |
Class name for the migration tool. More... | |
$queryCount = 0 | |
Number of queries executed. More... | |
$queryTime = 0 | |
Total time spent on queries (seconds). More... | |
$allowInsert = true | |
Allow inserts (for secondary processes like logs). More... | |
Data Fields inherited from Component | |
$filemtimeTtl = 0 | |
Data Fields inherited from Thing | |
const | HIDDEN = 0 |
Property is hidden. More... | |
const | READABLE = 1 |
Property is readable. More... | |
const | WRITEABLE = 2 |
Property is writeable. More... | |
const | READWRITE = 3 |
Property is readable and writeable. More... | |
Protected Member Functions | |
startTimer () | |
checkTimer ($sql, $args) | |
prepareArgs (&$sql, &$args) | |
logChange ($table, $action, $key, $old, $new) | |
Log a change. More... | |
logAddId ($id) | |
Add a auto-increment ID to the latest change log. More... | |
logChanges ($table, $action, $columns, $where, $args=null) | |
Log the changes for an action. More... | |
aggregate ($table, $function, $where=null, $args=null) | |
getMigrate () | |
getPdo () | |
getVersion () | |
Protected Member Functions inherited from Component | |
init () | |
done () | |
getSession () | |
Protected Member Functions inherited from Thing | |
publish ($property, $visibility=self::READABLE) | |
Publish a property (or hide it again). More... | |
configure ($config) | |
Configure the object. More... | |
_get ($key) | |
Default getter if no specific setter is defined, and the property is also not published (readable). More... | |
_set ($key, $value) | |
Default setter if no specific setter is defined, and the property is also not published (writeable). More... | |
Protected Attributes | |
$_connection = [] | |
$_attributes = [] | |
$_version = null | |
$_pdo = null | |
$_migrate = null | |
$_startTime = null | |
$_duploCount = [] | |
$_logAddId = false | |
$_scout = [] | |
Protected Attributes inherited from Component | |
$_fred = null | |
$_config = null | |
$_name = null | |
$_components = [] | |
Local components (key = component name, value = component). More... | |
$_session = null | |
Protected Attributes inherited from Thing | |
$_published = [] | |
Published properties (key = name of property, value = visibility). More... | |
|
protected |
all | ( | $sql, | |
$args = null |
|||
) |
average | ( | $table, | |
$column, | |||
$where = null , |
|||
$args = null |
|||
) |
columns | ( | $table, | |
$database = null |
|||
) |
count | ( | $table, | |
$where = null , |
|||
$args = null |
|||
) |
date | ( | $time = null | ) |
dateTime | ( | $time = null | ) |
delete | ( | $table, | |
$where = null , |
|||
$args = null |
|||
) |
each | ( | $callback, | |
$sql, | |||
$args = null |
|||
) |
Run a callback function for every row in an SQL resultset.
A seperate conection is used without buffering. This makes it possible to process huge datasets one record at a time, without running out of memory.
callable | $callback | Callback function (first and only parameter is the row). If the function returns explicitly false the execution of further rows is halted. |
string | $sql | SQL statement to execute. |
array | $args | Variables to bind to the statement. |
execute | ( | $sql, | |
$args = null , |
|||
$pdo = null |
|||
) |
exists | ( | $table, | |
$where = null , |
|||
$args = null |
|||
) |
explain | ( | $sql, | |
$args = null |
|||
) |
fetch | ( | $statement | ) |
insert | ( | $table, | |
$columns, | |||
$replace_if_exists = false |
|||
) |
Insert a record.
string | $table | Table name. |
array | $columns | Columns (key = column name, value = value or values - for multiple records). |
bool | $replace_if_exists | If true, an existing record will be replaced (if alse, an exception will be thrown when a record with the same key already exists). |
isSelection | ( | $sql | ) |
keyColumn | ( | $table | ) |
kill | ( | $id | ) |
lastInsertId | ( | ) |
limit | ( | $sql, | |
$limit, | |||
$offset = null |
|||
) |
|
protected |
|
protected |
Log a change.
string | $table | Table name. |
string | $action | Change type (see ACTION_* constants). |
array | $key | Key of the changed record (key = column name, value = value). |
array | $old | Original values for changed columns (key = column name, value = value). |
array | $new | New values for changed columns (key = column name, value = value). |
|
protected |
Log the changes for an action.
string | $table | Table name. |
string | $action | Change type (see ACTION_* constants). |
array | $columns | New values (key = column name, value = value). |
string | array | $where | Where. |
string | array | $args | Arguments (assoc.array) or extra statement for the where. |
max | ( | $table, | |
$column, | |||
$where = null , |
|||
$args = null |
|||
) |
min | ( | $table, | |
$column, | |||
$where = null , |
|||
$args = null |
|||
) |
Smallest value from records that meet the requirements.
string | $table | Table name. |
string | $column | Column to get value from. |
string | array | $where | Where. |
string | array | $args | Arguments (assoc.array) or extra statement for the where. |
multiColumns | ( | $columns | ) |
optimize | ( | $table = null | ) |
prepareWhere | ( | & | $where, |
& | $args = null , |
||
$glue = 'and' |
|||
) |
Prepare a where statement.
string | array | $where | If this a string, it is used directly as the where clause. If it is an assoc.array, it is translated to a where statement. Key => value pairs are translated as follow:
|
string | array | $args | If the $where is a string, and this is also, this value is added to the where. Otherwise the arguments resulting from the translation will be added to this array. |
string | $glue | Glue to combine seperate arguments. |
processes | ( | ) |
query | ( | $sql, | |
$args = null , |
|||
$pdo = null |
|||
) |
record | ( | $sql, | |
$args = null |
|||
) |
Returns an array from an SQL statement.
If the query returns only one column, the result is an array of those values. With two columns, the first one becomes the key, and the second one the value of an assoc.array. With three or more columns the first column will be the key, and the others the value.
string | $sql | SQL statement to execute. |
array | $args | Variables to bind to the statement. |
replace | ( | $table, | |
$columns | |||
) |
scout | ( | $table, | |
$columns = null , |
|||
$where = null , |
|||
$args = null |
|||
) |
Pre-select records (will be used by select()).
string | $table | Table name. |
array | $columns | Columns to select from the table. Leaving this empty will clear the cache for this table. |
string | array | $where | Where. |
string | array | $args | Arguments (assoc.array) or extra statement for the where. |
select | ( | $table, | |
$columns = '*' , |
|||
$where = null , |
|||
$args = null , |
|||
$limit = null , |
|||
$offset = null |
|||
) |
Select records.
string | $table | Table name. |
array | $columns | Columns to select from the table. Prefix the first columns with a '+' to make this the key. |
string | array | $where | Where. |
string | array | $args | Arguments (assoc.array) or extra statement for the where. |
int | bool | $limit | Limit the number of rows (0 = no limit). |
int | $offset | Offset. |
single | ( | $sql, | |
$args = null , |
|||
$auto = true |
|||
) |
Return a single row from an SQL statement.
Returns a single value if the resulting row has only one column.
string | $sql | SQL statement to execute. |
array | $args | Variables to bind to the statement. |
bool | $auto | Set to false to always return a row. |
statement | ( | $sql, | |
$args = null , |
|||
$pdo = null |
|||
) |
sum | ( | $table, | |
$column, | |||
$where = null , |
|||
$args = null |
|||
) |
transaction | ( | $callback, | |
$throw = true |
|||
) |
Wrap a callback function in a transaction.
callable | $callback | Callback function (first and only parameter is this instance). |
bool | $throw | If true, an exception is re-thrown after the rollback. |
update | ( | $table, | |
$columns, | |||
$where = null , |
|||
$args = null , |
|||
$insert_if_not_exists = false |
|||
) |
Update one or more record(s).
string | $table | Table name. |
array | $columns | Columns to update (key = column name, value = new value). |
string | array | $where | Where. |
string | array | $args | Arguments (assoc.array) or extra statement for the where. |
bool | $insert_if_not_exists | If true, a new record will be inserted when there is not yet an existing record. |
$allowInsert = true |
$logTables = [] |
$logTimes |
Query time above which to add a note to the log (key = prio, value = edge; higher times first).
$logTimeSignificant = 3 |
$migrateClassName = __CLASS__ . '\\Migrate' |
$statTime = 3600 |