cvs: peardoc /en/package/database/mdb2-schema create.xml definition.xml dump.xml intro.xml update.xml usage.xml

From: Date: Wed, 27 Jun 2007 22:43:25 +0000
Subject: cvs: peardoc /en/package/database/mdb2-schema create.xml definition.xml dump.xml intro.xml update.xml usage.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-9829@lists.php.net to get a copy of this message
ifeghali Wed Jun 27 22:43:25 2007 UTC Added files: /peardoc/en/package/database/mdb2-schema create.xml definition.xml dump.xml update.xml Modified files: /peardoc/en/package/database/mdb2-schema intro.xml usage.xml Log: - new chapters - cosmetic fixes (4 spaces indentation)

http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb2-schema/intro.xml?r1=1.3&r2=1.4&diff_format=u Index: peardoc/en/package/database/mdb2-schema/intro.xml diff -u peardoc/en/package/database/mdb2-schema/intro.xml:1.3 peardoc/en/package/database/mdb2-schema/intro.xml:1.4 --- peardoc/en/package/database/mdb2-schema/intro.xml:1.3 Sun Jun 24 10:19:47 2007 +++ peardoc/en/package/database/mdb2-schema/intro.xml Wed Jun 27 22:43:25 2007 @@ -1,54 +1,54 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> -<!-- $Revision: 1.3 $ --> -<!-- $Id: intro.xml,v 1.3 2007/06/24 10:19:47 takagi Exp $ --> +<!-- $Revision: 1.4 $ --> +<!-- $Id: intro.xml,v 1.4 2007/06/27 22:43:25 ifeghali Exp $ --> <refentry id="package.database.mdb2-schema.intro"> - <refnamediv> + <refnamediv> <refname>Introduction</refname> <refpurpose>Introduction to MDB2_Schema</refpurpose> </refnamediv> <refsect1 id="package.database.mdb2-schema.intro.refsect1"> - <title>Introduction to MDB2_Schema</title> - <para> - MDB2_Schema builds upon MDB2 to provide tools to manage your database - schema using XML which is both platform- and database-independent. - </para> - <para> - The XML format is inherited from the Metabase package and was improved to - be able to represent new entities such as Data Manipulation Instructions - and Foreign Keys. - </para> - <para> - It enables users to maintain RDBMS independant - schema files in XML that can be used to create, alter and - drop database entities (also called as DDL: Data Definition - Language) and insert data (also called as DML: Data - Manipulation Language) into a database. Reverse engineering - database schemas from existing databases is also supported. It also - features the hability to parse database schemas and database data in - separated files. In this document the term &quot;schema file&quot; will be - used to designated any XML supported by MDB2_Schema, no matter its - nature. - </para> - <para> - Reading of XML files is handled by MDB2_Schema_Parser and writing to them - by MDB2_Schema_Writer. There is also the MDB2_Schema_Validate class which - is called after a XML file is parsed. It is supposed to check the logical - integrity of the schema file, for instance whether you are trying to - declare a index field that does not exist or even when you are trying - to insert a float in a date field. - </para> - <para> - Currently two parsers are available, one based on the package XML_Parser - (default) and another based on XML_Unserializer. As MDB2_Schema is still - beta both parsers have its own limitation. The default one can't parse more - than 2 nested expressions. The later doesn't respect DML instructions in - the order they are specified in the XML file. - </para> - <para> - Although MDB2_Schema API supports input from physical databases and XML - files, internally it operates with a database schema array that will be - described later in the documentation. - </para> + <title>Introduction to MDB2_Schema</title> + <para> + MDB2_Schema builds upon MDB2 to provide tools to manage your database + schema using XML which is both platform- and database-independent. + </para> + <para> + The XML format is inherited from the Metabase package and was improved to + be able to represent new entities such as Data Manipulation Instructions + and Foreign Keys. + </para> + <para> + It enables users to maintain RDBMS independant + schema files in XML that can be used to create, alter and + drop database entities (also called as DDL: Data Definition + Language) and insert data (also called as DML: Data + Manipulation Language) into a database. Reverse engineering + database schemas from existing databases is also supported. It also + features the hability to parse database schemas and database data in + separated files. However, in this document the term + &quot;schema file&quot; will be used to designate any XML supported by + MDB2_Schema, no matter its nature. + </para> + <para> + Reading of schema files is handled by MDB2_Schema_Parser and writing to + them by MDB2_Schema_Writer. There is also the MDB2_Schema_Validate class + which is called after something is parsed. It is supposed to check the + logical integrity of the schema file, for instance whether you are trying + to declare a index field that does not exist or even when you are trying + to insert a float in a date field. + </para> + <para> + Currently two parsers are available, one based on the package XML_Parser + (default) and another based on XML_Unserializer. As MDB2_Schema is still + beta, both parsers have its own limitation. The default one can't parse + more than 2 nested expressions. The later doesn't respect DML instructions + in the order they are specified in the schema file. + </para> + <para> + Although MDB2_Schema API supports input from physical databases and schema + files, internally it always operates using a database schema array that + will be described later in the documentation. + </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb2-schema/usage.xml?r1=1.3&r2=1.4&diff_format=u Index: peardoc/en/package/database/mdb2-schema/usage.xml diff -u peardoc/en/package/database/mdb2-schema/usage.xml:1.3 peardoc/en/package/database/mdb2-schema/usage.xml:1.4 --- peardoc/en/package/database/mdb2-schema/usage.xml:1.3 Sun Jun 24 10:19:47 2007 +++ peardoc/en/package/database/mdb2-schema/usage.xml Wed Jun 27 22:43:25 2007 @@ -1,83 +1,88 @@ <?xml version="1.0" encoding="ISO-8859-1" ?> -<!-- $Revision: 1.3 $ --> -<!-- $Id: usage.xml,v 1.3 2007/06/24 10:19:47 takagi Exp $ --> +<!-- $Revision: 1.4 $ --> +<!-- $Id: usage.xml,v 1.4 2007/06/27 22:43:25 ifeghali Exp $ --> <refentry id="package.database.mdb2-schema.usage"> - <refnamediv> - <refname>Installation and Usage Example</refname> - <refpurpose>Installation and example for the usage of MDB2_Schema</refpurpose> - </refnamediv> - - <refsect1 id="package.database.mdb2-schema.usage.installation"> - <title>Installation</title> - <para> - First you need MDB2 installed: - </para> - <programlisting> - <![CDATA[$ pear install --alldeps MDB2]]> - </programlisting> - - <para> - You should install a driver for each database you are working with. For - MySQL it would be: - </para> - <programlisting> - <![CDATA[$ pear install MDB2_Driver_Mysql]]> - </programlisting> - - <para> - For some hints refers to MDB2 documentation or try in a UNIX-like system: - </para> - <programlisting> - <![CDATA[$ pear remote-list | grep MDB2]]> - </programlisting> - - <para> - MDB2_Schema is a separate package, and can also be installed using the PEAR - installer: - </para> - <programlisting> - <![CDATA[$ pear install --alldeps MDB2_Schema-beta]]> - </programlisting> - - <para> - now you should be ready :) - </para> - </refsect1> - - <refsect1 id="package.database.mdb2-schema.usage.usage"> - <title>Usage Example</title> - <para> - To create an instance of the MDB2_Schema class you can use the - <methodname>factory()</methodname>, which accepts a <varname>$dsn</varname> - or an array. The factory method also accepts an existing MDB2 object. In - the example below, we will just use a <varname>$dsn</varname>. - </para> - <programlisting role="php"> - <![CDATA[ + <refnamediv> + <refname>Installation and Usage Example</refname> + <refpurpose>Installation and example for the usage of MDB2_Schema</refpurpose> + </refnamediv> + + <refsect1 id="package.database.mdb2-schema.usage.installation"> + <title>Installation</title> + <para> + First you need MDB2 installed: + </para> + <programlisting> + <![CDATA[$ pear install --alldeps MDB2]]> + </programlisting> + + <para> + You should install a driver for each database you are working with. For + MySQL it would be: + </para> + <programlisting> + <![CDATA[$ pear install MDB2_Driver_Mysql]]> + </programlisting> + + <para> + For some hints refers to MDB2 documentation or try in a UNIX-like system: + </para> + <programlisting> + <![CDATA[$ pear remote-list | grep MDB2]]> + </programlisting> + + <para> + MDB2_Schema is a separate package, and can also be installed using the + PEAR installer: + </para> + <programlisting> + <![CDATA[$ pear install --alldeps MDB2_Schema-beta]]> + </programlisting> + + <para> + now you should be ready :) + </para> + </refsect1> + + <refsect1 id="package.database.mdb2-schema.usage.usage"> + <title>Usage Example</title> + <para> + To create an instance of the MDB2_Schema class you can use the + <methodname>factory()</methodname>, which accepts a + <varname>$dsn</varname> or an array. The factory method also accepts an + existing MDB2 object. In the example below, we will just use a + <varname>$dsn</varname>. + </para> + <programlisting role="php"> + <![CDATA[ <?php -require_once 'MDB2/Schema.php'; +require_once 'MDB2/Schema.php'; -$options = array( - 'log_line_break' => '<br>', - 'idxname_format' => '%s', - 'debug' => true, - 'quote_identifier' => true, - 'force_defaults' => false, - 'portability' => false -); -$dsn = 'mysql://root:@localhost/DBname'; +$options = array( + 'log_line_break' => '<br>', + 'idxname_format' => '%s', + 'debug' => true, + 'quote_identifier' => true, + 'force_defaults' => false, + 'portability' => false +); +$dsn = 'mysql://root:@localhost/MDB2Example'; $schema =& MDB2_Schema::factory($dsn, $options); -if (PEAR::isError($schema)) { - $error = $schema->getMessage(); +if (PEAR::isError($schema)) { + $error = $schema->getMessage(); +} + +if (isset($error)) { + var_dump($error); } $schema->disconnect(); ?> - ]]> - </programlisting> - </refsect1> + ]]> + </programlisting> + </refsect1> </refentry> <!-- Keep this comment at the end of the file http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/mdb2-schema/create.xml?view=markup&rev=1.1 Index: peardoc/en/package/database/mdb2-schema/create.xml +++ peardoc/en/package/database/mdb2-schema/create.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <!-- $Id: create.xml,v 1.1 2007/06/27 22:43:25 ifeghali Exp $ --> <refentry id="package.database.mdb2-schema.create"> <refnamediv> <refname>Create Database</refname> <refpurpose>Restoring a database from MDB2 XML</refpurpose> </refnamediv> <refsect1 id="package.database.mdb2-schema.create.refsect1"> <title>Create Database</title> <para> When having a schema file, it is a breeze to create a database. Simply do the following: </para> <programlisting role="php"> <![CDATA[ <?php require_once 'MDB2/Schema.php'; $options = array( 'log_line_break' => '<br>', 'idxname_format' => '%s', 'debug' => true, 'quote_identifier' => true, 'force_defaults' => false, 'portability' => false ); $dsn = 'mysql://root:@localhost/MDB2Example'; $schema =& MDB2_Schema::factory($dsn, $options); if (PEAR::isError($schema)) { $error = $schema->getMessage(); } else { // first run with queries disabled to make sure everything is allright $disable_query = true; $definition = $schema->parseDatabaseDefinitionFile('example.xml') if (PEAR::isError($definition)) { $error = $definition->getMessage(); } else { $op = $schema->createDatabase($definition, array(), $disable_query); if (PEAR::isError($op)) { $error = $op->getMessage(); } } } if (isset($error)) { var_dump($error); } $schema->disconnect(); ?> ]]> </programlisting> </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-schema/definition.xml?view=markup&rev=1.1 Index: peardoc/en/package/database/mdb2-schema/definition.xml +++ peardoc/en/package/database/mdb2-schema/definition.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <!-- $Id: definition.xml,v 1.1 2007/06/27 22:43:25 ifeghali Exp $ --> <refentry id="package.database.mdb2-schema.definition"> <refnamediv> <refname>Get Database Definition</refname> <refpurpose>Generating the definition array of a physical database or MDB2 XML</refpurpose> </refnamediv> <refsect1 id="package.database.mdb2-schema.definition.intro"> <title>Introduction</title> <para> All the internal work of MDB2_Schema is done over array structures, which we will call &quot;definition array&quot;. Many methods, such as <methodname>createDatabase</methodname>, requires a definition array as input parameter, instead of a filename. Others take care of the conversion automatically, accepting both inputs. </para> <para> For the cases when you need a definition array, there are two avaliable methods to manually generate the definition array, one for each schema source - either a database or a schema file. Obviously you can also write it by hand, it is not intended to be done this way though. </para> </refsect1> <refsect1 id="package.database.mdb2-schema.definition.getdatabase"> <title>Get Database Definition</title> <para> You can use <methodname>getDefinitionFromDatabase()</methodname> to get the definition array from an existing database. </para> <programlisting role="php"> <![CDATA[ <?php require_once 'MDB2/Schema.php'; $options = array( 'log_line_break' => '<br>', 'idxname_format' => '%s', 'debug' => true, 'quote_identifier' => true, 'force_defaults' => false, 'portability' => false ); $dsn = 'mysql://root:@localhost/MDB2Example'; $schema =& MDB2_Schema::factory($dsn, $options); if (PEAR::isError($schema)) { $error = $schema->getMessage(); } else { // this method _attempts_ to get the defintition from the database // make sure you have tested it with your database to see if it // returns what you expect $definition = $schema->getDefinitionFromDatabase(); if (PEAR::isError($definition)) { $error = $definition->getMessage(); } } if (isset($error)) { var_dump($error); } $schema->disconnect(); ?> ]]> </programlisting> <para> Though you have to use the method with caution, if you use the method on a database created by hand. Some of the fields might be slightly different, but once you create your database using MDB2_Schema it is reliable and will return the same <varname>$definition</varname> every time. </para> </refsect1> <refsect1 id="package.database.mdb2-schema.definition.parsefile"> <title>Parse Database Definition</title> <para> You can use <methodname>parseDatabaseDefinitionFile()</methodname> to get the definition array from a schema file. </para> <programlisting role="php"> <![CDATA[ <?php require_once 'MDB2/Schema.php'; $options = array( 'log_line_break' => '<br>', 'idxname_format' => '%s', 'debug' => true, 'quote_identifier' => true, 'force_defaults' => false, 'portability' => false ); $dsn = 'mysql://root:@localhost/MDB2Example'; $schema =& MDB2_Schema::factory($dsn, $options); if (PEAR::isError($schema)) { $error = $schema->getMessage(); } else { $definition = $schema->parseDatabaseDefinitionFile('schema.xml'); if (PEAR::isError($definition)) { $error = $definition->getMessage(); } } if (isset($error)) { var_dump($error); } $schema->disconnect(); ?> ]]> </programlisting> <para> Although the method accepts more parameters, only the first one is required. </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-schema/dump.xml?view=markup&rev=1.1 Index: peardoc/en/package/database/mdb2-schema/dump.xml +++ peardoc/en/package/database/mdb2-schema/dump.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <!-- $Id: dump.xml,v 1.1 2007/06/27 22:43:25 ifeghali Exp $ --> <refentry id="package.database.mdb2-schema.dump"> <refnamediv> <refname>Dump Database</refname> <refpurpose>Dumping a database to MDB2 XML</refpurpose> </refnamediv> <refsect1 id="package.database.mdb2-schema.dump.refsect1"> <title>Dump Database</title> <para> You can use <methodname>dumpDatabase()</methodname> to copy your database to a schema file. <methodname>dumpDatabase()</methodname> accepts a database definition array, for instance: </para> <programlisting role="php"> <![CDATA[ <?php require_once 'MDB2/Schema.php'; $options = array( 'log_line_break' => '<br>', 'idxname_format' => '%s', 'debug' => true, 'quote_identifier' => true, 'force_defaults' => false, 'portability' => false ); $dsn = 'mysql://root:@localhost/MDB2Example'; $schema =& MDB2_Schema::factory($dsn, $options); if (PEAR::isError($schema)) { $error = $schema->getMessage(); } else { $dump_options = array( 'output_mode' => 'file', 'output' => 'schema.xml', 'end_of_line' => '\n' ); $definition = $schema->getDefinitionFromDatabase(); if (PEAR::isError($definition)) { $error = $definition->getMessage(); } else { $op = $schema->dumpDatabase($definition, $dump_options, MDB2_SCHEMA_DUMP_ALL); if (PEAR::isError($op)) { $error = $op->getMessage(); } } } if (isset($error)) { var_dump($error); } $schema->disconnect(); ?> ]]> </programlisting> <para> The first parameter is just the database definition array. The second parameter is the options where we choose to output to a file. The third option tells <methodname>dumpDatabase()</methodname> what to be dumped - either the structure, the data in the tables, or both. This is defined using the constants <constant>MDB2_SCHEMA_DUMP_STRUCTURE</constant>, <constant>MDB2_SCHEMA_DUMP_CONTENT</constant> and <constant>MDB2_SCHEMA_DUMP_ALL</constant>. </para> <para> Some databases don't accept a text field with a default value. Given that, notice that <varname>$options['force_defaults']</varname> has to be set to <symbol>false</symbol> when you want to create a field with the type <type>text</type>, as it is <symbol>true</symbol> by default. </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-schema/update.xml?view=markup&rev=1.1 Index: peardoc/en/package/database/mdb2-schema/update.xml +++ peardoc/en/package/database/mdb2-schema/update.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- $Revision: 1.1 $ --> <!-- $Id: update.xml,v 1.1 2007/06/27 22:43:25 ifeghali Exp $ --> <refentry id="package.database.mdb2-schema.update"> <refnamediv> <refname>Update Dabase</refname> <refpurpose>Updating a database against a new schema</refpurpose> </refnamediv> <refsect1 id="package.database.mdb2-schema.update.refsect1"> <title>Update Database</title> <para> Having MDB2_Schema to update your database, when its schema changes is also really easy. You can use the <methodname>getDefinitionFromDatabase()</methodname> method to determine the current database schema, and then just use <methodname>updateDatabase()</methodname> to do the actual update. However, you have to make sure, that <methodname>getDefinitionFromDatabase()</methodname> returns what you expect before you rely on it. See the respective chapter for more info. </para> <programlisting role="php"> <![CDATA[ <?php require_once 'MDB2/Schema.php'; $options = array( 'log_line_break' => '<br>', 'idxname_format' => '%s', 'debug' => true, 'quote_identifier' => true, 'force_defaults' => false, 'portability' => false ); $dsn = 'mysql://root:@localhost/MDB2Example'; $schema =& MDB2_Schema::factory($dsn, $options); if (PEAR::isError($schema)) { $error = $schema->getMessage(); } else { // first run with queries disabled to make sure everything is allright $disable_query = true; $previous_schema = $this->getDefinitionFromDatabase(); $op = $schema->updateDatabase('schema.xml', $previous_schema, array(), $disable_query); if (PEAR::isError($op)) { $error = $op->getMessage(); } } if (isset($error)) { var_dump($error); } $schema->disconnect(); ?> ]]> </programlisting> <para> The method accepts both, a filename or a definition array, as the first two parameters. Note how we mixed them in the example above. You may want to backup the current schema using <methodname>dumpDatabase()</methodname> for the case something goes wrong. </para> <para> When updating database schemas we can run into data persistence issues. This can be addressed with data manipulation ability, that will be documented later in this manual. </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 -->
« previous php.pear.doc (#9829) next »