# # Database Utility Functions # # # get_database_directory() # # Purpose: This returns the pathname of the database directory for this profile # subroutine(get_database_directory(node profile), ( # echo("get_database_directory()"); # if (profile?{"database"} and profile{"database"}?{"server"} and profile{"database"}{"server"}?{"database_directory"}) then ( string database_directory; if (@profile{"database"}{"options"}{"server"}{"database_directory"} ne "") then ( database_directory = @profile{"database"}{"options"}{"server"}{"database_directory"}; ); else ( database_directory = internal.LOGANALYSISINFO_DIRECTORY . "Databases" . internal.directory_divider . node_name(profile); ); database_directory .= internal.directory_divider . "main" . internal.directory_divider; # error("No database_directory in profile"); database_directory; )); #### get_database_directory #### # # drop_table_if_exists() # # Purpose: This drops a table from the database, if it exists # # Parameters: table: the name of the table # subroutine(drop_table_if_exists(string tablename), ( # echo("drop_table_if_exists(); table=" . tablename); if (database_table_exists(tablename)) then ( string query = "drop table " . tablename; database_sql_query(query, true, false); ); )); #### drop_table_if_exists #### # # force_xref_table_rebuild() # # Purpose: This forces an xref table to be rebuilt (at next use) # # Parameters: xrefgroupnum: the xref group number (0 for the first) # subroutine(force_xref_table_rebuild(node profile, int xrefgroupnum), ( # echo("force_xref_table_rebuild()"); drop_table_if_exists(("xref" . xrefgroupnum)); string database_directory = get_database_directory(profile); string xref_info_pathname = database_directory . "main_table" . internal.directory_divider . "xrefs" . directory_divider . xrefgroupnum . ".cfg"; # echo("xref_info_pathname: " . xref_info_pathname); if (file_exists(xref_info_pathname)) then delete_file(xref_info_pathname); )); #### force_xref_table_rebuild() #### # # rewrite_database_config() # # Purpose: This rewrites the database "config" file, in the database directory, from the latest data in the profile # # Parameters: profile: the profile # subroutine(rewrite_database_config(node profile), ( # echo("rewrite_database_config()"); # node config; string database_directory = get_database_directory(profile); # echo("database_directory: " . database_directory); string config_pathname = database_directory . "config"; if (file_exists(config_pathname)) then ( # Read config string config_string = read_file(config_pathname); node config = string_to_node(config_string); # echo("config: " . node_as_string(config)); # Delete the existing database field, and xrefs, from config. While we're at it, may as well rebuild log fields too, which makes it a full sweep. delete_node(config{"database"}{"fields"}); delete_node(config{"log"}{"fields"}); delete_node(config{"database"}{"cross_reference_groups"}); # echo("cleaned config: " . node_as_string(config)); # Copy database.fields, log.fields, and database.cross_reference_groups from the profile to config. insert_node(config{"database"}, clone_node(profile{"database"}{"fields"}), 0); insert_node(config{"database"}, clone_node(profile{"database"}{"cross_reference_groups"}), 1); insert_node(config{"log"}, clone_node(profile{"log"}{"fields"}), 0); # echo("rebuilt config: " . node_as_string(config, false)); # Write config back to disk write_file(config_pathname, node_as_string(config)); #echo("Rewrote database confir to " . config_pathname); ); # if config exists )); #### rewrite_database_config #### # # get_database_table_name() # # Purpose: This gets the database table name to use in a query, given its # base name. It adds necessary prefixes and suffixes. # # Parameters: profile: the profile # table_name: the name of the table (base name) # returns the name to use in a query # subroutine(get_database_table_name(node profile, string table_name), ( string final_table_name = @profile{"database"}{"options"}{"server"}{"sql_table_name_prefix"} . table_name . @profile{"database"}{"options"}{"server"}{"sql_table_name_suffix"}; final_table_name; )); #### get_database_table_name() ####