cvs: peardoc /en/package/database mdb2.xml /en/package/database/mdb2 intro-manager-module.xml intro-reverse-module.xml

From: Date: Tue, 27 Feb 2007 19:29:14 +0000
Subject: cvs: peardoc /en/package/database mdb2.xml /en/package/database/mdb2 intro-manager-module.xml intro-reverse-module.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-9478@lists.php.net to get a copy of this message
quipo Tue Feb 27 19:29:14 2007 UTC Added files: /peardoc/en/package/database/mdb2 intro-manager-module.xml intro-reverse-module.xml Modified files: /peardoc/en/package/database mdb2.xml Log: converted documentation written by Paul Scott and Stoyan Stefanov to peardoc. PLEASE TEST. Thank you.

http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb2.xml?r1=1.14&r2=1.15&diff_format=u Index: peardoc/en/package/database/mdb2.xml diff -u peardoc/en/package/database/mdb2.xml:1.14 peardoc/en/package/database/mdb2.xml:1.15 --- peardoc/en/package/database/mdb2.xml:1.14 Sun Dec 3 09:14:24 2006 +++ peardoc/en/package/database/mdb2.xml Tue Feb 27 19:29:14 2007 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> -<!-- $Revision: 1.14 $ --> +<!-- $Revision: 1.15 $ --> <sect1 id="package.database.mdb2"> <title>MDB2</title> <para> @@ -15,6 +15,8 @@ &package.database.mdb2.intro-execute; &package.database.mdb2.intro-transaction; &package.database.mdb2.intro-module; + &package.database.mdb2.intro-manager-module.xml; + &package.database.mdb2.intro-reverse-module.xml; &package.database.mdb2.intro-auto; &package.database.mdb2.intro-portability; &package.database.mdb2.intro-sequences; http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb2/intro-manager-module.xml?view=markup&rev=1.1 Index: peardoc/en/package/database/mdb2/intro-manager-module.xml +++ peardoc/en/package/database/mdb2/intro-manager-module.xml <?xml version="1.0" encoding="iso-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.database.mdb2.intro-manager-module"> <refnamediv> <refname>Manager Module</refname> <refpurpose>Module for managing database structure</refpurpose> </refnamediv> <refsect1 id="package.database.mdb2.intro-manager-module.desc"> &title.desc; <para> The Manager module provides methods for managing database structure. The methods can be grouped based on their responsibility: create, edit (alter or update), list or delete (drop) database elements. The following document lists the available methods, providing examples of their use. The following tables will be created, altered and finally dropped, in a database named "events_db": <programlisting> <![CDATA[ events(id, name, datetime); people(id, name); event_participants(event_id, person_id); ?> ]]> </programlisting> To include the Manager module functionality, you need to load it first. <example> <title>Loading the Manager module</title> <programlisting role="php"> <![CDATA[ <?php require_once 'MDB2.php'; $dsn = 'pgsql://someuser:apasswd@somehost'; $mdb2 =& MDB2::factory($dsn); if (PEAR::isError($mdb2)) { die($mdb2->getMessage()); } // loading the Manager module $mdb2->loadModule('Manager'); ?> ]]> </programlisting> </example> After including the module, you can access its methods like this: <example> <title>Creating a database</title> <programlisting role="php"> <![CDATA[ <?php // PHP5 $mdb2->createDatabase('events_db'); // PHP4 and PHP5 $mdb2->manager->createDatabase('events_db'); ?> ]]> </programlisting> </example> Further in the document the PHP5-compatible way will be used. </para> </refsect1> <refsect1 id="package.database.mdb2.intro-manager-module.create-elements"> <title>Creating database elements</title> <para> These are methods to create new databases, tables, indices, constraints and sequences. </para> <refsect2 id="package.database.mdb2.intro-manager-module.create-elements.create-db"> <title>Creating a database</title> <para> <example> <title>Creating and setting an events_db database</title> <programlisting role="php"> <![CDATA[ <?php // MDB2 setup require_once 'MDB2.php'; $dsn = 'mysql://root:test@localhost'; // note that DB name is omitted $mdb2 =& MDB2::factory($dsn); // loading the Manager module $mdb2->loadModule('Manager'); // create the database $mdb2->createDatabase('events_db'); // set the new database to work with it $mdb2->setDatabase('events_db'); // the next time the DSN could be like // mysql://root:test@localhost/events_db ?> ]]> </programlisting> </example> </para> </refsect2> <refsect2 id="package.database.mdb2.intro-manager-module.create-elements.create-tables"> <title>Creating tables</title> <para> Now that the database is created, we can proceed with adding some tables. The method <function>createTable</function> takes three parameters: the table name, an array of field definition and some extra options (optional and RDBMS-specific). <example> <title>Creating the events table</title> <programlisting role="php"> <![CDATA[ <?php $definition = array ( 'id' => array ( 'type' => 'integer', 'unsigned' => 1, 'notnull' => 1, 'default' => 0, ), 'name' => array ( 'type' => 'text', 'length' => 255 ), 'datetime' => array ( 'type' => 'timestamp' ) ); $mdb2->createTable('events', $definition); ?> ]]> </programlisting> </example> The keys of the definition array are the names of the fields in the table. The values are arrays containing the required key 'type' as well as other keys, depending on the value of 'type'. The values for the 'type' key are the same as the possible MDB2 datatypes. Depending on the datatype, the other options may vary. <table> <title>Options for the definition array based on datatype</title> <tgroup cols="5"> <thead> <row> <entry>Datatype</entry> <entry>length</entry> <entry>default</entry> <entry>not null</entry> <entry>unsigned</entry> </row> </thead> <tbody> <row> <entry>text</entry> <entry>x</entry> <entry>x</entry> <entry>x</entry> <entry></entry> </row> <row> <entry>boolean</entry> <entry></entry> <entry>x</entry> <entry>x</entry> <entry></entry> </row> <row> <entry>integer</entry> <entry></entry> <entry>x</entry> <entry>x</entry> <entry>x</entry> </row> <row> <entry>decimal</entry> <entry></entry> <entry>x</entry> <entry>x</entry> <entry></entry> </row> <row> <entry>float</entry> <entry></entry> <entry>x</entry> <entry>x</entry> <entry></entry> </row> <row> <entry>timestamp</entry> <entry></entry> <entry>x</entry> <entry>x</entry> <entry></entry> </row> <row> <entry>time</entry> <entry></entry> <entry>x</entry> <entry>x</entry> <entry></entry> </row> <row> <entry>date</entry> <entry></entry> <entry>x</entry> <entry>x</entry> <entry></entry> </row> <row> <entry>clob</entry> <entry>x</entry> <entry></entry> <entry>x</entry> <entry></entry> </row> <row> <entry>blob</entry> <entry>x</entry> <entry></entry> <entry>x</entry> <entry></entry> </row> </tbody> </tgroup> </table> The third parameter to <function>createTable</function> contains extra options for the table, based on the underlying RDBMS. Here's an example for MySQL. <example> <title>Creating the people table</title> <programlisting role="php"> <![CDATA[ <?php $table_options = array( 'comment' => 'Repository of people', 'character_set' => 'utf8', 'collate' => 'utf8_unicode_ci', 'type' => 'innodb', ); $definition = array ( 'id' => array ( 'type' => 'integer', 'unsigned' => 1, 'notnull' => 1, 'default' => 0, ), 'name' => array ( 'type' => 'text', 'length' => 255 ) ); $mdb2->createTable('people', $definition, $table_options); ?> ]]> </programlisting> </example> To round up the example database, here's the event_participants table creation code. <example> <title>Creating the event_participants table</title> <programlisting role="php"> <![CDATA[ <?php $definition = array ( 'event_id' => array ( 'type' => 'integer', 'unsigned' => 1, 'notnull' => 1, 'default' => 0, ), 'person_id' => array ( 'type' => 'integer', 'unsigned' => 1, 'notnull' => 1, 'default' => 0, ), ); $mdb2->createTable('event_participants', $definition, $table_options); ?> ]]> </programlisting> </example> </para> </refsect2> <refsect2 id="package.database.mdb2.intro-manager-module.create-elements.create-constraints"> <title>Creating constraints</title> <para> In the example events table, the id should be a primary key. Creating a primary key is a task done by the <function>createConstraint</function> method. It takes three parameters: the table name, the constraint name and the definition array. <example> <title>Creating primary keys in the events and people tables</title> <programlisting role="php"> <![CDATA[ <?php $definition = array ( 'primary' => true, 'fields' => array ( 'id' => array() ) ); $mdb2->createConstraint('events', 'keyname', $definition); $mdb2->createConstraint('people', 'keyname', $definition); ?> ]]> </programlisting> </example> Note: Some RDBMS may choose to ignore the name of the constraint, for example MySQL will not use the value <parameter>keyname<parameter> provided in the method call, but will use <parameter>PRIMARY<parameter> when a primary key is created. </para> <para> In the definition array, you specify which fields will be included in the constraint, using the <parameter>fields</parameter> key. The other possible keys in the definition array are <parameter>primary</parameter> and <parameter>unique</parameter>, which have boolean values. </para> <para> Let's create another key in the event_participants, where each row has to be unique, meaning that one person can appear only once for a specific event. The definitions array will have both fields in the <parameter>fields</parameter> key and the <parameter>unique</parameter> key will be set to true. <example> <title>Creating a unique constraint</title> <programlisting role="php"> <![CDATA[ <?php $definition = array ( 'unique' => true, 'fields' => array ( 'event_id' => array(), 'person_id' => array(), ) ); $mdb2->createConstraint('event_participants', 'unique_participant', $definition); ?> ]]> </programlisting> </example> </para> </refsect2> <refsect2 id="package.database.mdb2.intro-manager-module.create-elements.create-indices"> <title>Creating indices</title> <para> To create an index, the method <function>createIndex</function> is used, which has similar signature as <function>createConstraint</function>, so it takes table name, index name and a definition array. The definition array has one key <parameter>fields</parameter> with a value which is another associative array containing fields that will be a part of the index. The fields are defined as arrays with possible keys: <itemizedlist> <listitem> <parameter>sorting</parameter>, with values <parameter>ascending</parameter> and <parameter>descending</parameter> </listitem> <listitem> <parameter>length<parameter>, integer value </listitem> </itemizedlist> Not all RDBMS will support index sorting or length, in these cases the drivers will ignore them. In the test events database, we can assume that our application will show events occuring in a specific timeframe, so the selects will use the datetime field in WHERE conditions. It will help if there is an index on this field. <example> <title>Creating an index</title> <programlisting role="php"> <![CDATA[ <?php $definition = array( 'fields' => array( 'datetime' => array() ) ); $mdb2->createIndex('events', 'event_timestamp', $definition); ?> ]]> </programlisting> </example> </para> </refsect2> <refsect2 id="package.database.mdb2.intro-manager-module.create-elements.create-constraints"> <title>Creating sequences</title> <para> The way MDB2 handles sequences is <link linkend="package.database.mdb2.intro-sequences">described here</link>. For the events table in our example database, we'll need to have the 'id' auto-incrementing. For this purpose the method <function>nextId</function> is used to give the next value. <function>nextId</function> will create the sequence table if it doesn't exist, but we can create if beforehand with <function>createSequence</function>. It takes a sequence name and an optional start value for the sequence. <example> <title>Creating sequences</title> <programlisting role="php"> <![CDATA[ <?php $mdb2->createSequence('events'); $mdb2->createSequence('people', 1); ?> ]]> </programlisting> </example> In the default <classname>MDB2</classname> setup the example above will create two tables: events_seq and people_seq, each with one field called 'sequence', but the field name and the '_seq' postfix are configurable via the MDB2 options <parameter>seqname_format</parameter> and <parameter>seqcol_name</parameter>. </para> </refsect2> </refsect1> <refsect1 id="package.database.mdb2.intro-manager-module.alter-tables"> <title>Altering database tables</title> <para> Once a database table is created you can rename it or add, remove, rename and alter fields, using the <function>alterTable</function> method. <function>alterTable</function> takes three parameters: the table name, the definition of changes and a boolean "check-only" flag. If true, no changes will be made, but only a check if the proposed changes are feasable for the specific table and RDBMS. The second parameter (definition of changes) is an array with these keys: <itemizedlist> <listitem> <parameter>name</parameter>: new name for the table. This is the only key related to the table itself, the other keys contain field definitions </listitem> <listitem> <parameter>add<parameter>: fields to be added </listitem> <listitem> <parameter>remove<parameter>: fields to be removed </listitem> <listitem> <parameter>change<parameter>: fields to be changed </listitem> <listitem> <parameter>rename<parameter>: fields to be renamed </listitem> </itemizedlist> The values for add/remove/change/rename keys have slightly different structures, but they all contain field definitions. You can check the <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Manager_Common.html#methodalterTable">API docs</ulink> for more information and an examples. </para> </refsect1> <refsect1 id="package.database.mdb2.intro-manager-module.list-elements"> <title>Listing database elements</title> <para> To see what's in the database, you can use the list*() family of functions, namely: <itemizedlist> <listitem> <function>listDatabases</function> </listitem> <listitem> <function>listFunctions</function> </listitem> <listitem> <function>listSequences</function>: takes optional database name as a parameter. If not supplied, the currently selected database is assumed. </listitem> <listitem> <function>listTableConstraints</function>: takes a table name </listitem> <listitem> <function>listTableFields</function>: takes a table name </listitem> <listitem> <function>listTableIndexes</function>: takes a table name </listitem> <listitem> <function>listTables</function>: takes an optional database name </listitem> <listitem> <function>listTableTriggers</function>: takes a table name </listitem> <listitem> <function>listTableViews</function>: takes a table name </listitem> <listitem> <function>listUsers</function> </listitem> <listitem> <function>listViews</function>: takes an optional database name </listitem> </itemizedlist> <example> <title>Listing database elements</title> <programlisting role="php"> <![CDATA[ <?php $dbs = $mdb2->listDatabases(); print_r($dbs); /* prints: Array ( [0] => information_schema [1] => events_db [2] => mysql [3] => test [4] => test_db [5] => test_db_explain [6] => test_mdb2 ) */ $seqs = $mdb2->listSequences('events_db'); print_r($seqs); /* prints: Array ( [0] => events [1] => people ) */ $cons = $mdb2->listTableConstraints('event_participants'); print_r($cons); /* prints: Array ( [0] => unique_participant ) */ $fields = $mdb2->listTableFields('events'); print_r($fields); /* prints: Array ( [0] => id [1] => name [2] => datetime ) */ $idx = $mdb2->listTableIndexes('events'); print_r($idx); /* prints: Array ( [0] => event_timestamp ) */ $tables = $mdb2->listTables(); print_r($tables); /* prints: Array ( [0] => event_participants [1] => events [2] => people ) */ // currently there is no method to create a view, // so let's do it "manually" $sql = "CREATE VIEW names_only AS SELECT name FROM people"; $mdb2->exec($sql); $sql = "CREATE VIEW last_ten_events AS SELECT * FROM events ORDER BY id DESC LIMIT 0,10"; $mdb2->exec($sql); // list views $views = $mdb2->listViews(); print_r($views); /* prints: Array ( [0] => last_ten_events [1] => names_only ) */ /* Not implemented in the MySQL driver listTableTriggers() listTableViews() listFunctions() */ ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1 id="package.database.mdb2.intro-manager-module.delete-elements"> <title>Deleting database elements</title> <para> For every create*() method as shown above, there is a corresponding drop*() method to delete a database, a table, field, index or constraint. The drop*() methods do not check if the item to be deleted exists, so it's developer's responsibility to check for PEAR errors. <example> <title>Drop database elements</title> <programlisting role="php"> <![CDATA[ <?php // let's say our normal setup is to die on PEAR errors PEAR::setErrorHandling(PEAR_ERROR_DIE); // for the next statements we'll temporarily // change the error handling PEAR::pushErrorHandling(PEAR_ERROR_RETURN); // drop a sequence $result = $mdb2->dropSequence('nonexisting'); if (PEAR::isError($result)) { echo 'The sequence does not exist'; } else { echo 'Sequence dropped'; } // another sequence $result = $mdb2->dropSequence('people'); // drop a constraint $mdb2->dropConstraint('events', 'PRIMARY', true); // note: the third parameter gives a hint // that this is a primary key constraint $mdb2->dropConstraint('event_participants', 'unique_participant'); // drop an index $mdb2->dropIndex('events', 'event_timestamp'); // drop a table $mdb2->dropTable('events'); // drop the database already! $mdb2->dropDatabase('events_db'); // revert to the usual error handling PEAR::popErrorHandling(); ?> ]]> </programlisting> </example> </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb2/intro-reverse-module.xml?view=markup&rev=1.1 Index: peardoc/en/package/database/mdb2/intro-reverse-module.xml +++ peardoc/en/package/database/mdb2/intro-reverse-module.xml <?xml version="1.0" encoding="iso-8859-1" ?> <!-- $Revision: 1.1 $ --> <refentry id="package.database.mdb2.intro-reverse-module"> <refnamediv> <refname>Reverse Module</refname> <refpurpose>Module for managing database structure</refpurpose> </refnamediv> <refsect1 id="package.database.mdb2.intro-reverse-module.desc"> &title.desc; <para> The Reverse module is an extension of the <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Module_Common.html">Common Module driver</ulink>, and is made up in the following structure: <itemizedlist> <listitem> <simpara> <ulink url="http://pear.php.net/package/MDB2/docs/latest/MDB2/MDB2_Driver_Reverse_Common.html">Common driver file</ulink> that encapsulates an API that can be used to execute the reverse driver queries. </simpara> </listitem> <listitem> <simpara> 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) </simpara> </listitem> </itemizedlist> </para> </refsect1> <refsect1 id="package.database.mdb2.intro-reverse-module.usage"> <title>Usage</title> <para> 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> <title>Loading the Reverse module</title> <programlisting role="php"> <![CDATA[ <?php // Load up the MDB2 Base class require 'MDB2.php'; // define a DSN to connect to our database $dsn = array( 'phptype' => 'mysql', 'username' => 'someuser', 'password' => 'somepass', 'hostspec' => 'somehost', 'database' => 'somedb', ); // create MDB2 instance // MDB2 has a number of connection options - singleton(), factory() and connect() $mdb2 = &MDB2::connect($dsn); if(PEAR::isError($mdb2)) { // Die with the user info message on failure for any reason die($mdb2->getuserinfo()); } // We have a valid connection, so let's do some magic! // Load the Reverse Module using MDB2's loadModule method $mdb2->loadModule('Reverse', null, true); ?> ]]> </programlisting> </example> In the above example, we have created a valid connection to a database that does exist, using a valid user and password for our database server. We now have access to all of the MDB2 Reverse functionality within our example application. For the purposes of this document, we will set up an example table as such: <example> <title>Create a table</title> <programlisting role="php"> <![CDATA[ <?php // Define a table name that we will be working with $fields = array( 'id' => array( 'type' => 'integer', 'unsigned' => true, 'autoincrement' => true, ), 'somename' => array( 'type' => 'text', 'length' => 12, ), 'somedate' => array( 'type' => 'date', ), ); $table = 'sometable'; // Create the table //Load the Manager Module $mdb2->loadModule('Manager', null, true); //create the table with the manager module $mdb2->manager->createTable($table, $fields); ?> ]]> </programlisting> </example> </para> <refsect2 id="package.database.mdb2.intro-reverse-module.usage.gettablefielddefinition"> <title><function>getTableFieldDefinition</function> Method</title> <para> The <function>getTableFieldDefinition</function> 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> <title>Get the table definition</title> <programlisting role="php"> <![CDATA[ <?php // Set the field that we would like to reverse engineer (get definition of) $field = 'somedate'; // Here we get the definition of the table field and return it to a variable // The return will either be a mixed array on success, or an MDB2 error on failure, //so we don't need additional checks $def = $mdb2->getTableFieldDefinition($table, $field); // finally dumping the variable to screen var_dump($def); ?> ]]> </programlisting> </example> The return will look something like the following, depending on your field definition: <programlisting role="php"> <![CDATA[ array(1) { [0] => array(5) { ['notnull'] => bool(false) ['nativetype'] => string(4) "date" ['default'] => NULL ['type'] => string(4) "date" ['mdb2type'] => string(4) "date" } } ]]> </programlisting> </para> </refsect2> <refsect2 id="package.database.mdb2.intro-reverse-module.usage.additional-table-info"> <title>Additional table information</title> <para> 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: <itemizedlist> <listitem> <function>getTableIndexDefinition</function>: - requires a table name and an index name to return information about the table index as an array </listitem> <listitem> <function>getTableConstraintDefinition</function>: 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 or an Unique Key </listitem> <listitem> <function>getSequenceDefinition</function>: 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. </listitem> <listitem> <function>getTriggerDefinition</function>: takes a trigger name as an argument, and will return information as an array on the trigger queried. </listitem> </itemizedlist> </para> </refsect2> <refsect2 id="package.database.mdb2.intro-reverse-module.usage.tableinfo"> <title><function>tableInfo</function> Method</title> <para> 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 <function>tableInfo</function> 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> <title><function>tableinfo</function> usage 1</title> <programlisting role="php"> <![CDATA[ <?php // Default Mode - NULL $tableInfo = $mdb2->tableInfo($table, NULL); var_dump($tableInfo); //will produce: array(3) { [0] => array(11) { ['notnull'] => bool(true) ['nativetype'] => string(3) "int" ['length'] => int(4) ['unsigned'] => int(1) ['default'] => string(0) "" ['autoincrement'] => bool(true) ['type'] => string(3) "int" ['mdb2type'] => string(7) "integer" ['name'] => string(2) "id" ['table'] => string(9) "sometable" ['flags'] => string(29) "primary_key not_null unsigned" } [1] => array(10) { ['notnull'] => bool(false) ['nativetype'] => string(7) "varchar" ['length'] => string(2) "12" ['fixed'] => bool(false) ['default'] => NULL ['type'] => string(7) "varchar" ['mdb2type'] => string(4) "text" ['name'] => string(8) "somename" ['table'] => string(9) "sometable" ['flags'] => string(0) "" } [2] => array(8) { ['notnull'] => bool(false) ['nativetype'] => string(4) "date" ['default'] => NULL ['type'] => string(4) "date" ['mdb2type'] => string(4) "date" ['name'] => string(8) "somedate" ['table'] => string(9) "sometable" ['flags'] => string(0) "" } } ?> ]]> </programlisting> </example> In the subsequent examples, we will only include the first table field definition, as it effectively illustrates the differences in modes. Now, let's change the mode to <parameter>MDB2_TABLEINFO_ORDER</parameter>. In addition to the information found in the default output, a notation of the number of columns is provided by the <parameter>num_fields</parameter> element while the order element provides an array with the column names as the keys and their location index number (corresponding to the keys in the the default output) as the values. <example> <title><function>tableinfo</function> usage 2</title> <programlisting role="php"> <![CDATA[ <?php $tableInfo = $mdb2->tableInfo($table, MDB2_TABLEINFO_ORDER); var_dump($tableInfo); array(5) { ['num_fields'] => int(3) [0] => array(11) { ['notnull'] => bool(true) ['nativetype'] => string(3) "int" ['length'] => int(4) ['unsigned'] => int(1) ['default'] => string(0) "" ['autoincrement'] => bool(true) ['type'] => string(3) "int" ['mdb2type'] => string(7) "integer" ['name'] => string(2) "id" ['table'] => string(9) "sometable" ['flags'] => string(29) "primary_key not_null unsigned" } [1] => array(10) { ['notnull'] => bool(false) ['nativetype'] => string(7) "varchar" ['length'] => string(2) "12" ['fixed'] => bool(false) ['default'] => NULL ['type'] => string(7) "varchar" ['mdb2type'] => string(4) "text" ['name'] => string(8) "somename" ['table'] => string(9) "sometable" ['flags'] => string(0) "" } [2] => array(8) { ['notnull'] => bool(false) ['nativetype'] => string(4) "date" ['default'] => NULL ['type'] => string(4) "date" ['mdb2type'] => string(4) "date" ['name'] => string(8) "somedate" ['table'] => string(9) "sometable" ['flags'] => string(0) "" } ['order'] => array(3) { ['id'] => int(0) ['somename'] => int(1) ['somedate'] => int(2) } } ?> ]]> </programlisting> </example> Changing the mode to <parameter>MDB2_TABLEINFO_ORDERTABLE<parameter> will give us some additional information by adding additional dimensions to the array in which the table names are keys, and the field names are sub keys. This type of query can be useful when querying complex joins, where some field names may be the same. </para> <para> NOTE: The <parameter>flags</parameter> 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: <itemizedlist> <listitem><parameter>primary_key</parameter></listitem> <listitem><parameter>unique_key</parameter></listitem> <listitem><parameter>multiple_key</parameter></listitem> <listitem><parameter>not_null</parameter></listitem> <listitem><parameter>unsigned</parameter></listitem> </itemizedlist> Most DBMS's only provide the <parameter>table</parameter> and <parameter>flags</parameter> elements if the result is a table name. If the <parameter>portability</parameter> option is set to <parameter>MDB2_PORTABILITY_FIX_CASE</parameter>, the names of tables and fields will be lower- or upper-cased. If the case is set to <parameter>CASE_UPPER</parameter> all tables and fields will be uppercased, as is seen in Oracle and Firebird/Interbase, while <parameter>CASE_LOWER</parameter> will lower case all fields and tables – this is the default setting. </para> </refsect2> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
« previous php.pear.doc (#9478) next »