adoSchema::ExtractSchema PHP Method

ExtractSchema() public method

Call this method to create an XML schema string from an existing database. If the data parameter is set to TRUE, AXMLS will include the data from the database in the schema.
public ExtractSchema ( boolean $data = FALSE ) : string
$data boolean Include data in schema dump
return string Generated XML schema
    function ExtractSchema($data = FALSE)
    {
        $old_mode = $this->db->SetFetchMode(ADODB_FETCH_NUM);
        $schema = '<?xml version="1.0"?>' . "\n" . '<schema version="' . $this->schemaVersion . '">' . "\n";
        if (is_array($tables = $this->db->MetaTables('TABLES'))) {
            foreach ($tables as $table) {
                $schema .= '	<table name="' . $table . '">' . "\n";
                // grab details from database
                $rs = $this->db->Execute('SELECT * FROM ' . $table . ' WHERE 1=1');
                $fields = $this->db->MetaColumns($table);
                $indexes = $this->db->MetaIndexes($table);
                if (is_array($fields)) {
                    foreach ($fields as $details) {
                        $extra = '';
                        $content = array();
                        if ($details->max_length > 0) {
                            $extra .= ' size="' . $details->max_length . '"';
                        }
                        if ($details->primary_key) {
                            $content[] = '<KEY/>';
                        } elseif ($details->not_null) {
                            $content[] = '<NOTNULL/>';
                        }
                        if ($details->has_default) {
                            $content[] = '<DEFAULT value="' . $details->default_value . '"/>';
                        }
                        if ($details->auto_increment) {
                            $content[] = '<AUTOINCREMENT/>';
                        }
                        // this stops the creation of 'R' columns,
                        // AUTOINCREMENT is used to create auto columns
                        $details->primary_key = 0;
                        $type = $rs->MetaType($details);
                        $schema .= '		<field name="' . $details->name . '" type="' . $type . '"' . $extra . '>';
                        if (!empty($content)) {
                            $schema .= "\n\t\t\t" . implode("\n\t\t\t", $content) . "\n\t\t";
                        }
                        $schema .= '</field>' . "\n";
                    }
                }
                if (is_array($indexes)) {
                    foreach ($indexes as $index => $details) {
                        $schema .= '		<index name="' . $index . '">' . "\n";
                        if ($details['unique']) {
                            $schema .= '			<UNIQUE/>' . "\n";
                        }
                        foreach ($details['columns'] as $column) {
                            $schema .= '			<col>' . $column . '</col>' . "\n";
                        }
                        $schema .= '		</index>' . "\n";
                    }
                }
                if ($data) {
                    $rs = $this->db->Execute('SELECT * FROM ' . $table);
                    if (is_object($rs)) {
                        $schema .= '		<data>' . "\n";
                        while ($row = $rs->FetchRow()) {
                            foreach ($row as $key => $val) {
                                $row[$key] = htmlentities($val);
                            }
                            $schema .= '			<row><f>' . implode('</f><f>', $row) . '</f></row>' . "\n";
                        }
                        $schema .= '		</data>' . "\n";
                    }
                }
                $schema .= '	</table>' . "\n";
            }
        }
        $this->db->SetFetchMode($old_mode);
        $schema .= '</schema>';
        return $schema;
    }

Usage Example

    if ($dbc) {
        $existing_db = $db->SelectDB($dbname);
    }
} else {
    $dbc = false;
}
// Quick hack to ensure MySQL behaves itself (#2323)
$db->Execute("SET sql_mode := ''");
$current_version = $dp_version_major . '.' . $dp_version_minor;
$current_version .= isset($dp_version_patch) ? '.' . $dp_version_patch : '';
$current_version .= isset($dp_version_prepatch) ? '-' . $dp_version_prepatch : '';
if ($dobackup) {
    if ($dbc) {
        require_once DP_BASE_DIR . '/lib/adodb/adodb-xmlschema.inc.php';
        $schema = new adoSchema($db);
        $sql = $schema->ExtractSchema(true);
        header('Content-Disposition: attachment; filename="dPdbBackup' . date('Ymd') . date('His') . '.xml"');
        header('Content-Type: text/xml');
        echo $sql;
        exit;
    } else {
        $backupMsg = 'ERROR: No Database Connection available! - Backup not performed!';
    }
}
?>
<html>
<head>
 <title>dotProject Installer</title>
 <meta name="Description" content="dotProject Installer">
  <link rel="stylesheet" type="text/css" href="../style/default/main.css">
</head>
All Usage Examples Of adoSchema::ExtractSchema