r $sqlArray; /** * @var object ADOdb connection object * @access private */ var $db; /** * @var object ADOdb Data Dictionary * @access private */ var $dict; /** * @var string Current XML element * @access private */ var $currentElement = ''; /** * @var string If set (to 'ALTER' or 'REPLACE'), upgrade an existing database * @access private */ var $upgrade = ''; /** * @var string Optional object prefix * @access private */ var $objectPrefix = ''; /** * @var long Original Magic Quotes Runtime value * @access private */ var $mgq; /** * @var long System debug * @access private */ var $debug; /** * @var string Regular expression to find schema version * @access private */ var $versionRegex = '//'; /** * @var string Current schema version * @access private */ var $schemaVersion; /** * @var int Success of last Schema execution */ var $success; /** * @var bool Execute SQL inline as it is generated */ var $executeInline; /** * @var bool Continue SQL execution if errors occur */ var $continueOnError; /** * Creates an adoSchema object * * Creating an adoSchema object is the first step in processing an XML schema. * The only parameter is an ADOdb database connection object, which must already * have been created. * * @param object $db ADOdb database connection object. */ function adoSchema( &$db ) { // Initialize the environment $this->mgq = get_magic_quotes_runtime(); set_magic_quotes_runtime(0); $this->db =& $db; $this->debug = $this->db->debug; $this->dict = NewDataDictionary( $this->db ); $this->sqlArray = array(); $this->schemaVersion = XMLS_SCHEMA_VERSION; $this->executeInline( XMLS_EXECUTE_INLINE ); $this->continueOnError( XMLS_CONTINUE_ON_ERROR ); $this->setUpgradeMethod(); } /** * Sets the method to be used for upgrading an existing database * * Use this method to specify how existing database objects should be upgraded. * The method option can be set to ALTER, REPLACE, BEST, or NONE. ALTER attempts to * alter each database object directly, REPLACE attempts to rebuild each object * from scratch, BEST attempts to determine the best upgrade method for each * object, and NONE disables upgrading. * * This method is not yet used by AXMLS, but exists for backward compatibility. * The ALTER method is automatically assumed when the adoSchema object is * instantiated; other upgrade methods are not currently supported. * * @param string $method Upgrade method (ALTER|REPLACE|BEST|NONE) * @returns string Upgrade method used */ function SetUpgradeMethod( $method = '' ) { if( !is_string( $method ) ) { return FALSE; } $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; } /** * 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. * @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 from a file (see the DTD for the proper format) * and generate the SQL necessary to create the database described by the schema. * * @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' )) ) { // die( 'Unable to open file' ); return FALSE; } // do version detection here if( $this->SchemaFileVersion( $filename ) != $this->schemaVersion ) { return FALSE; } if ( $returnSchema ) { $xmlstring = ''; while( $data = fread( $fp, 100000 ) ) { $xmlstring .= $data; } 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 ) ) { return FALSE; } // do version detection here if( $this->SchemaStringVersion( $xmlstring ) != $this->schemaVersion ) { 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(), Ex