The Reverse module is an extension of the Common Module driver, and is made up in the following structure:
Common driver file that encapsulates an API that can be used to execute the reverse driver queries.
A set of DBMS specific drivers that are specifically written against the supported database back end (e.g. mysql.php for MySQL, and pgsql.php for PostgreSQL)
In order to use the MDB2 Reverse drivers, it is necessary to first load the Reverse driver into your MDB2 instance. Lets start a MDB2 instance and set up our connection:
Example 36-1. Loading the Reverse module
|
Example 36-2. Create a table
|
The getTableFieldDefinition() Method exists primarily to get an array that defines a table field. This array can then be used to re-create the table elsewhere, or for any other purpose that may be necessary. Using the MDB2 instance defined above, we will connect to a database, define a table that we want to work with, and reverse engineer a specific field that we are interested in. First, we need to define the table and field that we want to work with; then, it is as easy as a single line of code to get the table definition back as an array of mixed values, then using var_dump to view the results:
Example 36-3. Get the table definition
|
array(1) { [0] => array(5) { ['notnull'] => bool(false) ['nativetype'] => string(4) "date" ['default'] => NULL ['type'] => string(4) "date" ['mdb2type'] => string(4) "date" } } |
A number of other methods exist to gain information about a selected table. You may use any of the following methods according to the information needed:
getTableIndexDefinition(): - requires a table name and an index name to return information about the table index as an array
getTableConstraintDefinition(): requires a table name and a constraint to query against. This will return any constraint definitions that exist on the table. A constraint as is defined here is usually a Primary Key, an Unique Key or a Foreign Key. The returned array has the same structure as the one used in the Manager module with createConstraint().
getSequenceDefinition(): requires a sequence name. It will return information about the existing table sequence. The method will return an array on success or an MDB2 error on failure.
getTriggerDefinition(): takes a trigger name as an argument, and will return information as an array on the trigger queried.
This method will return a lot of information regarding a table, and can be used in a number of different ways. The information that it returns will differ slightly across different RDBM's and may give some variance in results. This method can be used to query either a table definition, or a resultset, which makes it great for creating optimized tables. The tableInfo() method allows you to pass a parameter for the mode that you would like to see the results presented as. In order to demonstrate the results more effectively, we will use a series of examples to do some queries and return the results in different modes. NOTE: Either a table OR a resultset can be passed as the first parameter to get information on the table. In these examples, we will be using the table that we defined above.
Example 36-4. tableinfo() usage 1
|
Example 36-5. tableinfo() usage 2
|
NOTE: The flags element will contain a space-separated list of additional information about the field. This data is inconsistent between DBMS's due to the way each DBMS works:
primary_key
unique_key
multiple_key
not_null
unsigned