2 // Copyright (c) 2004 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 *******************************************************************************/
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
13 * Last Editor: $Author: jlim $
14 * @author Richard Tango-Lowy & Dan Cech
15 * @version $Revision: 1.12 $
18 * @tutorial getting_started.pkg
21 function _file_get_contents($file)
23 if (function_exists('file_get_contents')) return file_get_contents($file);
25 $f = fopen($file,'r');
29 while ($s = fread($f,100000)) $t .= $s;
38 if( !defined( 'XMLS_DEBUG' ) ) {
39 define( 'XMLS_DEBUG', FALSE );
45 if( !defined( 'XMLS_PREFIX' ) ) {
46 define( 'XMLS_PREFIX', '%%P' );
50 * Maximum length allowed for object prefix
52 if( !defined( 'XMLS_PREFIX_MAXLEN' ) ) {
53 define( 'XMLS_PREFIX_MAXLEN', 10 );
57 * Execute SQL inline as it is generated
59 if( !defined( 'XMLS_EXECUTE_INLINE' ) ) {
60 define( 'XMLS_EXECUTE_INLINE', FALSE );
64 * Continue SQL Execution if an error occurs?
66 if( !defined( 'XMLS_CONTINUE_ON_ERROR' ) ) {
67 define( 'XMLS_CONTINUE_ON_ERROR', FALSE );
71 * Current Schema Version
73 if( !defined( 'XMLS_SCHEMA_VERSION' ) ) {
74 define( 'XMLS_SCHEMA_VERSION', '0.2' );
78 * Default Schema Version. Used for Schemas without an explicit version set.
80 if( !defined( 'XMLS_DEFAULT_SCHEMA_VERSION' ) ) {
81 define( 'XMLS_DEFAULT_SCHEMA_VERSION', '0.1' );
85 * Default Schema Version. Used for Schemas without an explicit version set.
87 if( !defined( 'XMLS_DEFAULT_UPGRADE_METHOD' ) ) {
88 define( 'XMLS_DEFAULT_UPGRADE_METHOD', 'ALTER' );
92 * Include the main ADODB library
94 if( !defined( '_ADODB_LAYER' ) ) {
95 require( 'adodb.inc.php' );
96 require( 'adodb-datadict.inc.php' );
100 * Abstract DB Object. This class provides basic methods for database objects, such
101 * as tables and indexes.
114 * var string current element
121 function dbObject( &$parent, $attributes = NULL ) {
122 $this->parent
= $parent;
126 * XML Callback to process start elements
130 function _tag_open( &$parser, $tag, $attributes ) {
135 * XML Callback to process CDATA elements
139 function _tag_cdata( &$parser, $cdata ) {
144 * XML Callback to process end elements
148 function _tag_close( &$parser, $tag ) {
152 function create(&$xmls) {
157 * Destroys the object
164 * Checks whether the specified RDBMS is supported by the current
165 * database object or its ranking ancestor.
167 * @param string $platform RDBMS platform name (from ADODB platform list).
168 * @return boolean TRUE if RDBMS is supported; otherwise returns FALSE.
170 function supportedPlatform( $platform = NULL ) {
171 return is_object( $this->parent
) ?
$this->parent
->supportedPlatform( $platform ) : TRUE;
175 * Returns the prefix set by the ranking ancestor of the database object.
177 * @param string $name Prefix string.
178 * @return string Prefix.
180 function prefix( $name = '' ) {
181 return is_object( $this->parent
) ?
$this->parent
->prefix( $name ) : $name;
185 * Extracts a field ID from the specified field.
187 * @param string $field Field.
188 * @return string Field ID.
190 function FieldID( $field ) {
191 return strtoupper( preg_replace( '/^`(.+)`$/', '$1', $field ) );
196 * Creates a table object in ADOdb's datadict format
198 * This class stores information about a database table. As charactaristics
199 * of the table are loaded from the external source, methods and properties
200 * of this class are used to build up the table description in ADOdb's
206 class dbTable
extends dbObject
{
209 * @var string Table name
214 * @var array Field specifier: Meta-information about each field
216 var $fields = array();
219 * @var array List of table indexes.
221 var $indexes = array();
224 * @var array Table options: Table-level options
229 * @var string Field index: Keeps track of which field is currently being processed
234 * @var boolean Mark table for destruction
240 * @var boolean Mark field for destruction (not yet implemented)
243 var $drop_field = array();
246 * Iniitializes a new table object.
248 * @param string $prefix DB Object prefix
249 * @param array $attributes Array of table attributes.
251 function dbTable( &$parent, $attributes = NULL ) {
252 $this->parent
= $parent;
253 $this->name
= $this->prefix($attributes['NAME']);
257 * XML Callback to process start elements. Elements currently
258 * processed are: INDEX, DROP, FIELD, KEY, NOTNULL, AUTOINCREMENT & DEFAULT.
262 function _tag_open( &$parser, $tag, $attributes ) {
263 $this->currentElement
= strtoupper( $tag );
265 switch( $this->currentElement
) {
267 if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
268 xml_set_object( $parser, $this->addIndex( $attributes ) );
272 if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
273 xml_set_object( $parser, $this->addData( $attributes ) );
281 $fieldName = $attributes['NAME'];
282 $fieldType = $attributes['TYPE'];
283 $fieldSize = isset( $attributes['SIZE'] ) ?
$attributes['SIZE'] : NULL;
284 $fieldOpts = isset( $attributes['OPTS'] ) ?
$attributes['OPTS'] : NULL;
286 $this->addField( $fieldName, $fieldType, $fieldSize, $fieldOpts );
290 case 'AUTOINCREMENT':
291 // Add a field option
292 $this->addFieldOpt( $this->current_field
, $this->currentElement
);
295 // Add a field option to the table object
297 // Work around ADOdb datadict issue that misinterprets empty strings.
298 if( $attributes['VALUE'] == '' ) {
299 $attributes['VALUE'] = " '' ";
302 $this->addFieldOpt( $this->current_field
, $this->currentElement
, $attributes['VALUE'] );
306 // Add a field option to the table object
307 $this->addFieldOpt( $this->current_field
, $this->currentElement
);
310 // print_r( array( $tag, $attributes ) );
315 * XML Callback to process CDATA elements
319 function _tag_cdata( &$parser, $cdata ) {
320 switch( $this->currentElement
) {
323 if( isset( $this->current_field
) ) {
324 $this->addFieldOpt( $this->current_field
, $this->currentElement
, $cdata );
326 $this->addTableOpt( $cdata );
331 $this->addTableOpt( $cdata );
339 * XML Callback to process end elements
343 function _tag_close( &$parser, $tag ) {
344 $this->currentElement
= '';
346 switch( strtoupper( $tag ) ) {
348 $this->parent
->addSQL( $this->create( $this->parent
) );
349 xml_set_object( $parser, $this->parent
);
353 unset($this->current_field
);
360 * Adds an index to a table object
362 * @param array $attributes Index attributes
363 * @return object dbIndex object
365 function addIndex( $attributes ) {
366 $name = strtoupper( $attributes['NAME'] );
367 $this->indexes
[$name] = new dbIndex( $this, $attributes );
368 return $this->indexes
[$name];
372 * Adds data to a table object
374 * @param array $attributes Data attributes
375 * @return object dbData object
377 function addData( $attributes ) {
378 if( !isset( $this->data
) ) {
379 $this->data
= new dbData( $this, $attributes );
385 * Adds a field to a table object
387 * $name is the name of the table to which the field should be added.
388 * $type is an ADODB datadict field type. The following field types
389 * are supported as of ADODB 3.40:
391 * - X: CLOB (character large object) or largest varchar size
392 * if CLOB is not supported
393 * - C2: Multibyte varchar
394 * - X2: Multibyte CLOB
395 * - B: BLOB (binary large object)
396 * - D: Date (some databases do not support this, and we return a datetime type)
397 * - T: Datetime or Timestamp
398 * - L: Integer field suitable for storing booleans (0 or 1)
399 * - I: Integer (mapped to I4)
400 * - I1: 1-byte integer
401 * - I2: 2-byte integer
402 * - I4: 4-byte integer
403 * - I8: 8-byte integer
404 * - F: Floating point number
405 * - N: Numeric or decimal number
407 * @param string $name Name of the table to which the field will be added.
408 * @param string $type ADODB datadict field type.
409 * @param string $size Field size
410 * @param array $opts Field options array
411 * @return array Field specifier array
413 function addField( $name, $type, $size = NULL, $opts = NULL ) {
414 $field_id = $this->FieldID( $name );
416 // Set the field index so we know where we are
417 $this->current_field
= $field_id;
419 // Set the field name (required)
420 $this->fields
[$field_id]['NAME'] = $name;
422 // Set the field type (required)
423 $this->fields
[$field_id]['TYPE'] = $type;
425 // Set the field size (optional)
426 if( isset( $size ) ) {
427 $this->fields
[$field_id]['SIZE'] = $size;
430 // Set the field options
431 if( isset( $opts ) ) {
432 $this->fields
[$field_id]['OPTS'][] = $opts;
437 * Adds a field option to the current field specifier
439 * This method adds a field option allowed by the ADOdb datadict
440 * and appends it to the given field.
442 * @param string $field Field name
443 * @param string $opt ADOdb field option
444 * @param mixed $value Field option value
445 * @return array Field specifier array
447 function addFieldOpt( $field, $opt, $value = NULL ) {
448 if( !isset( $value ) ) {
449 $this->fields
[$this->FieldID( $field )]['OPTS'][] = $opt;
450 // Add the option and value
452 $this->fields
[$this->FieldID( $field )]['OPTS'][] = array( $opt => $value );
457 * Adds an option to the table
459 * This method takes a comma-separated list of table-level options
460 * and appends them to the table object.
462 * @param string $opt Table option
463 * @return array Options
465 function addTableOpt( $opt ) {
466 if(isset($this->currentPlatform
)) {
467 $this->opts
[$this->parent
->db
->databaseType
] = $opt;
474 * Generates the SQL that will create the table in the database
476 * @param object $xmls adoSchema object
477 * @return array Array containing table creation SQL
479 function create( &$xmls ) {
482 // drop any existing indexes
483 if( is_array( $legacy_indexes = $xmls->dict
->MetaIndexes( $this->name
) ) ) {
484 foreach( $legacy_indexes as $index => $index_details ) {
485 $sql[] = $xmls->dict
->DropIndexSQL( $index, $this->name
);
489 // remove fields to be dropped from table object
490 foreach( $this->drop_field
as $field ) {
491 unset( $this->fields
[$field] );
495 if( is_array( $legacy_fields = $xmls->dict
->MetaColumns( $this->name
) ) ) {
497 if( $this->drop_table
) {
498 $sql[] = $xmls->dict
->DropTableSQL( $this->name
);
503 // drop any existing fields not in schema
504 foreach( $legacy_fields as $field_id => $field ) {
505 if( !isset( $this->fields
[$field_id] ) ) {
506 $sql[] = $xmls->dict
->DropColumnSQL( $this->name
, '`'.$field->name
.'`' );
509 // if table doesn't exist
511 if( $this->drop_table
) {
515 $legacy_fields = array();
518 // Loop through the field specifier array, building the associative array for the field options
521 foreach( $this->fields
as $field_id => $finfo ) {
522 // Set an empty size if it isn't supplied
523 if( !isset( $finfo['SIZE'] ) ) {
527 // Initialize the field array with the type and size
528 $fldarray[$field_id] = array(
529 'NAME' => $finfo['NAME'],
530 'TYPE' => $finfo['TYPE'],
531 'SIZE' => $finfo['SIZE']
534 // Loop through the options array and add the field options.
535 if( isset( $finfo['OPTS'] ) ) {
536 foreach( $finfo['OPTS'] as $opt ) {
537 // Option has an argument.
538 if( is_array( $opt ) ) {
540 $value = $opt[key( $opt )];
541 @$fldarray[$field_id][$key] .= $value;
542 // Option doesn't have arguments
544 $fldarray[$field_id][$opt] = $opt;
550 if( empty( $legacy_fields ) ) {
551 // Create the new table
552 $sql[] = $xmls->dict
->CreateTableSQL( $this->name
, $fldarray, $this->opts
);
553 logMsg( end( $sql ), 'Generated CreateTableSQL' );
555 // Upgrade an existing table
556 logMsg( "Upgrading {$this->name} using '{$xmls->upgrade}'" );
557 switch( $xmls->upgrade
) {
558 // Use ChangeTableSQL
560 logMsg( 'Generated ChangeTableSQL (ALTERing table)' );
561 $sql[] = $xmls->dict
->ChangeTableSQL( $this->name
, $fldarray, $this->opts
);
564 logMsg( 'Doing upgrade REPLACE (testing)' );
565 $sql[] = $xmls->dict
->DropTableSQL( $this->name
);
566 $sql[] = $xmls->dict
->CreateTableSQL( $this->name
, $fldarray, $this->opts
);
574 foreach( $this->indexes
as $index ) {
575 $sql[] = $index->create( $xmls );
578 if( isset( $this->data
) ) {
579 $sql[] = $this->data
->create( $xmls );
586 * Marks a field or table for destruction
589 if( isset( $this->current_field
) ) {
590 // Drop the current field
591 logMsg( "Dropping field '{$this->current_field}' from table '{$this->name}'" );
592 // $this->drop_field[$this->current_field] = $xmls->dict->DropColumnSQL( $this->name, $this->current_field );
593 $this->drop_field
[$this->current_field
] = $this->current_field
;
595 // Drop the current table
596 logMsg( "Dropping table '{$this->name}'" );
597 // $this->drop_table = $xmls->dict->DropTableSQL( $this->name );
598 $this->drop_table
= TRUE;
604 * Creates an index object in ADOdb's datadict format
606 * This class stores information about a database index. As charactaristics
607 * of the index are loaded from the external source, methods and properties
608 * of this class are used to build up the index description in ADOdb's
614 class dbIndex
extends dbObject
{
617 * @var string Index name
622 * @var array Index options: Index-level options
627 * @var array Indexed fields: Table columns included in this index
629 var $columns = array();
632 * @var boolean Mark index for destruction
638 * Initializes the new dbIndex object.
640 * @param object $parent Parent object
641 * @param array $attributes Attributes
645 function dbIndex( &$parent, $attributes = NULL ) {
646 $this->parent
= $parent;
648 $this->name
= $this->prefix ($attributes['NAME']);
652 * XML Callback to process start elements
654 * Processes XML opening tags.
655 * Elements currently processed are: DROP, CLUSTERED, BITMAP, UNIQUE, FULLTEXT & HASH.
659 function _tag_open( &$parser, $tag, $attributes ) {
660 $this->currentElement
= strtoupper( $tag );
662 switch( $this->currentElement
) {
672 $this->addIndexOpt( $this->currentElement
);
675 // print_r( array( $tag, $attributes ) );
680 * XML Callback to process CDATA elements
682 * Processes XML cdata.
686 function _tag_cdata( &$parser, $cdata ) {
687 switch( $this->currentElement
) {
690 $this->addField( $cdata );
698 * XML Callback to process end elements
702 function _tag_close( &$parser, $tag ) {
703 $this->currentElement
= '';
705 switch( strtoupper( $tag ) ) {
707 xml_set_object( $parser, $this->parent
);
713 * Adds a field to the index
715 * @param string $name Field name
716 * @return string Field list
718 function addField( $name ) {
719 $this->columns
[$this->FieldID( $name )] = $name;
721 // Return the field list
722 return $this->columns
;
726 * Adds options to the index
728 * @param string $opt Comma-separated list of index options.
729 * @return string Option list
731 function addIndexOpt( $opt ) {
732 $this->opts
[] = $opt;
734 // Return the options list
739 * Generates the SQL that will create the index in the database
741 * @param object $xmls adoSchema object
742 * @return array Array containing index creation SQL
744 function create( &$xmls ) {
749 // eliminate any columns that aren't in the table
750 foreach( $this->columns
as $id => $col ) {
751 if( !isset( $this->parent
->fields
[$id] ) ) {
752 unset( $this->columns
[$id] );
756 return $xmls->dict
->CreateIndexSQL( $this->name
, $this->parent
->name
, $this->columns
, $this->opts
);
760 * Marks an index for destruction
768 * Creates a data object in ADOdb's datadict format
770 * This class stores information about table data.
775 class dbData
extends dbObject
{
782 * Initializes the new dbIndex object.
784 * @param object $parent Parent object
785 * @param array $attributes Attributes
789 function dbData( &$parent, $attributes = NULL ) {
790 $this->parent
= $parent;
794 * XML Callback to process start elements
796 * Processes XML opening tags.
797 * Elements currently processed are: DROP, CLUSTERED, BITMAP, UNIQUE, FULLTEXT & HASH.
801 function _tag_open( &$parser, $tag, $attributes ) {
802 $this->currentElement
= strtoupper( $tag );
804 switch( $this->currentElement
) {
806 $this->row
= count( $this->data
);
807 $this->data
[$this->row
] = array();
810 $this->addField($attributes);
812 // print_r( array( $tag, $attributes ) );
817 * XML Callback to process CDATA elements
819 * Processes XML cdata.
823 function _tag_cdata( &$parser, $cdata ) {
824 switch( $this->currentElement
) {
827 $this->addData( $cdata );
835 * XML Callback to process end elements
839 function _tag_close( &$parser, $tag ) {
840 $this->currentElement
= '';
842 switch( strtoupper( $tag ) ) {
844 xml_set_object( $parser, $this->parent
);
850 * Adds a field to the index
852 * @param string $name Field name
853 * @return string Field list
855 function addField( $attributes ) {
856 if( isset( $attributes['NAME'] ) ) {
857 $name = $attributes['NAME'];
859 $name = count($this->data
[$this->row
]);
862 // Set the field index so we know where we are
863 $this->current_field
= $this->FieldID( $name );
867 * Adds options to the index
869 * @param string $opt Comma-separated list of index options.
870 * @return string Option list
872 function addData( $cdata ) {
873 if( !isset( $this->data
[$this->row
] ) ) {
874 $this->data
[$this->row
] = array();
877 if( !isset( $this->data
[$this->row
][$this->current_field
] ) ) {
878 $this->data
[$this->row
][$this->current_field
] = '';
881 $this->data
[$this->row
][$this->current_field
] .= $cdata;
885 * Generates the SQL that will create the index in the database
887 * @param object $xmls adoSchema object
888 * @return array Array containing index creation SQL
890 function create( &$xmls ) {
891 $table = $xmls->dict
->TableName($this->parent
->name
);
892 $table_field_count = count($this->parent
->fields
);
895 // eliminate any columns that aren't in the table
896 foreach( $this->data
as $row ) {
897 $table_fields = $this->parent
->fields
;
900 foreach( $row as $field_id => $field_data ) {
901 if( !array_key_exists( $field_id, $table_fields ) ) {
902 if( is_numeric( $field_id ) ) {
903 $field_id = reset( array_keys( $table_fields ) );
909 $name = $table_fields[$field_id]['NAME'];
911 switch( $table_fields[$field_id]['TYPE'] ) {
916 $fields[$name] = $xmls->db
->qstr( $field_data );
923 $fields[$name] = intval($field_data);
926 $fields[$name] = $field_data;
929 unset($table_fields[$field_id]);
932 // check that at least 1 column is specified
933 if( empty( $fields ) ) {
937 // check that no required columns are missing
938 if( count( $fields ) < $table_field_count ) {
939 foreach( $table_fields as $field ) {
940 if (isset( $field['OPTS'] ))
941 if( ( in_array( 'NOTNULL', $field['OPTS'] ) ||
in_array( 'KEY', $field['OPTS'] ) ) && !in_array( 'AUTOINCREMENT', $field['OPTS'] ) ) {
947 $sql[] = 'INSERT INTO '. $table .' ('. implode( ',', array_keys( $fields ) ) .') VALUES ('. implode( ',', $fields ) .')';
955 * Creates the SQL to execute a list of provided SQL queries
960 class dbQuerySet
extends dbObject
{
963 * @var array List of SQL queries
965 var $queries = array();
968 * @var string String used to build of a query line by line
973 * @var string Query prefix key
978 * @var boolean Auto prefix enable (TRUE)
980 var $prefixMethod = 'AUTO';
983 * Initializes the query set.
985 * @param object $parent Parent object
986 * @param array $attributes Attributes
988 function dbQuerySet( &$parent, $attributes = NULL ) {
989 $this->parent
= $parent;
991 // Overrides the manual prefix key
992 if( isset( $attributes['KEY'] ) ) {
993 $this->prefixKey
= $attributes['KEY'];
996 $prefixMethod = isset( $attributes['PREFIXMETHOD'] ) ?
strtoupper( trim( $attributes['PREFIXMETHOD'] ) ) : '';
998 // Enables or disables automatic prefix prepending
999 switch( $prefixMethod ) {
1001 $this->prefixMethod
= 'AUTO';
1004 $this->prefixMethod
= 'MANUAL';
1007 $this->prefixMethod
= 'NONE';
1013 * XML Callback to process start elements. Elements currently
1014 * processed are: QUERY.
1018 function _tag_open( &$parser, $tag, $attributes ) {
1019 $this->currentElement
= strtoupper( $tag );
1021 switch( $this->currentElement
) {
1023 // Create a new query in a SQL queryset.
1024 // Ignore this query set if a platform is specified and it's different than the
1025 // current connection platform.
1026 if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
1029 $this->discardQuery();
1033 // print_r( array( $tag, $attributes ) );
1038 * XML Callback to process CDATA elements
1040 function _tag_cdata( &$parser, $cdata ) {
1041 switch( $this->currentElement
) {
1042 // Line of queryset SQL data
1044 $this->buildQuery( $cdata );
1052 * XML Callback to process end elements
1056 function _tag_close( &$parser, $tag ) {
1057 $this->currentElement
= '';
1059 switch( strtoupper( $tag ) ) {
1061 // Add the finished query to the open query set.
1065 $this->parent
->addSQL( $this->create( $this->parent
) );
1066 xml_set_object( $parser, $this->parent
);
1075 * Re-initializes the query.
1077 * @return boolean TRUE
1079 function newQuery() {
1086 * Discards the existing query.
1088 * @return boolean TRUE
1090 function discardQuery() {
1091 unset( $this->query
);
1097 * Appends a line to a query that is being built line by line
1099 * @param string $data Line of SQL data or NULL to initialize a new query
1100 * @return string SQL query string.
1102 function buildQuery( $sql = NULL ) {
1103 if( !isset( $this->query
) OR empty( $sql ) ) {
1107 $this->query
.= $sql;
1109 return $this->query
;
1113 * Adds a completed query to the query list
1115 * @return string SQL of added query
1117 function addQuery() {
1118 if( !isset( $this->query
) ) {
1122 $this->queries
[] = $return = trim($this->query
);
1124 unset( $this->query
);
1130 * Creates and returns the current query set
1132 * @param object $xmls adoSchema object
1133 * @return array Query set
1135 function create( &$xmls ) {
1136 foreach( $this->queries
as $id => $query ) {
1137 switch( $this->prefixMethod
) {
1139 // Enable auto prefix replacement
1141 // Process object prefix.
1142 // Evaluate SQL statements to prepend prefix to objects
1143 $query = $this->prefixQuery( '/^\s*((?is)INSERT\s+(INTO\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix
);
1144 $query = $this->prefixQuery( '/^\s*((?is)UPDATE\s+(FROM\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix
);
1145 $query = $this->prefixQuery( '/^\s*((?is)DELETE\s+(FROM\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix
);
1147 // SELECT statements aren't working yet
1148 #$data = preg_replace( '/(?ias)(^\s*SELECT\s+.*\s+FROM)\s+(\W\s*,?\s*)+((?i)\s+WHERE.*$)/', "\1 $prefix\2 \3", $data );
1151 // If prefixKey is set and has a value then we use it to override the default constant XMLS_PREFIX.
1152 // If prefixKey is not set, we use the default constant XMLS_PREFIX
1153 if( isset( $this->prefixKey
) AND( $this->prefixKey
!== '' ) ) {
1154 // Enable prefix override
1155 $query = str_replace( $this->prefixKey
, $xmls->objectPrefix
, $query );
1157 // Use default replacement
1158 $query = str_replace( XMLS_PREFIX
, $xmls->objectPrefix
, $query );
1162 $this->queries
[$id] = trim( $query );
1165 // Return the query set array
1166 return $this->queries
;
1170 * Rebuilds the query with the prefix attached to any objects
1172 * @param string $regex Regex used to add prefix
1173 * @param string $query SQL query string
1174 * @param string $prefix Prefix to be appended to tables, indices, etc.
1175 * @return string Prefixed SQL query string.
1177 function prefixQuery( $regex, $query, $prefix = NULL ) {
1178 if( !isset( $prefix ) ) {
1182 if( preg_match( $regex, $query, $match ) ) {
1183 $preamble = $match[1];
1184 $postamble = $match[5];
1185 $objectList = explode( ',', $match[3] );
1186 // $prefix = $prefix . '_';
1190 foreach( $objectList as $object ) {
1191 if( $prefixedList !== '' ) {
1192 $prefixedList .= ', ';
1195 $prefixedList .= $prefix . trim( $object );
1198 $query = $preamble . ' ' . $prefixedList . ' ' . $postamble;
1206 * Loads and parses an XML file, creating an array of "ready-to-run" SQL statements
1208 * This class is used to load and parse the XML file, to create an array of SQL statements
1209 * that can be used to build a database, and to build the database using the SQL array.
1211 * @tutorial getting_started.pkg
1213 * @author Richard Tango-Lowy & Dan Cech
1214 * @version $Revision: 1.12 $
1221 * @var array Array containing SQL queries to generate all objects
1227 * @var object ADOdb connection object
1233 * @var object ADOdb Data Dictionary
1239 * @var string Current XML element
1242 var $currentElement = '';
1245 * @var string If set (to 'ALTER' or 'REPLACE'), upgrade an existing database
1251 * @var string Optional object prefix
1254 var $objectPrefix = '';
1257 * @var long Original Magic Quotes Runtime value
1263 * @var long System debug
1269 * @var string Regular expression to find schema version
1272 var $versionRegex = '/<schema.*?( version="([^"]*)")?.*?>/';
1275 * @var string Current schema version
1281 * @var int Success of last Schema execution
1286 * @var bool Execute SQL inline as it is generated
1291 * @var bool Continue SQL execution if errors occur
1293 var $continueOnError;
1296 * Creates an adoSchema object
1298 * Creating an adoSchema object is the first step in processing an XML schema.
1299 * The only parameter is an ADOdb database connection object, which must already
1300 * have been created.
1302 * @param object $db ADOdb database connection object.
1304 function adoSchema( $db ) {
1305 // Initialize the environment
1306 $this->mgq
= get_magic_quotes_runtime();
1307 ini_set("magic_quotes_runtime", 0);
1308 #set_magic_quotes_runtime(0);
1311 $this->debug
= $this->db
->debug
;
1312 $this->dict
= NewDataDictionary( $this->db
);
1313 $this->sqlArray
= array();
1314 $this->schemaVersion
= XMLS_SCHEMA_VERSION
;
1315 $this->executeInline( XMLS_EXECUTE_INLINE
);
1316 $this->continueOnError( XMLS_CONTINUE_ON_ERROR
);
1317 $this->setUpgradeMethod();
1321 * Sets the method to be used for upgrading an existing database
1323 * Use this method to specify how existing database objects should be upgraded.
1324 * The method option can be set to ALTER, REPLACE, BEST, or NONE. ALTER attempts to
1325 * alter each database object directly, REPLACE attempts to rebuild each object
1326 * from scratch, BEST attempts to determine the best upgrade method for each
1327 * object, and NONE disables upgrading.
1329 * This method is not yet used by AXMLS, but exists for backward compatibility.
1330 * The ALTER method is automatically assumed when the adoSchema object is
1331 * instantiated; other upgrade methods are not currently supported.
1333 * @param string $method Upgrade method (ALTER|REPLACE|BEST|NONE)
1334 * @returns string Upgrade method used
1336 function SetUpgradeMethod( $method = '' ) {
1337 if( !is_string( $method ) ) {
1341 $method = strtoupper( $method );
1343 // Handle the upgrade methods
1346 $this->upgrade
= $method;
1349 $this->upgrade
= $method;
1352 $this->upgrade
= 'ALTER';
1355 $this->upgrade
= 'NONE';
1358 // Use default if no legitimate method is passed.
1359 $this->upgrade
= XMLS_DEFAULT_UPGRADE_METHOD
;
1362 return $this->upgrade
;
1366 * Enables/disables inline SQL execution.
1368 * Call this method to enable or disable inline execution of the schema. If the mode is set to TRUE (inline execution),
1369 * AXMLS applies the SQL to the database immediately as each schema entity is parsed. If the mode
1370 * is set to FALSE (post execution), AXMLS parses the entire schema and you will need to call adoSchema::ExecuteSchema()
1371 * to apply the schema to the database.
1373 * @param bool $mode execute
1374 * @return bool current execution mode
1376 * @see ParseSchema(), ExecuteSchema()
1378 function ExecuteInline( $mode = NULL ) {
1379 if( is_bool( $mode ) ) {
1380 $this->executeInline
= $mode;
1383 return $this->executeInline
;
1387 * Enables/disables SQL continue on error.
1389 * Call this method to enable or disable continuation of SQL execution if an error occurs.
1390 * If the mode is set to TRUE (continue), AXMLS will continue to apply SQL to the database, even if an error occurs.
1391 * If the mode is set to FALSE (halt), AXMLS will halt execution of generated sql if an error occurs, though parsing
1392 * of the schema will continue.
1394 * @param bool $mode execute
1395 * @return bool current continueOnError mode
1397 * @see addSQL(), ExecuteSchema()
1399 function ContinueOnError( $mode = NULL ) {
1400 if( is_bool( $mode ) ) {
1401 $this->continueOnError
= $mode;
1404 return $this->continueOnError
;
1408 * Loads an XML schema from a file and converts it to SQL.
1410 * Call this method to load the specified schema (see the DTD for the proper format) from
1411 * the filesystem and generate the SQL necessary to create the database described.
1412 * @see ParseSchemaString()
1414 * @param string $file Name of XML schema file.
1415 * @param bool $returnSchema Return schema rather than parsing.
1416 * @return array Array of SQL queries, ready to execute
1418 function ParseSchema( $filename, $returnSchema = FALSE ) {
1419 return $this->ParseSchemaString( $this->ConvertSchemaFile( $filename ), $returnSchema );
1423 * Loads an XML schema from a file and converts it to SQL.
1425 * Call this method to load the specified schema from a file (see the DTD for the proper format)
1426 * and generate the SQL necessary to create the database described by the schema.
1428 * @param string $file Name of XML schema file.
1429 * @param bool $returnSchema Return schema rather than parsing.
1430 * @return array Array of SQL queries, ready to execute.
1432 * @deprecated Replaced by adoSchema::ParseSchema() and adoSchema::ParseSchemaString()
1433 * @see ParseSchema(), ParseSchemaString()
1435 function ParseSchemaFile( $filename, $returnSchema = FALSE ) {
1437 if( !($fp = fopen( $filename, 'r' )) ) {
1438 // die( 'Unable to open file' );
1442 // do version detection here
1443 if( $this->SchemaFileVersion( $filename ) != $this->schemaVersion
) {
1447 if ( $returnSchema )
1450 while( $data = fread( $fp, 100000 ) ) {
1451 $xmlstring .= $data;
1458 $xmlParser = $this->create_parser();
1461 while( $data = fread( $fp, 4096 ) ) {
1462 if( !xml_parse( $xmlParser, $data, feof( $fp ) ) ) {
1464 "XML error: %s at line %d",
1465 xml_error_string( xml_get_error_code( $xmlParser) ),
1466 xml_get_current_line_number( $xmlParser)
1471 xml_parser_free( $xmlParser );
1473 return $this->sqlArray
;
1477 * Converts an XML schema string to SQL.
1479 * Call this method to parse a string containing an XML schema (see the DTD for the proper format)
1480 * and generate the SQL necessary to create the database described by the schema.
1481 * @see ParseSchema()
1483 * @param string $xmlstring XML schema string.
1484 * @param bool $returnSchema Return schema rather than parsing.
1485 * @return array Array of SQL queries, ready to execute.
1487 function ParseSchemaString( $xmlstring, $returnSchema = FALSE ) {
1488 if( !is_string( $xmlstring ) OR empty( $xmlstring ) ) {
1492 // do version detection here
1493 if( $this->SchemaStringVersion( $xmlstring ) != $this->schemaVersion
) {
1497 if ( $returnSchema )
1504 $xmlParser = $this->create_parser();
1506 if( !xml_parse( $xmlParser, $xmlstring, TRUE ) ) {
1508 "XML error: %s at line %d",
1509 xml_error_string( xml_get_error_code( $xmlParser) ),
1510 xml_get_current_line_number( $xmlParser)
1514 xml_parser_free( $xmlParser );
1516 return $this->sqlArray
;
1520 * Loads an XML schema from a file and converts it to uninstallation SQL.
1522 * Call this method to load the specified schema (see the DTD for the proper format) from
1523 * the filesystem and generate the SQL necessary to remove the database described.
1524 * @see RemoveSchemaString()
1526 * @param string $file Name of XML schema file.
1527 * @param bool $returnSchema Return schema rather than parsing.
1528 * @return array Array of SQL queries, ready to execute
1530 function RemoveSchema( $filename, $returnSchema = FALSE ) {
1531 return $this->RemoveSchemaString( $this->ConvertSchemaFile( $filename ), $returnSchema );
1535 * Converts an XML schema string to uninstallation SQL.
1537 * Call this method to parse a string containing an XML schema (see the DTD for the proper format)
1538 * and generate the SQL necessary to uninstall the database described by the schema.
1539 * @see RemoveSchema()
1541 * @param string $schema XML schema string.
1542 * @param bool $returnSchema Return schema rather than parsing.
1543 * @return array Array of SQL queries, ready to execute.
1545 function RemoveSchemaString( $schema, $returnSchema = FALSE ) {
1547 // grab current version
1548 if( !( $version = $this->SchemaStringVersion( $schema ) ) ) {
1552 return $this->ParseSchemaString( $this->TransformSchema( $schema, 'remove-' . $version), $returnSchema );
1556 * Applies the current XML schema to the database (post execution).
1558 * Call this method to apply the current schema (generally created by calling
1559 * ParseSchema() or ParseSchemaString() ) to the database (creating the tables, indexes,
1560 * and executing other SQL specified in the schema) after parsing.
1561 * @see ParseSchema(), ParseSchemaString(), ExecuteInline()
1563 * @param array $sqlArray Array of SQL statements that will be applied rather than
1564 * the current schema.
1565 * @param boolean $continueOnErr Continue to apply the schema even if an error occurs.
1566 * @returns integer 0 if failure, 1 if errors, 2 if successful.
1568 function ExecuteSchema( $sqlArray = NULL, $continueOnErr = NULL ) {
1569 if( !is_bool( $continueOnErr ) ) {
1570 $continueOnErr = $this->ContinueOnError();
1573 if( !isset( $sqlArray ) ) {
1574 $sqlArray = $this->sqlArray
;
1577 if( !is_array( $sqlArray ) ) {
1580 $this->success
= $this->dict
->ExecuteSQLArray( $sqlArray, $continueOnErr );
1583 return $this->success
;
1587 * Returns the current SQL array.
1589 * Call this method to fetch the array of SQL queries resulting from
1590 * ParseSchema() or ParseSchemaString().
1592 * @param string $format Format: HTML, TEXT, or NONE (PHP array)
1593 * @return array Array of SQL statements or FALSE if an error occurs
1595 function PrintSQL( $format = 'NONE' ) {
1597 return $this->getSQL( $format, $sqlArray );
1601 * Saves the current SQL array to the local filesystem as a list of SQL queries.
1603 * Call this method to save the array of SQL queries (generally resulting from a
1604 * parsed XML schema) to the filesystem.
1606 * @param string $filename Path and name where the file should be saved.
1607 * @return boolean TRUE if save is successful, else FALSE.
1609 function SaveSQL( $filename = './schema.sql' ) {
1611 if( !isset( $sqlArray ) ) {
1612 $sqlArray = $this->sqlArray
;
1614 if( !isset( $sqlArray ) ) {
1618 $fp = fopen( $filename, "w" );
1620 foreach( $sqlArray as $key => $query ) {
1621 fwrite( $fp, $query . ";\n" );
1627 * Create an xml parser
1629 * @return object PHP XML parser object
1633 function create_parser() {
1634 // Create the parser
1635 $xmlParser = xml_parser_create();
1636 xml_set_object( $xmlParser, $this );
1638 // Initialize the XML callback functions
1639 xml_set_element_handler( $xmlParser, '_tag_open', '_tag_close' );
1640 xml_set_character_data_handler( $xmlParser, '_tag_cdata' );
1646 * XML Callback to process start elements
1650 function _tag_open( &$parser, $tag, $attributes ) {
1651 switch( strtoupper( $tag ) ) {
1653 $this->obj
= new dbTable( $this, $attributes );
1654 xml_set_object( $parser, $this->obj
);
1657 if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) {
1658 $this->obj
= new dbQuerySet( $this, $attributes );
1659 xml_set_object( $parser, $this->obj
);
1663 // print_r( array( $tag, $attributes ) );
1669 * XML Callback to process CDATA elements
1673 function _tag_cdata( &$parser, $cdata ) {
1677 * XML Callback to process end elements
1682 function _tag_close( &$parser, $tag ) {
1687 * Converts an XML schema string to the specified DTD version.
1689 * Call this method to convert a string containing an XML schema to a different AXMLS
1690 * DTD version. For instance, to convert a schema created for an pre-1.0 version for
1691 * AXMLS (DTD version 0.1) to a newer version of the DTD (e.g. 0.2). If no DTD version
1692 * parameter is specified, the schema will be converted to the current DTD version.
1693 * If the newFile parameter is provided, the converted schema will be written to the specified
1695 * @see ConvertSchemaFile()
1697 * @param string $schema String containing XML schema that will be converted.
1698 * @param string $newVersion DTD version to convert to.
1699 * @param string $newFile File name of (converted) output file.
1700 * @return string Converted XML schema or FALSE if an error occurs.
1702 function ConvertSchemaString( $schema, $newVersion = NULL, $newFile = NULL ) {
1704 // grab current version
1705 if( !( $version = $this->SchemaStringVersion( $schema ) ) ) {
1709 if( !isset ($newVersion) ) {
1710 $newVersion = $this->schemaVersion
;
1713 if( $version == $newVersion ) {
1716 $result = $this->TransformSchema( $schema, 'convert-' . $version . '-' . $newVersion);
1719 if( is_string( $result ) AND is_string( $newFile ) AND ( $fp = fopen( $newFile, 'w' ) ) ) {
1720 fwrite( $fp, $result );
1727 // compat for pre-4.3 - jlim
1728 function _file_get_contents($path)
1730 if (function_exists('file_get_contents')) return file_get_contents($path);
1731 return join('',file($path));
1735 * Converts an XML schema file to the specified DTD version.
1737 * Call this method to convert the specified XML schema file to a different AXMLS
1738 * DTD version. For instance, to convert a schema created for an pre-1.0 version for
1739 * AXMLS (DTD version 0.1) to a newer version of the DTD (e.g. 0.2). If no DTD version
1740 * parameter is specified, the schema will be converted to the current DTD version.
1741 * If the newFile parameter is provided, the converted schema will be written to the specified
1743 * @see ConvertSchemaString()
1745 * @param string $filename Name of XML schema file that will be converted.
1746 * @param string $newVersion DTD version to convert to.
1747 * @param string $newFile File name of (converted) output file.
1748 * @return string Converted XML schema or FALSE if an error occurs.
1750 function ConvertSchemaFile( $filename, $newVersion = NULL, $newFile = NULL ) {
1752 // grab current version
1753 if( !( $version = $this->SchemaFileVersion( $filename ) ) ) {
1757 if( !isset ($newVersion) ) {
1758 $newVersion = $this->schemaVersion
;
1761 if( $version == $newVersion ) {
1762 $result = _file_get_contents( $filename );
1764 // remove unicode BOM if present
1765 if( substr( $result, 0, 3 ) == sprintf( '%c%c%c', 239, 187, 191 ) ) {
1766 $result = substr( $result, 3 );
1769 $result = $this->TransformSchema( $filename, 'convert-' . $version . '-' . $newVersion, 'file' );
1772 if( is_string( $result ) AND is_string( $newFile ) AND ( $fp = fopen( $newFile, 'w' ) ) ) {
1773 fwrite( $fp, $result );
1780 function TransformSchema( $schema, $xsl, $schematype='string' )
1782 // Fail if XSLT extension is not available
1783 if( ! function_exists( 'xslt_create' ) ) {
1787 $xsl_file = dirname( __FILE__
) . '/xsl/' . $xsl . '.xsl';
1790 if( !is_readable( $xsl_file ) ) {
1794 switch( $schematype )
1797 if( !is_readable( $schema ) ) {
1801 $schema = _file_get_contents( $schema );
1805 if( !is_string( $schema ) ) {
1810 $arguments = array (
1812 '/_xsl' => _file_get_contents( $xsl_file )
1815 // create an XSLT processor
1816 $xh = xslt_create ();
1818 // set error handler
1819 xslt_set_error_handler ($xh, array (&$this, 'xslt_error_handler'));
1821 // process the schema
1822 $result = xslt_process ($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);
1830 * Processes XSLT transformation errors
1832 * @param object $parser XML parser object
1833 * @param integer $errno Error number
1834 * @param integer $level Error level
1835 * @param array $fields Error information fields
1839 function xslt_error_handler( $parser, $errno, $level, $fields ) {
1840 if( is_array( $fields ) ) {
1842 'Message Type' => ucfirst( $fields['msgtype'] ),
1843 'Message Code' => $fields['code'],
1844 'Message' => $fields['msg'],
1845 'Error Number' => $errno,
1849 switch( $fields['URI'] ) {
1851 $msg['Input'] = 'XML';
1854 $msg['Input'] = 'XSL';
1857 $msg['Input'] = $fields['URI'];
1860 $msg['Line'] = $fields['line'];
1863 'Message Type' => 'Error',
1864 'Error Number' => $errno,
1866 'Fields' => var_export( $fields, TRUE )
1870 $error_details = $msg['Message Type'] . ' in XSLT Transformation' . "\n"
1873 foreach( $msg as $label => $details ) {
1874 $error_details .= '<tr><td><b>' . $label . ': </b></td><td>' . htmlentities( $details ) . '</td></tr>' . "\n";
1877 $error_details .= '</table>';
1879 trigger_error( $error_details, E_USER_ERROR
);
1883 * Returns the AXMLS Schema Version of the requested XML schema file.
1885 * Call this method to obtain the AXMLS DTD version of the requested XML schema file.
1886 * @see SchemaStringVersion()
1888 * @param string $filename AXMLS schema file
1889 * @return string Schema version number or FALSE on error
1891 function SchemaFileVersion( $filename ) {
1893 if( !($fp = fopen( $filename, 'r' )) ) {
1894 // die( 'Unable to open file' );
1899 while( $data = fread( $fp, 4096 ) ) {
1900 if( preg_match( $this->versionRegex
, $data, $matches ) ) {
1901 return !empty( $matches[2] ) ?
$matches[2] : XMLS_DEFAULT_SCHEMA_VERSION
;
1909 * Returns the AXMLS Schema Version of the provided XML schema string.
1911 * Call this method to obtain the AXMLS DTD version of the provided XML schema string.
1912 * @see SchemaFileVersion()
1914 * @param string $xmlstring XML schema string
1915 * @return string Schema version number or FALSE on error
1917 function SchemaStringVersion( $xmlstring ) {
1918 if( !is_string( $xmlstring ) OR empty( $xmlstring ) ) {
1922 if( preg_match( $this->versionRegex
, $xmlstring, $matches ) ) {
1923 return !empty( $matches[2] ) ?
$matches[2] : XMLS_DEFAULT_SCHEMA_VERSION
;
1930 * Extracts an XML schema from an existing database.
1932 * Call this method to create an XML schema string from an existing database.
1933 * If the data parameter is set to TRUE, AXMLS will include the data from the database
1936 * @param boolean $data Include data in schema dump
1937 * @return string Generated XML schema
1939 function ExtractSchema( $data = FALSE ) {
1940 $old_mode = $this->db
->SetFetchMode( ADODB_FETCH_NUM
);
1942 $schema = '<?xml version="1.0"?>' . "\n"
1943 . '<schema version="' . $this->schemaVersion
. '">' . "\n";
1945 if( is_array( $tables = $this->db
->MetaTables( 'TABLES' ) ) ) {
1946 foreach( $tables as $table ) {
1947 $schema .= ' <table name="' . $table . '">' . "\n";
1949 // grab details from database
1950 $rs = $this->db
->Execute( 'SELECT * FROM ' . $table . ' WHERE 1=1' );
1951 $fields = $this->db
->MetaColumns( $table );
1952 $indexes = $this->db
->MetaIndexes( $table );
1954 if( is_array( $fields ) ) {
1955 foreach( $fields as $details ) {
1959 if( $details->max_length
> 0 ) {
1960 $extra .= ' size="' . $details->max_length
. '"';
1963 if( $details->primary_key
) {
1964 $content[] = '<KEY/>';
1965 } elseif( $details->not_null
) {
1966 $content[] = '<NOTNULL/>';
1969 if( $details->has_default
) {
1970 $content[] = '<DEFAULT value="' . $details->default_value
. '"/>';
1973 if( $details->auto_increment
) {
1974 $content[] = '<AUTOINCREMENT/>';
1977 // this stops the creation of 'R' columns,
1978 // AUTOINCREMENT is used to create auto columns
1979 $details->primary_key
= 0;
1980 $type = $rs->MetaType( $details );
1982 $schema .= ' <field name="' . $details->name
. '" type="' . $type . '"' . $extra . '>';
1984 if( !empty( $content ) ) {
1985 $schema .= "\n " . implode( "\n ", $content ) . "\n ";
1988 $schema .= '</field>' . "\n";
1992 if( is_array( $indexes ) ) {
1993 foreach( $indexes as $index => $details ) {
1994 $schema .= ' <index name="' . $index . '">' . "\n";
1996 if( $details['unique'] ) {
1997 $schema .= ' <UNIQUE/>' . "\n";
2000 foreach( $details['columns'] as $column ) {
2001 $schema .= ' <col>' . $column . '</col>' . "\n";
2004 $schema .= ' </index>' . "\n";
2009 $rs = $this->db
->Execute( 'SELECT * FROM ' . $table );
2011 if( is_object( $rs ) ) {
2012 $schema .= ' <data>' . "\n";
2014 while( $row = $rs->FetchRow() ) {
2015 foreach( $row as $key => $val ) {
2016 $row[$key] = htmlentities($val);
2019 $schema .= ' <row><f>' . implode( '</f><f>', $row ) . '</f></row>' . "\n";
2022 $schema .= ' </data>' . "\n";
2026 $schema .= ' </table>' . "\n";
2030 $this->db
->SetFetchMode( $old_mode );
2032 $schema .= '</schema>';
2037 * Sets a prefix for database objects
2039 * Call this method to set a standard prefix that will be prepended to all database tables
2040 * and indices when the schema is parsed. Calling setPrefix with no arguments clears the prefix.
2042 * @param string $prefix Prefix that will be prepended.
2043 * @param boolean $underscore If TRUE, automatically append an underscore character to the prefix.
2044 * @return boolean TRUE if successful, else FALSE
2046 function SetPrefix( $prefix = '', $underscore = TRUE ) {
2049 case empty( $prefix ):
2050 logMsg( 'Cleared prefix' );
2051 $this->objectPrefix
= '';
2054 case strlen( $prefix ) > XMLS_PREFIX_MAXLEN
:
2055 // prefix contains invalid characters
2056 case !preg_match( '/^[a-z][a-z0-9_]+$/i', $prefix ):
2057 logMsg( 'Invalid prefix: ' . $prefix );
2061 if( $underscore AND substr( $prefix, -1 ) != '_' ) {
2066 logMsg( 'Set prefix: ' . $prefix );
2067 $this->objectPrefix
= $prefix;
2072 * Returns an object name with the current prefix prepended.
2074 * @param string $name Name
2075 * @return string Prefixed name
2079 function prefix( $name = '' ) {
2081 if( !empty( $this->objectPrefix
) ) {
2082 // Prepend the object prefix to the table name
2083 // prepend after quote if used
2084 return preg_replace( '/^(`?)(.+)$/', '$1' . $this->objectPrefix
. '$2', $name );
2087 // No prefix set. Use name provided.
2092 * Checks if element references a specific platform
2094 * @param string $platform Requested platform
2095 * @returns boolean TRUE if platform check succeeds
2099 function supportedPlatform( $platform = NULL ) {
2100 $regex = '/^(\w*\|)*' . $this->db
->databaseType
. '(\|\w*)*$/';
2102 if( !isset( $platform ) OR preg_match( $regex, $platform ) ) {
2103 logMsg( "Platform $platform is supported" );
2106 logMsg( "Platform $platform is NOT supported" );
2112 * Clears the array of generated SQL.
2116 function clearSQL() {
2117 $this->sqlArray
= array();
2121 * Adds SQL into the SQL array.
2123 * @param mixed $sql SQL to Add
2124 * @return boolean TRUE if successful, else FALSE.
2128 function addSQL( $sql = NULL ) {
2129 if( is_array( $sql ) ) {
2130 foreach( $sql as $line ) {
2131 $this->addSQL( $line );
2137 if( is_string( $sql ) ) {
2138 $this->sqlArray
[] = $sql;
2140 // if executeInline is enabled, and either no errors have occurred or continueOnError is enabled, execute SQL.
2141 if( $this->ExecuteInline() && ( $this->success
== 2 ||
$this->ContinueOnError() ) ) {
2142 $saved = $this->db
->debug
;
2143 $this->db
->debug
= $this->debug
;
2144 $ok = $this->db
->Execute( $sql );
2145 $this->db
->debug
= $saved;
2148 if( $this->debug
) {
2149 ADOConnection
::outp( $this->db
->ErrorMsg() );
2163 * Gets the SQL array in the specified format.
2165 * @param string $format Format
2170 function getSQL( $format = NULL, $sqlArray = NULL ) {
2171 if( !is_array( $sqlArray ) ) {
2172 $sqlArray = $this->sqlArray
;
2175 if( !is_array( $sqlArray ) ) {
2179 switch( strtolower( $format ) ) {
2182 return !empty( $sqlArray ) ?
implode( ";\n\n", $sqlArray ) . ';' : '';
2184 return !empty( $sqlArray ) ?
nl2br( htmlentities( implode( ";\n\n", $sqlArray ) . ';' ) ) : '';
2187 return $this->sqlArray
;
2191 * Destroys an adoSchema object.
2193 * Call this method to clean up after an adoSchema object that is no longer in use.
2194 * @deprecated adoSchema now cleans up automatically.
2196 function Destroy() {
2197 ini_set("magic_quotes_runtime", $this->mgq
);
2198 #set_magic_quotes_runtime( $this->mgq );
2204 * Message logging function
2208 function logMsg( $msg, $title = NULL, $force = FALSE ) {
2209 if( XMLS_DEBUG
or $force ) {
2212 if( isset( $title ) ) {
2213 echo '<h3>' . htmlentities( $title ) . '</h3>';
2216 if( is_object( $this ) ) {
2217 echo '[' . get_class( $this ) . '] ';