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