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