< CREATE TABLE and KEYS   (Previous) Table of Contents (Next)   Include File for Building Schema >

Script for Building Schema

<?php

/**
 * User interface for the Schema Builder
 *
 * Will work from the command line or web browser.  To use it from a command
 * line, do this: <kbd>php buildSchema.php <action></kbd>
 *
 * Where <action> is one of the following:
 *   + <kbd>b</kbd> for build all tables
 *   + <kbd>d</kbd> for drop all tables
 *   + <kbd>bd</kbd> for build and drop all tables
 *
 * NOTE:  Before running this script, you need to do two things:
 *   + edit the DSN's in <kbd>./dsns.inc</kbd>
 *   + edit the <var>$build_dbmss</var> array, below, uncommenting
 *     the DBMS's you want to build schemas in.
 *
 * @package    Portability
 * @author     Daniel Convissor <danielc@analysisandsolutions.com>
 * @copyright  2002-2005 The Analysis and Solutions Company 
 * @license    http://www.analysisandsolutions.com/software/license.txt  Simple Public License
 * @link       http://www.analysisandsolutions.com/presentations/portability/
 * @see        buildSchema.inc, viewSchema.php
 */

$build_dbmss = array(
//#    'access',  // doesn't allow DEFAULT's in column definitions
//    'db2',
//    'fbsql',
//    'firebird',
//    'mysql',
//    'mysqli',
//    'oci8',
//    'pgsql',
//    'sqlite',
//    'sybase',  // the sybase and mssql extensions
//    'mssql',   // can't be loaded at the same time
);


if (php_sapi_name() != 'cli') {

?>

<head><title>buildSchema</title></head><pre>
<a href="buildSchema.php?action=b">Build Only</a>
<a href="buildSchema.php?action=bd">Build and Drop</a>
<a href="buildSchema.php?action=d">Drop Only</a>

<a href="viewSchema.php">View Schema</a>

<?php

}


if (empty($build_dbmss)) {
    echo 'You need to uncomment at least one item in the'
         . ' $build_dbmss array inside this script.' . "\n";
    exit;
}

if (!empty($_GET['action'])) {
    $action = $_GET['action'];
} elseif (!empty($_SERVER['argv'][1])) {
    $action = $_SERVER['argv'][1];
} else {
    $action = '';
}

if (!preg_match('/^b|d|bd$/', $action)) {
    echo "SYNTAX:  php buildSchema.php action\n";
    echo "The action argument must be one of the following:\n";
    echo "b  = Build only\n";
    echo "d  = Drop only\n";
    echo "bd = Build and Drop\n";
    exit;
}

require_once './dsns.inc';
require_once './portability.inc';
require_once './buildSchema.inc';

require_once 'DB.php';
$options = array(
    'debug'       => 2,
    'portability' => DB_PORTABILITY_ALL,
);

foreach ($build_dbmss as $dbms) {
    echo "============  $dbms  ============\n";
    if (!array_key_exists($dbms, $dsns)) {
        echo $dbms . ' does not match an element in $dsns.' . "\n";
        echo ' Edit the $build_dbmss or $dsns arrays to fix the problem.' . "\n";
        continue;
    }

    $db =& DB::connect($dsns[$dbms], $options);
    if (DB::isError($db)) {
        die($db->getUserInfo());
    }
    $db->setErrorHandling(PEAR_ERROR_RETURN);

    $p =& new Portability($db);

    if ($action != 'd') {
        buildSchema($p, $db);
    }

    if ($action == 'd' || $action == 'bd') {
        dropSchema($p, $db);
    }
}

?>

DONE