Flourish PHP Unframework

fORMDatabase

static class, v1.0.0b32

Holds a single instance of the fDatabase class and provides database manipulation functionality for ORM code

Changes:
1.0.0b32Added support to addWhereClause() for the ^~ and $~ operators 6/20/11
1.0.0b31Fixed a bug with addWhereClause() generating invalid SQL 5/10/11
1.0.0b30Fixed insertFromAndGroupByClauses() to insert MAX() around columns in related tables in the ORDER BY clause when a GROUP BY is used 2/3/11
1.0.0b29Added code to handle old PCRE engines that don't support unicode character properties 12/6/10
1.0.0b28Fixed a bug in the fProgrammerException that is thrown when an improperly formatted OR condition is provided 11/24/10
1.0.0b27Fixed addWhereClause() to ignore fuzzy search clauses with no values to match 10/19/10
1.0.0b26Fixed insertFromAndGroupByClauses() to handle SQL where a table is references in more than one capitalization 7/26/10
1.0.0b25Fixed insertFromAndGroupByClauses() to properly handle recursive relationships 7/22/10
1.0.0b24Fixed parseSearchTerms() to work with non-ascii terms 6/30/10
1.0.0b23Fixed error messages in retrieve() 4/23/10
1.0.0b22Added support for IBM DB2, fixed an issue with building record sets or records that have recursive relationships 4/13/10
1.0.0b21Changed injectFromAndGroupByClauses() to be able to handle table aliases that contain other aliases inside of them 3/3/10
1.0.0b20Fixed a bug where joining to a table two separate ways could cause table alias issues and incorrect SQL to be generated 12/16/09
1.0.0b19Added the ability to compare columns with the =:, !:, <:, <=:, >: and >=: operators 12/8/09
1.0.0b18Fixed a bug affecting where conditions with columns that are not null but have a default value 11/3/09
1.0.0b17Added support for multiple databases 10/28/09
1.0.0b16Internal Backwards Compatibility Break - Renamed methods and significantly changed parameters and functionality for SQL statements to use value placeholders, identifier escaping and to handle schemas 10/22/09
1.0.0b15Streamlined intersection operator SQL and added support for the second value being NULL 9/21/09
1.0.0b14Added support for the intersection operator >< to createWhereClause() 7/13/09
1.0.0b13Added support for the AND LIKE operator &~ to createWhereClause() 7/9/09
1.0.0b12Added support for the NOT LIKE operator !~ to createWhereClause() 7/8/09
1.0.0b11Added support for concatenated columns to escapeBySchema() 6/19/09
1.0.0b10Updated createWhereClause() to properly handle NULLs for arrays of values when doing = and != comparisons 6/17/09
1.0.0b9Changed replacement values in preg_replace() calls to be properly escaped 6/11/09
1.0.0b8Fixed a bug with creatingWhereClause() where a null value would not be escaped property 5/12/09
1.0.0b7Fixed a bug where an OR condition in createWhereClause() could not have one of the values be an array 4/22/09
1.0.0b6insertFromAndGroupByClauses() will no longer wrap ungrouped columns if in a CAST or CASE statement for ORDER BY clauses of queries with a GROUP BY clause 3/23/09
1.0.0b5Fixed parseSearchTerms() to include stop words when they are the only thing in the search string 12/31/08
1.0.0b4Fixed a bug where loading a related record in the same table through a one-to-many relationship caused recursion 12/24/08
1.0.0b3Fixed a bug from 1.0.0b2 12/5/08
1.0.0b2Added support for != and <> to createWhereClause() and createHavingClause() 12/4/08
1.0.0bThe initial implementation 8/4/07

Static Methods

::addHavingClause() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Creates a HAVING clause from an array of conditions

Signature

array addHavingClause( fDatabase $db, fSchema $schema, array $params, string $table, array $conditions )

Parameters

fDatabase $db The database the query will be executed on
fSchema $schema The schema for the database
array $params The params for the fDatabase::query() call
string $table The table the query is being executed on
array $conditions The array of conditions - see fRecordSet::build() for format

Returns

The params with the HAVING clause added

::addOrderByClause() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Adds an ORDER BY clause to an array of params for an fDatabase::query() call

Signature

array addOrderByClause( fDatabase $db, fSchema $schema, array $params, string $table, array $order_bys )

Parameters

fDatabase $db The database the query will be executed on
fSchema $schema The schema object for the database the query will be executed on
array $params The parameters for the fDatabase::query() call
string $table The table any ambigious column references will refer to
array $order_bys The array of order bys to use - see fRecordSet::build() for format

Returns

The params with a SQL ORDER BY clause added

::addPrimaryKeyWhereParams() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Add the appropriate SQL and params for a WHERE clause condition for primary keys of the table specified

Signature

array addPrimaryKeyWhereParams( fSchema $schema, array $params, string $table, string $table_alias, array &$values, array &$old_values )

Parameters

fSchema $schema The schema for the database the query will be run on
array $params The currently constructed params for fDatabase::query() - the first param should be a SQL statement
string $table The table to build the where clause for
string $table_alias The alias for the table
array &$values The values array for the fActiveRecord object
array &$old_values The old values array for the fActiveRecord object

Returns

The params to pass to fDatabase::query(), including the new primary key where condition

::addWhereClause() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Adds a WHERE clause, from an array of conditions, to the parameters for an fDatabase::query() call

Signature

array addWhereClause( fDatabase $db, fSchema $schema, array $params, string $table, array $conditions )

Parameters

fDatabase $db The database the query will be executed on
fSchema $schema The schema for the database
array $params The parameters for the fDatabase::query() call
string $table The table any ambigious column references will refer to
array $conditions The array of conditions - see fRecordSet::build() for format

Returns

The params with the SQL WHERE clause added

::attach() public

Allows attaching an fDatabase-compatible objects for by ORM code

If a $name other than default is used, any fActiveRecord classes that should use it will need to be configured by passing the class name and $name to mapClassToDatabase(). The $name parameter should be unique per database or database master/slave setup.

The $role is used by code to allow for master/slave database setups. There can only be one database object attached for either of the roles, 'read' or 'write'. If the role 'both' is specified, it will be applied to both the 'read' and 'write' roles. Any sort of logic for picking one out of multiple databases should be done before this method is called.

Signature

void attach( fDatabase $database, string $name='default', string $role='both' )

Parameters

fDatabase $database An object that is compatible with fDatabase
string $name The name for the database instance
string $role If the database should be used for 'read', 'write' or 'both' operations

::injectFromAndGroupByClauses() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Finds all of the table names in the SQL and creates the appropriate FROM and GROUP BY clauses with all necessary joins

The SQL string should contain two placeholders, :from_clause and :group_by_clause, although the later may be omitted if necessary. All columns should be qualified with their full table name.

Here is an example SQL string to pass in presumming that the tables users and groups are in a relationship:

SELECT users.* FROM :from_clause WHERE groups.group_id = 5 :group_by_clause ORDER BY lower(users.first_name) ASC

Signature

array injectFromAndGroupByClauses( fDatabase $db, fSchema $schema, array $params, string $table )

Parameters

fDatabase $db The database the query is to be executed on
fSchema $schema The schema for the database
array $params The parameters for the fDatabase::query() call
string $table The main table to be queried

Returns

The params with the SQL FROM and GROUP BY clauses injected

::makeCondition() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Makes a condition for a SQL statement out of fDatabase::escape() placeholders

Signature

string makeCondition( fSchema $schema, string $table, string $column, string $comparison_operator, mixed $value )

Parameters

fSchema $schema The schema object for the database the query will be executed on
string $table The table to create the condition for
string $column The column to make the condition for
string $comparison_operator The comparison operator for the condition
mixed $value The value for the condition, which allows the $comparison_operator to be tweaked for NULL values

Returns

A SQL condition using fDatabase::escape() placeholders

::parseSearchTerms() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Parses a search string into search terms, supports quoted phrases and removes extra punctuation

Signature

void parseSearchTerms( string $terms, boolean $ignore_stop_words=FALSE )

Parameters

string $terms A text string from a form input to parse into search terms
boolean $ignore_stop_words If stop words should be ignored, this setting will be ignored if all words are stop words

::reset() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Resets the configuration of the class

Signature

void reset( )

::retrieve() public

Return the instance of the fDatabase class

Signature

fDatabase retrieve( string $class='fActiveRecord', string $role='either' )

Parameters

string $class The class to retrieve the database for - if not specified, the default database will be returned
string $role If the database will be used for 'write', 'read' or 'either' operations

Returns

The database instance

::splitHavingConditions() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

Removed aggregate function calls from where conditions array and puts them in a having conditions array

Signature

array splitHavingConditions( array &$where_conditions )

Parameters

array &$where_conditions The where conditions to look through for aggregate functions

Returns

The conditions to be put in a HAVING clause