Skip to content

Instantly share code, notes, and snippets.

@jbratu
Last active June 23, 2021 14:01
Show Gist options
  • Save jbratu/8fe4e049925222bb0f03d6183f395350 to your computer and use it in GitHub Desktop.
Save jbratu/8fe4e049925222bb0f03d6183f395350 to your computer and use it in GitHub Desktop.
Given a list of UNC prefixes of tables and volumes that are currently attached in OpenInsight it will list out those tables and save them to a data structure that is suitable for reattaching with code.
Function CS_SAVE_SELECTED_ATTACHED_TABLES(void)
/*
Usage:
Run routine To parse the list of attached tables that meet the specified
criteria In FilterByUNCPrefix and save the list
To an OS file In a form that is suitable For copying And pasting into
source code For re-attaching the same list of tables.
*/
*Location to save the generated list of attached tables
OutputFile = "C:\temp\tables_" : @APPID<1>
*If not empty this CSV list of table names will be the only tables saved
FilterByTableName = ""
*FilterByTableName := "TABLE1,TABLE2"
Swap "," With @FM In FilterByTableName
*UNC Path prefixes for tables. I.e. \\SERVER-A\
*Only volumes matching these prefixes will be saved to our OutputFile.
*If this variable is null all tables except those in REVBOOT will be saved.
FilterByUNCPrefix = ""
*FilterByUNCPrefix<-1> = "\\SERVER-A\"
*FilterByUNCPrefix<-1> = "\\192.168.222.2\"
FilterByUNCPrefixCount = DCOUNT(FilterByUNCPrefix,@FM)
declare function Set_FSError
$Insert Logical
*Readnext through the systables which contains a list of currently attached tables.
TABLE_NAME = "SYSTABLES"
*Storage for list of attached tables
AttachEntry = ''
*Storage for list of aliases
AliasEntry = ''
*Internal data structure for list of attached tables
AttachEntryStruct = ''
AliasEntryStruct = ''
open TABLE_NAME To TABLE else
status = Set_FSError()
return
End
select TABLE
Done = False$
Loop
ReadNext @ID else Done = True$
Until Done Do
read @RECORD From TABLE, @ID Then
Gosub processRecord
End Else
status = Set_FSError()
return
End
Repeat
*Write the list of attached tables to a file
Swap @FM With \0D0A\ In AttachEntry
Swap @FM With \0D0A\ In AliasEntry
FileContents = AttachEntry : \0D0A\ : AliasEntry
OSWrite FileContents To OutputFile : ".txt"
OSWrite AttachEntryStruct To OutputFile : "_attach.dat"
OSWrite AliasEntryStruct To OutputFile : "_alias.dat"
return
*
* End Main
*
processRecord:
//Don't bother processing Index or Dictionary tables
If @ID[1,1] EQ "!" Then Return
If @ID[1,5] EQ "DICT." Then Return
//Check the volume against certain system volumes and exclude them
Vol = Field(@RECORD<1>, "*", 2)
*Exclude tables with no volume
If Vol = '' Then Return
If @ID NE @RECORD<2> then
//This is an alias because the attached name doesn't match the systables name
isAlias = True$
end else
isAlias = False$
end
*Exclude tables in system volumes
ExcludeList = "REVBOOT,REPOSITORY,MEMORY_RESIDENT,SYSCOLUMNS"
If InList(ExcludeList, Vol, ',') Then Return
If FilterByUNCPrefix EQ "" Then
Match = True$
End Else
Match = False$
For i = 1 To FilterByUNCPrefixCount
*Get one of the filters to check against
FilterToCheck = FilterByUNCPrefix<i>
*Does the current vol match our filter?
If FilterToCheck EQ Vol[1,Len(FilterToCheck)] Then
*Yes, we have a match
Match = True$
*Break the loop
i = FilterByUNCPrefixCount
End Else
*No, no match
End
Next
End
If FilterByTableName NE '' Then
*This was supplied a list of tables that should be saved.
*Only include this table if it matches the list.
TableName = @RECORD<2>
If InList(FilterByTableName, TableName, @FM) Then
Match = True$
End Else
Match = False$
End
End
If Match Then
TableName = @RECORD<2>
App = @RECORD<3>
if isAlias EQ False$ Then
*This is not an alias, do a normal attach.
*Build a list of tables in an openinsight data structure suitable for compiling in source code
*Generated output will look like:
*Tables<-1> = 'TEST_VOL,PERSON_INFO_TEST,GLOBAL' ;* TEST_VOL
*AttachEntry<-1> = "Tables<-1> = '" : VOL : "," : TableName : "," : APP : "' ;* " : VOL
*Without comment
AttachEntry<-1> = "Tables<-1> = '" : VOL : "," : TableName : "," : APP : "'"
AttachEntryStruct<-1> = VOL : @VM : TableName : @VM : APP
end else
*This is an alias so the table name is actually the alias name
AliasName = @ID
AliasEntry<-1> = "Aliases<-1> = '" : VOL : "," : TableName : "," : APP : "," : AliasName : "'"
AliasEntryStruct = VOL : @VM : TableName : @VM : APP : @VM : AliasName
end
End
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment