📄 adodb-xmlschema03.inc.php
字号:
$method = strtoupper( $method ); // Handle the upgrade methods switch( $method ) { case 'ALTER': $this->upgrade = $method; break; case 'REPLACE': $this->upgrade = $method; break; case 'BEST': $this->upgrade = 'ALTER'; break; case 'NONE': $this->upgrade = 'NONE'; break; default: // Use default if no legitimate method is passed. $this->upgrade = XMLS_DEFAULT_UPGRADE_METHOD; } return $this->upgrade; } /** * Specifies how to handle existing data row when there is a unique key conflict. * * The existingData setting specifies how the parser should handle existing rows * when a unique key violation occurs during the insert. This can happen when inserting * data into an existing table with one or more primary keys or unique indexes. * The existingData method takes one of three options: XMLS_MODE_INSERT attempts * to always insert the data as a new row. In the event of a unique key violation, * the database will generate an error. XMLS_MODE_UPDATE attempts to update the * any existing rows with the new data based upon primary or unique key fields in * the schema. If the data row in the schema specifies no unique fields, the row * data will be inserted as a new row. XMLS_MODE_IGNORE specifies that any data rows * that would result in a unique key violation be ignored; no inserts or updates will * take place. For backward compatibility, the default setting is XMLS_MODE_INSERT, * but XMLS_MODE_UPDATE will generally be the most appropriate setting. * * @param int $mode XMLS_MODE_INSERT, XMLS_MODE_UPDATE, or XMLS_MODE_IGNORE * @return int current mode */ function ExistingData( $mode = NULL ) { if( is_int( $mode ) ) { switch( $mode ) { case XMLS_MODE_UPDATE: $mode = XMLS_MODE_UPDATE; break; case XMLS_MODE_IGNORE: $mode = XMLS_MODE_IGNORE; break; case XMLS_MODE_INSERT: $mode = XMLS_MODE_INSERT; break; default: $mode = XMLS_EXISITNG_DATA; break; } $this->existingData = $mode; } return $this->existingData; } /** * Enables/disables inline SQL execution. * * Call this method to enable or disable inline execution of the schema. If the mode is set to TRUE (inline execution), * AXMLS applies the SQL to the database immediately as each schema entity is parsed. If the mode * is set to FALSE (post execution), AXMLS parses the entire schema and you will need to call adoSchema::ExecuteSchema() * to apply the schema to the database. * * @param bool $mode execute * @return bool current execution mode * * @see ParseSchema(), ExecuteSchema() */ function ExecuteInline( $mode = NULL ) { if( is_bool( $mode ) ) { $this->executeInline = $mode; } return $this->executeInline; } /** * Enables/disables SQL continue on error. * * Call this method to enable or disable continuation of SQL execution if an error occurs. * If the mode is set to TRUE (continue), AXMLS will continue to apply SQL to the database, even if an error occurs. * If the mode is set to FALSE (halt), AXMLS will halt execution of generated sql if an error occurs, though parsing * of the schema will continue. * * @param bool $mode execute * @return bool current continueOnError mode * * @see addSQL(), ExecuteSchema() */ function ContinueOnError( $mode = NULL ) { if( is_bool( $mode ) ) { $this->continueOnError = $mode; } return $this->continueOnError; } /** * Loads an XML schema from a file and converts it to SQL. * * Call this method to load the specified schema (see the DTD for the proper format) from * the filesystem and generate the SQL necessary to create the database * described. This method automatically converts the schema to the latest * axmls schema version. * @see ParseSchemaString() * * @param string $file Name of XML schema file. * @param bool $returnSchema Return schema rather than parsing. * @return array Array of SQL queries, ready to execute */ function ParseSchema( $filename, $returnSchema = FALSE ) { return $this->ParseSchemaString( $this->ConvertSchemaFile( $filename ), $returnSchema ); } /** * Loads an XML schema from a file and converts it to SQL. * * Call this method to load the specified schema directly from a file (see * the DTD for the proper format) and generate the SQL necessary to create * the database described by the schema. Use this method when you are dealing * with large schema files. Otherwise, ParseSchema() is faster. * This method does not automatically convert the schema to the latest axmls * schema version. You must convert the schema manually using either the * ConvertSchemaFile() or ConvertSchemaString() method. * @see ParseSchema() * @see ConvertSchemaFile() * @see ConvertSchemaString() * * @param string $file Name of XML schema file. * @param bool $returnSchema Return schema rather than parsing. * @return array Array of SQL queries, ready to execute. * * @deprecated Replaced by adoSchema::ParseSchema() and adoSchema::ParseSchemaString() * @see ParseSchema(), ParseSchemaString() */ function ParseSchemaFile( $filename, $returnSchema = FALSE ) { // Open the file if( !($fp = fopen( $filename, 'r' )) ) { logMsg( 'Unable to open file' ); return FALSE; } // do version detection here if( $this->SchemaFileVersion( $filename ) != $this->schemaVersion ) { logMsg( 'Invalid Schema Version' ); return FALSE; } if( $returnSchema ) { $xmlstring = ''; while( $data = fread( $fp, 4096 ) ) { $xmlstring .= $data . "\n"; } return $xmlstring; } $this->success = 2; $xmlParser = $this->create_parser(); // Process the file while( $data = fread( $fp, 4096 ) ) { if( !xml_parse( $xmlParser, $data, feof( $fp ) ) ) { die( sprintf( "XML error: %s at line %d", xml_error_string( xml_get_error_code( $xmlParser) ), xml_get_current_line_number( $xmlParser) ) ); } } xml_parser_free( $xmlParser ); return $this->sqlArray; } /** * Converts an XML schema string to SQL. * * Call this method to parse a string containing an XML schema (see the DTD for the proper format) * and generate the SQL necessary to create the database described by the schema. * @see ParseSchema() * * @param string $xmlstring XML schema string. * @param bool $returnSchema Return schema rather than parsing. * @return array Array of SQL queries, ready to execute. */ function ParseSchemaString( $xmlstring, $returnSchema = FALSE ) { if( !is_string( $xmlstring ) OR empty( $xmlstring ) ) { logMsg( 'Empty or Invalid Schema' ); return FALSE; } // do version detection here if( $this->SchemaStringVersion( $xmlstring ) != $this->schemaVersion ) { logMsg( 'Invalid Schema Version' ); return FALSE; } if( $returnSchema ) { return $xmlstring; } $this->success = 2; $xmlParser = $this->create_parser(); if( !xml_parse( $xmlParser, $xmlstring, TRUE ) ) { die( sprintf( "XML error: %s at line %d", xml_error_string( xml_get_error_code( $xmlParser) ), xml_get_current_line_number( $xmlParser) ) ); } xml_parser_free( $xmlParser ); return $this->sqlArray; } /** * Loads an XML schema from a file and converts it to uninstallation SQL. * * Call this method to load the specified schema (see the DTD for the proper format) from * the filesystem and generate the SQL necessary to remove the database described. * @see RemoveSchemaString() * * @param string $file Name of XML schema file. * @param bool $returnSchema Return schema rather than parsing. * @return array Array of SQL queries, ready to execute */ function RemoveSchema( $filename, $returnSchema = FALSE ) { return $this->RemoveSchemaString( $this->ConvertSchemaFile( $filename ), $returnSchema ); } /** * Converts an XML schema string to uninstallation SQL. * * Call this method to parse a string containing an XML schema (see the DTD for the proper format) * and generate the SQL necessary to uninstall the database described by the schema. * @see RemoveSchema() * * @param string $schema XML schema string. * @param bool $returnSchema Return schema rather than parsing. * @return array Array of SQL queries, ready to execute. */ function RemoveSchemaString( $schema, $returnSchema = FALSE ) { // grab current version if( !( $version = $this->SchemaStringVersion( $schema ) ) ) { return FALSE; } return $this->ParseSchemaString( $this->TransformSchema( $schema, 'remove-' . $version), $returnSchema ); } /** * Applies the current XML schema to the database (post execution). * * Call this method to apply the current schema (generally created by calling * ParseSchema() or ParseSchemaString() ) to the database (creating the tables, indexes, * and executing other SQL specified in the schema) after parsing. * @see ParseSchema(), ParseSchemaString(), ExecuteInline() * * @param array $sqlArray Array of SQL statements that will be applied rather than * the current schema. * @param boolean $continueOnErr Continue to apply the schema even if an error occurs. * @returns integer 0 if failure, 1 if errors, 2 if successful. */ function ExecuteSchema( $sqlArray = NULL, $continueOnErr = NULL ) { if( !is_bool( $continueOnErr ) ) { $continueOnErr = $this->ContinueOnError(); } if( !isset( $sqlArray ) ) { $sqlArray = $this->sqlArray; } if( !is_array( $sqlArray ) ) { $this->success = 0; } else { $this->success = $this->dict->ExecuteSQLArray( $sqlArray, $continueOnErr ); } return $this->success; } /** * Returns the current SQL array. * * Call this method to fetch the array of SQL queries resulting from * ParseSchema() or ParseSchemaString(). * * @param string $format Format: HTML, TEXT, or NONE (PHP array) * @return array Array of SQL statements or FALSE if an error occurs */ function PrintSQL( $format = 'NONE' ) { $sqlArray = null; return $this->getSQL( $format, $sqlArray ); } /** * Saves the current SQL array to the local filesystem as a list of SQL queries. * * Call this method to save the array of SQL queries (generally resulting from a * parsed XML schema) to the filesystem. * * @param string $filename Path and name where the file should be saved. * @return boolean TRUE if save is successful, else FALSE. */ function SaveSQL( $filename = './schema.sql' ) { if( !isset( $sqlArray ) ) { $sqlArray = $this->sqlArray; } if( !isset( $sqlArray ) ) { return FALSE; } $fp = fopen( $filename, "w" ); foreach( $sqlArray as $key => $query ) { fwrite( $fp, $query . ";\n" ); } fclose( $fp ); } /** * Create an xml parser * * @return object PHP XML parser object * * @access private */ function &create_parser() { // Create the parser $xmlParser = xml_parser_create(); xml_set_object( $xmlParser, $this ); // Initialize the XML callback functions xml_set_element_handler( $xmlParser, '_tag_open', '_tag_close' ); xml_set_character_data_handler( $xmlParser, '_tag_cdata' ); return $xmlParser; } /** * XML Callback to process start elements * * @access private */ function _tag_open( &$parser, $tag, $attributes ) { switch( strtoupper( $tag ) ) { case 'TABLE': if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) { $this->obj = new dbTable( $this, $attributes ); xml_set_object( $parser, $this->obj ); } break; case 'SQL': if( !isset( $attributes['PLATFORM'] ) OR $this->supportedPlatform( $attributes['PLATFORM'] ) ) { $this->obj = new dbQuerySet( $this, $attributes ); xml_set_object( $parser, $this->obj ); } break; default: // print_r( array( $tag, $attributes ) ); } } /** * XML Callback to process CDATA elements * * @access private */ function _tag_cdata( &$parser, $cdata ) { } /** * XML Callback to process end elements * * @access private * @internal */ function _tag_close( &$parser, $tag ) { } /** * Converts an XML schema string to the specified DTD version. * * Call this method to convert a string containing an XML schema to a different AXMLS * DTD version. For instance, to convert a schema created for an pre-1.0 version for * AXMLS (DTD version 0.1) to a newer version of the DTD (e.g. 0.2). If no DTD version * parameter is specified, the schema will be converted to the current DTD version. * If the newFile parameter is provided, the converted schema will be written to the specified * file. * @see ConvertSchemaFile() * * @param string $schema String containing XML schema that will be converted. * @param string $newVersion DTD version to convert to. * @param string $newFile File name of (converted) output file. * @return string Converted XML schema or FALSE if an error occurs. */ function ConvertSchemaString( $schema, $newVersion = NULL, $newFile = NULL ) { // grab current version if( !( $version = $this->SchemaStringVersion( $schema ) ) ) { return FALSE; } if( !isset ($newVersion) ) { $newVersion = $this->schemaVersion; } if( $version == $newVersion ) { $result = $schema; } else { $result = $this->TransformSchema( $schema, 'convert-' . $version . '-' . $newVersion); } if( is_string( $result ) AND is_string( $newFile ) AND ( $fp = fopen( $newFile, 'w' ) ) ) { fwrite( $fp, $result ); fclose( $fp ); } return $result; } /* // compat for pre-4.3 - jlim function _file_get_contents($path) { if (function_exists('file_get_contents')) return file_get_contents($path); return join('',file($path)); }*/ /** * Converts an XML schema file to the specified DTD version. * * Call this method to convert the specified XML schema file to a different AXMLS * DTD version. For instance, to convert a schema created for an pre-1.0 version for * AXMLS (DTD version 0.1) to a newer version of the DTD (e.g. 0.2). If no DTD version * parameter is specified, the schema will be converted to the current DTD version. * If the newFile parameter is provided, the converted schema will be written to the specified * file. * @see ConvertSchemaString() * * @param string $filename Name of XML schema file that will be converted. * @param string $newVersion DTD version to convert to. * @param string $newFile File name of (converted) output file. * @return string Converted XML schema or FALSE if an error occurs. */ function ConvertSchemaFile( $filename, $newVersion = NULL, $newFile = NULL ) { // grab current version if( !( $version = $this->SchemaFileVersion( $filename ) ) ) { return FALSE; } if( !isset ($newVersion) ) { $newVersion = $this->schemaVersion; } if( $version == $newVersion ) { $result = _file_get_contents( $filename ); // remove unicode BOM if present if( substr( $result, 0, 3 ) == sprintf( '%c%c%c', 239, 187, 191 ) ) { $result = substr( $result, 3 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -