Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  /**
   3   * ADOdb XML Schema (v0.2).
   4   *
   5   * xmlschema is a class that allows the user to quickly and easily
   6   * build a database on any ADOdb-supported platform using a simple
   7   * XML schema.
   8   *
   9   * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
  10   *
  11   * @package ADOdb
  12   * @link https://adodb.org Project's web site and documentation
  13   * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
  14   *
  15   * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
  16   * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
  17   * any later version. This means you can use it in proprietary products.
  18   * See the LICENSE.md file distributed with this source code for details.
  19   * @license BSD-3-Clause
  20   * @license LGPL-2.1-or-later
  21   *
  22   * @copyright 2004-2005 ars Cognita Inc., all rights reserved
  23   * @copyright 2005-2013 John Lim
  24   * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
  25   * @author Richard Tango-Lowy
  26   * @author Dan Cech
  27   */
  28  
  29  function _file_get_contents($file)
  30  {
  31   	 if (function_exists('file_get_contents')) return file_get_contents($file);
  32  
  33  	 $f = fopen($file,'r');
  34  	 if (!$f) return '';
  35  	 $t = '';
  36  
  37  	 while ($s = fread($f,100000)) $t .= $s;
  38  	 fclose($f);
  39  	 return $t;
  40  }
  41  
  42  
  43  /**
  44  * Debug on or off
  45  */
  46  if( !defined( 'XMLS_DEBUG' ) ) {
  47  	 define( 'XMLS_DEBUG', FALSE );
  48  }
  49  
  50  /**
  51  * Default prefix key
  52  */
  53  if( !defined( 'XMLS_PREFIX' ) ) {
  54  	 define( 'XMLS_PREFIX', '%%P' );
  55  }
  56  
  57  /**
  58  * Maximum length allowed for object prefix
  59  */
  60  if( !defined( 'XMLS_PREFIX_MAXLEN' ) ) {
  61  	 define( 'XMLS_PREFIX_MAXLEN', 10 );
  62  }
  63  
  64  /**
  65  * Execute SQL inline as it is generated
  66  */
  67  if( !defined( 'XMLS_EXECUTE_INLINE' ) ) {
  68  	 define( 'XMLS_EXECUTE_INLINE', FALSE );
  69  }
  70  
  71  /**
  72  * Continue SQL Execution if an error occurs?
  73  */
  74  if( !defined( 'XMLS_CONTINUE_ON_ERROR' ) ) {
  75  	 define( 'XMLS_CONTINUE_ON_ERROR', FALSE );
  76  }
  77  
  78  /**
  79  * Current Schema Version
  80  */
  81  if( !defined( 'XMLS_SCHEMA_VERSION' ) ) {
  82  	 define( 'XMLS_SCHEMA_VERSION', '0.2' );
  83  }
  84  
  85  /**
  86  * Default Schema Version.  Used for Schemas without an explicit version set.
  87  */
  88  if( !defined( 'XMLS_DEFAULT_SCHEMA_VERSION' ) ) {
  89  	 define( 'XMLS_DEFAULT_SCHEMA_VERSION', '0.1' );
  90  }
  91  
  92  /**
  93  * Default Schema Version.  Used for Schemas without an explicit version set.
  94  */
  95  if( !defined( 'XMLS_DEFAULT_UPGRADE_METHOD' ) ) {
  96  	 define( 'XMLS_DEFAULT_UPGRADE_METHOD', 'ALTER' );
  97  }
  98  
  99  /**
 100  * Include the main ADODB library
 101  */
 102  if( !defined( '_ADODB_LAYER' ) ) {
 103  	 require ( 'adodb.inc.php' );
 104  	 require ( 'adodb-datadict.inc.php' );
 105  }
 106  
 107  /**
 108  * Abstract DB Object. This class provides basic methods for database objects, such
 109  * as tables and indexes.
 110  *
 111  * @package axmls
 112  * @access private
 113  */
 114  class dbObject {
 115  
 116  	 /**
 117  	 * var object Parent
 118  	 */
 119  	 var $parent;
 120  
 121  	 /**
 122  	 * var string current element
 123  	 */
 124  	 var $currentElement;
 125  
 126  	 /**
 127  	 * NOP
 128  	 */
 129  	function __construct( &$parent, $attributes = NULL ) {
 130  	 	 $this->parent = $parent;
 131  	 }
 132  
 133  	 /**
 134  	 * XML Callback to process start elements
 135  	 *
 136  	 * @access private
 137  	 */
 138  	function _tag_open( &$parser, $tag, $attributes ) {
 139  
 140  	 }
 141  
 142  	 /**
 143  	 * XML Callback to process CDATA elements
 144  	 *
 145  	 * @access private
 146  	 */
 147  	function _tag_cdata( &$parser, $cdata ) {
 148  
 149  	 }
 150  
 151  	 /**
 152  	 * XML Callback to process end elements
 153  	 *
 154  	 * @access private
 155  	 */
 156  	function _tag_close( &$parser, $tag ) {
 157  
 158  	 }
 159  
 160  	function create(&$xmls) {
 161  	 	 return array();
 162  	 }
 163  
 164  	 /**
 165  	 * Destroys the object
 166  	 */
 167  	function destroy() {
 168  	 }
 169  
 170  	 /**
 171  	 * Checks whether the specified RDBMS is supported by the current
 172  	 * database object or its ranking ancestor.
 173  	 *
 174  	 * @param string $platform RDBMS platform name (from ADODB platform list).
 175  	 * @return boolean TRUE if RDBMS is supported; otherwise returns FALSE.
 176  	 */
 177  	function supportedPlatform( $platform = NULL ) {
 178  	 	 return is_object( $this->parent ) ? $this->parent->supportedPlatform( $platform ) : TRUE;
 179  	 }
 180  
 181  	 /**
 182  	 * Returns the prefix set by the ranking ancestor of the database object.
 183  	 *
 184  	 * @param string $name Prefix string.
 185  	 * @return string Prefix.
 186  	 */
 187  	function prefix( $name = '' ) {
 188  	 	 return is_object( $this->parent ) ? $this->parent->prefix( $name ) : $name;
 189  	 }
 190  
 191  	 /**
 192  	 * Extracts a field ID from the specified field.
 193  	 *
 194  	 * @param string $field Field.
 195  	 * @return string Field ID.
 196  	 */
 197  	function FieldID( $field ) {
 198  	 	 return strtoupper( preg_replace( '/^`(.+)`$/', '$1', $field ) );
 199  	 }
 200  }
 201  
 202  /**
 203  * Creates a table object in ADOdb's datadict format
 204  *
 205  * This class stores information about a database table. As charactaristics
 206  * of the table are loaded from the external source, methods and properties
 207  * of this class are used to build up the table description in ADOdb's
 208  * datadict format.
 209  *
 210  * @package axmls
 211  * @access private
 212  */
 213  class dbTable extends dbObject {
 214  
 215  	 /**
 216  	 * @var string Table name
 217  	 */
 218  	 var $name;
 219  
 220  	 /**
 221  	 * @var array Field specifier: Meta-information about each field
 222  	 */
 223  	 var $fields = array();
 224  
 225  	 /**
 226  	 * @var array List of table indexes.
 227  	 */
 228  	 var $indexes = array();
 229  
 230  	 /**
 231  	 * @var array Table options: Table-level options
 232  	 */
 233  	 var $opts = array();
 234  
 235  	 /**
 236  	 * @var string Field index: Keeps track of which field is currently being processed
 237  	 */
 238  	 var $current_field;
 239  
 240  	 /**
 241  	 * @var boolean Mark table for destruction
 242  	 * @access private
 243  	 */
 244  	 var $drop_table;
 245  
 246  	 /**
 247  	 * @var boolean Mark field for destruction (not yet implemented)
 248  	 * @access private
 249  	 */
 250  	 var $drop_field = array();
 251  
 252  	 /**
 253  	 * Iniitializes a new table object.
 254  	 *
 255  	 * @param string $prefix DB Object prefix
 256  	 * @param array $attributes Array of table attributes.
 257  	 */
 258  	function __construct( &$parent, $attributes = NULL ) {
 259  	 	 $this->parent = $parent;
 260  	 	 $this->name = $this->prefix($attributes['NAME']);
 261  	 }
 262  
 263  	 /**
 264  	 * XML Callback to process start elements. Elements currently
 265  	 * processed are: INDEX, DROP, FIELD, KEY, NOTNULL, AUTOINCREMENT & DEFAULT.
 266  	 *
 267  	 * @access private
 268  	 */
 269  	function _tag_open( &$parser, $tag, $attributes ) {
 270  	 	 $this->currentElement = strtoupper( $tag );
 271  
 272  	 	 switch( $this->currentElement ) {
 273  	 	 	 case 'INDEX':
 274  	 	 	 	 if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
 275  	 	 	 	 	 $index = $this->addIndex( $attributes );
 276  	 	 	 	 	 xml_set_object( $parser,  $index );
 277  	 	 	 	 }
 278  	 	 	 	 break;
 279  	 	 	 case 'DATA':
 280  	 	 	 	 if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
 281  	 	 	 	 	 $data = $this->addData( $attributes );
 282  	 	 	 	 	 xml_set_object( $parser, $data );
 283  	 	 	 	 }
 284  	 	 	 	 break;
 285  	 	 	 case 'DROP':
 286  	 	 	 	 $this->drop();
 287  	 	 	 	 break;
 288  	 	 	 case 'FIELD':
 289  	 	 	 	 // Add a field
 290  	 	 	 	 $fieldName = $attributes['NAME'];
 291  	 	 	 	 $fieldType = $attributes['TYPE'];
 292  	 	 	 	 $fieldSize = isset( $attributes['SIZE'] ) ? $attributes['SIZE'] : NULL;
 293  	 	 	 	 $fieldOpts = isset( $attributes['OPTS'] ) ? $attributes['OPTS'] : NULL;
 294  
 295  	 	 	 	 $this->addField( $fieldName, $fieldType, $fieldSize, $fieldOpts );
 296  	 	 	 	 break;
 297  	 	 	 case 'KEY':
 298  	 	 	 case 'NOTNULL':
 299  	 	 	 case 'AUTOINCREMENT':
 300  	 	 	 	 // Add a field option
 301  	 	 	 	 $this->addFieldOpt( $this->current_field, $this->currentElement );
 302  	 	 	 	 break;
 303  	 	 	 case 'DEFAULT':
 304  	 	 	 	 // Add a field option to the table object
 305  
 306  	 	 	 	 // Work around ADOdb datadict issue that misinterprets empty strings.
 307  	 	 	 	 if( $attributes['VALUE'] == '' ) {
 308  	 	 	 	 	 $attributes['VALUE'] = " '' ";
 309  	 	 	 	 }
 310  
 311  	 	 	 	 $this->addFieldOpt( $this->current_field, $this->currentElement, $attributes['VALUE'] );
 312  	 	 	 	 break;
 313  	 	 	 case 'DEFDATE':
 314  	 	 	 case 'DEFTIMESTAMP':
 315  	 	 	 	 // Add a field option to the table object
 316  	 	 	 	 $this->addFieldOpt( $this->current_field, $this->currentElement );
 317  	 	 	 	 break;
 318  	 	 	 default:
 319  	 	 	 	 // print_r( array( $tag, $attributes ) );
 320  	 	 }
 321  	 }
 322  
 323  	 /**
 324  	 * XML Callback to process CDATA elements
 325  	 *
 326  	 * @access private
 327  	 */
 328  	function _tag_cdata( &$parser, $cdata ) {
 329  	 	 switch( $this->currentElement ) {
 330  	 	 	 // Table constraint
 331  	 	 	 case 'CONSTRAINT':
 332  	 	 	 	 if( isset( $this->current_field ) ) {
 333  	 	 	 	 	 $this->addFieldOpt( $this->current_field, $this->currentElement, $cdata );
 334  	 	 	 	 } else {
 335  	 	 	 	 	 $this->addTableOpt( $cdata );
 336  	 	 	 	 }
 337  	 	 	 	 break;
 338  	 	 	 // Table option
 339  	 	 	 case 'OPT':
 340  	 	 	 	 $this->addTableOpt( $cdata );
 341  	 	 	 	 break;
 342  	 	 	 default:
 343  
 344  	 	 }
 345  	 }
 346  
 347  	 /**
 348  	 * XML Callback to process end elements
 349  	 *
 350  	 * @access private
 351  	 */
 352  	function _tag_close( &$parser, $tag ) {
 353  	 	 $this->currentElement = '';
 354  
 355  	 	 switch( strtoupper( $tag ) ) {
 356  	 	 	 case 'TABLE':
 357  	 	 	 	 $this->parent->addSQL( $this->create( $this->parent ) );
 358  	 	 	 	 xml_set_object( $parser, $this->parent );
 359  	 	 	 	 $this->destroy();
 360  	 	 	 	 break;
 361  	 	 	 case 'FIELD':
 362  	 	 	 	 unset($this->current_field);
 363  	 	 	 	 break;
 364  
 365  	 	 }
 366  	 }
 367  
 368  	 /**
 369  	 * Adds an index to a table object
 370  	 *
 371  	 * @param array $attributes Index attributes
 372  	 * @return object dbIndex object
 373  	 */
 374  	function addIndex( $attributes ) {
 375  	 	 $name = strtoupper( $attributes['NAME'] );
 376  	 	 $this->indexes[$name] = new dbIndex( $this, $attributes );
 377  	 	 return $this->indexes[$name];
 378  	 }
 379  
 380  	 /**
 381  	 * Adds data to a table object
 382  	 *
 383  	 * @param array $attributes Data attributes
 384  	 * @return object dbData object
 385  	 */
 386  	function addData( $attributes ) {
 387  	 	 if( !isset( $this->data ) ) {
 388  	 	 	 $this->data = new dbData( $this, $attributes );
 389  	 	 }
 390  	 	 return $this->data;
 391  	 }
 392  
 393  	 /**
 394  	 * Adds a field to a table object
 395  	 *
 396  	 * $name is the name of the table to which the field should be added.
 397  	 * $type is an ADODB datadict field type. The following field types
 398  	 * are supported as of ADODB 3.40:
 399  	 * 	 - C:  varchar
 400  	 *	 - X:  CLOB (character large object) or largest varchar size
 401  	 *	    if CLOB is not supported
 402  	 *	 - C2: Multibyte varchar
 403  	 *	 - X2: Multibyte CLOB
 404  	 *	 - B:  BLOB (binary large object)
 405  	 *	 - D:  Date (some databases do not support this, and we return a datetime type)
 406  	 *	 - T:  Datetime or Timestamp
 407  	 *	 - L:  Integer field suitable for storing booleans (0 or 1)
 408  	 *	 - I:  Integer (mapped to I4)
 409  	 *	 - I1: 1-byte integer
 410  	 *	 - I2: 2-byte integer
 411  	 *	 - I4: 4-byte integer
 412  	 *	 - I8: 8-byte integer
 413  	 *	 - F:  Floating point number
 414  	 *	 - N:  Numeric or decimal number
 415  	 *
 416  	 * @param string $name Name of the table to which the field will be added.
 417  	 * @param string $type	 ADODB datadict field type.
 418  	 * @param string $size	 Field size
 419  	 * @param array $opts	 Field options array
 420  	 * @return array Field specifier array
 421  	 */
 422  	function addField( $name, $type, $size = NULL, $opts = NULL ) {
 423  	 	 $field_id = $this->FieldID( $name );
 424  
 425  	 	 // Set the field index so we know where we are
 426  	 	 $this->current_field = $field_id;
 427  
 428  	 	 // Set the field name (required)
 429  	 	 $this->fields[$field_id]['NAME'] = $name;
 430  
 431  	 	 // Set the field type (required)
 432  	 	 $this->fields[$field_id]['TYPE'] = $type;
 433  
 434  	 	 // Set the field size (optional)
 435  	 	 if( isset( $size ) ) {
 436  	 	 	 $this->fields[$field_id]['SIZE'] = $size;
 437  	 	 }
 438  
 439  	 	 // Set the field options
 440  	 	 if( isset( $opts ) ) {
 441  	 	 	 $this->fields[$field_id]['OPTS'][] = $opts;
 442  	 	 }
 443  	 }
 444  
 445  	 /**
 446  	 * Adds a field option to the current field specifier
 447  	 *
 448  	 * This method adds a field option allowed by the ADOdb datadict
 449  	 * and appends it to the given field.
 450  	 *
 451  	 * @param string $field	 Field name
 452  	 * @param string $opt ADOdb field option
 453  	 * @param mixed $value Field option value
 454  	 * @return array Field specifier array
 455  	 */
 456  	function addFieldOpt( $field, $opt, $value = NULL ) {
 457  	 	 if( !isset( $value ) ) {
 458  	 	 	 $this->fields[$this->FieldID( $field )]['OPTS'][] = $opt;
 459  	 	 // Add the option and value
 460  	 	 } else {
 461  	 	 	 $this->fields[$this->FieldID( $field )]['OPTS'][] = array( $opt => $value );
 462  	 	 }
 463  	 }
 464  
 465  	 /**
 466  	 * Adds an option to the table
 467  	 *
 468  	 * This method takes a comma-separated list of table-level options
 469  	 * and appends them to the table object.
 470  	 *
 471  	 * @param string $opt Table option
 472  	 * @return array Options
 473  	 */
 474  	function addTableOpt( $opt ) {
 475  	 	 if(isset($this->currentPlatform)) {
 476  	 	 	 $this->opts[$this->parent->db->databaseType] = $opt;
 477  	 	 }
 478  	 	 return $this->opts;
 479  	 }
 480  
 481  
 482  	 /**
 483  	 * Generates the SQL that will create the table in the database
 484  	 *
 485  	 * @param object $xmls adoSchema object
 486  	 * @return array Array containing table creation SQL
 487  	 */
 488  	function create( &$xmls ) {
 489  	 	 $sql = array();
 490  
 491  	 	 // drop any existing indexes
 492  	 	 if( is_array( $legacy_indexes = $xmls->dict->MetaIndexes( $this->name ) ) ) {
 493  	 	 	 foreach( $legacy_indexes as $index => $index_details ) {
 494  	 	 	 	 $sql[] = $xmls->dict->DropIndexSQL( $index, $this->name );
 495  	 	 	 }
 496  	 	 }
 497  
 498  	 	 // remove fields to be dropped from table object
 499  	 	 foreach( $this->drop_field as $field ) {
 500  	 	 	 unset( $this->fields[$field] );
 501  	 	 }
 502  
 503  	 	 // if table exists
 504  	 	 if( is_array( $legacy_fields = $xmls->dict->MetaColumns( $this->name ) ) ) {
 505  	 	 	 // drop table
 506  	 	 	 if( $this->drop_table ) {
 507  	 	 	 	 $sql[] = $xmls->dict->DropTableSQL( $this->name );
 508  
 509  	 	 	 	 return $sql;
 510  	 	 	 }
 511  
 512  	 	 	 // drop any existing fields not in schema
 513  	 	 	 foreach( $legacy_fields as $field_id => $field ) {
 514  	 	 	 	 if( !isset( $this->fields[$field_id] ) ) {
 515  	 	 	 	 	 $sql[] = $xmls->dict->DropColumnSQL( $this->name, '`'.$field->name.'`' );
 516  	 	 	 	 }
 517  	 	 	 }
 518  	 	 // if table doesn't exist
 519  	 	 } else {
 520  	 	 	 if( $this->drop_table ) {
 521  	 	 	 	 return $sql;
 522  	 	 	 }
 523  
 524  	 	 	 $legacy_fields = array();
 525  	 	 }
 526  
 527  	 	 // Loop through the field specifier array, building the associative array for the field options
 528  	 	 $fldarray = array();
 529  
 530  	 	 foreach( $this->fields as $field_id => $finfo ) {
 531  	 	 	 // Set an empty size if it isn't supplied
 532  	 	 	 if( !isset( $finfo['SIZE'] ) ) {
 533  	 	 	 	 $finfo['SIZE'] = '';
 534  	 	 	 }
 535  
 536  	 	 	 // Initialize the field array with the type and size
 537  	 	 	 $fldarray[$field_id] = array(
 538  	 	 	 	 'NAME' => $finfo['NAME'],
 539  	 	 	 	 'TYPE' => $finfo['TYPE'],
 540  	 	 	 	 'SIZE' => $finfo['SIZE']
 541  	 	 	 );
 542  
 543  	 	 	 // Loop through the options array and add the field options.
 544  	 	 	 if( isset( $finfo['OPTS'] ) ) {
 545  	 	 	 	 foreach( $finfo['OPTS'] as $opt ) {
 546  	 	 	 	 	 // Option has an argument.
 547  	 	 	 	 	 if( is_array( $opt ) ) {
 548  	 	 	 	 	 	 $key = key( $opt );
 549  	 	 	 	 	 	 $value = $opt[key( $opt )];
 550  	 	 	 	 	 	 @$fldarray[$field_id][$key] .= $value;
 551  	 	 	 	 	 // Option doesn't have arguments
 552  	 	 	 	 	 } else {
 553  	 	 	 	 	 	 $fldarray[$field_id][$opt] = $opt;
 554  	 	 	 	 	 }
 555  	 	 	 	 }
 556  	 	 	 }
 557  	 	 }
 558  
 559  	 	 if( empty( $legacy_fields ) ) {
 560  	 	 	 // Create the new table
 561  	 	 	 $sql[] = $xmls->dict->CreateTableSQL( $this->name, $fldarray, $this->opts );
 562  	 	 	 logMsg( end( $sql ), 'Generated CreateTableSQL' );
 563  	 	 } else {
 564  	 	 	 // Upgrade an existing table
 565  	 	 	 logMsg( "Upgrading {$this->name} using '{$xmls->upgrade}'" );
 566  	 	 	 switch( $xmls->upgrade ) {
 567  	 	 	 	 // Use ChangeTableSQL
 568  	 	 	 	 case 'ALTER':
 569  	 	 	 	 	 logMsg( 'Generated ChangeTableSQL (ALTERing table)' );
 570  	 	 	 	 	 $sql[] = $xmls->dict->ChangeTableSQL( $this->name, $fldarray, $this->opts );
 571  	 	 	 	 	 break;
 572  	 	 	 	 case 'REPLACE':
 573  	 	 	 	 	 logMsg( 'Doing upgrade REPLACE (testing)' );
 574  	 	 	 	 	 $sql[] = $xmls->dict->DropTableSQL( $this->name );
 575  	 	 	 	 	 $sql[] = $xmls->dict->CreateTableSQL( $this->name, $fldarray, $this->opts );
 576  	 	 	 	 	 break;
 577  	 	 	 	 // ignore table
 578  	 	 	 	 default:
 579  	 	 	 	 	 return array();
 580  	 	 	 }
 581  	 	 }
 582  
 583  	 	 foreach( $this->indexes as $index ) {
 584  	 	 	 $sql[] = $index->create( $xmls );
 585  	 	 }
 586  
 587  	 	 if( isset( $this->data ) ) {
 588  	 	 	 $sql[] = $this->data->create( $xmls );
 589  	 	 }
 590  
 591  	 	 return $sql;
 592  	 }
 593  
 594  	 /**
 595  	 * Marks a field or table for destruction
 596  	 */
 597  	function drop() {
 598  	 	 if( isset( $this->current_field ) ) {
 599  	 	 	 // Drop the current field
 600  	 	 	 logMsg( "Dropping field '{$this->current_field}' from table '{$this->name}'" );
 601  	 	 	 // $this->drop_field[$this->current_field] = $xmls->dict->DropColumnSQL( $this->name, $this->current_field );
 602  	 	 	 $this->drop_field[$this->current_field] = $this->current_field;
 603  	 	 } else {
 604  	 	 	 // Drop the current table
 605  	 	 	 logMsg( "Dropping table '{$this->name}'" );
 606  	 	 	 // $this->drop_table = $xmls->dict->DropTableSQL( $this->name );
 607  	 	 	 $this->drop_table = TRUE;
 608  	 	 }
 609  	 }
 610  }
 611  
 612  /**
 613  * Creates an index object in ADOdb's datadict format
 614  *
 615  * This class stores information about a database index. As charactaristics
 616  * of the index are loaded from the external source, methods and properties
 617  * of this class are used to build up the index description in ADOdb's
 618  * datadict format.
 619  *
 620  * @package axmls
 621  * @access private
 622  */
 623  class dbIndex extends dbObject {
 624  
 625  	 /**
 626  	 * @var string	 Index name
 627  	 */
 628  	 var $name;
 629  
 630  	 /**
 631  	 * @var array	 Index options: Index-level options
 632  	 */
 633  	 var $opts = array();
 634  
 635  	 /**
 636  	 * @var array	 Indexed fields: Table columns included in this index
 637  	 */
 638  	 var $columns = array();
 639  
 640  	 /**
 641  	 * @var boolean Mark index for destruction
 642  	 * @access private
 643  	 */
 644  	 var $drop = FALSE;
 645  
 646  	 /**
 647  	 * Initializes the new dbIndex object.
 648  	 *
 649  	 * @param object $parent Parent object
 650  	 * @param array $attributes Attributes
 651  	 *
 652  	 * @internal
 653  	 */
 654  	function __construct( &$parent, $attributes = NULL ) {
 655  	 	 $this->parent = $parent;
 656  
 657  	 	 $this->name = $this->prefix ($attributes['NAME']);
 658  	 }
 659  
 660  	 /**
 661  	 * XML Callback to process start elements
 662  	 *
 663  	 * Processes XML opening tags.
 664  	 * Elements currently processed are: DROP, CLUSTERED, BITMAP, UNIQUE, FULLTEXT & HASH.
 665  	 *
 666  	 * @access private
 667  	 */
 668  	function _tag_open( &$parser, $tag, $attributes ) {
 669  	 	 $this->currentElement = strtoupper( $tag );
 670  
 671  	 	 switch( $this->currentElement ) {
 672  	 	 	 case 'DROP':
 673  	 	 	 	 $this->drop();
 674  	 	 	 	 break;
 675  	 	 	 case 'CLUSTERED':
 676  	 	 	 case 'BITMAP':
 677  	 	 	 case 'UNIQUE':
 678  	 	 	 case 'FULLTEXT':
 679  	 	 	 case 'HASH':
 680  	 	 	 	 // Add index Option
 681  	 	 	 	 $this->addIndexOpt( $this->currentElement );
 682  	 	 	 	 break;
 683  	 	 	 default:
 684  	 	 	 	 // print_r( array( $tag, $attributes ) );
 685  	 	 }
 686  	 }
 687  
 688  	 /**
 689  	 * XML Callback to process CDATA elements
 690  	 *
 691  	 * Processes XML cdata.
 692  	 *
 693  	 * @access private
 694  	 */
 695  	function _tag_cdata( &$parser, $cdata ) {
 696  	 	 switch( $this->currentElement ) {
 697  	 	 	 // Index field name
 698  	 	 	 case 'COL':
 699  	 	 	 	 $this->addField( $cdata );
 700  	 	 	 	 break;
 701  	 	 	 default:
 702  
 703  	 	 }
 704  	 }
 705  
 706  	 /**
 707  	 * XML Callback to process end elements
 708  	 *
 709  	 * @access private
 710  	 */
 711  	function _tag_close( &$parser, $tag ) {
 712  	 	 $this->currentElement = '';
 713  
 714  	 	 switch( strtoupper( $tag ) ) {
 715  	 	 	 case 'INDEX':
 716  	 	 	 	 xml_set_object( $parser, $this->parent );
 717  	 	 	 	 break;
 718  	 	 }
 719  	 }
 720  
 721  	 /**
 722  	 * Adds a field to the index
 723  	 *
 724  	 * @param string $name Field name
 725  	 * @return string Field list
 726  	 */
 727  	function addField( $name ) {
 728  	 	 $this->columns[$this->FieldID( $name )] = $name;
 729  
 730  	 	 // Return the field list
 731  	 	 return $this->columns;
 732  	 }
 733  
 734  	 /**
 735  	 * Adds options to the index
 736  	 *
 737  	 * @param string $opt Comma-separated list of index options.
 738  	 * @return string Option list
 739  	 */
 740  	function addIndexOpt( $opt ) {
 741  	 	 $this->opts[] = $opt;
 742  
 743  	 	 // Return the options list
 744  	 	 return $this->opts;
 745  	 }
 746  
 747  	 /**
 748  	 * Generates the SQL that will create the index in the database
 749  	 *
 750  	 * @param object $xmls adoSchema object
 751  	 * @return array Array containing index creation SQL
 752  	 */
 753  	function create( &$xmls ) {
 754  	 	 if( $this->drop ) {
 755  	 	 	 return NULL;
 756  	 	 }
 757  
 758  	 	 // eliminate any columns that aren't in the table
 759  	 	 foreach( $this->columns as $id => $col ) {
 760  	 	 	 if( !isset( $this->parent->fields[$id] ) ) {
 761  	 	 	 	 unset( $this->columns[$id] );
 762  	 	 	 }
 763  	 	 }
 764  
 765  	 	 return $xmls->dict->CreateIndexSQL( $this->name, $this->parent->name, $this->columns, $this->opts );
 766  	 }
 767  
 768  	 /**
 769  	 * Marks an index for destruction
 770  	 */
 771  	function drop() {
 772  	 	 $this->drop = TRUE;
 773  	 }
 774  }
 775  
 776  /**
 777  * Creates a data object in ADOdb's datadict format
 778  *
 779  * This class stores information about table data.
 780  *
 781  * @package axmls
 782  * @access private
 783  */
 784  class dbData extends dbObject {
 785  
 786  	 var $data = array();
 787  
 788  	 var $row;
 789  
 790  	 /**
 791  	 * Initializes the new dbIndex object.
 792  	 *
 793  	 * @param object $parent Parent object
 794  	 * @param array $attributes Attributes
 795  	 *
 796  	 * @internal
 797  	 */
 798  	function __construct( &$parent, $attributes = NULL ) {
 799  	 	 $this->parent = $parent;
 800  	 }
 801  
 802  	 /**
 803  	 * XML Callback to process start elements
 804  	 *
 805  	 * Processes XML opening tags.
 806  	 * Elements currently processed are: DROP, CLUSTERED, BITMAP, UNIQUE, FULLTEXT & HASH.
 807  	 *
 808  	 * @access private
 809  	 */
 810  	function _tag_open( &$parser, $tag, $attributes ) {
 811  	 	 $this->currentElement = strtoupper( $tag );
 812  
 813  	 	 switch( $this->currentElement ) {
 814  	 	 	 case 'ROW':
 815  	 	 	 	 $this->row = count( $this->data );
 816  	 	 	 	 $this->data[$this->row] = array();
 817  	 	 	 	 break;
 818  	 	 	 case 'F':
 819  	 	 	 	 $this->addField($attributes);
 820  	 	 	 default:
 821  	 	 	 	 // print_r( array( $tag, $attributes ) );
 822  	 	 }
 823  	 }
 824  
 825  	 /**
 826  	 * XML Callback to process CDATA elements
 827  	 *
 828  	 * Processes XML cdata.
 829  	 *
 830  	 * @access private
 831  	 */
 832  	function _tag_cdata( &$parser, $cdata ) {
 833  	 	 switch( $this->currentElement ) {
 834  	 	 	 // Index field name
 835  	 	 	 case 'F':
 836  	 	 	 	 $this->addData( $cdata );
 837  	 	 	 	 break;
 838  	 	 	 default:
 839  
 840  	 	 }
 841  	 }
 842  
 843  	 /**
 844  	 * XML Callback to process end elements
 845  	 *
 846  	 * @access private
 847  	 */
 848  	function _tag_close( &$parser, $tag ) {
 849  	 	 $this->currentElement = '';
 850  
 851  	 	 switch( strtoupper( $tag ) ) {
 852  	 	 	 case 'DATA':
 853  	 	 	 	 xml_set_object( $parser, $this->parent );
 854  	 	 	 	 break;
 855  	 	 }
 856  	 }
 857  
 858  	 /**
 859  	 * Adds a field to the index
 860  	 *
 861  	 * @param string $name Field name
 862  	 * @return string Field list
 863  	 */
 864  	function addField( $attributes ) {
 865  	 	 if( isset( $attributes['NAME'] ) ) {
 866  	 	 	 $name = $attributes['NAME'];
 867  	 	 } else {
 868  	 	 	 $name = count($this->data[$this->row]);
 869  	 	 }
 870  
 871  	 	 // Set the field index so we know where we are
 872  	 	 $this->current_field = $this->FieldID( $name );
 873  	 }
 874  
 875  	 /**
 876  	 * Adds options to the index
 877  	 *
 878  	 * @param string $opt Comma-separated list of index options.
 879  	 * @return string Option list
 880  	 */
 881  	function addData( $cdata ) {
 882  	 	 if( !isset( $this->data[$this->row] ) ) {
 883  	 	 	 $this->data[$this->row] = array();
 884  	 	 }
 885  
 886  	 	 if( !isset( $this->data[$this->row][$this->current_field] ) ) {
 887  	 	 	 $this->data[$this->row][$this->current_field] = '';
 888  	 	 }
 889  
 890  	 	 $this->data[$this->row][$this->current_field] .= $cdata;
 891  	 }
 892  
 893  	 /**
 894  	 * Generates the SQL that will create the index in the database
 895  	 *
 896  	 * @param object $xmls adoSchema object
 897  	 * @return array Array containing index creation SQL
 898  	 */
 899  	function create( &$xmls ) {
 900  	 	 $table = $xmls->dict->TableName($this->parent->name);
 901  	 	 $table_field_count = count($this->parent->fields);
 902  	 	 $sql = array();
 903  
 904  	 	 // eliminate any columns that aren't in the table
 905  	 	 foreach( $this->data as $row ) {
 906  	 	 	 $table_fields = $this->parent->fields;
 907  	 	 	 $fields = array();
 908  
 909  	 	 	 foreach( $row as $field_id => $field_data ) {
 910  	 	 	 	 if( !array_key_exists( $field_id, $table_fields ) ) {
 911  	 	 	 	 	 if( is_numeric( $field_id ) ) {
 912  	 	 	 	 	 	 $field_id = reset( array_keys( $table_fields ) );
 913  	 	 	 	 	 } else {
 914  	 	 	 	 	 	 continue;
 915  	 	 	 	 	 }
 916  	 	 	 	 }
 917  
 918  	 	 	 	 $name = $table_fields[$field_id]['NAME'];
 919  
 920  	 	 	 	 switch( $table_fields[$field_id]['TYPE'] ) {
 921  	 	 	 	 	 case 'C':
 922  	 	 	 	 	 case 'C2':
 923  	 	 	 	 	 case 'X':
 924  	 	 	 	 	 case 'X2':
 925  	 	 	 	 	 	 $fields[$name] = $xmls->db->qstr( $field_data );
 926  	 	 	 	 	 	 break;
 927  	 	 	 	 	 case 'I':
 928  	 	 	 	 	 case 'I1':
 929  	 	 	 	 	 case 'I2':
 930  	 	 	 	 	 case 'I4':
 931  	 	 	 	 	 case 'I8':
 932  	 	 	 	 	 	 $fields[$name] = intval($field_data);
 933  	 	 	 	 	 	 break;
 934  	 	 	 	 	 default:
 935  	 	 	 	 	 	 $fields[$name] = $field_data;
 936  	 	 	 	 }
 937  
 938  	 	 	 	 unset($table_fields[$field_id]);
 939  	 	 	 }
 940  
 941  	 	 	 // check that at least 1 column is specified
 942  	 	 	 if( empty( $fields ) ) {
 943  	 	 	 	 continue;
 944  	 	 	 }
 945  
 946  	 	 	 // check that no required columns are missing
 947  	 	 	 if( count( $fields ) < $table_field_count ) {
 948  	 	 	 	 foreach( $table_fields as $field ) {
 949  	 	 	 	 	 if (isset( $field['OPTS'] ))
 950  	 	 	 	 	 	 if( ( in_array( 'NOTNULL', $field['OPTS'] ) || in_array( 'KEY', $field['OPTS'] ) ) && !in_array( 'AUTOINCREMENT', $field['OPTS'] ) ) {
 951  	 	 	 	 	 	 	 continue(2);
 952  	 	 	 	 	 	 }
 953  	 	 	 	 }
 954  	 	 	 }
 955  
 956  	 	 	 $sql[] = 'INSERT INTO '. $table .' ('. implode( ',', array_keys( $fields ) ) .') VALUES ('. implode( ',', $fields ) .')';
 957  	 	 }
 958  
 959  	 	 return $sql;
 960  	 }
 961  }
 962  
 963  /**
 964  * Creates the SQL to execute a list of provided SQL queries
 965  *
 966  * @package axmls
 967  * @access private
 968  */
 969  class dbQuerySet extends dbObject {
 970  
 971  	 /**
 972  	 * @var array	 List of SQL queries
 973  	 */
 974  	 var $queries = array();
 975  
 976  	 /**
 977  	 * @var string	 String used to build of a query line by line
 978  	 */
 979  	 var $query;
 980  
 981  	 /**
 982  	 * @var string	 Query prefix key
 983  	 */
 984  	 var $prefixKey = '';
 985  
 986  	 /**
 987  	 * @var boolean	 Auto prefix enable (TRUE)
 988  	 */
 989  	 var $prefixMethod = 'AUTO';
 990  
 991  	 /**
 992  	 * Initializes the query set.
 993  	 *
 994  	 * @param object $parent Parent object
 995  	 * @param array $attributes Attributes
 996  	 */
 997  	function __construct( &$parent, $attributes = NULL ) {
 998  	 	 $this->parent = $parent;
 999  
1000  	 	 // Overrides the manual prefix key
1001  	 	 if( isset( $attributes['KEY'] ) ) {
1002  	 	 	 $this->prefixKey = $attributes['KEY'];
1003  	 	 }
1004  
1005  	 	 $prefixMethod = isset( $attributes['PREFIXMETHOD'] ) ? strtoupper( trim( $attributes['PREFIXMETHOD'] ) ) : '';
1006  
1007  	 	 // Enables or disables automatic prefix prepending
1008  	 	 switch( $prefixMethod ) {
1009  	 	 	 case 'AUTO':
1010  	 	 	 	 $this->prefixMethod = 'AUTO';
1011  	 	 	 	 break;
1012  	 	 	 case 'MANUAL':
1013  	 	 	 	 $this->prefixMethod = 'MANUAL';
1014  	 	 	 	 break;
1015  	 	 	 case 'NONE':
1016  	 	 	 	 $this->prefixMethod = 'NONE';
1017  	 	 	 	 break;
1018  	 	 }
1019  	 }
1020  
1021  	 /**
1022  	 * XML Callback to process start elements. Elements currently
1023  	 * processed are: QUERY.
1024  	 *
1025  	 * @access private
1026  	 */
1027  	function _tag_open( &$parser, $tag, $attributes ) {
1028  	 	 $this->currentElement = strtoupper( $tag );
1029  
1030  	 	 switch( $this->currentElement ) {
1031  	 	 	 case 'QUERY':
1032  	 	 	 	 // Create a new query in a SQL queryset.
1033  	 	 	 	 // Ignore this query set if a platform is specified and it's different than the
1034  	 	 	 	 // current connection platform.
1035  	 	 	 	 if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
1036  	 	 	 	 	 $this->newQuery();
1037  	 	 	 	 } else {
1038  	 	 	 	 	 $this->discardQuery();
1039  	 	 	 	 }
1040  	 	 	 	 break;
1041  	 	 	 default:
1042  	 	 	 	 // print_r( array( $tag, $attributes ) );
1043  	 	 }
1044  	 }
1045  
1046  	 /**
1047  	 * XML Callback to process CDATA elements
1048  	 */
1049  	function _tag_cdata( &$parser, $cdata ) {
1050  	 	 switch( $this->currentElement ) {
1051  	 	 	 // Line of queryset SQL data
1052  	 	 	 case 'QUERY':
1053  	 	 	 	 $this->buildQuery( $cdata );
1054  	 	 	 	 break;
1055  	 	 	 default:
1056  
1057  	 	 }
1058  	 }
1059  
1060  	 /**
1061  	 * XML Callback to process end elements
1062  	 *
1063  	 * @access private
1064  	 */
1065  	function _tag_close( &$parser, $tag ) {
1066  	 	 $this->currentElement = '';
1067  
1068  	 	 switch( strtoupper( $tag ) ) {
1069  	 	 	 case 'QUERY':
1070  	 	 	 	 // Add the finished query to the open query set.
1071  	 	 	 	 $this->addQuery();
1072  	 	 	 	 break;
1073  	 	 	 case 'SQL':
1074  	 	 	 	 $this->parent->addSQL( $this->create( $this->parent ) );
1075  	 	 	 	 xml_set_object( $parser, $this->parent );
1076  	 	 	 	 $this->destroy();
1077  	 	 	 	 break;
1078  	 	 	 default:
1079  
1080  	 	 }
1081  	 }
1082  
1083  	 /**
1084  	 * Re-initializes the query.
1085  	 *
1086  	 * @return boolean TRUE
1087  	 */
1088  	function newQuery() {
1089  	 	 $this->query = '';
1090  
1091  	 	 return TRUE;
1092  	 }
1093  
1094  	 /**
1095  	 * Discards the existing query.
1096  	 *
1097  	 * @return boolean TRUE
1098  	 */
1099  	function discardQuery() {
1100  	 	 unset( $this->query );
1101  
1102  	 	 return TRUE;
1103  	 }
1104  
1105  	 /**
1106  	 * Appends a line to a query that is being built line by line
1107  	 *
1108  	 * @param string $data Line of SQL data or NULL to initialize a new query
1109  	 * @return string SQL query string.
1110  	 */
1111  	function buildQuery( $sql = NULL ) {
1112  	 	 if( !isset( $this->query ) OR empty( $sql ) ) {
1113  	 	 	 return FALSE;
1114  	 	 }
1115  
1116  	 	 $this->query .= $sql;
1117  
1118  	 	 return $this->query;
1119  	 }
1120  
1121  	 /**
1122  	 * Adds a completed query to the query list
1123  	 *
1124  	 * @return string	 SQL of added query
1125  	 */
1126  	function addQuery() {
1127  	 	 if( !isset( $this->query ) ) {
1128  	 	 	 return FALSE;
1129  	 	 }
1130  
1131  	 	 $this->queries[] = $return = trim($this->query);
1132  
1133  	 	 unset( $this->query );
1134  
1135  	 	 return $return;
1136  	 }
1137  
1138  	 /**
1139  	 * Creates and returns the current query set
1140  	 *
1141  	 * @param object $xmls adoSchema object
1142  	 * @return array Query set
1143  	 */
1144  	function create( &$xmls ) {
1145  	 	 foreach( $this->queries as $id => $query ) {
1146  	 	 	 switch( $this->prefixMethod ) {
1147  	 	 	 	 case 'AUTO':
1148  	 	 	 	 	 // Enable auto prefix replacement
1149  
1150  	 	 	 	 	 // Process object prefix.
1151  	 	 	 	 	 // Evaluate SQL statements to prepend prefix to objects
1152  	 	 	 	 	 $query = $this->prefixQuery( '/^\s*((?is)INSERT\s+(INTO\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix );
1153  	 	 	 	 	 $query = $this->prefixQuery( '/^\s*((?is)UPDATE\s+(FROM\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix );
1154  	 	 	 	 	 $query = $this->prefixQuery( '/^\s*((?is)DELETE\s+(FROM\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix );
1155  
1156  	 	 	 	 	 // SELECT statements aren't working yet
1157  	 	 	 	 	 #$data = preg_replace( '/(?ias)(^\s*SELECT\s+.*\s+FROM)\s+(\W\s*,?\s*)+((?i)\s+WHERE.*$)/', "\1 $prefix\2 \3", $data );
1158  
1159  	 	 	 	 case 'MANUAL':
1160  	 	 	 	 	 // If prefixKey is set and has a value then we use it to override the default constant XMLS_PREFIX.
1161  	 	 	 	 	 // If prefixKey is not set, we use the default constant XMLS_PREFIX
1162  	 	 	 	 	 if( isset( $this->prefixKey ) AND( $this->prefixKey !== '' ) ) {
1163  	 	 	 	 	 	 // Enable prefix override
1164  	 	 	 	 	 	 $query = str_replace( $this->prefixKey, $xmls->objectPrefix, $query );
1165  	 	 	 	 	 } else {
1166  	 	 	 	 	 	 // Use default replacement
1167  	 	 	 	 	 	 $query = str_replace( XMLS_PREFIX , $xmls->objectPrefix, $query );
1168  	 	 	 	 	 }
1169  	 	 	 }
1170  
1171  	 	 	 $this->queries[$id] = trim( $query );
1172  	 	 }
1173  
1174  	 	 // Return the query set array
1175  	 	 return $this->queries;
1176  	 }
1177  
1178  	 /**
1179  	 * Rebuilds the query with the prefix attached to any objects
1180  	 *
1181  	 * @param string $regex Regex used to add prefix
1182  	 * @param string $query SQL query string
1183  	 * @param string $prefix Prefix to be appended to tables, indices, etc.
1184  	 * @return string Prefixed SQL query string.
1185  	 */
1186  	function prefixQuery( $regex, $query, $prefix = NULL ) {
1187  	 	 if( !isset( $prefix ) ) {
1188  	 	 	 return $query;
1189  	 	 }
1190  
1191  	 	 if( preg_match( $regex, $query, $match ) ) {
1192  	 	 	 $preamble = $match[1];
1193  	 	 	 $postamble = $match[5];
1194  	 	 	 $objectList = explode( ',', $match[3] );
1195  	 	 	 // $prefix = $prefix . '_';
1196  
1197  	 	 	 $prefixedList = '';
1198  
1199  	 	 	 foreach( $objectList as $object ) {
1200  	 	 	 	 if( $prefixedList !== '' ) {
1201  	 	 	 	 	 $prefixedList .= ', ';
1202  	 	 	 	 }
1203  
1204  	 	 	 	 $prefixedList .= $prefix . trim( $object );
1205  	 	 	 }
1206  
1207  	 	 	 $query = $preamble . ' ' . $prefixedList . ' ' . $postamble;
1208  	 	 }
1209  
1210  	 	 return $query;
1211  	 }
1212  }
1213  
1214  /**
1215  * Loads and parses an XML file, creating an array of "ready-to-run" SQL statements
1216  *
1217  * This class is used to load and parse the XML file, to create an array of SQL statements
1218  * that can be used to build a database, and to build the database using the SQL array.
1219  *
1220  * @tutorial getting_started.pkg
1221  *
1222  * @author Richard Tango-Lowy & Dan Cech
1223  * @version $Revision: 1.12 $
1224  *
1225  * @package axmls
1226  */
1227  class adoSchema {
1228  
1229  	 /**
1230  	 * @var array	 Array containing SQL queries to generate all objects
1231  	 * @access private
1232  	 */
1233  	 var $sqlArray;
1234  
1235  	 /**
1236  	 * @var object	 ADOdb connection object
1237  	 * @access private
1238  	 */
1239  	 var $db;
1240  
1241  	 /**
1242  	 * @var object	 ADOdb Data Dictionary
1243  	 * @access private
1244  	 */
1245  	 var $dict;
1246  
1247  	 /**
1248  	 * @var string Current XML element
1249  	 * @access private
1250  	 */
1251  	 var $currentElement = '';
1252  
1253  	 /**
1254  	 * @var string If set (to 'ALTER' or 'REPLACE'), upgrade an existing database
1255  	 * @access private
1256  	 */
1257  	 var $upgrade = '';
1258  
1259  	 /**
1260  	 * @var string Optional object prefix
1261  	 * @access private
1262  	 */
1263  	 var $objectPrefix = '';
1264  
1265  	 /**
1266  	 * @var long	 System debug
1267  	 * @access private
1268  	 */
1269  	 var $debug;
1270  
1271  	 /**
1272  	 * @var string Regular expression to find schema version
1273  	 * @access private
1274  	 */
1275  	 var $versionRegex = '/<schema.*?( version="([^"]*)")?.*?>/';
1276  
1277  	 /**
1278  	 * @var string Current schema version
1279  	 * @access private
1280  	 */
1281  	 var $schemaVersion;
1282  
1283  	 /**
1284  	 * @var int	 Success of last Schema execution
1285  	 */
1286  	 var $success;
1287  
1288  	 /**
1289  	 * @var bool	 Execute SQL inline as it is generated
1290  	 */
1291  	 var $executeInline;
1292  
1293  	 /**
1294  	 * @var bool	 Continue SQL execution if errors occur
1295  	 */
1296  	 var $continueOnError;
1297  
1298  	 /**
1299  	 * Creates an adoSchema object
1300  	 *
1301  	 * Creating an adoSchema object is the first step in processing an XML schema.
1302  	 * The only parameter is an ADOdb database connection object, which must already
1303  	 * have been created.
1304  	 *
1305  	 * @param object $db ADOdb database connection object.
1306  	 */
1307  	function __construct( $db ) {
1308  	 	 $this->db = $db;
1309  	 	 $this->debug = $this->db->debug;
1310  	 	 $this->dict = newDataDictionary( $this->db );
1311  	 	 $this->sqlArray = array();
1312  	 	 $this->schemaVersion = XMLS_SCHEMA_VERSION;
1313  	 	 $this->executeInline( XMLS_EXECUTE_INLINE );
1314  	 	 $this->continueOnError( XMLS_CONTINUE_ON_ERROR );
1315  	 	 $this->setUpgradeMethod();
1316  	 }
1317  
1318  	 /**
1319  	 * Sets the method to be used for upgrading an existing database
1320  	 *
1321  	 * Use this method to specify how existing database objects should be upgraded.
1322  	 * The method option can be set to ALTER, REPLACE, BEST, or NONE. ALTER attempts to
1323  	 * alter each database object directly, REPLACE attempts to rebuild each object
1324  	 * from scratch, BEST attempts to determine the best upgrade method for each
1325  	 * object, and NONE disables upgrading.
1326  	 *
1327  	 * This method is not yet used by AXMLS, but exists for backward compatibility.
1328  	 * The ALTER method is automatically assumed when the adoSchema object is
1329  	 * instantiated; other upgrade methods are not currently supported.
1330  	 *
1331  	 * @param string $method Upgrade method (ALTER|REPLACE|BEST|NONE)
1332  	 * @returns string Upgrade method used
1333  	 */
1334  	function SetUpgradeMethod( $method = '' ) {
1335  	 	 if( !is_string( $method ) ) {
1336  	 	 	 return FALSE;
1337  	 	 }
1338  
1339  	 	 $method = strtoupper( $method );
1340  
1341  	 	 // Handle the upgrade methods
1342  	 	 switch( $method ) {
1343  	 	 	 case 'ALTER':
1344  	 	 	 	 $this->upgrade = $method;
1345  	 	 	 	 break;
1346  	 	 	 case 'REPLACE':
1347  	 	 	 	 $this->upgrade = $method;
1348  	 	 	 	 break;
1349  	 	 	 case 'BEST':
1350  	 	 	 	 $this->upgrade = 'ALTER';
1351  	 	 	 	 break;
1352  	 	 	 case 'NONE':
1353  	 	 	 	 $this->upgrade = 'NONE';
1354  	 	 	 	 break;
1355  	 	 	 default:
1356  	 	 	 	 // Use default if no legitimate method is passed.
1357  	 	 	 	 $this->upgrade = XMLS_DEFAULT_UPGRADE_METHOD;
1358  	 	 }
1359  
1360  	 	 return $this->upgrade;
1361  	 }
1362  
1363  	 /**
1364  	 * Enables/disables inline SQL execution.
1365  	 *
1366  	 * Call this method to enable or disable inline execution of the schema. If the mode is set to TRUE (inline execution),
1367  	 * AXMLS applies the SQL to the database immediately as each schema entity is parsed. If the mode
1368  	 * is set to FALSE (post execution), AXMLS parses the entire schema and you will need to call adoSchema::ExecuteSchema()
1369  	 * to apply the schema to the database.
1370  	 *
1371  	 * @param bool $mode execute
1372  	 * @return bool current execution mode
1373  	 *
1374  	 * @see ParseSchema(), ExecuteSchema()
1375  	 */
1376  	function ExecuteInline( $mode = NULL ) {
1377  	 	 if( is_bool( $mode ) ) {
1378  	 	 	 $this->executeInline = $mode;
1379  	 	 }
1380  
1381  	 	 return $this->executeInline;
1382  	 }
1383  
1384  	 /**
1385  	 * Enables/disables SQL continue on error.
1386  	 *
1387  	 * Call this method to enable or disable continuation of SQL execution if an error occurs.
1388  	 * If the mode is set to TRUE (continue), AXMLS will continue to apply SQL to the database, even if an error occurs.
1389  	 * If the mode is set to FALSE (halt), AXMLS will halt execution of generated sql if an error occurs, though parsing
1390  	 * of the schema will continue.
1391  	 *
1392  	 * @param bool $mode execute
1393  	 * @return bool current continueOnError mode
1394  	 *
1395  	 * @see addSQL(), ExecuteSchema()
1396  	 */
1397  	function ContinueOnError( $mode = NULL ) {
1398  	 	 if( is_bool( $mode ) ) {
1399  	 	 	 $this->continueOnError = $mode;
1400  	 	 }
1401  
1402  	 	 return $this->continueOnError;
1403  	 }
1404  
1405  	 /**
1406  	 * Loads an XML schema from a file and converts it to SQL.
1407  	 *
1408  	 * Call this method to load the specified schema (see the DTD for the proper format) from
1409  	 * the filesystem and generate the SQL necessary to create the database described.
1410  	 * @see ParseSchemaString()
1411  	 *
1412  	 * @param string $file Name of XML schema file.
1413  	 * @param bool $returnSchema Return schema rather than parsing.
1414  	 * @return array Array of SQL queries, ready to execute
1415  	 */
1416  	function ParseSchema( $filename, $returnSchema = FALSE ) {
1417  	 	 return $this->ParseSchemaString( $this->ConvertSchemaFile( $filename ), $returnSchema );
1418  	 }
1419  
1420  	 /**
1421  	 * Loads an XML schema from a file and converts it to SQL.
1422  	 *
1423  	 * Call this method to load the specified schema from a file (see the DTD for the proper format)
1424  	 * and generate the SQL necessary to create the database described by the schema.
1425  	 *
1426  	 * @param string $file Name of XML schema file.
1427  	 * @param bool $returnSchema Return schema rather than parsing.
1428  	 * @return array Array of SQL queries, ready to execute.
1429  	 *
1430  	 * @deprecated Replaced by adoSchema::ParseSchema() and adoSchema::ParseSchemaString()
1431  	 * @see ParseSchema(), ParseSchemaString()
1432  	 */
1433  	function ParseSchemaFile( $filename, $returnSchema = FALSE ) {
1434  	 	 // Open the file
1435  	 	 if( !($fp = fopen( $filename, 'r' )) ) {
1436  	 	 	 // die( 'Unable to open file' );
1437  	 	 	 return FALSE;
1438  	 	 }
1439  
1440  	 	 // do version detection here
1441  	 	 if( $this->SchemaFileVersion( $filename ) != $this->schemaVersion ) {
1442  	 	 	 return FALSE;
1443  	 	 }
1444  
1445  	 	 if ( $returnSchema )
1446  	 	 {
1447  	 	 	 $xmlstring = '';
1448  	 	 	 while( $data = fread( $fp, 100000 ) ) {
1449  	 	 	 	 $xmlstring .= $data;
1450  	 	 	 }
1451  	 	 	 return $xmlstring;
1452  	 	 }
1453  
1454  	 	 $this->success = 2;
1455  
1456  	 	 $xmlParser = $this->create_parser();
1457  
1458  	 	 // Process the file
1459  	 	 while( $data = fread( $fp, 4096 ) ) {
1460  	 	 	 if( !xml_parse( $xmlParser, $data, feof( $fp ) ) ) {
1461  	 	 	 	 die( sprintf(
1462  	 	 	 	 	 "XML error: %s at line %d",
1463  	 	 	 	 	 xml_error_string( xml_get_error_code( $xmlParser) ),
1464  	 	 	 	 	 xml_get_current_line_number( $xmlParser)
1465  	 	 	 	 ) );
1466  	 	 	 }
1467  	 	 }
1468  
1469  	 	 xml_parser_free( $xmlParser );
1470  
1471  	 	 return $this->sqlArray;
1472  	 }
1473  
1474  	 /**
1475  	 * Converts an XML schema string to SQL.
1476  	 *
1477  	 * Call this method to parse a string containing an XML schema (see the DTD for the proper format)
1478  	 * and generate the SQL necessary to create the database described by the schema.
1479  	 * @see ParseSchema()
1480  	 *
1481  	 * @param string $xmlstring XML schema string.
1482  	 * @param bool $returnSchema Return schema rather than parsing.
1483  	 * @return array Array of SQL queries, ready to execute.
1484  	 */
1485  	function ParseSchemaString( $xmlstring, $returnSchema = FALSE ) {
1486  	 	 if( !is_string( $xmlstring ) OR empty( $xmlstring ) ) {
1487  	 	 	 return FALSE;
1488  	 	 }
1489  
1490  	 	 // do version detection here
1491  	 	 if( $this->SchemaStringVersion( $xmlstring ) != $this->schemaVersion ) {
1492  	 	 	 return FALSE;
1493  	 	 }
1494  
1495  	 	 if ( $returnSchema )
1496  	 	 {
1497  	 	 	 return $xmlstring;
1498  	 	 }
1499  
1500  	 	 $this->success = 2;
1501  
1502  	 	 $xmlParser = $this->create_parser();
1503  
1504  	 	 if( !xml_parse( $xmlParser, $xmlstring, TRUE ) ) {
1505  	 	 	 die( sprintf(
1506  	 	 	 	 "XML error: %s at line %d",
1507  	 	 	 	 xml_error_string( xml_get_error_code( $xmlParser) ),
1508  	 	 	 	 xml_get_current_line_number( $xmlParser)
1509  	 	 	 ) );
1510  	 	 }
1511  
1512  	 	 xml_parser_free( $xmlParser );
1513  
1514  	 	 return $this->sqlArray;
1515  	 }
1516  
1517  	 /**
1518  	 * Loads an XML schema from a file and converts it to uninstallation SQL.
1519  	 *
1520  	 * Call this method to load the specified schema (see the DTD for the proper format) from
1521  	 * the filesystem and generate the SQL necessary to remove the database described.
1522  	 * @see RemoveSchemaString()
1523  	 *
1524  	 * @param string $file Name of XML schema file.
1525  	 * @param bool $returnSchema Return schema rather than parsing.
1526  	 * @return array Array of SQL queries, ready to execute
1527  	 */
1528  	function RemoveSchema( $filename, $returnSchema = FALSE ) {
1529  	 	 return $this->RemoveSchemaString( $this->ConvertSchemaFile( $filename ), $returnSchema );
1530  	 }
1531  
1532  	 /**
1533  	 * Converts an XML schema string to uninstallation SQL.
1534  	 *
1535  	 * Call this method to parse a string containing an XML schema (see the DTD for the proper format)
1536  	 * and generate the SQL necessary to uninstall the database described by the schema.
1537  	 * @see RemoveSchema()
1538  	 *
1539  	 * @param string $schema XML schema string.
1540  	 * @param bool $returnSchema Return schema rather than parsing.
1541  	 * @return array Array of SQL queries, ready to execute.
1542  	 */
1543  	function RemoveSchemaString( $schema, $returnSchema = FALSE ) {
1544  
1545  	 	 // grab current version
1546  	 	 if( !( $version = $this->SchemaStringVersion( $schema ) ) ) {
1547  	 	 	 return FALSE;
1548  	 	 }
1549  
1550  	 	 return $this->ParseSchemaString( $this->TransformSchema( $schema, 'remove-' . $version), $returnSchema );
1551  	 }
1552  
1553  	 /**
1554  	 * Applies the current XML schema to the database (post execution).
1555  	 *
1556  	 * Call this method to apply the current schema (generally created by calling
1557  	 * ParseSchema() or ParseSchemaString() ) to the database (creating the tables, indexes,
1558  	 * and executing other SQL specified in the schema) after parsing.
1559  	 * @see ParseSchema(), ParseSchemaString(), ExecuteInline()
1560  	 *
1561  	 * @param array $sqlArray Array of SQL statements that will be applied rather than
1562  	 *	 	 the current schema.
1563  	 * @param boolean $continueOnErr Continue to apply the schema even if an error occurs.
1564  	 * @returns integer 0 if failure, 1 if errors, 2 if successful.
1565  	 */
1566  	function ExecuteSchema( $sqlArray = NULL, $continueOnErr =  NULL ) {
1567  	 	 if( !is_bool( $continueOnErr ) ) {
1568  	 	 	 $continueOnErr = $this->ContinueOnError();
1569  	 	 }
1570  
1571  	 	 if( !isset( $sqlArray ) ) {
1572  	 	 	 $sqlArray = $this->sqlArray;
1573  	 	 }
1574  
1575  	 	 if( !is_array( $sqlArray ) ) {
1576  	 	 	 $this->success = 0;
1577  	 	 } else {
1578  	 	 	 $this->success = $this->dict->ExecuteSQLArray( $sqlArray, $continueOnErr );
1579  	 	 }
1580  
1581  	 	 return $this->success;
1582  	 }
1583  
1584  	 /**
1585  	 * Returns the current SQL array.
1586  	 *
1587  	 * Call this method to fetch the array of SQL queries resulting from
1588  	 * ParseSchema() or ParseSchemaString().
1589  	 *
1590  	 * @param string $format Format: HTML, TEXT, or NONE (PHP array)
1591  	 * @return array Array of SQL statements or FALSE if an error occurs
1592  	 */
1593  	function PrintSQL( $format = 'NONE' ) {
1594  	 	 $sqlArray = null;
1595  	 	 return $this->getSQL( $format, $sqlArray );
1596  	 }
1597  
1598  	 /**
1599  	 * Saves the current SQL array to the local filesystem as a list of SQL queries.
1600  	 *
1601  	 * Call this method to save the array of SQL queries (generally resulting from a
1602  	 * parsed XML schema) to the filesystem.
1603  	 *
1604  	 * @param string $filename Path and name where the file should be saved.
1605  	 * @return boolean TRUE if save is successful, else FALSE.
1606  	 */
1607  	function SaveSQL( $filename = './schema.sql' ) {
1608  
1609  	 	 if( !isset( $sqlArray ) ) {
1610  	 	 	 $sqlArray = $this->sqlArray;
1611  	 	 }
1612  	 	 if( !isset( $sqlArray ) ) {
1613  	 	 	 return FALSE;
1614  	 	 }
1615  
1616  	 	 $fp = fopen( $filename, "w" );
1617  
1618  	 	 foreach( $sqlArray as $key => $query ) {
1619  	 	 	 fwrite( $fp, $query . ";\n" );
1620  	 	 }
1621  	 	 fclose( $fp );
1622  	 }
1623  
1624  	 /**
1625  	 * Create an xml parser
1626  	 *
1627  	 * @return object PHP XML parser object
1628  	 *
1629  	 * @access private
1630  	 */
1631  	function create_parser() {
1632  	 	 // Create the parser
1633  	 	 $xmlParser = xml_parser_create();
1634  	 	 xml_set_object( $xmlParser, $this );
1635  
1636  	 	 // Initialize the XML callback functions
1637  	 	 xml_set_element_handler( $xmlParser, '_tag_open', '_tag_close' );
1638  	 	 xml_set_character_data_handler( $xmlParser, '_tag_cdata' );
1639  
1640  	 	 return $xmlParser;
1641  	 }
1642  
1643  	 /**
1644  	 * XML Callback to process start elements
1645  	 *
1646  	 * @access private
1647  	 */
1648  	function _tag_open( &$parser, $tag, $attributes ) {
1649  	 	 switch( strtoupper( $tag ) ) {
1650  	 	 	 case 'TABLE':
1651  	 	 	 	 $this->obj = new dbTable( $this, $attributes );
1652  	 	 	 	 xml_set_object( $parser, $this->obj );
1653  	 	 	 	 break;
1654  	 	 	 case 'SQL':
1655  	 	 	 	 if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
1656  	 	 	 	 	 $this->obj = new dbQuerySet( $this, $attributes );
1657  	 	 	 	 	 xml_set_object( $parser, $this->obj );
1658  	 	 	 	 }
1659  	 	 	 	 break;
1660  	 	 	 default:
1661  	 	 	 	 // print_r( array( $tag, $attributes ) );
1662  	 	 }
1663  
1664  	 }
1665  
1666  	 /**
1667  	 * XML Callback to process CDATA elements
1668  	 *
1669  	 * @access private
1670  	 */
1671  	function _tag_cdata( &$parser, $cdata ) {
1672  	 }
1673  
1674  	 /**
1675  	 * XML Callback to process end elements
1676  	 *
1677  	 * @access private
1678  	 * @internal
1679  	 */
1680  	function _tag_close( &$parser, $tag ) {
1681  
1682  	 }
1683  
1684  	 /**
1685  	 * Converts an XML schema string to the specified DTD version.
1686  	 *
1687  	 * Call this method to convert a string containing an XML schema to a different AXMLS
1688  	 * DTD version. For instance, to convert a schema created for an pre-1.0 version for
1689  	 * AXMLS (DTD version 0.1) to a newer version of the DTD (e.g. 0.2). If no DTD version
1690  	 * parameter is specified, the schema will be converted to the current DTD version.
1691  	 * If the newFile parameter is provided, the converted schema will be written to the specified
1692  	 * file.
1693  	 * @see ConvertSchemaFile()
1694  	 *
1695  	 * @param string $schema String containing XML schema that will be converted.
1696  	 * @param string $newVersion DTD version to convert to.
1697  	 * @param string $newFile File name of (converted) output file.
1698  	 * @return string Converted XML schema or FALSE if an error occurs.
1699  	 */
1700  	function ConvertSchemaString( $schema, $newVersion = NULL, $newFile = NULL ) {
1701  
1702  	 	 // grab current version
1703  	 	 if( !( $version = $this->SchemaStringVersion( $schema ) ) ) {
1704  	 	 	 return FALSE;
1705  	 	 }
1706  
1707  	 	 if( !isset ($newVersion) ) {
1708  	 	 	 $newVersion = $this->schemaVersion;
1709  	 	 }
1710  
1711  	 	 if( $version == $newVersion ) {
1712  	 	 	 $result = $schema;
1713  	 	 } else {
1714  	 	 	 $result = $this->TransformSchema( $schema, 'convert-' . $version . '-' . $newVersion);
1715  	 	 }
1716  
1717  	 	 if( is_string( $result ) AND is_string( $newFile ) AND ( $fp = fopen( $newFile, 'w' ) ) ) {
1718  	 	 	 fwrite( $fp, $result );
1719  	 	 	 fclose( $fp );
1720  	 	 }
1721  
1722  	 	 return $result;
1723  	 }
1724  
1725  	 // compat for pre-4.3 - jlim
1726  	function _file_get_contents($path)
1727  	 {
1728  	 	 if (function_exists('file_get_contents')) return file_get_contents($path);
1729  	 	 return join('',file($path));
1730  	 }
1731  
1732  	 /**
1733  	 * Converts an XML schema file to the specified DTD version.
1734  	 *
1735  	 * Call this method to convert the specified XML schema file to a different AXMLS
1736  	 * DTD version. For instance, to convert a schema created for an pre-1.0 version for
1737  	 * AXMLS (DTD version 0.1) to a newer version of the DTD (e.g. 0.2). If no DTD version
1738  	 * parameter is specified, the schema will be converted to the current DTD version.
1739  	 * If the newFile parameter is provided, the converted schema will be written to the specified
1740  	 * file.
1741  	 * @see ConvertSchemaString()
1742  	 *
1743  	 * @param string $filename Name of XML schema file that will be converted.
1744  	 * @param string $newVersion DTD version to convert to.
1745  	 * @param string $newFile File name of (converted) output file.
1746  	 * @return string Converted XML schema or FALSE if an error occurs.
1747  	 */
1748  	function ConvertSchemaFile( $filename, $newVersion = NULL, $newFile = NULL ) {
1749  
1750  	 	 // grab current version
1751  	 	 if( !( $version = $this->SchemaFileVersion( $filename ) ) ) {
1752  	 	 	 return FALSE;
1753  	 	 }
1754  
1755  	 	 if( !isset ($newVersion) ) {
1756  	 	 	 $newVersion = $this->schemaVersion;
1757  	 	 }
1758  
1759  	 	 if( $version == $newVersion ) {
1760  	 	 	 $result = _file_get_contents( $filename );
1761  
1762  	 	 	 // remove unicode BOM if present
1763  	 	 	 if( substr( $result, 0, 3 ) == sprintf( '%c%c%c', 239, 187, 191 ) ) {
1764  	 	 	 	 $result = substr( $result, 3 );
1765  	 	 	 }
1766  	 	 } else {
1767  	 	 	 $result = $this->TransformSchema( $filename, 'convert-' . $version . '-' . $newVersion, 'file' );
1768  	 	 }
1769  
1770  	 	 if( is_string( $result ) AND is_string( $newFile ) AND ( $fp = fopen( $newFile, 'w' ) ) ) {
1771  	 	 	 fwrite( $fp, $result );
1772  	 	 	 fclose( $fp );
1773  	 	 }
1774  
1775  	 	 return $result;
1776  	 }
1777  
1778  	function TransformSchema( $schema, $xsl, $schematype='string' )
1779  	 {
1780  	 	 // Fail if XSLT extension is not available
1781  	 	 if( ! function_exists( 'xslt_create' ) ) {
1782  	 	 	 return FALSE;
1783  	 	 }
1784  
1785  	 	 $xsl_file = dirname( __FILE__ ) . '/xsl/' . $xsl . '.xsl';
1786  
1787  	 	 // look for xsl
1788  	 	 if( !is_readable( $xsl_file ) ) {
1789  	 	 	 return FALSE;
1790  	 	 }
1791  
1792  	 	 switch( $schematype )
1793  	 	 {
1794  	 	 	 case 'file':
1795  	 	 	 	 if( !is_readable( $schema ) ) {
1796  	 	 	 	 	 return FALSE;
1797  	 	 	 	 }
1798  
1799  	 	 	 	 $schema = _file_get_contents( $schema );
1800  	 	 	 	 break;
1801  	 	 	 case 'string':
1802  	 	 	 default:
1803  	 	 	 	 if( !is_string( $schema ) ) {
1804  	 	 	 	 	 return FALSE;
1805  	 	 	 	 }
1806  	 	 }
1807  
1808  	 	 $arguments = array (
1809  	 	 	 '/_xml' => $schema,
1810  	 	 	 '/_xsl' => _file_get_contents( $xsl_file )
1811  	 	 );
1812  
1813  	 	 // create an XSLT processor
1814  	 	 $xh = xslt_create ();
1815  
1816  	 	 // set error handler
1817  	 	 xslt_set_error_handler ($xh, array (&$this, 'xslt_error_handler'));
1818  
1819  	 	 // process the schema
1820  	 	 $result = xslt_process ($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);
1821  
1822  	 	 xslt_free ($xh);
1823  
1824  	 	 return $result;
1825  	 }
1826  
1827  	 /**
1828  	 * Processes XSLT transformation errors
1829  	 *
1830  	 * @param object $parser XML parser object
1831  	 * @param integer $errno Error number
1832  	 * @param integer $level Error level
1833  	 * @param array $fields Error information fields
1834  	 *
1835  	 * @access private
1836  	 */
1837  	function xslt_error_handler( $parser, $errno, $level, $fields ) {
1838  	 	 if( is_array( $fields ) ) {
1839  	 	 	 $msg = array(
1840  	 	 	 	 'Message Type' => ucfirst( $fields['msgtype'] ),
1841  	 	 	 	 'Message Code' => $fields['code'],
1842  	 	 	 	 'Message' => $fields['msg'],
1843  	 	 	 	 'Error Number' => $errno,
1844  	 	 	 	 'Level' => $level
1845  	 	 	 );
1846  
1847  	 	 	 switch( $fields['URI'] ) {
1848  	 	 	 	 case 'arg:/_xml':
1849  	 	 	 	 	 $msg['Input'] = 'XML';
1850  	 	 	 	 	 break;
1851  	 	 	 	 case 'arg:/_xsl':
1852  	 	 	 	 	 $msg['Input'] = 'XSL';
1853  	 	 	 	 	 break;
1854  	 	 	 	 default:
1855  	 	 	 	 	 $msg['Input'] = $fields['URI'];
1856  	 	 	 }
1857  
1858  	 	 	 $msg['Line'] = $fields['line'];
1859  	 	 } else {
1860  	 	 	 $msg = array(
1861  	 	 	 	 'Message Type' => 'Error',
1862  	 	 	 	 'Error Number' => $errno,
1863  	 	 	 	 'Level' => $level,
1864  	 	 	 	 'Fields' => var_export( $fields, TRUE )
1865  	 	 	 );
1866  	 	 }
1867  
1868  	 	 $error_details = $msg['Message Type'] . ' in XSLT Transformation' . "\n"
1869  	 	 	 	 	    . '<table>' . "\n";
1870  
1871  	 	 foreach( $msg as $label => $details ) {
1872  	 	 	 $error_details .= '<tr><td><b>' . $label . ': </b></td><td>' . htmlentities( $details ) . '</td></tr>' . "\n";
1873  	 	 }
1874  
1875  	 	 $error_details .= '</table>';
1876  
1877  	 	 trigger_error( $error_details, E_USER_ERROR );
1878  	 }
1879  
1880  	 /**
1881  	 * Returns the AXMLS Schema Version of the requested XML schema file.
1882  	 *
1883  	 * Call this method to obtain the AXMLS DTD version of the requested XML schema file.
1884  	 * @see SchemaStringVersion()
1885  	 *
1886  	 * @param string $filename AXMLS schema file
1887  	 * @return string Schema version number or FALSE on error
1888  	 */
1889  	function SchemaFileVersion( $filename ) {
1890  	 	 // Open the file
1891  	 	 if( !($fp = fopen( $filename, 'r' )) ) {
1892  	 	 	 // die( 'Unable to open file' );
1893  	 	 	 return FALSE;
1894  	 	 }
1895  
1896  	 	 // Process the file
1897  	 	 while( $data = fread( $fp, 4096 ) ) {
1898  	 	 	 if( preg_match( $this->versionRegex, $data, $matches ) ) {
1899  	 	 	 	 return !empty( $matches[2] ) ? $matches[2] : XMLS_DEFAULT_SCHEMA_VERSION;
1900  	 	 	 }
1901  	 	 }
1902  
1903  	 	 return FALSE;
1904  	 }
1905  
1906  	 /**
1907  	 * Returns the AXMLS Schema Version of the provided XML schema string.
1908  	 *
1909  	 * Call this method to obtain the AXMLS DTD version of the provided XML schema string.
1910  	 * @see SchemaFileVersion()
1911  	 *
1912  	 * @param string $xmlstring XML schema string
1913  	 * @return string Schema version number or FALSE on error
1914  	 */
1915  	function SchemaStringVersion( $xmlstring ) {
1916  	 	 if( !is_string( $xmlstring ) OR empty( $xmlstring ) ) {
1917  	 	 	 return FALSE;
1918  	 	 }
1919  
1920  	 	 if( preg_match( $this->versionRegex, $xmlstring, $matches ) ) {
1921  	 	 	 return !empty( $matches[2] ) ? $matches[2] : XMLS_DEFAULT_SCHEMA_VERSION;
1922  	 	 }
1923  
1924  	 	 return FALSE;
1925  	 }
1926  
1927  	 /**
1928  	 * Extracts an XML schema from an existing database.
1929  	 *
1930  	 * Call this method to create an XML schema string from an existing database.
1931  	 * If the data parameter is set to TRUE, AXMLS will include the data from the database
1932  	 * in the schema.
1933  	 *
1934  	 * @param boolean $data Include data in schema dump
1935  	 * @return string Generated XML schema
1936  	 */
1937  	function ExtractSchema( $data = FALSE ) {
1938  	 	 $old_mode = $this->db->SetFetchMode( ADODB_FETCH_NUM );
1939  
1940  	 	 $schema = '<?xml version="1.0"?>' . "\n"
1941  	 	 	 	 . '<schema version="' . $this->schemaVersion . '">' . "\n";
1942  
1943  	 	 if( is_array( $tables = $this->db->MetaTables( 'TABLES' ) ) ) {
1944  	 	 	 foreach( $tables as $table ) {
1945  	 	 	 	 $schema .= '	 <table name="' . $table . '">' . "\n";
1946  
1947  	 	 	 	 // grab details from database
1948  	 	 	 	 $rs = $this->db->Execute( 'SELECT * FROM ' . $table . ' WHERE 1=1' );
1949  	 	 	 	 $fields = $this->db->MetaColumns( $table );
1950  	 	 	 	 $indexes = $this->db->MetaIndexes( $table );
1951  
1952  	 	 	 	 if( is_array( $fields ) ) {
1953  	 	 	 	 	 foreach( $fields as $details ) {
1954  	 	 	 	 	 	 $extra = '';
1955  	 	 	 	 	 	 $content = array();
1956  
1957  	 	 	 	 	 	 if( $details->max_length > 0 ) {
1958  	 	 	 	 	 	 	 $extra .= ' size="' . $details->max_length . '"';
1959  	 	 	 	 	 	 }
1960  
1961  	 	 	 	 	 	 if( $details->primary_key ) {
1962  	 	 	 	 	 	 	 $content[] = '<KEY/>';
1963  	 	 	 	 	 	 } elseif( $details->not_null ) {
1964  	 	 	 	 	 	 	 $content[] = '<NOTNULL/>';
1965  	 	 	 	 	 	 }
1966  
1967  	 	 	 	 	 	 if( $details->has_default ) {
1968  	 	 	 	 	 	 	 $content[] = '<DEFAULT value="' . $details->default_value . '"/>';
1969  	 	 	 	 	 	 }
1970  
1971  	 	 	 	 	 	 if( $details->auto_increment ) {
1972  	 	 	 	 	 	 	 $content[] = '<AUTOINCREMENT/>';
1973  	 	 	 	 	 	 }
1974  
1975  	 	 	 	 	 	 // this stops the creation of 'R' columns,
1976  	 	 	 	 	 	 // AUTOINCREMENT is used to create auto columns
1977  	 	 	 	 	 	 $details->primary_key = 0;
1978  	 	 	 	 	 	 $type = $rs->MetaType( $details );
1979  
1980  	 	 	 	 	 	 $schema .= '	 	 <field name="' . $details->name . '" type="' . $type . '"' . $extra . '>';
1981  
1982  	 	 	 	 	 	 if( !empty( $content ) ) {
1983  	 	 	 	 	 	 	 $schema .= "\n	 	 	 " . implode( "\n	 	 	 ", $content ) . "\n	 	 ";
1984  	 	 	 	 	 	 }
1985  
1986  	 	 	 	 	 	 $schema .= '</field>' . "\n";
1987  	 	 	 	 	 }
1988  	 	 	 	 }
1989  
1990  	 	 	 	 if( is_array( $indexes ) ) {
1991  	 	 	 	 	 foreach( $indexes as $index => $details ) {
1992  	 	 	 	 	 	 $schema .= '	 	 <index name="' . $index . '">' . "\n";
1993  
1994  	 	 	 	 	 	 if( $details['unique'] ) {
1995  	 	 	 	 	 	 	 $schema .= '	 	 	 <UNIQUE/>' . "\n";
1996  	 	 	 	 	 	 }
1997  
1998  	 	 	 	 	 	 foreach( $details['columns'] as $column ) {
1999  	 	 	 	 	 	 	 $schema .= '	 	 	 <col>' . $column . '</col>' . "\n";
2000  	 	 	 	 	 	 }
2001  
2002  	 	 	 	 	 	 $schema .= '	 	 </index>' . "\n";
2003  	 	 	 	 	 }
2004  	 	 	 	 }
2005  
2006  	 	 	 	 if( $data ) {
2007  	 	 	 	 	 $rs = $this->db->Execute( 'SELECT * FROM ' . $table );
2008  
2009  	 	 	 	 	 if( is_object( $rs ) ) {
2010  	 	 	 	 	 	 $schema .= '	 	 <data>' . "\n";
2011  
2012  	 	 	 	 	 	 while( $row = $rs->FetchRow() ) {
2013  	 	 	 	 	 	 	 foreach( $row as $key => $val ) {
2014  	 	 	 	 	 	 	 	 $row[$key] = htmlentities($val);
2015  	 	 	 	 	 	 	 }
2016  
2017  	 	 	 	 	 	 	 $schema .= '	 	 	 <row><f>' . implode( '</f><f>', $row ) . '</f></row>' . "\n";
2018  	 	 	 	 	 	 }
2019  
2020  	 	 	 	 	 	 $schema .= '	 	 </data>' . "\n";
2021  	 	 	 	 	 }
2022  	 	 	 	 }
2023  
2024  	 	 	 	 $schema .= '	 </table>' . "\n";
2025  	 	 	 }
2026  	 	 }
2027  
2028  	 	 $this->db->SetFetchMode( $old_mode );
2029  
2030  	 	 $schema .= '</schema>';
2031  	 	 return $schema;
2032  	 }
2033  
2034  	 /**
2035  	 * Sets a prefix for database objects
2036  	 *
2037  	 * Call this method to set a standard prefix that will be prepended to all database tables
2038  	 * and indices when the schema is parsed. Calling setPrefix with no arguments clears the prefix.
2039  	 *
2040  	 * @param string $prefix Prefix that will be prepended.
2041  	 * @param boolean $underscore If TRUE, automatically append an underscore character to the prefix.
2042  	 * @return boolean TRUE if successful, else FALSE
2043  	 */
2044  	function SetPrefix( $prefix = '', $underscore = TRUE ) {
2045  	 	 switch( TRUE ) {
2046  	 	 	 // clear prefix
2047  	 	 	 case empty( $prefix ):
2048  	 	 	 	 logMsg( 'Cleared prefix' );
2049  	 	 	 	 $this->objectPrefix = '';
2050  	 	 	 	 return TRUE;
2051  	 	 	 // prefix too long
2052  	 	 	 case strlen( $prefix ) > XMLS_PREFIX_MAXLEN:
2053  	 	 	 // prefix contains invalid characters
2054  	 	 	 case !preg_match( '/^[a-z][a-z0-9_]+$/i', $prefix ):
2055  	 	 	 	 logMsg( 'Invalid prefix: ' . $prefix );
2056  	 	 	 	 return FALSE;
2057  	 	 }
2058  
2059  	 	 if( $underscore AND substr( $prefix, -1 ) != '_' ) {
2060  	 	 	 $prefix .= '_';
2061  	 	 }
2062  
2063  	 	 // prefix valid
2064  	 	 logMsg( 'Set prefix: ' . $prefix );
2065  	 	 $this->objectPrefix = $prefix;
2066  	 	 return TRUE;
2067  	 }
2068  
2069  	 /**
2070  	 * Returns an object name with the current prefix prepended.
2071  	 *
2072  	 * @param string	 $name Name
2073  	 * @return string	 Prefixed name
2074  	 *
2075  	 * @access private
2076  	 */
2077  	function prefix( $name = '' ) {
2078  	 	 // if prefix is set
2079  	 	 if( !empty( $this->objectPrefix ) ) {
2080  	 	 	 // Prepend the object prefix to the table name
2081  	 	 	 // prepend after quote if used
2082  	 	 	 return preg_replace( '/^(`?)(.+)$/', '$1' . $this->objectPrefix . '$2', $name );
2083  	 	 }
2084  
2085  	 	 // No prefix set. Use name provided.
2086  	 	 return $name;
2087  	 }
2088  
2089  	 /**
2090  	 * Checks if element references a specific platform
2091  	 *
2092  	 * @param string $platform Requested platform
2093  	 * @returns boolean TRUE if platform check succeeds
2094  	 *
2095  	 * @access private
2096  	 */
2097  	function supportedPlatform( $platform = NULL ) {
2098  	 	 $regex = '/^(\w*\|)*' . $this->db->databaseType . '(\|\w*)*$/';
2099  
2100  	 	 if( !isset( $platform ) OR preg_match( $regex, $platform ) ) {
2101  	 	 	 logMsg( "Platform $platform is supported" );
2102  	 	 	 return TRUE;
2103  	 	 } else {
2104  	 	 	 logMsg( "Platform $platform is NOT supported" );
2105  	 	 	 return FALSE;
2106  	 	 }
2107  	 }
2108  
2109  	 /**
2110  	 * Clears the array of generated SQL.
2111  	 *
2112  	 * @access private
2113  	 */
2114  	function clearSQL() {
2115  	 	 $this->sqlArray = array();
2116  	 }
2117  
2118  	 /**
2119  	 * Adds SQL into the SQL array.
2120  	 *
2121  	 * @param mixed $sql SQL to Add
2122  	 * @return boolean TRUE if successful, else FALSE.
2123  	 *
2124  	 * @access private
2125  	 */
2126  	function addSQL( $sql = NULL ) {
2127  	 	 if( is_array( $sql ) ) {
2128  	 	 	 foreach( $sql as $line ) {
2129  	 	 	 	 $this->addSQL( $line );
2130  	 	 	 }
2131  
2132  	 	 	 return TRUE;
2133  	 	 }
2134  
2135  	 	 if( is_string( $sql ) ) {
2136  	 	 	 $this->sqlArray[] = $sql;
2137  
2138  	 	 	 // if executeInline is enabled, and either no errors have occurred or continueOnError is enabled, execute SQL.
2139  	 	 	 if( $this->ExecuteInline() && ( $this->success == 2 || $this->ContinueOnError() ) ) {
2140  	 	 	 	 $saved = $this->db->debug;
2141  	 	 	 	 $this->db->debug = $this->debug;
2142  	 	 	 	 $ok = $this->db->Execute( $sql );
2143  	 	 	 	 $this->db->debug = $saved;
2144  
2145  	 	 	 	 if( !$ok ) {
2146  	 	 	 	 	 if( $this->debug ) {
2147  	 	 	 	 	 	 ADOConnection::outp( $this->db->ErrorMsg() );
2148  	 	 	 	 	 }
2149  
2150  	 	 	 	 	 $this->success = 1;
2151  	 	 	 	 }
2152  	 	 	 }
2153  
2154  	 	 	 return TRUE;
2155  	 	 }
2156  
2157  	 	 return FALSE;
2158  	 }
2159  
2160  	 /**
2161  	 * Gets the SQL array in the specified format.
2162  	 *
2163  	 * @param string $format Format
2164  	 * @return mixed SQL
2165  	 *
2166  	 * @access private
2167  	 */
2168  	function getSQL( $format = NULL, $sqlArray = NULL ) {
2169  	 	 if( !is_array( $sqlArray ) ) {
2170  	 	 	 $sqlArray = $this->sqlArray;
2171  	 	 }
2172  
2173  	 	 if( !is_array( $sqlArray ) ) {
2174  	 	 	 return FALSE;
2175  	 	 }
2176  
2177  	 	 switch( strtolower( $format ) ) {
2178  	 	 	 case 'string':
2179  	 	 	 case 'text':
2180  	 	 	 	 return !empty( $sqlArray ) ? implode( ";\n\n", $sqlArray ) . ';' : '';
2181  	 	 	 case'html':
2182  	 	 	 	 return !empty( $sqlArray ) ? nl2br( htmlentities( implode( ";\n\n", $sqlArray ) . ';' ) ) : '';
2183  	 	 }
2184  
2185  	 	 return $this->sqlArray;
2186  	 }
2187  
2188  	 /**
2189  	 * Destroys an adoSchema object.
2190  	 *
2191  	 * Call this method to clean up after an adoSchema object that is no longer in use.
2192  	 * @deprecated adoSchema now cleans up automatically.
2193  	 */
2194  	function Destroy() {}
2195  }
2196  
2197  /**
2198  * Message logging function
2199  *
2200  * @access private
2201  */
2202  function logMsg( $msg, $title = NULL, $force = FALSE ) {
2203  	 if( XMLS_DEBUG or $force ) {
2204  	 	 echo '<pre>';
2205  
2206  	 	 if( isset( $title ) ) {
2207  	 	 	 echo '<h3>' . htmlentities( $title ) . '</h3>';
2208  	 	 }
2209  
2210  	 	 if( is_object( $this ) ) {
2211  	 	 	 echo '[' . get_class( $this ) . '] ';
2212  	 	 }
2213  
2214  	 	 print_r( $msg );
2215  
2216  	 	 echo '</pre>';
2217  	 }
2218  }