/** * Loads database schema from dump file. * * @param file input file to be read * * @return database schema from dump fle */ public static function load_database($files) { // one or more files to load as database if (!is_array($files)) { $files = array($files); } pgsql8::$track_pg_identifiers = true; pgsql8::$known_pg_identifiers = array(); $database = new SimpleXMLElement('<dbsteward></dbsteward>'); dbx::set_default_schema($database, 'public'); foreach ($files as $file) { dbsteward::notice("Loading " . $file); $fp = fopen($file, 'r'); if ($fp === false) { throw new exception("failed to open database dump file " . $file); } $line = fgets($fp); while ($line != null) { // blindly include LITERAL_SQL_INCLUDE lines in the database literal_sql collection if (preg_match(self::PATTERN_LITERAL_SQL, $line, $matches) > 0) { dbx::add_sql($database, trim($line)); // clear the line that was literally obsorbed $line = ' '; } $line = trim(self::strip_comment(trim($line))); if (strlen($line) == 0) { $line = fgets($fp); continue; } else { if (preg_match(self::PATTERN_INSERT_INTO, $line, $matches) > 0) { pgsql8_parser_insert_into::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_DELETE_FROM, $line, $matches) > 0) { pgsql8_parser_delete_from::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_CREATE_LANGUAGE, $line, $matches) > 0) { pgsql8_parser_create_language::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_ALTER_LANGUAGE, $line, $matches) > 0) { pgsql8_parser_alter_language::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_CREATE_TYPE, $line, $matches) > 0) { pgsql8_parser_create_type::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_CREATE_SCHEMA, $line, $matches) > 0) { pgsql8_parser_create_schema::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_DEFAULT_SCHEMA, $line, $matches) > 0) { dbx::set_default_schema($database, $matches[1]); } else { if (preg_match(self::PATTERN_ALTER_SCHEMA, $line, $matches) > 0) { pgsql8_parser_alter_schema::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_CREATE_TABLE, $line, $matches) > 0) { pgsql8_parser_create_table::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_ALTER_TABLE, $line, $matches) > 0) { pgsql8_parser_alter_table::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_CREATE_SEQUENCE, $line, $matches) > 0) { pgsql8_parser_create_sequence::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_ALTER_SEQUENCE, $line, $matches) > 0) { pgsql8_parser_alter_sequence::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_CREATE_INDEX, $line, $matches) > 0) { pgsql8_parser_create_index::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_CREATE_VIEW, $line, $matches) > 0) { pgsql8_parser_create_view::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_ALTER_VIEW, $line, $matches) > 0) { pgsql8_parser_alter_view::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_CREATE_TRIGGER, $line, $matches) > 0) { pgsql8_parser_create_trigger::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_CREATE_FUNCTION, $line, $matches) > 0) { pgsql8_parser_create_function::parse($database, self::get_whole_function($fp, $line)); } else { if (preg_match(self::PATTERN_ALTER_FUNCTION, $line, $matches) > 0) { pgsql8_parser_alter_function::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_GRANT_REVOKE, $line, $matches) > 0) { pgsql8_parser_grant_revoke::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_CONFIG_PARAMETER, $line, $matches) > 0) { pgsql8_parser_config_parameter::parse($database, self::get_whole_command($fp, $line)); } else { if (preg_match(self::PATTERN_SET, $line, $matches) > 0 || preg_match(self::PATTERN_COMMENT, $line, $matches) > 0 || preg_match(self::PATTERN_SELECT, $line, $matches) > 0 || preg_match(self::PATTERN_BEGIN_END, $line, $matches) > 0) { // @TODO: implement these pg_dump modifiers? self::get_whole_command($fp, $line); } else { throw new exception("Line did not match to any patterns: " . $line); } } } } } } } } } } } } } } } } } } } } } } $line = fgets($fp); /* Development debug: every line, save our current rendition of $database to disk xml_parser::save_xml(dirname(__FILE__) . '/../../../../dbsteward_monitor.xml', $database->asXML()); /**/ //echo $line . "\n"; } fclose($fp); } pgsql8::$track_pg_identifiers = false; return $database; }