Source for file Tool.php
Documentation is available at Tool.php
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith, Igor Feghali |
// | All rights reserved. |
// +----------------------------------------------------------------------+
// | MDB2_Schema enables users to maintain RDBMS independant schema files |
// | in XML that can be used to manipulate both data and database schemas |
// | 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, Igor Feghali 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: Christian Weiske <cweiske@php.net> |
// +----------------------------------------------------------------------+
// $Id: Tool.php,v 1.3 2008/02/06 23:13:51 ifeghali Exp $
require_once 'MDB2/Schema.php';
require_once 'MDB2/Schema/Tool/ParameterException.php';
* Command line tool to work with database schemas
* - dump a database schema to stdout
* - import schema into database
* - create a diff between two schemas
* - apply diff to database
* @author Christian Weiske <cweiske@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/MDB2_Schema/
* @param array $args Array of command line arguments
$this->{'do' . ucfirst($strAction)}($args);
} catch (MDB2_Schema_Tool_ParameterException $e) {
$this->{'doHelp' . ucfirst ($strAction)}($e->getMessage ());
}//public function __construct($args)
* Runs the tool with command line arguments
public static function run()
$args = $GLOBALS['argv'];
self ::toStdErr ($e->getMessage () . "\n");
}//public static function run()
* Reads the first parameter from the argument array and
* @param array &$args Command line parameters
* @return string Action to execute
throw new MDB2_Schema_Tool_ParameterException ('Unknown mode "' . $arg . '"');
}//protected function getAction(&$args)
* Writes the message to stderr
* @param string $msg Message to print
protected static function toStdErr($msg)
}//protected static function toStdErr($msg)
* Displays generic help to stdout
Usage: mdb2_schematool mode parameters
Works with database schemas
mode: (- and -- are optional)
h, help Show this help screen
d, dump Dump a schema to stdout
l, load Load a schema into database
i, diff Create a diff between two schemas and dump it to stdout
a, apply Apply a diff to a database
}//protected function doHelp()
* Displays the help screen for "dump" command
Usage: mdb2_schematool dump [-p] DSN
Dumps a database schema to stdout
DSN: Data source name in the form of
driver://user:password@host/database
User and password may be omitted.
Using -p reads password from stdin which is more secure than passing it in the parameter.
}//protected function doHelpDump()
* Displays the help screen for "load" command
Usage: mdb2_schematool load [-p] source [-p] destination
Loads a database schema from source to destination
(Creates the database schema at destination)
source can be a DSN or a schema file,
destination should be a DSN
DSN: Data source name in the form of
driver://user:password@host/database
User and password may be omitted.
Using -p reads password from stdin which is more secure than passing it in the parameter.
}//protected function doHelpLoad()
* Returns an array of options for MDB2_Schema constructor
* @return array Options for MDB2_Schema constructor
'log_line_break' => '<br>',
'idxname_format' => '%s',
'quote_identifier' => true ,
'force_defaults' => false ,
}//protected function getSchemaOptions()
* Checks if the passed parameter is a PEAR_Error object
* and throws an exception in that case.
* @param mixed $object Some variable to check
* @param string $location Where the error occured
if (PEAR ::isError ($object)) {
//debug_print_backtrace();
throw new Exception ('Error ' . $location
. "\n" . $object->getMessage ()
. "\n" . $object->getUserInfo ()
}//protected function throwExceptionOnError($object, $location = '')
* Loads a file or a dsn from the arguments
* @param array &$args Array of arguments to the program
* @return array Array of ('file'|'dsn', $value)
throw new MDB2_Schema_Tool_ParameterException ('File or DSN expected');
if (strpos($arg, '://') === false ) {
return array ('file', $arg);
throw new Exception ('Schema file does not exist');
//read password if necessary
$arg = self ::setPasswordIntoDsn ($arg, $password);
return array ('dsn', $arg);
}//protected function getFileOrDsn(&$args)
* Takes a DSN data source name and integrates the given
* @param string $dsn Data source name
* @param string $password Password
* @return string DSN with password
//simple try to integrate password
if (strpos($dsn, '@') === false ) {
//no @ -> no user and no password
return str_replace('://', '://:' . $password . '@', $dsn);
} else if (strpos($dsn, ':@') !== false ) {
}//protected function setPasswordIntoDsn($dsn, $password)
* Reads a password from stdin
* @param string $dsn DSN name to put into the message
* @return string Password
$stdin = fopen('php://stdin', 'r');
self ::toStdErr ('Please insert password for ' . $dsn . "\n");
while (false !== ($char = fgetc($stdin))) {
if (ord($char) == 10 || $char == "\n" || $char == "\r") {
}//protected function readPasswordFromStdin()
* Creates a database schema dump and sends it to stdout
* @param array $args Command line arguments
protected function doDump($args)
throw new MDB2_Schema_Tool_ParameterException (
'Dumping a schema file as a schema file does not make much sense'
$definition = $schema->getDefinitionFromDatabase ();
'output' => 'php://stdout',
$op = $schema->dumpDatabase (
}//protected function doDump($args)
* Loads a database schema
* @param array $args Command line arguments
protected function doLoad($args)
if ($typeDest == 'file') {
throw new MDB2_Schema_Tool_ParameterException (
'A schema can only be loaded into a database, not a file'
if ($typeSource == 'file') {
$definition = $schemaDest->parseDatabaseDefinitionFile ($dsnSource);
$where = 'loading schema file';
$definition = $schemaSource->getDefinitionFromDatabase ();
$where = 'loading definition from database';
//create destination database from definition
$op = $schemaDest->createDatabase ($definition, array (), $simulate);
}//protected function doLoad($args)
}//class MDB2_Schema_Tool
Documentation generated on Mon, 11 Mar 2019 15:16:45 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|