Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

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