Source for file Writer.php
Documentation is available at Writer.php
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
// | API as well as database abstraction for PHP applications. |
// | This LICENSE is in the BSD license style. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution. |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
// | Lukas Smith nor the names of his contributors may be used to endorse |
// | or promote products derived from this software without specific prior|
// | written permission. |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
// $Id: Writer.php,v 1.27 2006/04/11 08:21:17 lsmith Exp $
* Writes an XML schema file
* @author Lukas Smith <smith@pooteeweet.org>
* This method is used to communicate an error and invoke error
* callbacks etc. Basically a wrapper for PEAR::raiseError
* without the message string.
* @param int|PEAR_Error integer error code or and PEAR_Error instance
* @param int error mode, see PEAR_Error docs
* error level (E_USER_NOTICE etc). If error mode is
* PEAR_ERROR_CALLBACK, this is the callback function,
* either as a function name, or as an array of an
* object and method name. For other error modes this
* @param string Extra debug information. Defaults to the last
* query and native error code.
* @return object a PEAR error object
function &raiseError($code = null , $mode = null , $options = null , $userinfo = null )
// {{{ _escapeSpecialChars()
* add escapecharacters to all special characters in a string
* @param string string that should be escaped
* @return string escaped string
for ($char = 0 , $count = strlen($string); $char < $count; $char++ ) {
switch ($string[$char]) {
$code = ord($string[$char]);
if ($code < 32 || $code > 127 ) {
$escaped .= $string[$char];
* dump the structure of a sequence
* @param string boolean value or variable definition
* @return string with xml boolea definition
function _dumpBoolean ($boolean)
if ($boolean !== 'true' || $boolean !== 'false'
|| preg_match('/<variable>.*</variable>/', $boolean)
return $boolean ? 'true' : 'false';
* dump the structure of a sequence
* @param string sequence name
* @param string end of line characters
* @return mixed string xml sequence definition on success, or a error object
function dumpSequence($sequence_definition, $sequence_name, $eol, $dump = MDB2_SCHEMA_DUMP_ALL )
$buffer = " $eol <sequence>$eol <name>$sequence_name</name>$eol";
$start = $sequence_definition['start'];
$buffer.= " <start>$start</start>$eol";
$buffer.= " <table>". $sequence_definition['on']['table'];
$buffer.= " </table>$eol <field>". $sequence_definition['on']['field'];
$buffer.= " </field>$eol </on>$eol";
$buffer.= " </sequence>$eol";
* Dump a previously parsed database structure in the Metabase schema
* XML based format suitable for the Metabase parser. This function
* may optionally dump the database definition with initialization
* commands that specify the data that is currently present in the tables.
* @param array associative array that takes pairs of tag
* names and values that define dump options.
* 'output_mode' => String
* 'file' : dump into a file
* default: dump using a function
* depending on the 'Output_Mode'
* 'end_of_line' => String
* end of line delimiter that should be used
* @param integer determines what data to dump
* MDB2_SCHEMA_DUMP_ALL : the entire db
* MDB2_SCHEMA_DUMP_STRUCTURE : only the structure of the db
* MDB2_SCHEMA_DUMP_CONTENT : only the content of the db
* @return mixed MDB2_OK on success, or a error object
function dumpDatabase($database_definition, $arguments, $dump = MDB2_SCHEMA_DUMP_ALL )
if (array_key_exists('output_mode', $arguments) && $arguments['output_mode'] == 'file') {
$fp = fopen($arguments['output'], 'w');
$output = $arguments['output'];
'no valid output function specified');
'no output method specified');
$eol = array_key_exists('end_of_line', $arguments) ? $arguments['end_of_line'] : "\n";
&& is_array($database_definition['sequences'])
foreach ($database_definition['sequences'] as $sequence_name => $sequence) {
$sequences[$table][] = $sequence_name;
$buffer = '<?xml version="1.0" encoding="ISO-8859-1" ?>'. $eol;
$buffer.= " <database>$eol$eol <name>". $database_definition['name']. "</name>";
$buffer.= " $eol <create>". $this->_dumpBoolean ($database_definition['create']). "</create>";
$buffer.= " $eol <overwrite>". $this->_dumpBoolean ($database_definition['overwrite'])." </overwrite>$eol";
foreach ($database_definition['tables'] as $table_name => $table) {
$buffer = " $eol <table>$eol$eol <name>$table_name</name>$eol";
$buffer.= " $eol <declaration>$eol";
foreach ($table['fields'] as $field_name => $field) {
'it was not specified the type of the field "'.
$field_name. '" of the table "'. $table_name);
'type "'. $field['type']. '" is not yet supported');
$buffer.= " $eol <field>$eol <name>$field_name</name>$eol <type>";
$buffer.= $field['type']." </type>$eol";
$buffer.= " <unsigned>". $this->_dumpBoolean ($field['unsigned'])." </unsigned>$eol";
$buffer.= ' <length>'. $field['length']." </length>$eol";
$buffer.= " <notnull>". $this->_dumpBoolean ($field['notnull'])." </notnull>$eol";
$buffer.= " <notnull>false</notnull>$eol";
$buffer.= " <autoincrement>" . $field['autoincrement'] ." </autoincrement>$eol";
$buffer.= " </field>$eol";
foreach ($table['indexes'] as $index_name => $index) {
$buffer.= " $eol <index>$eol <name>$index_name</name>$eol";
$buffer.= " <unique>". $this->_dumpBoolean ($index['unique'])." </unique>$eol";
$buffer.= " <primary>". $this->_dumpBoolean ($index['primary'])." </primary>$eol";
foreach ($index['fields'] as $field_name => $field) {
$buffer.= " <field>$eol <name>$field_name</name>$eol";
$buffer.= ' <sorting>'. $field['sorting']." </sorting>$eol";
$buffer.= " </field>$eol";
$buffer.= " </index>$eol";
$buffer.= " $eol </declaration>$eol";
&& !empty ($table['initialization'])
$buffer = " $eol <initialization>$eol";
foreach ($table['initialization'] as $instruction) {
switch ($instruction['type']) {
$buffer.= " $eol <insert>$eol";
foreach ($instruction['fields'] as $field_name => $field) {
$buffer.= " $eol <field>$eol <name>$field_name</name>$eol <value>";
$buffer.= " $eol </insert>$eol";
$buffer.= " $eol </initialization>$eol";
$buffer.= " $eol </table>$eol";
if (isset ($sequences[$table_name])) {
foreach ($sequences[$table_name] as $sequence) {
$database_definition['sequences'][$sequence],
if (PEAR ::isError ($result)) {
if (isset ($sequences[''])) {
foreach ($sequences[''] as $sequence) {
$database_definition['sequences'][$sequence],
if (PEAR ::isError ($result)) {
$buffer = " $eol</database>$eol";
Documentation generated on Mon, 11 Mar 2019 14:39:19 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|