![]() |
FRED™
3.0
FRED™: Framework for Rapid and Easy Development
|
Database access layer (PDO based). More...
Public Member Functions | |
| createPdo ($options=null, $attributes=null) | |
| Create a PDO instance. More... | |
| 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... | |
| escapeLike ($value, $escape='\\') | |
| Escape a value used for a "like" comparision. 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... | |
| batch ($callback, $sql, $args=null, $limit=1000) | |
| Run a callback function for every row in a batched 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, $action=self::ACTION_INSERT, $update=null) | |
| Insert a record. More... | |
| create ($table, $columns) | |
| Create a record (if it does not exists). More... | |
| replace ($table, $columns) | |
| Replace a record (insert if not exists). More... | |
| upsert ($table, $columns, $update) | |
| Insert a record, or update it if it already exists. More... | |
| select ($table, $columns=' *', $where=null, $args=null, $limit=null, $offset=null) | |
| Select records. More... | |
| update ($table, $columns, $where, $args=null, $insert_if_not_exists=false) | |
| Update one or more record(s). More... | |
| inc ($table, $columns, $where) | |
| Increment one or more columns, or insert a new record if none exists. More... | |
| delete ($table, $where, $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' |
| Insert a new record. More... | |
| const | ACTION_CREATE = 'create' |
| Create a new record (if none exists, otherwise ignore). More... | |
| const | ACTION_REPLACE = 'replace' |
| Replace an existing record (overwrite). More... | |
| const | ACTION_UPDATE = 'update' |
| Update an existing record. More... | |
| const | ACTION_UPSERT = 'upsert' |
| Insert a new record, or update the existing record if it already exists. More... | |
| const | ACTION_DELETE = 'delete' |
| Delete an existing record. More... | |
| const | EVENT_OPEN = 'db:open' |
| const | EVENT_LOG_CHANGE = 'db:logChange' |
| const | EVENT_LOG_ADD_ID = 'db:logAddId' |
| $multiOperators = ['|' => 'or','&' => 'and'] | |
| Operators (key) and glue (value) for multi column where conditions. More... | |
| $statTime = 3600 | |
| Query time above which to increment a stat counter for the specific query (id = SQL). More... | |
| $statPrefix = 'db' | |
| $logTimes | |
| $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 an auto-increment ID to the latest change log. More... | |
| logChanges ($table, $action, $columns, $where, $args=null) | |
| Log the changes for an action. More... | |
| updateSql ($columns, &$args) | |
| aggregate ($table, $function, $where=null, $args=null) | |
| Run an aggregation query. More... | |
| 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... | |
| alias ($alias, $property, $object=null) | |
| Define an alias for a property (of another object). 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 | |
| $_defaultOptions = [\PDO::MYSQL_ATTR_FOUND_ROWS => true] | |
| $_defaultAttributes = [\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true] | |
| $_eachOptions = [\PDO::ATTR_PERSISTENT => false] | |
| Do not (re-) use an existing connection (might be busy). More... | |
| $_eachAttributes = [\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false] | |
| Do not buffer; retrieve one by one. More... | |
| $_connection = [] | |
| Connection parameters (keys 'dsn', 'username', 'password', 'options'). More... | |
| $_attributes = [] | |
| Connection specific attributes (key = attribute, value = value). More... | |
| $_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... | |
| $_aliases = [] | |
| Alias properties (key = alias, value = [object,property]). More... | |
|
protected |
| all | ( | $sql, | |
$args = null |
|||
| ) |
| average | ( | $table, | |
| $column, | |||
$where = null, |
|||
$args = null |
|||
| ) |
| batch | ( | $callback, | |
| $sql, | |||
$args = null, |
|||
$limit = 1000 |
|||
| ) |
Run a callback function for every row in a batched SQL resultset.
The rows are fetched in batches. This makes it possible to process huge datasets one record at a time, without using a separate connection, and without running out of memory. Note that this may not work perfectly when the dataset changes frequently (items added or deleted in the beginning shift the contents of later batches).
| callable | $callback | Callback function (parameters row and index). 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. |
| int | $limit | Batch size. |
| columns | ( | $table, | |
$database = null |
|||
| ) |
| count | ( | $table, | |
$where = null, |
|||
$args = null |
|||
| ) |
| create | ( | $table, | |
| $columns | |||
| ) |
| createPdo | ( | $options = null, |
|
$attributes = null |
|||
| ) |
| date | ( | $time = null | ) |
| dateTime | ( | $time = null | ) |
| delete | ( | $table, | |
| $where, | |||
$args = null |
|||
| ) |
| each | ( | $callback, | |
| $sql, | |||
$args = null |
|||
| ) |
Run a callback function for every row in an SQL resultset.
A separate 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 (parameters row and index). 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. |
| escapeLike | ( | $value, | |
$escape = '\\' |
|||
| ) |
| execute | ( | $sql, | |
$args = null, |
|||
$pdo = null |
|||
| ) |
| exists | ( | $table, | |
$where = null, |
|||
$args = null |
|||
| ) |
| explain | ( | $sql, | |
$args = null |
|||
| ) |
| fetch | ( | $statement | ) |
| inc | ( | $table, | |
| $columns, | |||
| $where | |||
| ) |
Increment one or more columns, or insert a new record if none exists.
| string | $table | Table name. |
| string | array | $columns | Column to increment (string), or columns to update (array; key = column name, value = increment). |
| array | $where | Key to increment. |
| insert | ( | $table, | |
| $columns, | |||
$action = self::ACTION_INSERT, |
|||
$update = null |
|||
| ) |
Insert a record.
| string | $table | Table name. |
| array | $columns | Columns (key = column name, value = value or values - for multiple records). |
| string | $action | Insert type to perform (see ACTION_* constants). |
| string | array | $update | Update for an upsert action. |
| 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, | |||
$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 (empty = all). |
| 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. |
| upsert | ( | $table, | |
| $columns, | |||
| $update | |||
| ) |
Insert a record, or update it if it already exists.
| string | $table | Table name. |
| array | $columns | Columns (key = column name, value = value or values - for multiple records). |
| array | $update | Columns to update if the record already exists (key = column name, value = new value). |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
| $allowInsert = true |
| $logTables = [] |
| $logTimes |
| $logTimeSignificant = 3 |
| $migrateClassName = __CLASS__ . '\\Migrate' |
| $multiOperators = ['|' => 'or','&' => 'and'] |
| $statTime = 3600 |
| const ACTION_CREATE = 'create' |
| const ACTION_REPLACE = 'replace' |
| const ACTION_UPSERT = 'upsert' |