Function SQL_MFS(Code, BFS, Handle, Name, FMC, Record, Status)
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//
// This program is proprietary and is not to be used by or disclosed to others, nor is it to	
// be copied without written permission from SRP Computer Solutions, Inc.						
//
// Name       : SQL_MFS
//
// Description: MFS for passing records to the SQL data tables
//
// Notes:       To install this MFS, use the SET_MFS(Table, "SQL_MFS*LSL2", 1) command to add it to
//              the end of the MFS chain. NOTE: for the best results. Make sure you are in
//              development engine with the database manager open. Then run the SET_MFS
//              command followed by running the menu option Database:Save in the DB Manager. 
//
// Parameters:																					
//    Code	    [in] -- An integer value indicating the operation to be performed (1 = read a
//                      record, 4 = delete a record, 11 = open a file, etc.)
//    BFS		[in] -- The list of MFSs and the BFS name for the current file or volume. This
//                      is a subvalue mark-delimited array, with the current MFS name as the
//                      first value in the array, and the BFS name as the last value.
//    Handle	[in] -- The file handle of the file or media map being accessed.
//    Name	    [in] -- The name (key) of the record or file being accessed.
//    FMC		[in] -- Various functions.
//    Record	[in] -- The entire record (for record-oriented functions) or a newly-created
//                      handle (for "get handle" functions).
//    Status	[in] -- A return code indicating the success or failure of an operation.
//
// History (Date, Initials, Notes)																
//    01/04/00   pcs   Original programmer
//    01/20/00   pcs   CLEARFILE requires the Arev table name to be removed before processing
//    05/20/05   axf   Process will now also track the accessing of records.
//    11/26/07   dmb   Code clean-up. No functional changes were made.
//    03/08/21	 dmb   Refactor method to store the OpenInsight table name so this MFS will
//					   chain forward correctly with other MFS routines.
//
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
$INSERT FILE.SYSTEM.EQUATES
$INSERT FSERRORS_HDR
$insert ACTION_SETUP

Declare Subroutine Pass_To_SQL, SRP_Stopwatch
Declare function    RetStack

ON CODE GOSUB READ.RECORD,READO.RECORD,WRITE.RECORD,DELETE.RECORD,LOCK.RECORD,UNLOCK.RECORD,SELECT,READNEXT,CLEARSELECT,CLEARFILE,OPEN.FILE,CREATE.FILE,RENAME.FILE,MOVE.FILE,DELETE.FILE,OPEN.MEDIA,CREATE.MEDIA,READ.MEDIA,WRITE.MEDIA,UNLOCK.ALL,FLUSH,INSTALL,RESERVED,RESERVED,RESERVED,OMNI.SCRIPT,CLOSE.MEDIA,RECORD.COUNT, REMAKE.FILE,CREATE.INDEX,DELETE.INDEX,UPDATE.INDEX,SELECT.INDEX,READNEXT.INDEX

Return

READ.RECORD:
	GoSub Call_NextFS
return

READO.RECORD:
	GoSub Call_NextFS
return

CREATE.MEDIA:
	GoSub Call_NextFS
return

OPEN.MEDIA:
	GoSub Call_NextFS
return

READ.MEDIA:
	GoSub Call_NextFS
return

WRITE.MEDIA:
	GoSub Call_NextFS
return

CLOSE.MEDIA:
	GoSub Call_NextFS
return

CLEARFILE:
	GoSub Call_NextFS
return

CREATE.FILE:
	GoSub Call_NextFS
return

DELETE.FILE:
	GoSub Call_NextFS
return

MOVE.FILE:
	GoSub Call_NextFS
return

OPEN.FILE:
	// Attaching Arev Table name to Handle for checking purposes
	GoSub Call_NextFS
* 	If Index(RECORD, @TM, 1) EQ 0 then
*	    RECORD = NAME:@TM:RECORD
* 	end
    // Load the handle and table name into the labelled common.
    If Status then
        TableName       = Name[1, '*']
        Accountname     = Name[Col2() + 1, '999']
        Volume          = Record[-1, 'B' : @TM]
        Volume          = Volume[14, 9999]
        Volume[-12, 12] = ''
        If Volume EQ '' then Volume = 'REVBOOT'
        Locate TableName in TableNames@ using @FM Setting fPos then
            If TableHandles@<fPos> EQ Record else
                // There is a new handle for the indicated table. This could be the same table name from a different
                // volume or an updated handle for the same table. Either way, just append a new handle/table pair
                // to the lookup arrays.
                TableNames@         := TableName     : @FM
                TableAccounts@      := AccountName   : @FM
                TableHandles@       := Record        : @FM
                TableVolumes@       := Volume        : @FM
            end
        end else
            TableNames@         := TableName     : @FM
            TableAccounts@      := AccountName   : @FM
            TableHandles@       := Record        : @FM
            TableVolumes@       := Volume        : @FM
        end
    end
return

REMAKE.FILE:
	GoSub Call_NextFS
return

RENAME.FILE:
	GoSub Call_NextFS
return

SELECT:
	GoSub Call_NextFS
return

READNEXT:
	GoSub Call_NextFS
return

CLEARSELECT:
	GoSub Call_NextFS
return

LOCK.RECORD:
	GoSub Call_NextFS
return

UNLOCK.RECORD:
	GoSub Call_NextFS
return

CREATE.INDEX:
	GoSub Call_NextFS
return

UPDATE.INDEX:
	GoSub Call_NextFS
return

DELETE.INDEX:
	GoSub Call_NextFS
return

SELECT.INDEX:
	GoSub Call_NextFS
return

READNEXT.INDEX:
	GoSub Call_NextFS
return

RESERVED:
	GoSub Call_NextFS
return

OMNI.SCRIPT:
	GoSub Call_NextFS
return

RECORD.COUNT:
	GoSub Call_NextFS
return

LOCK.SEMAPHORE:
	GoSub Call_NextFS
return

UNLOCK.SEMAPHORE:
	GoSub Call_NextFS
return

SET.USER.SEMAPHORE:
	GoSub Call_NextFS
return

WRITE.RECORD:
    GoSub Prep_Vars
    Activity = "Write"
    Pass_To_SQL(Activity, UseTable, UseID)
    GoSub Call_NextFS
return

DELETE.RECORD:
	GoSub Prep_Vars
    Activity = "Delete"
    Pass_To_SQL(Activity, UseTable, UseID)
	GoSub Call_NextFS
return

NEXT_FS:
	GoSub Call_NextFS
return

// Install, unlock all and flush are called directly, no need to call next FS.
INSTALL:
	STATUS = 1
return

FLUSH:
	STATUS = 1
return

UNLOCK.ALL:
	STATUS = 1
return

Prep_Vars:
*    TempUseTable = Field(HANDLE, @TM, 1)
*    UseTable = Field(TempUseTable, "*", 1)
	UseTable = TableName
    UseID = Name
return

Call_NextFS:
	FS = DELETE(BFS, 1, 1, 1)
	NEXTFS = FS<1, 1, 1>
	@FILE.ERROR = ""
	CALL @NEXTFS(CODE, FS, HANDLE, NAME, FMC, RECORD, STATUS)
return

Remove_Arev_Table_Name:
 	FS = DELETE(BFS, 1, 1, 1)
 	NEXTFS = FS<1, 1, 1>
 	@FILE.ERROR = ""
 	
 	If Index(FS, @SVM, 1) GT 0 then
	    Real_Handle = HANDLE
 	end else
	    Real_Handle = Field(HANDLE, @TM, 2)
 	end
 	
 	CALL @NEXTFS(CODE, FS, Real_Handle, NAME, FMC, RECORD, STATUS)
return
