Source for file adodb-xmlschema.inc.php
Documentation is available at adodb-xmlschema.inc.php
// Copyright (c) 2004 ars Cognita Inc., all rights reserved
/* ******************************************************************************
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence.
*******************************************************************************/
* xmlschema is a class that allows the user to quickly and easily
* build a database on any ADOdb-supported platform using a simple
* Last Editor: $Author: markwest $
* @author Richard Tango-Lowy & Dan Cech
* @version $Revision: 19377 $
* @tutorial getting_started.pkg
while ($s = fread($f,100000)) $t .= $s;
define( 'XMLS_DEBUG', FALSE );
define( 'XMLS_PREFIX', '%%P' );
* Maximum length allowed for object prefix
if( !defined( 'XMLS_PREFIX_MAXLEN' ) ) {
define( 'XMLS_PREFIX_MAXLEN', 10 );
* Execute SQL inline as it is generated
if( !defined( 'XMLS_EXECUTE_INLINE' ) ) {
define( 'XMLS_EXECUTE_INLINE', FALSE );
* Continue SQL Execution if an error occurs?
if( !defined( 'XMLS_CONTINUE_ON_ERROR' ) ) {
define( 'XMLS_CONTINUE_ON_ERROR', FALSE );
if( !defined( 'XMLS_SCHEMA_VERSION' ) ) {
define( 'XMLS_SCHEMA_VERSION', '0.2' );
* Default Schema Version. Used for Schemas without an explicit version set.
if( !defined( 'XMLS_DEFAULT_SCHEMA_VERSION' ) ) {
define( 'XMLS_DEFAULT_SCHEMA_VERSION', '0.1' );
* Default Schema Version. Used for Schemas without an explicit version set.
if( !defined( 'XMLS_DEFAULT_UPGRADE_METHOD' ) ) {
define( 'XMLS_DEFAULT_UPGRADE_METHOD', 'ALTER' );
* Include the main ADODB library
require ( 'adodb.inc.php' );
require ( 'adodb-datadict.inc.php' );
* Abstract DB Object. This class provides basic methods for database objects, such
* var string current element
function dbObject( &$parent, $attributes = NULL ) {
$this->parent = & $parent;
* XML Callback to process start elements
function _tag_open( &$parser, $tag, $attributes ) {
* XML Callback to process CDATA elements
function _tag_cdata( &$parser, $cdata ) {
* XML Callback to process end elements
function _tag_close( &$parser, $tag ) {
* Checks whether the specified RDBMS is supported by the current
* database object or its ranking ancestor.
* @param string $platform RDBMS platform name (from ADODB platform list).
* @return boolean TRUE if RDBMS is supported; otherwise returns FALSE.
function supportedPlatform( $platform = NULL ) {
return is_object( $this->parent ) ? $this->parent->supportedPlatform( $platform ) : TRUE;
* Returns the prefix set by the ranking ancestor of the database object.
* @param string $name Prefix string.
function prefix( $name = '' ) {
return is_object( $this->parent ) ? $this->parent->prefix( $name ) : $name;
* Extracts a field ID from the specified field.
* @param string $field Field.
* @return string Field ID.
function FieldID( $field ) {
* Creates a table object in ADOdb's datadict format
* This class stores information about a database table. As charactaristics
* of the table are loaded from the external source, methods and properties
* of this class are used to build up the table description in ADOdb's
class dbTable extends dbObject {
* @var array Field specifier: Meta-information about each field
* @var array List of table indexes.
* @var array Table options: Table-level options
* @var string Field index: Keeps track of which field is currently being processed
* @var boolean Mark table for destruction
* @var boolean Mark field for destruction (not yet implemented)
var $drop_field = array();
* Iniitializes a new table object.
* @param string $prefix DB Object prefix
* @param array $attributes Array of table attributes.
function dbTable( &$parent, $attributes = NULL ) {
$this->parent = & $parent;
$this->name = $this->prefix($attributes['NAME']);
* XML Callback to process start elements. Elements currently
* processed are: INDEX, DROP, FIELD, KEY, NOTNULL, AUTOINCREMENT & DEFAULT.
function _tag_open( &$parser, $tag, $attributes ) {
switch( $this->currentElement ) {
if( !isset ( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
if( !isset ( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
$fieldName = $attributes['NAME'];
$fieldType = $attributes['TYPE'];
$fieldSize = isset ( $attributes['SIZE'] ) ? $attributes['SIZE'] : NULL;
$fieldOpts = isset ( $attributes['OPTS'] ) ? $attributes['OPTS'] : NULL;
$this->addField( $fieldName, $fieldType, $fieldSize, $fieldOpts );
$this->addFieldOpt( $this->current_field, $this->currentElement );
// Add a field option to the table object
// Work around ADOdb datadict issue that misinterprets empty strings.
if( $attributes['VALUE'] == '' ) {
$attributes['VALUE'] = " '' ";
$this->addFieldOpt( $this->current_field, $this->currentElement, $attributes['VALUE'] );
// Add a field option to the table object
$this->addFieldOpt( $this->current_field, $this->currentElement );
// print_r( array( $tag, $attributes ) );
* XML Callback to process CDATA elements
function _tag_cdata( &$parser, $cdata ) {
switch( $this->currentElement ) {
if( isset ( $this->current_field ) ) {
$this->addFieldOpt( $this->current_field, $this->currentElement, $cdata );
$this->addTableOpt( $cdata );
$this->addTableOpt( $cdata );
* XML Callback to process end elements
function _tag_close( &$parser, $tag ) {
$this->currentElement = '';
$this->parent->addSQL( $this->create( $this->parent ) );
unset ($this->current_field);
* Adds an index to a table object
* @param array $attributes Index attributes
* @return object dbIndex object
function &addIndex( $attributes ) {
$this->indexes[$name] = & new dbIndex( $this, $attributes );
return $this->indexes[$name];
* Adds data to a table object
* @param array $attributes Data attributes
* @return object dbData object
function &addData( $attributes ) {
if( !isset ( $this->data ) ) {
$this->data = & new dbData( $this, $attributes );
* Adds a field to a table object
* $name is the name of the table to which the field should be added.
* $type is an ADODB datadict field type. The following field types
* are supported as of ADODB 3.40:
* - X: CLOB (character large object) or largest varchar size
* if CLOB is not supported
* - C2: Multibyte varchar
* - B: BLOB (binary large object)
* - D: Date (some databases do not support this, and we return a datetime type)
* - T: Datetime or Timestamp
* - L: Integer field suitable for storing booleans (0 or 1)
* - I: Integer (mapped to I4)
* - F: Floating point number
* - N: Numeric or decimal number
* @param string $name Name of the table to which the field will be added.
* @param string $type ADODB datadict field type.
* @param string $size Field size
* @param array $opts Field options array
* @return array Field specifier array
function addField( $name, $type, $size = NULL, $opts = NULL ) {
$field_id = $this->FieldID( $name );
// Set the field index so we know where we are
$this->current_field = $field_id;
// Set the field name (required)
$this->fields[$field_id]['NAME'] = $name;
// Set the field type (required)
$this->fields[$field_id]['TYPE'] = $type;
// Set the field size (optional)
$this->fields[$field_id]['SIZE'] = $size;
$this->fields[$field_id]['OPTS'][] = $opts;
* Adds a field option to the current field specifier
* This method adds a field option allowed by the ADOdb datadict
* and appends it to the given field.
* @param string $field Field name
* @param string $opt ADOdb field option
* @param mixed $value Field option value
* @return array Field specifier array
function addFieldOpt( $field, $opt, $value = NULL ) {
$this->fields[$this->FieldID( $field )]['OPTS'][] = $opt;
// Add the option and value
$this->fields[$this->FieldID( $field )]['OPTS'][] = array( $opt => $value );
* Adds an option to the table
* This method takes a comma-separated list of table-level options
* and appends them to the table object.
* @param string $opt Table option
function addTableOpt( $opt ) {
* Generates the SQL that will create the table in the database
* @param object $xmls adoSchema object
* @return array Array containing table creation SQL
function create( &$xmls ) {
// drop any existing indexes
if( is_array( $legacy_indexes = $xmls->dict->MetaIndexes( $this->name ) ) ) {
foreach( $legacy_indexes as $index => $index_details ) {
$sql[] = $xmls->dict->DropIndexSQL( $index, $this->name );
// remove fields to be dropped from table object
foreach( $this->drop_field as $field ) {
unset ( $this->fields[$field] );
if( is_array( $legacy_fields = $xmls->dict->MetaColumns( $this->name ) ) ) {
if( $this->drop_table ) {
$sql[] = $xmls->dict->DropTableSQL( $this->name );
// drop any existing fields not in schema
foreach( $legacy_fields as $field_id => $field ) {
if( !isset ( $this->fields[$field_id] ) ) {
$sql[] = $xmls->dict->DropColumnSQL( $this->name, '`'. $field->name. '`' );
// if table doesn't exist
if( $this->drop_table ) {
$legacy_fields = array();
// Loop through the field specifier array, building the associative array for the field options
foreach( $this->fields as $field_id => $finfo ) {
// Set an empty size if it isn't supplied
if( !isset ( $finfo['SIZE'] ) ) {
// Initialize the field array with the type and size
$fldarray[$field_id] = array(
'NAME' => $finfo['NAME'],
'TYPE' => $finfo['TYPE'],
// Loop through the options array and add the field options.
if( isset ( $finfo['OPTS'] ) ) {
foreach( $finfo['OPTS'] as $opt ) {
// Option has an argument.
$value = $opt[key( $opt )];
@$fldarray[$field_id][$key] .= $value;
// Option doesn't have arguments
$fldarray[$field_id][$opt] = $opt;
if( empty( $legacy_fields ) ) {
$sql[] = $xmls->dict->CreateTableSQL( $this->name, $fldarray, $this->opts );
logMsg( end( $sql ), 'Generated CreateTableSQL' );
// Upgrade an existing table
logMsg( "Upgrading {$this->name} using '{ $xmls->upgrade}' " );
switch( $xmls->upgrade ) {
logMsg( 'Generated ChangeTableSQL (ALTERing table)' );
$sql[] = $xmls->dict->ChangeTableSQL( $this->name, $fldarray, $this->opts );
logMsg( 'Doing upgrade REPLACE (testing)' );
$sql[] = $xmls->dict->DropTableSQL( $this->name );
$sql[] = $xmls->dict->CreateTableSQL( $this->name, $fldarray, $this->opts );
foreach( $this->indexes as $index ) {
$sql[] = $index->create( $xmls );
if( isset( $this->data ) ) {
$sql[] = $this->data->create( $xmls );
* Marks a field or table for destruction
if( isset( $this->current_field ) ) {
// Drop the current field
logMsg( "Dropping field '{ $this->current_field}' from table '{ $this->name}' " );
// $this->drop_field[$this->current_field] = $xmls->dict->DropColumnSQL( $this->name, $this->current_field );
$this->drop_field[$this->current_field] = $this->current_field;
// Drop the current table
logMsg( "Dropping table '{ $this->name}' " );
// $this->drop_table = $xmls->dict->DropTableSQL( $this->name );
$this->drop_table = TRUE;
* Creates an index object in ADOdb's datadict format
* This class stores information about a database index. As charactaristics
* of the index are loaded from the external source, methods and properties
* of this class are used to build up the index description in ADOdb's
class dbIndex extends dbObject {
* @var array Index options: Index-level options
* @var array Indexed fields: Table columns included in this index
* @var boolean Mark index for destruction
* Initializes the new dbIndex object.
* @param object $parent Parent object
* @param array $attributes Attributes
function dbIndex( &$parent, $attributes = NULL ) {
$this->parent =& $parent;
$this->name = $this->prefix ($attributes['NAME']);
* XML Callback to process start elements
* Processes XML opening tags.
* Elements currently processed are: DROP, CLUSTERED, BITMAP, UNIQUE, FULLTEXT & HASH.
function _tag_open( &$parser, $tag, $attributes ) {
switch( $this->currentElement ) {
$this->addIndexOpt( $this->currentElement );
// print_r( array( $tag, $attributes ) );
* XML Callback to process CDATA elements
function _tag_cdata( &$parser, $cdata ) {
switch( $this->currentElement ) {
$this->addField( $cdata );
* XML Callback to process end elements
function _tag_close( &$parser, $tag ) {
$this->currentElement = '';
* Adds a field to the index
* @param string $name Field name
* @return string Field list
function addField( $name ) {
$this->columns[$this->FieldID( $name )] = $name;
* Adds options to the index
* @param string $opt Comma-separated list of index options.
* @return string Option list
function addIndexOpt( $opt ) {
// Return the options list
* Generates the SQL that will create the index in the database
* @param object $xmls adoSchema object
* @return array Array containing index creation SQL
function create( &$xmls ) {
// eliminate any columns that aren't in the table
foreach( $this->columns as $id => $col ) {
if( !isset( $this->parent->fields[$id] ) ) {
unset( $this->columns[$id] );
return $xmls->dict->CreateIndexSQL( $this->name, $this->parent->name, $this->columns, $this->opts );
* Marks an index for destruction
* Creates a data object in ADOdb's datadict format
* This class stores information about table data.
class dbData extends dbObject {
* Initializes the new dbIndex object.
* @param object $parent Parent object
* @param array $attributes Attributes
function dbData( &$parent, $attributes = NULL ) {
$this->parent =& $parent;
* XML Callback to process start elements
* Processes XML opening tags.
* Elements currently processed are: DROP, CLUSTERED, BITMAP, UNIQUE, FULLTEXT & HASH.
function _tag_open( &$parser, $tag, $attributes ) {
switch( $this->currentElement ) {
$this->row = count( $this->data );
$this->data[$this->row] = array();
$this->addField($attributes);
// print_r( array( $tag, $attributes ) );
* XML Callback to process CDATA elements
function _tag_cdata( &$parser, $cdata ) {
switch( $this->currentElement ) {
$this->addData( $cdata );
* XML Callback to process end elements
function _tag_close( &$parser, $tag ) {
$this->currentElement = '';
* Adds a field to the index
* @param string $name Field name
* @return string Field list
function addField( $attributes ) {
if( isset( $attributes['NAME'] ) ) {
$name = $attributes['NAME'];
$name = count($this->data[$this->row]);
// Set the field index so we know where we are
$this->current_field = $this->FieldID( $name );
* Adds options to the index
* @param string $opt Comma-separated list of index options.
* @return string Option list
function addData( $cdata ) {
if( !isset( $this->data[$this->row] ) ) {
$this->data[$this->row] = array();
if( !isset( $this->data[$this->row][$this->current_field] ) ) {
$this->data[$this->row][$this->current_field] = '';
$this->data[$this->row][$this->current_field] .= $cdata;
* Generates the SQL that will create the index in the database
* @param object $xmls adoSchema object
* @return array Array containing index creation SQL
function create( &$xmls ) {
$table = $xmls->dict->TableName($this->parent->name);
$table_field_count = count($this->parent->fields);
// eliminate any columns that aren't in the table
foreach( $this->data as $row ) {
$table_fields = $this->parent->fields;
foreach( $row as $field_id => $field_data ) {
$name = $table_fields[$field_id]['NAME'];
switch( $table_fields[$field_id]['TYPE'] ) {
$fields[$name] = $xmls->db->qstr( $field_data );
$fields[$name] = intval($field_data);
$fields[$name] = $field_data;
unset($table_fields[$field_id]);
// check that at least 1 column is specified
// check that no required columns are missing
if( count( $fields ) < $table_field_count ) {
foreach( $table_fields as $field ) {
if (isset( $field['OPTS'] ))
if( ( in_array( 'NOTNULL', $field['OPTS'] ) || in_array( 'KEY', $field['OPTS'] ) ) && !in_array( 'AUTOINCREMENT', $field['OPTS'] ) ) {
$sql[] = 'INSERT INTO '. $table .' ('. implode( ',', array_keys( $fields ) ) .') VALUES ('. implode( ',', $fields ) .')';
* Creates the SQL to execute a list of provided SQL queries
class dbQuerySet extends dbObject {
* @var array List of SQL queries
* @var string String used to build of a query line by line
* @var string Query prefix key
* @var boolean Auto prefix enable (TRUE)
var $prefixMethod = 'AUTO';
* Initializes the query set.
* @param object $parent Parent object
* @param array $attributes Attributes
function dbQuerySet( &$parent, $attributes = NULL ) {
$this->parent =& $parent;
// Overrides the manual prefix key
if( isset( $attributes['KEY'] ) ) {
$this->prefixKey = $attributes['KEY'];
$prefixMethod = isset( $attributes['PREFIXMETHOD'] ) ? strtoupper( trim( $attributes['PREFIXMETHOD'] ) ) : '';
// Enables or disables automatic prefix prepending
switch( $prefixMethod ) {
$this->prefixMethod = 'AUTO';
$this->prefixMethod = 'MANUAL';
$this->prefixMethod = 'NONE';
* XML Callback to process start elements. Elements currently
function _tag_open( &$parser, $tag, $attributes ) {
switch( $this->currentElement ) {
// Create a new query in a SQL queryset.
// Ignore this query set if a platform is specified and it's different than the
// current connection platform.
if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
// print_r( array( $tag, $attributes ) );
* XML Callback to process CDATA elements
function _tag_cdata( &$parser, $cdata ) {
switch( $this->currentElement ) {
// Line of queryset SQL data
* XML Callback to process end elements
function _tag_close( &$parser, $tag ) {
$this->currentElement = '';
// Add the finished query to the open query set.
$this->parent->addSQL( $this->create( $this->parent ) );
* Re-initializes the query.
* Discards the existing query.
function discardQuery() {
* Appends a line to a query that is being built line by line
* @param string $data Line of SQL data or NULL to initialize a new query
* @return string SQL query string.
if( !isset( $this->query ) OR empty( $sql ) ) {
* Adds a completed query to the query list
* @return string SQL of added query
if( !isset( $this->query ) ) {
$this->queries[] = $return = trim($this->query);
* Creates and returns the current query set
* @param object $xmls adoSchema object
* @return array Query set
function create( &$xmls ) {
foreach( $this->queries as $id => $query ) {
switch( $this->prefixMethod ) {
// Enable auto prefix replacement
// Process object prefix.
// Evaluate SQL statements to prepend prefix to objects
$query = $this->prefixQuery( '/^\s*((?is)INSERT\s+(INTO\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix );
$query = $this->prefixQuery( '/^\s*((?is)UPDATE\s+(FROM\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix );
$query = $this->prefixQuery( '/^\s*((?is)DELETE\s+(FROM\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix );
// SELECT statements aren't working yet
#$data = preg_replace( '/(?ias)(^\s*SELECT\s+.*\s+FROM)\s+(\W\s*,?\s*)+((?i)\s+WHERE.*$)/', "\1 $prefix\2 \3", $data );
// If prefixKey is set and has a value then we use it to override the default constant XMLS_PREFIX.
// If prefixKey is not set, we use the default constant XMLS_PREFIX
if( isset( $this->prefixKey ) AND( $this->prefixKey !== '' ) ) {
// Enable prefix override
$query = str_replace( $this->prefixKey, $xmls->objectPrefix, $query );
// Use default replacement
$query = str_replace( <a href="../axmls/_includes---classes---adodb---adodb-xmlschema.inc.php.html#defineXMLS_PREFIX">XMLS_PREFIX</a> , $xmls->objectPrefix, $query );
$this->queries[$id] = trim( $query );
// Return the query set array
* Rebuilds the query with the prefix attached to any objects
* @param string $regex Regex used to add prefix
* @param string $query SQL query string
* @param string $prefix Prefix to be appended to tables, indices, etc.
* @return string Prefixed SQL query string.
function prefixQuery( $regex, $query, $prefix = NULL ) {
if( !isset( $prefix ) ) {
$objectList = explode( ',', $match[3] );
// $prefix = $prefix . '_';
foreach( $objectList as $object ) {
if( $prefixedList !== '' ) {
$prefixedList .= $prefix . trim( $object );
$query = $preamble . ' ' . $prefixedList . ' ' . $postamble;
* Loads and parses an XML file, creating an array of "ready-to-run" SQL statements
* This class is used to load and parse the XML file, to create an array of SQL statements
* that can be used to build a database, and to build the database using the SQL array.
* @tutorial getting_started.pkg
* @author Richard Tango-Lowy & Dan Cech
* @version $Revision: 19377 $
* @var array Array containing SQL queries to generate all objects
* @var object ADOdb connection object
* @var object ADOdb Data Dictionary
* @var string Current XML element
var $currentElement = '';
* @var string If set (to 'ALTER' or 'REPLACE'), upgrade an existing database
* @var string Optional object prefix
* @var long Original Magic Quotes Runtime value
* @var string Regular expression to find schema version
var $versionRegex = '/<schema.*?( version="([^"]*)")?.*?>/';
* @var string Current schema version
* @var int Success of last Schema execution
* @var bool Execute SQL inline as it is generated
* @var bool Continue SQL execution if errors occur
* Creates an adoSchema object
* Creating an adoSchema object is the first step in processing an XML schema.
* The only parameter is an ADOdb database connection object, which must already
* @param object $db ADOdb database connection object.
// Initialize the environment
$this->debug = $this->db->debug;
$this->sqlArray = array();
$this->schemaVersion = <a href="../axmls/_includes---classes---adodb---adodb-xmlschema.inc.php.html#defineXMLS_SCHEMA_VERSION">XMLS_SCHEMA_VERSION</a>;
$this->executeInline( <a href="../axmls/_includes---classes---adodb---adodb-xmlschema.inc.php.html#defineXMLS_EXECUTE_INLINE">XMLS_EXECUTE_INLINE</a> );
$this->continueOnError( <a href="../axmls/_includes---classes---adodb---adodb-xmlschema.inc.php.html#defineXMLS_CONTINUE_ON_ERROR">XMLS_CONTINUE_ON_ERROR</a> );
$this->setUpgradeMethod();
* Sets the method to be used for upgrading an existing database
* Use this method to specify how existing database objects should be upgraded.
* The method option can be set to ALTER, REPLACE, BEST, or NONE. ALTER attempts to
* alter each database object directly, REPLACE attempts to rebuild each object
* from scratch, BEST attempts to determine the best upgrade method for each
* object, and NONE disables upgrading.
* This method is not yet used by AXMLS, but exists for backward compatibility.
* The ALTER method is automatically assumed when the adoSchema object is
* instantiated; other upgrade methods are not currently supported.
* @param string $method Upgrade method (ALTER|REPLACE|BEST|NONE)
* @returns string Upgrade method used
// Handle the upgrade methods
$this->upgrade = $method;
$this->upgrade = $method;
$this->upgrade = 'ALTER';
// Use default if no legitimate method is passed.
$this->upgrade = <a href="../axmls/_includes---classes---adodb---adodb-xmlschema.inc.php.html#defineXMLS_DEFAULT_UPGRADE_METHOD">XMLS_DEFAULT_UPGRADE_METHOD</a>;
* Enables/disables inline SQL execution.
* Call this method to enable or disable inline execution of the schema. If the mode is set to TRUE (inline execution),
* AXMLS applies the SQL to the database immediately as each schema entity is parsed. If the mode
* is set to FALSE (post execution), AXMLS parses the entire schema and you will need to call adoSchema::ExecuteSchema()
* to apply the schema to the database.
* @param bool $mode execute
* @return bool current execution mode
* @see ParseSchema(), ExecuteSchema()
* Enables/disables SQL continue on error.
* Call this method to enable or disable continuation of SQL execution if an error occurs.
* If the mode is set to TRUE (continue), AXMLS will continue to apply SQL to the database, even if an error occurs.
* If the mode is set to FALSE (halt), AXMLS will halt execution of generated sql if an error occurs, though parsing
* of the schema will continue.
* @param bool $mode execute
* @return bool current continueOnError mode
* @see addSQL(), ExecuteSchema()
* Loads an XML schema from a file and converts it to SQL.
* Call this method to load the specified schema (see the DTD for the proper format) from
* the filesystem and generate the SQL necessary to create the database described.
* @see ParseSchemaString()
* @param string $file Name of XML schema file.
* @param bool $returnSchema Return schema rather than parsing.
* @return array Array of SQL queries, ready to execute
function ParseSchema( $filename, $returnSchema = FALSE ) {
* Loads an XML schema from a file and converts it to SQL.
* Call this method to load the specified schema from a file (see the DTD for the proper format)
* and generate the SQL necessary to create the database described by the schema.
* @param string $file Name of XML schema file.
* @param bool $returnSchema Return schema rather than parsing.
* @return array Array of SQL queries, ready to execute.
* @deprecated Replaced by adoSchema::ParseSchema() and adoSchema::ParseSchemaString()
* @see ParseSchema(), ParseSchemaString()
if( !($fp = fopen( $filename, 'r' )) ) {
// die( 'Unable to open file' );
// do version detection here
while( $data = fread( $fp, 100000 ) ) {
$xmlParser = $this->create_parser();
while( $data = fread( $fp, 4096 ) ) {
"XML error: %s at line %d",
* Converts an XML schema string to SQL.
* Call this method to parse a string containing an XML schema (see the DTD for the proper format)
* and generate the SQL necessary to create the database described by the schema.
* @param string $xmlstring XML schema string.
* @param bool $returnSchema Return schema rather than parsing.
* @return array Array of SQL queries, ready to execute.
if( !is_string( $xmlstring ) OR empty( $xmlstring ) ) {
// do version detection here
$xmlParser = $this->create_parser();
if( !xml_parse( $xmlParser, $xmlstring, TRUE ) ) {
"XML error: %s at line %d",
* Loads an XML schema from a file and converts it to uninstallation SQL.
* Call this method to load the specified schema (see the DTD for the proper format) from
* the filesystem and generate the SQL necessary to remove the database described.
* @see RemoveSchemaString()
* @param string $file Name of XML schema file.
* @param bool $returnSchema Return schema rather than parsing.
* @return array Array of SQL queries, ready to execute
* Converts an XML schema string to uninstallation SQL.
* Call this method to parse a string containing an XML schema (see the DTD for the proper format)
* and generate the SQL necessary to uninstall the database described by the schema.
* @param string $schema XML schema string.
* @param bool $returnSchema Return schema rather than parsing.
* @return array Array of SQL queries, ready to execute.
* Applies the current XML schema to the database (post execution).
* Call this method to apply the current schema (generally created by calling
* ParseSchema() or ParseSchemaString() ) to the database (creating the tables, indexes,
* and executing other SQL specified in the schema) after parsing.
* @see ParseSchema(), ParseSchemaString(), ExecuteInline()
* @param array $sqlArray Array of SQL statements that will be applied rather than
* @param boolean $continueOnErr Continue to apply the schema even if an error occurs.
* @returns integer 0 if failure, 1 if errors, 2 if successful.
function ExecuteSchema( $sqlArray = NULL, $continueOnErr = NULL ) {
if( !isset( $sqlArray ) ) {
$sqlArray = $this->sqlArray;
$this->success = $this->dict->ExecuteSQLArray( $sqlArray, $continueOnErr );
* Returns the current SQL array.
* Call this method to fetch the array of SQL queries resulting from
* ParseSchema() or ParseSchemaString().
* @param string $format Format: HTML, TEXT, or NONE (PHP array)
* @return array Array of SQL statements or FALSE if an error occurs
return $this->getSQL( $format, $sqlArray );
* Saves the current SQL array to the local filesystem as a list of SQL queries.
* Call this method to save the array of SQL queries (generally resulting from a
* parsed XML schema) to the filesystem.
* @param string $filename Path and name where the file should be saved.
* @return boolean TRUE if save is successful, else FALSE.
function SaveSQL( $filename = './schema.sql' ) {
if( !isset( $sqlArray ) ) {
$sqlArray = $this->sqlArray;
if( !isset( $sqlArray ) ) {
$fp = fopen( $filename, "w" );
foreach( $sqlArray as $key => $query ) {
fwrite( $fp, $query . ";\n" );
* @return object PHP XML parser object
function &create_parser() {
// Initialize the XML callback functions
* XML Callback to process start elements
function _tag_open( &$parser, $tag, $attributes ) {
$this->obj = new dbTable( $this, $attributes );
if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
$this->obj = new dbQuerySet( $this, $attributes );
// print_r( array( $tag, $attributes ) );
* XML Callback to process CDATA elements
function _tag_cdata( &$parser, $cdata ) {
* XML Callback to process end elements
function _tag_close( &$parser, $tag ) {
* Converts an XML schema string to the specified DTD version.
* Call this method to convert a string containing an XML schema to a different AXMLS
* DTD version. For instance, to convert a schema created for an pre-1.0 version for
* AXMLS (DTD version 0.1) to a newer version of the DTD (e.g. 0.2). If no DTD version
* parameter is specified, the schema will be converted to the current DTD version.
* If the newFile parameter is provided, the converted schema will be written to the specified
* @see ConvertSchemaFile()
* @param string $schema String containing XML schema that will be converted.
* @param string $newVersion DTD version to convert to.
* @param string $newFile File name of (converted) output file.
* @return string Converted XML schema or FALSE if an error occurs.
if( !isset ($newVersion) ) {
$newVersion = $this->schemaVersion;
if( $version == $newVersion ) {
$result = $this->TransformSchema( $schema, 'convert-' . $version . '-' . $newVersion);
// compat for pre-4.3 - jlim
* Converts an XML schema file to the specified DTD version.
* Call this method to convert the specified XML schema file to a different AXMLS
* DTD version. For instance, to convert a schema created for an pre-1.0 version for
* AXMLS (DTD version 0.1) to a newer version of the DTD (e.g. 0.2). If no DTD version
* parameter is specified, the schema will be converted to the current DTD version.
* If the newFile parameter is provided, the converted schema will be written to the specified
* @see ConvertSchemaString()
* @param string $filename Name of XML schema file that will be converted.
* @param string $newVersion DTD version to convert to.
* @param string $newFile File name of (converted) output file.
* @return string Converted XML schema or FALSE if an error occurs.
if( !isset ($newVersion) ) {
$newVersion = $this->schemaVersion;
if( $version == $newVersion ) {
// remove unicode BOM if present
if( substr( $result, 0, 3 ) == sprintf( '%c%c%c', 239, 187, 191 ) ) {
$result = substr( $result, 3 );
$result = $this->TransformSchema( $filename, 'convert-' . $version . '-' . $newVersion, 'file' );
// Fail if XSLT extension is not available
$xsl_file = dirname( __FILE__ ) . '/xsl/' . $xsl . '.xsl';
// create an XSLT processor
xslt_set_error_handler ($xh, array (&$this, 'xslt_error_handler'));
$result = xslt_process ($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);
* Processes XSLT transformation errors
* @param object $parser XML parser object
* @param integer $errno Error number
* @param integer $level Error level
* @param array $fields Error information fields
function xslt_error_handler( $parser, $errno, $level, $fields ) {
'Message Type' => ucfirst( $fields['msgtype'] ),
'Message Code' => $fields['code'],
'Message' => $fields['msg'],
'Error Number' => $errno,
switch( $fields['URI'] ) {
$msg['Input'] = $fields['URI'];
$msg['Line'] = $fields['line'];
'Message Type' => 'Error',
'Error Number' => $errno,
$error_details = $msg['Message Type'] . ' in XSLT Transformation' . "\n"
foreach( $msg as $label => $details ) {
$error_details .= '<tr><td><b>' . $label . ': </b></td><td>' . htmlentities( $details ) . '</td></tr>' . "\n";
$error_details .= '</table>';
* Returns the AXMLS Schema Version of the requested XML schema file.
* Call this method to obtain the AXMLS DTD version of the requested XML schema file.
* @see SchemaStringVersion()
* @param string $filename AXMLS schema file
* @return string Schema version number or FALSE on error
if( !($fp = fopen( $filename, 'r' )) ) {
// die( 'Unable to open file' );
while( $data = fread( $fp, 4096 ) ) {
if( preg_match( $this->versionRegex, $data, $matches ) ) {
return !empty( $matches[2] ) ? $matches[2] : <a href="../axmls/_includes---classes---adodb---adodb-xmlschema.inc.php.html#defineXMLS_DEFAULT_SCHEMA_VERSION">XMLS_DEFAULT_SCHEMA_VERSION</a>;
* Returns the AXMLS Schema Version of the provided XML schema string.
* Call this method to obtain the AXMLS DTD version of the provided XML schema string.
* @see SchemaFileVersion()
* @param string $xmlstring XML schema string
* @return string Schema version number or FALSE on error
if( !is_string( $xmlstring ) OR empty( $xmlstring ) ) {
if( preg_match( $this->versionRegex, $xmlstring, $matches ) ) {
return !empty( $matches[2] ) ? $matches[2] : <a href="../axmls/_includes---classes---adodb---adodb-xmlschema.inc.php.html#defineXMLS_DEFAULT_SCHEMA_VERSION">XMLS_DEFAULT_SCHEMA_VERSION</a>;
* Extracts an XML schema from an existing database.
* 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
* @param boolean $data Include data in schema dump
* @return string Generated XML schema
$old_mode = $this->db->SetFetchMode( <a href="../Zikula 1.0.1/_includes---classes---adodb---adodb.inc.php.html#defineADODB_FETCH_NUM">ADODB_FETCH_NUM</a> );
$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 );
foreach( $fields as $details ) {
if( $details->max_length > 0 ) {
$extra .= ' size="' . $details->max_length . '"';
if( $details->primary_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 " . implode( "\n ", $content ) . "\n ";
$schema .= '</field>' . "\n";
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";
$rs = $this->db->Execute( 'SELECT * FROM ' . $table );
$schema .= ' <data>' . "\n";
while( $row = $rs->FetchRow() ) {
foreach( $row as $key => $val ) {
$schema .= ' <row><f>' . implode( '</f><f>', $row ) . '</f></row>' . "\n";
$schema .= ' </data>' . "\n";
$schema .= ' </table>' . "\n";
$this->db->SetFetchMode( $old_mode );
* Sets a prefix for database objects
* Call this method to set a standard prefix that will be prepended to all database tables
* and indices when the schema is parsed. Calling setPrefix with no arguments clears the prefix.
* @param string $prefix Prefix that will be prepended.
* @param boolean $underscore If TRUE, automatically append an underscore character to the prefix.
* @return boolean TRUE if successful, else FALSE
function SetPrefix( $prefix = '', $underscore = TRUE ) {
logMsg( 'Cleared prefix' );
$this->objectPrefix = '';
case strlen( $prefix ) > <a href="../axmls/_includes---classes---adodb---adodb-xmlschema.inc.php.html#defineXMLS_PREFIX_MAXLEN">XMLS_PREFIX_MAXLEN</a>:
// prefix contains invalid characters
case !preg_match( '/^[a-z][a-z0-9_]+$/i', $prefix ):
logMsg( 'Invalid prefix: ' . $prefix );
if( $underscore AND substr( $prefix, -1 ) != '_' ) {
logMsg( 'Set prefix: ' . $prefix );
$this->objectPrefix = $prefix;
* Returns an object name with the current prefix prepended.
* @param string $name Name
* @return string Prefixed name
function prefix( $name = '' ) {
if( !empty( $this->objectPrefix ) ) {
// Prepend the object prefix to the table name
// prepend after quote if used
return preg_replace( '/^(`?)(.+)$/', '$1' . $this->objectPrefix . '$2', $name );
// No prefix set. Use name provided.
* Checks if element references a specific platform
* @param string $platform Requested platform
* @returns boolean TRUE if platform check succeeds
function supportedPlatform( $platform = NULL ) {
$regex = '/^(\w*\|)*' . $this->db->databaseType . '(\|\w*)*$/';
if( !isset( $platform ) OR preg_match( $regex, $platform ) ) {
logMsg( "Platform $platform is supported " );
logMsg( "Platform $platform is NOT supported " );
* Clears the array of generated SQL.
$this->sqlArray = array();
* Adds SQL into the SQL array.
* @param mixed $sql SQL to Add
* @return boolean TRUE if successful, else FALSE.
function addSQL( $sql = NULL ) {
foreach( $sql as $line ) {
if( is_string( $sql ) ) {
$this->sqlArray[] = $sql;
// if executeInline is enabled, and either no errors have occurred or continueOnError is enabled, execute SQL.
$saved = $this->db->debug;
$this->db->debug = $this->debug;
$ok = $this->db->Execute( $sql );
$this->db->debug = $saved;
ADOConnection::outp( $this->db->ErrorMsg() );
* Gets the SQL array in the specified format.
* @param string $format Format
function getSQL( $format = NULL, $sqlArray = NULL ) {
if( !is_array( $sqlArray ) ) {
$sqlArray = $this->sqlArray;
if( !is_array( $sqlArray ) ) {
switch( strtolower( $format ) ) {
return !empty( $sqlArray ) ? implode( ";\n\n", $sqlArray ) . ';' : '';
* Destroys an adoSchema object.
* Call this method to clean up after an adoSchema object that is no longer in use.
* @deprecated adoSchema now cleans up automatically.
* Message logging function
function logMsg( $msg, $title = NULL, $force = FALSE ) {
if( <a href="../axmls/_includes---classes---adodb---adodb-xmlschema.inc.php.html#defineXMLS_DEBUG">XMLS_DEBUG</a> or $force ) {
|