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' );
52 if( !defined(
'XMLS_PREFIX_MAXLEN' ) ) {
53 define(
'XMLS_PREFIX_MAXLEN', 10 );
59 if( !defined(
'XMLS_EXECUTE_INLINE' ) ) {
60 define(
'XMLS_EXECUTE_INLINE', FALSE );
66 if( !defined(
'XMLS_CONTINUE_ON_ERROR' ) ) {
67 define(
'XMLS_CONTINUE_ON_ERROR', FALSE );
73 if( !defined(
'XMLS_SCHEMA_VERSION' ) ) {
74 define(
'XMLS_SCHEMA_VERSION',
'0.3' );
80 if( !defined(
'XMLS_DEFAULT_SCHEMA_VERSION' ) ) {
81 define(
'XMLS_DEFAULT_SCHEMA_VERSION',
'0.1' );
89 if( !defined(
'XMLS_MODE_INSERT' ) ) {
90 define(
'XMLS_MODE_INSERT', 0 );
92 if( !defined(
'XMLS_MODE_UPDATE' ) ) {
93 define(
'XMLS_MODE_UPDATE', 1 );
95 if( !defined(
'XMLS_MODE_IGNORE' ) ) {
96 define(
'XMLS_MODE_IGNORE', 2 );
98 if( !defined(
'XMLS_EXISTING_DATA' ) ) {
99 define(
'XMLS_EXISTING_DATA', XMLS_MODE_INSERT );
105 if( !defined(
'XMLS_DEFAULT_UPGRADE_METHOD' ) ) {
106 define(
'XMLS_DEFAULT_UPGRADE_METHOD',
'ALTER' );
112 if( !defined(
'_ADODB_LAYER' ) ) {
113 require(
'adodb.inc.php' );
114 require(
'adodb-datadict.inc.php' );
139 function dbObject( &$parent, $attributes = NULL ) {
189 return is_object( $this->parent ) ? $this->parent->supportedPlatform( $platform ) : TRUE;
199 return is_object( $this->parent ) ? $this->parent->prefix( $name ) : $name;
209 return strtoupper( preg_replace(
'/^`(.+)`$/',
'$1', $field ) );
234 var $fields = array();
239 var $indexes = array();
261 var $drop_field = array();
267 var $currentPlatform =
true;
278 $this->name = $this->
prefix($attributes[
'NAME']);
288 $this->currentElement = strtoupper( $tag );
290 switch( $this->currentElement ) {
292 if( !isset( $attributes[
'PLATFORM'] ) OR $this->
supportedPlatform( $attributes[
'PLATFORM'] ) ) {
293 xml_set_object( $parser, $this->addIndex( $attributes ) );
297 if( !isset( $attributes[
'PLATFORM'] ) OR $this->
supportedPlatform( $attributes[
'PLATFORM'] ) ) {
298 xml_set_object( $parser, $this->addData( $attributes ) );
306 $fieldName = $attributes[
'NAME'];
307 $fieldType = $attributes[
'TYPE'];
308 $fieldSize = isset( $attributes[
'SIZE'] ) ? $attributes[
'SIZE'] : NULL;
309 $fieldOpts = !empty( $attributes[
'OPTS'] ) ? $attributes[
'OPTS'] : NULL;
311 $this->addField( $fieldName, $fieldType, $fieldSize, $fieldOpts );
315 case 'AUTOINCREMENT':
320 $this->addFieldOpt( $this->current_field, $this->currentElement );
326 if( $attributes[
'VALUE'] ==
'' ) {
327 $attributes[
'VALUE'] =
" '' ";
330 $this->addFieldOpt( $this->current_field, $this->currentElement, $attributes[
'VALUE'] );
335 $this->currentPlatform = ( !isset( $attributes[
'PLATFORM'] ) OR $this->
supportedPlatform( $attributes[
'PLATFORM'] ) );
348 switch( $this->currentElement ) {
351 if( isset( $this->current_field ) ) {
352 $this->addFieldOpt( $this->current_field, $this->currentElement, $cdata );
354 $this->addTableOpt( $cdata );
359 if( isset( $this->current_field ) ) {
360 $this->addFieldOpt( $this->current_field, $cdata );
362 $this->addTableOpt( $cdata );
376 $this->currentElement =
'';
378 switch( strtoupper( $tag ) ) {
380 $this->parent->addSQL( $this->
create( $this->parent ) );
381 xml_set_object( $parser, $this->parent );
385 unset($this->current_field);
389 $this->currentPlatform =
true;
403 $name = strtoupper( $attributes[
'NAME'] );
404 $this->indexes[$name] =
new dbIndex( $this, $attributes );
405 return $this->indexes[$name];
415 if( !isset( $this->data ) ) {
416 $this->data =
new dbData( $this, $attributes );
450 function addField( $name, $type, $size = NULL, $opts = NULL ) {
451 $field_id = $this->
FieldID( $name );
454 $this->current_field = $field_id;
457 $this->fields[$field_id][
'NAME'] = $name;
460 $this->fields[$field_id][
'TYPE'] = $type;
463 if( isset( $size ) ) {
464 $this->fields[$field_id][
'SIZE'] = $size;
468 if( isset( $opts ) ) {
469 $this->fields[$field_id][
'OPTS'] = array($opts);
471 $this->fields[$field_id][
'OPTS'] = array();
487 if( $this->currentPlatform ) {
488 if( !isset( $value ) ) {
489 $this->fields[$this->
FieldID( $field )][
'OPTS'][] = $opt;
492 $this->fields[$this->
FieldID( $field )][
'OPTS'][] = array( $opt => $value );
507 if(isset($this->currentPlatform)) {
508 $this->opts[$this->parent->db->databaseType] = $opt;
524 if( is_array( $legacy_indexes = $xmls->dict->MetaIndexes( $this->name ) ) ) {
525 foreach( $legacy_indexes as $index => $index_details ) {
526 $sql[] = $xmls->dict->DropIndexSQL( $index, $this->name );
531 foreach( $this->drop_field as $field ) {
532 unset( $this->fields[$field] );
536 if( is_array( $legacy_fields = $xmls->dict->MetaColumns( $this->name ) ) ) {
538 if( $this->drop_table ) {
539 $sql[] = $xmls->dict->DropTableSQL( $this->name );
545 foreach( $legacy_fields as $field_id => $field ) {
546 if( !isset( $this->fields[$field_id] ) ) {
547 $sql[] = $xmls->dict->DropColumnSQL( $this->name, $field->name );
552 if( $this->drop_table ) {
556 $legacy_fields = array();
562 foreach( $this->fields as $field_id => $finfo ) {
564 if( !isset( $finfo[
'SIZE'] ) ) {
569 $fldarray[$field_id] = array(
570 'NAME' => $finfo[
'NAME'],
571 'TYPE' => $finfo[
'TYPE'],
572 'SIZE' => $finfo[
'SIZE']
576 if( isset( $finfo[
'OPTS'] ) ) {
577 foreach( $finfo[
'OPTS'] as $opt ) {
579 if( is_array( $opt ) ) {
581 $value = $opt[key( $opt )];
582 @$fldarray[$field_id][$key] .= $value;
585 $fldarray[$field_id][$opt] = $opt;
591 if( empty( $legacy_fields ) ) {
593 $sql[] = $xmls->dict->CreateTableSQL( $this->name, $fldarray, $this->opts );
594 logMsg( end(
$sql ),
'Generated CreateTableSQL' );
597 logMsg(
"Upgrading {$this->name} using '{$xmls->upgrade}'" );
598 switch( $xmls->upgrade ) {
601 logMsg(
'Generated ChangeTableSQL (ALTERing table)' );
602 $sql[] = $xmls->dict->ChangeTableSQL( $this->name, $fldarray, $this->opts );
605 logMsg(
'Doing upgrade REPLACE (testing)' );
606 $sql[] = $xmls->dict->DropTableSQL( $this->name );
607 $sql[] = $xmls->dict->CreateTableSQL( $this->name, $fldarray, $this->opts );
615 foreach( $this->indexes as $index ) {
616 $sql[] = $index->create( $xmls );
619 if( isset( $this->data ) ) {
620 $sql[] = $this->data->create( $xmls );
630 if( isset( $this->current_field ) ) {
632 logMsg(
"Dropping field '{$this->current_field}' from table '{$this->name}'" );
634 $this->drop_field[$this->current_field] = $this->current_field;
637 logMsg(
"Dropping table '{$this->name}'" );
639 $this->drop_table = TRUE;
670 var $columns = array();
689 $this->name = $this->
prefix ($attributes[
'NAME']);
701 $this->currentElement = strtoupper( $tag );
703 switch( $this->currentElement ) {
713 $this->addIndexOpt( $this->currentElement );
728 switch( $this->currentElement ) {
731 $this->addField( $cdata );
744 $this->currentElement =
'';
746 switch( strtoupper( $tag ) ) {
748 xml_set_object( $parser, $this->parent );
760 $this->columns[$this->
FieldID( $name )] = $name;
763 return $this->columns;
773 $this->opts[] = $opt;
791 foreach( $this->columns as $id => $col ) {
792 if( !isset( $this->parent->fields[$id] ) ) {
793 unset( $this->columns[$id] );
797 return $xmls->dict->CreateIndexSQL( $this->name, $this->parent->name, $this->columns, $this->opts );
844 $this->currentElement = strtoupper( $tag );
846 switch( $this->currentElement ) {
848 $this->row = count( $this->data );
849 $this->data[$this->row] = array();
852 $this->addField($attributes);
866 switch( $this->currentElement ) {
869 $this->addData( $cdata );
882 $this->currentElement =
'';
884 switch( strtoupper( $tag ) ) {
886 xml_set_object( $parser, $this->parent );
899 if( !isset( $this->row ) || !isset( $this->data[$this->row] ) ) {
904 if( isset( $attributes[
'NAME'] ) ) {
905 $this->current_field = $this->
FieldID( $attributes[
'NAME'] );
907 $this->current_field = count( $this->data[$this->row] );
911 if( !isset( $this->data[$this->row][$this->current_field] ) ) {
912 $this->data[$this->row][$this->current_field] =
'';
924 if ( isset( $this->data[$this->row][$this->current_field] ) ) {
926 $this->data[$this->row][$this->current_field] .= $cdata;
937 $table = $xmls->dict->TableName($this->parent->name);
938 $table_field_count = count($this->parent->fields);
939 $tables = $xmls->db->MetaTables();
942 $ukeys = $xmls->db->MetaPrimaryKeys( $table );
943 if( !empty( $this->parent->indexes ) and !empty( $ukeys ) ) {
944 foreach( $this->parent->indexes as $indexObj ) {
945 if( !in_array( $indexObj->name, $ukeys ) ) $ukeys[] = $indexObj->name;
950 foreach( $this->data as $row ) {
951 $table_fields = $this->parent->fields;
953 $rawfields = array();
955 foreach( $row as $field_id => $field_data ) {
956 if( !array_key_exists( $field_id, $table_fields ) ) {
957 if( is_numeric( $field_id ) ) {
958 $field_id = reset( array_keys( $table_fields ) );
964 $name = $table_fields[$field_id][
'NAME'];
966 switch( $table_fields[$field_id][
'TYPE'] ) {
972 $fields[$name] = intval($field_data);
979 $fields[$name] = $xmls->db->qstr( $field_data );
980 $rawfields[$name] = $field_data;
983 unset($table_fields[$field_id]);
988 if( empty( $fields ) ) {
993 if( count( $fields ) < $table_field_count ) {
994 foreach( $table_fields as $field ) {
995 if( isset( $field[
'OPTS'] ) and ( in_array(
'NOTNULL', $field[
'OPTS'] ) || in_array(
'KEY', $field[
'OPTS'] ) ) && !in_array(
'AUTOINCREMENT', $field[
'OPTS'] ) ) {
1003 if( !in_array( $table, $tables ) or ( $mode = $xmls->existingData() ) == XMLS_MODE_INSERT ) {
1005 logMsg(
"$table doesn't exist, inserting or mode is INSERT" );
1006 $sql[] =
'INSERT INTO '. $table .
' ('. implode(
',', array_keys( $fields ) ) .
') VALUES ('. implode(
',', $fields ) .
')';
1011 $mfields = array_merge( $fields, $rawfields );
1012 $keyFields = array_intersect( $ukeys, array_keys( $mfields ) );
1014 if( empty( $ukeys ) or count( $keyFields ) == 0 ) {
1016 logMsg(
"Either schema or data has no unique keys, so safe to insert" );
1017 $sql[] =
'INSERT INTO '. $table .
' ('. implode(
',', array_keys( $fields ) ) .
') VALUES ('. implode(
',', $fields ) .
')';
1023 foreach( $ukeys as $key ) {
1024 if( isset( $mfields[$key] ) and $mfields[$key] ) {
1025 if( $where ) $where .=
' AND ';
1026 $where .= $key .
' = ' . $xmls->db->qstr( $mfields[$key] );
1029 $records = $xmls->db->Execute(
'SELECT * FROM ' . $table .
' WHERE ' . $where );
1030 switch( $records->RecordCount() ) {
1033 logMsg(
"No matching records. Inserting new row with unique data" );
1034 $sql[] = $xmls->db->GetInsertSQL( $records, $mfields );
1038 logMsg(
"One matching record..." );
1039 if( $mode == XMLS_MODE_UPDATE ) {
1040 logMsg(
"...Updating existing row from unique data" );
1041 $sql[] = $xmls->db->GetUpdateSQL( $records, $mfields );
1046 logMsg(
"More than one matching record. Ignoring row." );
1064 var $queries = array();
1074 var $prefixKey =
'';
1079 var $prefixMethod =
'AUTO';
1091 if( isset( $attributes[
'KEY'] ) ) {
1092 $this->prefixKey = $attributes[
'KEY'];
1095 $prefixMethod = isset( $attributes[
'PREFIXMETHOD'] ) ? strtoupper( trim( $attributes[
'PREFIXMETHOD'] ) ) :
'';
1098 switch( $prefixMethod ) {
1100 $this->prefixMethod =
'AUTO';
1103 $this->prefixMethod =
'MANUAL';
1106 $this->prefixMethod =
'NONE';
1118 $this->currentElement = strtoupper( $tag );
1120 switch( $this->currentElement ) {
1125 if( !isset( $attributes[
'PLATFORM'] ) OR $this->
supportedPlatform( $attributes[
'PLATFORM'] ) ) {
1128 $this->discardQuery();
1140 switch( $this->currentElement ) {
1143 $this->buildQuery( $cdata );
1156 $this->currentElement =
'';
1158 switch( strtoupper( $tag ) ) {
1164 $this->parent->addSQL( $this->
create( $this->parent ) );
1165 xml_set_object( $parser, $this->parent );
1190 unset( $this->query );
1202 if( !isset( $this->query ) OR empty(
$sql ) ) {
1206 $this->query .=
$sql;
1208 return $this->query;
1217 if( !isset( $this->query ) ) {
1221 $this->queries[] = $return = trim($this->query);
1223 unset( $this->query );
1235 foreach( $this->queries as $id => $query ) {
1236 switch( $this->prefixMethod ) {
1242 $query = $this->prefixQuery(
'/^\s*((?is)INSERT\s+(INTO\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix );
1243 $query = $this->prefixQuery(
'/^\s*((?is)UPDATE\s+(FROM\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix );
1244 $query = $this->prefixQuery(
'/^\s*((?is)DELETE\s+(FROM\s+)?)((\w+\s*,?\s*)+)(\s.*$)/', $query, $xmls->objectPrefix );
1247 #$data = preg_replace( '/(?ias)(^\s*SELECT\s+.*\s+FROM)\s+(\W\s*,?\s*)+((?i)\s+WHERE.*$)/', "\1 $prefix\2 \3", $data ); 1252 if( isset( $this->prefixKey ) AND( $this->prefixKey !==
'' ) ) {
1254 $query = str_replace( $this->prefixKey, $xmls->objectPrefix, $query );
1257 $query = str_replace( XMLS_PREFIX , $xmls->objectPrefix, $query );
1261 $this->queries[$id] = trim( $query );
1265 return $this->queries;
1277 if( !isset( $prefix ) ) {
1281 if( preg_match( $regex, $query, $match ) ) {
1282 $preamble = $match[1];
1283 $postamble = $match[5];
1284 $objectList = explode(
',', $match[3] );
1289 foreach( $objectList as $object ) {
1290 if( $prefixedList !==
'' ) {
1291 $prefixedList .=
', ';
1294 $prefixedList .= $prefix . trim( $object );
1297 $query = $preamble .
' ' . $prefixedList .
' ' . $postamble;
1353 var $objectPrefix =
'';
1371 var $versionRegex =
'/<schema.*?( version="([^"]*)")?.*?>/';
1392 var $continueOnError;
1410 $this->mgq = get_magic_quotes_runtime();
1411 #set_magic_quotes_runtime(0); 1412 ini_set(
"magic_quotes_runtime", 0);
1415 $this->
debug = $this->db->debug;
1416 $this->dict = NewDataDictionary( $this->db );
1417 $this->sqlArray = array();
1418 $this->schemaVersion = XMLS_SCHEMA_VERSION;
1419 $this->executeInline( XMLS_EXECUTE_INLINE );
1420 $this->continueOnError( XMLS_CONTINUE_ON_ERROR );
1421 $this->existingData( XMLS_EXISTING_DATA );
1422 $this->setUpgradeMethod();
1442 if( !is_string( $method ) ) {
1446 $method = strtoupper( $method );
1451 $this->upgrade = $method;
1454 $this->upgrade = $method;
1457 $this->upgrade =
'ALTER';
1460 $this->upgrade =
'NONE';
1464 $this->upgrade = XMLS_DEFAULT_UPGRADE_METHOD;
1467 return $this->upgrade;
1490 if( is_int( $mode ) ) {
1492 case XMLS_MODE_UPDATE:
1493 $mode = XMLS_MODE_UPDATE;
1495 case XMLS_MODE_IGNORE:
1496 $mode = XMLS_MODE_IGNORE;
1498 case XMLS_MODE_INSERT:
1499 $mode = XMLS_MODE_INSERT;
1502 $mode = XMLS_EXISTING_DATA;
1505 $this->existingData = $mode;
1508 return $this->existingData;
1525 if( is_bool( $mode ) ) {
1526 $this->executeInline = $mode;
1529 return $this->executeInline;
1546 if( is_bool( $mode ) ) {
1547 $this->continueOnError = $mode;
1550 return $this->continueOnError;
1567 return $this->ParseSchemaString( $this->ConvertSchemaFile( $filename ), $returnSchema );
1593 if( !($fp = fopen( $filename,
'r' )) ) {
1594 logMsg(
'Unable to open file' );
1599 if( $this->SchemaFileVersion( $filename ) != $this->schemaVersion ) {
1600 logMsg(
'Invalid Schema Version' );
1604 if( $returnSchema ) {
1606 while( $data = fread( $fp, 4096 ) ) {
1607 $xmlstring .= $data .
"\n";
1614 $xmlParser = $this->create_parser();
1616 $previousValueOfEntityLoader = libxml_disable_entity_loader(TRUE);
1619 while( $data = fread( $fp, 4096 ) ) {
1620 if( !xml_parse( $xmlParser, $data, feof( $fp ) ) ) {
1622 "XML error: %s at line %d",
1623 xml_error_string( xml_get_error_code( $xmlParser) ),
1624 xml_get_current_line_number( $xmlParser)
1629 libxml_disable_entity_loader($previousValueOfEntityLoader);
1630 xml_parser_free( $xmlParser );
1632 return $this->sqlArray;
1647 if( !is_string( $xmlstring ) OR empty( $xmlstring ) ) {
1648 logMsg(
'Empty or Invalid Schema' );
1653 if( $this->SchemaStringVersion( $xmlstring ) != $this->schemaVersion ) {
1654 logMsg(
'Invalid Schema Version' );
1658 if( $returnSchema ) {
1664 $xmlParser = $this->create_parser();
1666 $previousValueOfEntityLoader = libxml_disable_entity_loader(TRUE);
1668 if( !xml_parse( $xmlParser, $xmlstring, TRUE ) ) {
1670 "XML error: %s at line %d",
1671 xml_error_string( xml_get_error_code( $xmlParser) ),
1672 xml_get_current_line_number( $xmlParser)
1676 libxml_disable_entity_loader($previousValueOfEntityLoader);
1677 xml_parser_free( $xmlParser );
1679 return $this->sqlArray;
1694 return $this->RemoveSchemaString( $this->ConvertSchemaFile( $filename ), $returnSchema );
1711 if( !( $version = $this->SchemaStringVersion( $schema ) ) ) {
1715 return $this->ParseSchemaString( $this->TransformSchema( $schema,
'remove-' . $version), $returnSchema );
1732 if( !is_bool( $continueOnErr ) ) {
1733 $continueOnErr = $this->ContinueOnError();
1736 if( !isset( $sqlArray ) ) {
1737 $sqlArray = $this->sqlArray;
1740 if( !is_array( $sqlArray ) ) {
1743 $this->success = $this->dict->ExecuteSQLArray( $sqlArray, $continueOnErr );
1746 return $this->success;
1760 return $this->getSQL( $format, $sqlArray );
1774 if( !isset( $sqlArray ) ) {
1775 $sqlArray = $this->sqlArray;
1777 if( !isset( $sqlArray ) ) {
1781 $fp = fopen( $filename,
"w" );
1783 foreach( $sqlArray as $key => $query ) {
1784 fwrite( $fp, $query .
";\n" );
1798 $xmlParser = xml_parser_create();
1799 xml_set_object( $xmlParser, $this );
1802 xml_set_element_handler( $xmlParser,
'_tag_open',
'_tag_close' );
1803 xml_set_character_data_handler( $xmlParser,
'_tag_cdata' );
1814 switch( strtoupper( $tag ) ) {
1816 if( !isset( $attributes[
'PLATFORM'] ) OR $this->
supportedPlatform( $attributes[
'PLATFORM'] ) ) {
1817 $this->obj =
new dbTable( $this, $attributes );
1818 xml_set_object( $parser, $this->obj );
1822 if( !isset( $attributes[
'PLATFORM'] ) OR $this->
supportedPlatform( $attributes[
'PLATFORM'] ) ) {
1823 $this->obj =
new dbQuerySet( $this, $attributes );
1824 xml_set_object( $parser, $this->obj );
1870 if( !( $version = $this->SchemaStringVersion( $schema ) ) ) {
1874 if( !isset ($newVersion) ) {
1875 $newVersion = $this->schemaVersion;
1878 if( $version == $newVersion ) {
1881 $result = $this->TransformSchema( $schema,
'convert-' . $version .
'-' . $newVersion);
1884 if( is_string(
$result ) AND is_string( $newFile ) AND ( $fp = fopen( $newFile,
'w' ) ) ) {
1919 if( !( $version = $this->SchemaFileVersion( $filename ) ) ) {
1923 if( !isset ($newVersion) ) {
1924 $newVersion = $this->schemaVersion;
1927 if( $version == $newVersion ) {
1931 if( substr(
$result, 0, 3 ) == sprintf(
'%c%c%c', 239, 187, 191 ) ) {
1935 $result = $this->TransformSchema( $filename,
'convert-' . $version .
'-' . $newVersion,
'file' );
1938 if( is_string(
$result ) AND is_string( $newFile ) AND ( $fp = fopen( $newFile,
'w' ) ) ) {
1949 if( ! function_exists(
'xslt_create' ) ) {
1953 $xsl_file = dirname( __FILE__ ) .
'/xsl/' . $xsl .
'.xsl';
1956 if( !is_readable( $xsl_file ) ) {
1960 switch( $schematype )
1963 if( !is_readable( $schema ) ) {
1971 if( !is_string( $schema ) ) {
1976 $arguments = array (
1982 $xh = xslt_create ();
1985 xslt_set_error_handler ($xh, array (&$this,
'xslt_error_handler'));
1988 $result = xslt_process ($xh,
'arg:/_xml',
'arg:/_xsl', NULL, $arguments);
2006 if( is_array( $fields ) ) {
2008 'Message Type' => ucfirst( $fields[
'msgtype'] ),
2009 'Message Code' => $fields[
'code'],
2010 'Message' => $fields[
'msg'],
2011 'Error Number' => $errno,
2015 switch( $fields[
'URI'] ) {
2017 $msg[
'Input'] =
'XML';
2020 $msg[
'Input'] =
'XSL';
2023 $msg[
'Input'] = $fields[
'URI'];
2026 $msg[
'Line'] = $fields[
'line'];
2029 'Message Type' =>
'Error',
2030 'Error Number' => $errno,
2032 'Fields' => var_export( $fields, TRUE )
2036 $error_details = $msg[
'Message Type'] .
' in XSLT Transformation' .
"\n" 2039 foreach( $msg as $label => $details ) {
2040 $error_details .=
'<tr><td><b>' . $label .
': </b></td><td>' . htmlentities( $details ) .
'</td></tr>' .
"\n";
2043 $error_details .=
'</table>';
2059 if( !($fp = fopen( $filename,
'r' )) ) {
2065 while( $data = fread( $fp, 4096 ) ) {
2066 if( preg_match( $this->versionRegex, $data, $matches ) ) {
2067 return !empty( $matches[2] ) ? $matches[2] : XMLS_DEFAULT_SCHEMA_VERSION;
2084 if( !is_string( $xmlstring ) OR empty( $xmlstring ) ) {
2088 if( preg_match( $this->versionRegex, $xmlstring, $matches ) ) {
2089 return !empty( $matches[2] ) ? $matches[2] : XMLS_DEFAULT_SCHEMA_VERSION;
2108 function ExtractSchema( $data = FALSE, $indent =
' ', $prefix =
'' , $stripprefix=
false) {
2109 $old_mode = $this->db->SetFetchMode( ADODB_FETCH_NUM );
2111 $schema =
'<?xml version="1.0"?>' .
"\n" 2112 .
'<schema version="' . $this->schemaVersion .
'">' .
"\n";
2114 if( is_array( $tables = $this->db->MetaTables(
'TABLES' , ($prefix) ? $prefix.
'%' :
'') ) ) {
2115 foreach( $tables as $table ) {
2116 if ($stripprefix) $table = str_replace(str_replace(
'\\_',
'_', $pfx ),
'', $table);
2117 $schema .= $indent .
'<table name="' . htmlentities( $table ) .
'">' .
"\n";
2120 $rs = $this->db->Execute(
'SELECT * FROM ' . $table .
' WHERE -1' );
2121 $fields = $this->db->MetaColumns( $table );
2122 $indexes = $this->db->MetaIndexes( $table );
2124 if( is_array( $fields ) ) {
2125 foreach( $fields as $details ) {
2129 if( isset($details->max_length) && $details->max_length > 0 ) {
2130 $extra .=
' size="' . $details->max_length .
'"';
2133 if( isset($details->primary_key) && $details->primary_key ) {
2134 $content[] =
'<KEY/>';
2135 } elseif( isset($details->not_null) && $details->not_null ) {
2136 $content[] =
'<NOTNULL/>';
2139 if( isset($details->has_default) && $details->has_default ) {
2140 $content[] =
'<DEFAULT value="' . htmlentities( $details->default_value ) .
'"/>';
2143 if( isset($details->auto_increment) && $details->auto_increment ) {
2144 $content[] =
'<AUTOINCREMENT/>';
2147 if( isset($details->unsigned) && $details->unsigned ) {
2148 $content[] =
'<UNSIGNED/>';
2153 $details->primary_key = 0;
2154 $type =
$rs->MetaType( $details );
2156 $schema .= str_repeat( $indent, 2 ) .
'<field name="' . htmlentities( $details->name ) .
'" type="' . $type .
'"' . $extra;
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";
2168 if( is_array( $indexes ) ) {
2169 foreach( $indexes as $index => $details ) {
2170 $schema .= str_repeat( $indent, 2 ) .
'<index name="' . $index .
'">' .
"\n";
2172 if( $details[
'unique'] ) {
2173 $schema .= str_repeat( $indent, 3 ) .
'<UNIQUE/>' .
"\n";
2176 foreach( $details[
'columns'] as $column ) {
2177 $schema .= str_repeat( $indent, 3 ) .
'<col>' . htmlentities( $column ) .
'</col>' .
"\n";
2180 $schema .= str_repeat( $indent, 2 ) .
'</index>' .
"\n";
2185 $rs = $this->db->Execute(
'SELECT * FROM ' . $table );
2187 if( is_object(
$rs ) && !
$rs->EOF ) {
2188 $schema .= str_repeat( $indent, 2 ) .
"<data>\n";
2190 while( $row =
$rs->FetchRow() ) {
2191 foreach( $row as $key => $val ) {
2192 if ( $val != htmlentities( $val ) ) {
2193 $row[$key] =
'<![CDATA[' . $val .
']]>';
2197 $schema .= str_repeat( $indent, 3 ) .
'<row><f>' . implode(
'</f><f>', $row ) .
"</f></row>\n";
2200 $schema .= str_repeat( $indent, 2 ) .
"</data>\n";
2204 $schema .= $indent .
"</table>\n";
2208 $this->db->SetFetchMode( $old_mode );
2210 $schema .=
'</schema>';
2227 case empty( $prefix ):
2228 logMsg(
'Cleared prefix' );
2229 $this->objectPrefix =
'';
2232 case strlen( $prefix ) > XMLS_PREFIX_MAXLEN:
2234 case !preg_match(
'/^[a-z][a-z0-9_]+$/i', $prefix ):
2235 logMsg(
'Invalid prefix: ' . $prefix );
2239 if( $underscore AND substr( $prefix, -1 ) !=
'_' ) {
2244 logMsg(
'Set prefix: ' . $prefix );
2245 $this->objectPrefix = $prefix;
2259 if( !empty( $this->objectPrefix ) ) {
2262 return preg_replace(
'/^(`?)(.+)$/',
'$1' . $this->objectPrefix .
'$2', $name );
2278 if( !empty( $platform ) ) {
2279 $regex =
'/(^|\|)' . $this->db->databaseType .
'(\||$)/i';
2281 if( preg_match(
'/^- /', $platform ) ) {
2282 if (preg_match ( $regex, substr( $platform, 2 ) ) ) {
2283 logMsg(
'Platform ' . $platform .
' is NOT supported' );
2287 if( !preg_match ( $regex, $platform ) ) {
2288 logMsg(
'Platform ' . $platform .
' is NOT supported' );
2294 logMsg(
'Platform ' . $platform .
' is supported' );
2304 $this->sqlArray = array();
2316 if( is_array(
$sql ) ) {
2317 foreach(
$sql as $line ) {
2318 $this->addSQL( $line );
2324 if( is_string(
$sql ) ) {
2325 $this->sqlArray[] =
$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;
2335 if( $this->
debug ) {
2336 ADOConnection::outp( $this->db->ErrorMsg() );
2357 function getSQL( $format = NULL, $sqlArray = NULL ) {
2358 if( !is_array( $sqlArray ) ) {
2359 $sqlArray = $this->sqlArray;
2362 if( !is_array( $sqlArray ) ) {
2366 switch( strtolower( $format ) ) {
2369 return !empty( $sqlArray ) ? implode(
";\n\n", $sqlArray ) .
';' :
'';
2371 return !empty( $sqlArray ) ? nl2br( htmlentities( implode(
";\n\n", $sqlArray ) .
';' ) ) :
'';
2374 return $this->sqlArray;
2384 ini_set(
"magic_quotes_runtime", $this->mgq );
2385 #set_magic_quotes_runtime( $this->mgq ); 2395 function logMsg( $msg, $title = NULL, $force = FALSE ) {
2396 if( XMLS_DEBUG or $force ) {
2399 if( isset( $title ) ) {
2400 echo
'<h3>' . htmlentities( $title ) .
'</h3>';
2403 if( @is_object( $this ) ) {
2404 echo
'[' . get_class( $this ) .
'] ';
dbTable(&$parent, $attributes=NULL)
SchemaFileVersion( $filename)
_tag_open(&$parser, $tag, $attributes)
ExecuteSchema( $sqlArray=NULL, $continueOnErr=NULL)
_tag_close(&$parser, $tag)
RemoveSchemaString( $schema, $returnSchema=FALSE)
_file_get_contents($file)
ConvertSchemaFile( $filename, $newVersion=NULL, $newFile=NULL)
ParseSchemaFile( $filename, $returnSchema=FALSE)
if(!defined('ADODB_ERROR_HANDLER_TYPE')) define('ADODB_ERROR_HANDLER_TYPE' E_USER_ERROR
if(isset($_REQUEST['nrows'])) else $rs
ContinueOnError( $mode=NULL)
_tag_close(&$parser, $tag)
RemoveSchema( $filename, $returnSchema=FALSE)
logMsg( $msg, $title=NULL, $force=FALSE)
supportedPlatform( $platform=NULL)
_tag_open(&$parser, $tag, $attributes)
_tag_open(&$parser, $tag, $attributes)
_tag_cdata(&$parser, $cdata)
_tag_close(&$parser, $tag)
SaveSQL( $filename='./schema.sql')
supportedPlatform( $platform=NULL)
_tag_close(&$parser, $tag)
prefixQuery( $regex, $query, $prefix=NULL)
_tag_cdata(&$parser, $cdata)
dbIndex(&$parent, $attributes=NULL)
ConvertSchemaString( $schema, $newVersion=NULL, $newFile=NULL)
_tag_cdata(&$parser, $cdata)
getSQL( $format=NULL, $sqlArray=NULL)
addFieldOpt( $field, $opt, $value=NULL)
_tag_open(&$parser, $tag, $attributes)
xslt_error_handler( $parser, $errno, $level, $fields)
addField( $name, $type, $size=NULL, $opts=NULL)
ExistingData( $mode=NULL)
_tag_cdata(&$parser, $cdata)
_tag_close(&$parser, $tag)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren't numeric.
SetPrefix( $prefix='', $underscore=TRUE)
dbObject(&$parent, $attributes=NULL)
_tag_cdata(&$parser, $cdata)
SchemaStringVersion( $xmlstring)
TransformSchema( $schema, $xsl, $schematype='string')
ExtractSchema( $data=FALSE, $indent=' ', $prefix='', $stripprefix=false)
dbData(&$parent, $attributes=NULL)
_tag_open(&$parser, $tag, $attributes)
ParseSchema( $filename, $returnSchema=FALSE)
_tag_open(&$parser, $tag, $attributes)
ParseSchemaString( $xmlstring, $returnSchema=FALSE)
debug($variable='', $name=' *variable *', $line=' *line *', $file=' *file *', $recursiveDepth=3, $debugLevel=E_DEBUG)
ExecuteInline( $mode=NULL)
_tag_cdata(&$parser, $cdata)
dbQuerySet(&$parent, $attributes=NULL)
_tag_close(&$parser, $tag)
PrintSQL( $format='NONE')
SetUpgradeMethod( $method='')