Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

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