Flourish PHP Unframework

fSchema

class, v1.0.0b51

Gets schema information for the selected database

Changes:
1.0.0b51Fixed handling of getting tables in table creation order when a table references itself, fixed default value detection for the last column in a MySQL table 1/12/12
1.0.0b50Fixed detection of explicitly named SQLite foreign key constraints 8/23/11
1.0.0b49Added support for spatial/geometric data types in MySQL and PostgreSQL 5/26/11
1.0.0b48Fixed a bug with getTables() not working on MySQL 4.x, fixed getKeys() to always return a reset array 5/24/11
1.0.0b47Backwards Compatibility Break - getTables(), getColumnInfo(), getDatabases(), getKeys() and getRelationships() now return database, schema, table and column names in lowercase, added the $creation_order parameter to getTables(), fixed bugs with getting column and key information from MSSQL, Oracle and SQLite 5/9/11
1.0.0b46Enhanced SQLite schema detection to cover situations where UNIQUE constraints are defined separately from the table and when comments are used in CREATE TABLE statements 2/6/11
1.0.0b45Fixed Oracle auto incrementing detection to work with INSERT OR UPDATE triggers, fixed detection of dynamic default date/time/timestamp values for DB2 and Oracle 12/4/10
1.0.0b44Fixed the list of valid elements for getColumnInfo() 11/28/10
1.0.0b43Added the comment element to the information returned by getColumnInfo() 11/28/10
1.0.0b42Fixed a bug with MySQL detecting default ON DELETE clauses 10/19/10
1.0.0b41Fixed handling MySQL table names that require quoting 8/24/10
1.0.0b40Fixed bugs in the documentation and error message of getColumnInfo() about what are valid elements 7/21/10
1.0.0b39Fixed a regression where key detection SQL was not compatible with PostgreSQL 8.1 4/13/10
1.0.0b38Added Oracle support to getDatabases() 4/13/10
1.0.0b37Fixed getDatabases() for MSSQL 4/9/10
1.0.0b36Fixed PostgreSQL to properly report explicit NULL default values via getColumnInfo() 3/30/10
1.0.0b35Added max_length values for various text and blob data types across all databases 3/29/10
1.0.0b34Added min_value and max_value attributes to getColumnInfo() to specify the valid range for numeric columns 3/16/10
1.0.0b33Changed it so that PostgreSQL unique indexes containing functions are ignored since they can't be properly detected at this point 3/14/10
1.0.0b32Fixed getTables() to not include views for MySQL 3/14/10
1.0.0b31Fixed the creation of the default caching key for enableCaching() 3/2/10
1.0.0b30Fixed the class to work with lower privilege Oracle accounts and added detection of Oracle number columns 1/25/10
1.0.0b29Added on_delete and on_update elements to one-to-one relationship info retrieved by getRelationships() 12/16/09
1.0.0b28Fixed a bug with detecting some multi-column unique constraints in SQL Server databases 11/13/09
1.0.0b27Added a parameter to enableCaching() to provide a key token that will allow cached values to be shared between multiple databases with the same schema 10/28/09
1.0.0b26Added the placeholder element to the output of getColumnInfo(), added support for PostgreSQL, MSSQL and Oracle "schemas", added support for parsing quoted SQLite identifiers 10/22/09
1.0.0b25One-to-one relationships utilizing the primary key as a foreign key are now properly detected 9/22/09
1.0.0b24Fixed MSSQL support to work with ODBC database connections 9/18/09
1.0.0b23Fixed a bug where one-to-one relationships were being listed as many-to-one 7/21/09
1.0.0b22PostgreSQL UNIQUE constraints that are created as indexes and not table constraints are now properly detected 7/8/09
1.0.0b21Added support for the UUID data type in PostgreSQL 6/18/09
1.0.0b20Add caching of merged info, improved performance of getColumnInfo() 6/15/09
1.0.0b19Fixed a couple of bugs with setKeysOverride() 6/4/09
1.0.0b18Added missing support for MySQL mediumint columns 5/18/09
1.0.0b17Fixed a bug with clearCache() not properly reseting the tables and databases list 5/13/09
1.0.0b16Backwards Compatibility Break - setCacheFile() changed to enableCaching() and now requires an fCache object, flushInfo() renamed to clearCache(), added Oracle support 5/4/09
1.0.0b15Added support for the three different types of identifier quoting in SQLite 3/28/09
1.0.0b14Added support for MySQL column definitions containing the COLLATE keyword 3/28/09
1.0.0b13Fixed a bug with detecting PostgreSQL columns having both a CHECK constraint and a UNIQUE constraint 2/27/09
1.0.0b12Fixed detection of multi-column primary keys in MySQL 2/27/09
1.0.0b11Fixed an issue parsing MySQL tables with comments 2/25/09
1.0.0b10Added the getDatabases() method 2/24/09
1.0.0b9Now detects unsigned and zerofill MySQL data types that do not have a parenthetical part 2/16/09
1.0.0b8Mapped the MySQL data type 'set' to 'varchar', however valid values are not implemented yet 2/1/09
1.0.0b7Fixed a bug with detecting MySQL timestamp columns 1/28/09
1.0.0b6Fixed a bug with detecting MySQL columns that accept NULL 1/19/09
1.0.0b5setColumnInfo(): fixed a bug with not grabbing the real database schema first, made general improvements 1/19/09
1.0.0b4Added support for MySQL binary data types, numeric data type options unsigned and zerofill, and per-column character set definitions 1/17/09
1.0.0b3Fixed detection of the data type of MySQL timestamp columns, added support for dynamic default date/time values 1/11/09
1.0.0b2Fixed a bug with detecting multi-column unique keys in MySQL 1/3/09
1.0.0bThe initial implementation 9/25/07

Methods

->__construct() public

Sets the database

Signature

fSchema __construct( fDatabase $database )

Parameters

fDatabase $database The fDatabase instance

->__get() 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

All requests that hit this method should be requests for callbacks

Signature

callback __get( string $method )

Parameters

string $method The method to create a callback for

Returns

The callback for the method requested

->clearCache() 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

Clears all of the schema info out of the object and, if set, the fCache object

Signature

void clearCache( )

->enableCaching() public

Sets the schema to be cached to the fCache object specified

Signature

void enableCaching( fCache $cache, string $key_token=NULL )

Parameters

fCache $cache The cache to cache to
string $key_token Internal use only! (this will be used in the cache key to uniquely identify the cache for this fSchema object)

->getColumnInfo() public

Returns column information for the table specified

If only a table is specified, column info is in the following format:

array(
    (string) {column name} => array(
        'type'           => (string)  {data type},
        'placeholder'    => (string)  {fDatabase::escape() placeholder for this data type},
        'not_null'       => (boolean) {if value can't be null},
        'default'        => (mixed)   {the default value},
        'valid_values'   => (array)   {the valid values for a varchar field},
        'max_length'     => (integer) {the maximum length in a varchar field},
        'min_value'      => (numeric) {the minimum value for an integer/float field},
        'max_value'      => (numeric) {the maximum value for an integer/float field},
        'decimal_places' => (integer) {the number of decimal places for a decimal/numeric/money/smallmoney field},
        'auto_increment' => (boolean) {if the integer primary key column is a serial/autoincrement/auto_increment/indentity column},
        'comment'        => (string)  {the SQL comment/description for the column}
    ), ...
)

If a table and column are specified, column info is in the following format:

array(
    'type'           => (string)  {data type},
    'placeholder'    => (string)  {fDatabase::escape() placeholder for this data type},
    'not_null'       => (boolean) {if value can't be null},
    'default'        => (mixed)   {the default value-may contain special strings CURRENT_TIMESTAMP, CURRENT_TIME or CURRENT_DATE},
    'valid_values'   => (array)   {the valid values for a varchar field},
    'max_length'     => (integer) {the maximum length in a char/varchar field},
    'min_value'      => (fNumber) {the minimum value for an integer/float field},
    'max_value'      => (fNumber) {the maximum value for an integer/float field},
    'decimal_places' => (integer) {the number of decimal places for a decimal/numeric/money/smallmoney field},
    'auto_increment' => (boolean) {if the integer primary key column is a serial/autoincrement/auto_increment/indentity column},
    'comment'        => (string)  {the SQL comment/description for the column}
)

If a table, column and element are specified, returned value is the single element specified.

The 'type' element is homogenized to a value from the following list:

  • 'varchar'
  • 'char'
  • 'text'
  • 'integer'
  • 'float'
  • 'timestamp'
  • 'date'
  • 'time'
  • 'boolean'
  • 'blob'

Please note that MySQL reports boolean data types as tinyint(1), so all tinyint(1) columns will be listed as boolean. This can be fixed by calling:

$schema->setColumnInfoOverride(
    array(
        'type'        => 'integer',
        'placeholder' => '%i',
        'default'     => {default integer},
        'min_value'   => new fNumber(-128),
        'max_value'   => new fNumber(127)
    ),
    '{table name}',
    '{column name}'
);

The 'comment' element pulls from the database's column comment facility with the exception of MSSQL and SQLite.

For MSSQL, the comment is pulled from the MS_Description extended property, which can be added via the Description field in SQL Server Management Studio, or via the sp_addextendedproperty stored procedure.

For SQLite, the comment is extracted from any SQL comment that is placed at the end of the line on which the column is defined:

CREATE TABLE users (
    user_id INTEGER PRIMARY KEY AUTOINCREMENT,
    name VARCHAR(200) NOT NULL -- This is the full name
);

For the SQLite users table defined above, the name column will have the comment This is the full name.

Signature

mixed getColumnInfo( string $table, string $column=NULL, string $element=NULL )

Parameters

string $table The table to get the column info for
string $column The column to get the info for
string $element The element to return: 'type', 'placeholder', 'not_null', 'default', 'valid_values', 'max_length', 'min_value', 'max_value', 'decimal_places', 'auto_increment', 'comment'

Returns

The column info for the table/column/element specified - see method description for format

->getDatabases() public

Returns the databases on the current server

Signature

array getDatabases( )

Returns

The databases on the current server

->getKeys() public

Returns a list of primary key, foreign key and unique key constraints for the table specified

The structure of the returned array is:

array(
     'primary' => array(
         {column name}, ...
     ),
     'unique'  => array(
         array(
             {column name}, ...
         ), ...
     ),
     'foreign' => array(
         array(
             'column'         => {column name},
             'foreign_table'  => {foreign table name},
             'foreign_column' => {foreign column name},
             'on_delete'      => {the ON DELETE action: 'no_action', 'restrict', 'cascade', 'set_null', or 'set_default'},
             'on_update'      => {the ON UPDATE action: 'no_action', 'restrict', 'cascade', 'set_null', or 'set_default'}
         ), ...
     )
)

Signature

array getKeys( string $table, string $key_type=NULL )

Parameters

string $table The table to return the keys for
string $key_type The type of key to return: 'primary', 'foreign', 'unique'

Returns

An array of all keys, or just the type specified - see method description for format

->getRelationships() public

Returns a list of one-to-one, many-to-one, one-to-many and many-to-many relationships for the table specified

The structure of the returned array is:

array(
    'one-to-one' => array(
        array(
            'table'          => (string) {the name of the table this relationship is for},
            'column'         => (string) {the column in the specified table},
            'related_table'  => (string) {the related table},
            'related_column' => (string) {the related column},
            'on_delete'      => (string) {the ON DELETE action: 'no_action', 'restrict', 'cascade', 'set_null', or 'set_default'},
            'on_update'      => (string) {the ON UPDATE action: 'no_action', 'restrict', 'cascade', 'set_null', or 'set_default'}
        ), ...
    ),
    'many-to-one' => array(
        array(
            'table'          => (string) {the name of the table this relationship is for},
            'column'         => (string) {the column in the specified table},
            'related_table'  => (string) {the related table},
            'related_column' => (string) {the related column}
        ), ...
    ),
    'one-to-many' => array(
        array(
            'table'          => (string) {the name of the table this relationship is for},
            'column'         => (string) {the column in the specified table},
            'related_table'  => (string) {the related table},
            'related_column' => (string) {the related column},
            'on_delete'      => (string) {the ON DELETE action: 'no_action', 'restrict', 'cascade', 'set_null', or 'set_default'},
            'on_update'      => (string) {the ON UPDATE action: 'no_action', 'restrict', 'cascade', 'set_null', or 'set_default'}
        ), ...
    ),
    'many-to-many' => array(
        array(
            'table'               => (string) {the name of the table this relationship is for},
            'column'              => (string) {the column in the specified table},
            'related_table'       => (string) {the related table},
            'related_column'      => (string) {the related column},
            'join_table'          => (string) {the table that joins the specified table to the related table},
            'join_column'         => (string) {the column in the join table that references 'column'},
            'join_related_column' => (string) {the column in the join table that references 'related_column'},
            'on_delete'           => (string) {the ON DELETE action: 'no_action', 'restrict', 'cascade', 'set_null', or 'set_default'},
            'on_update'           => (string) {the ON UPDATE action: 'no_action', 'restrict', 'cascade', 'set_null', or 'set_default'}
        ), ...
    )
)

Signature

array getRelationships( string $table, string $relationship_type=NULL )

Parameters

string $table The table to return the relationships for
string $relationship_type The type of relationship to return: 'one-to-one', 'many-to-one', 'one-to-many', 'many-to-many'

Returns

An array of all relationships, or just the type specified - see method description for format

->getTables() public

Returns the tables in the current database

Signature

array getTables( boolean|string $creation_order=NULL )

Parameters

boolean|string $creation_order TRUE to return in a valid table creation order, or a table name to return that table and any tables that depend on it, in table creation order

Returns

The tables in the current database, all converted to lowercase

->setColumnInfoOverride() public

Allows overriding of column info

Performs an array merge with the column info detected from the database.

To erase a whole table, set the $column_info to NULL. To erase a column, set the $column_info for that column to NULL.

If the $column_info parameter is not NULL, it should be an associative array containing one or more of the following keys. Please see getColumnInfo() for a description of each.

  • 'type'
  • 'placeholder'
  • 'not_null'
  • 'default'
  • 'valid_values'
  • 'max_length'
  • 'min_value'
  • 'max_value'
  • 'decimal_places'
  • 'auto_increment'
  • 'comment'

The following keys may be set to NULL:

  • 'not_null'
  • 'default'
  • 'valid_values'
  • 'max_length'
  • 'min_value'
  • 'max_value'
  • 'decimal_places'
  • 'comment'

The key 'auto_increment' should be a boolean.

The 'type' key should be one of:

  • 'blob'
  • 'boolean'
  • 'char'
  • 'date'
  • 'float'
  • 'integer'
  • 'text'
  • 'time'
  • 'timestamp'
  • 'varchar'

Signature

void setColumnInfoOverride( array $column_info, string $table, string $column=NULL )

Parameters

array $column_info The modified column info - see method description for format
string $table The table to override
string $column The column to override

->setKeysOverride() public

Allows overriding of key info. Replaces existing info, so be sure to provide full key info for type selected or all types.

Signature

void setKeysOverride( array $keys, string $table, string $key_type=NULL )

Parameters

array $keys The modified keys - see getKeys() for format
string $table The table to override
string $key_type The key type to override: 'primary', 'foreign', 'unique'