Compile function Copy_REACT_EVENT_Record_To_SQL(Connection, Key, Record)

/*****************************************************************************\
	Copies the given REACT_EVENT record to the MSSQL database.
	
	History
	-------
	05/24/2012    KRF   Original Programmer
	05/09/2024    DJS   Fixed EVENT_TYPE reference because it is a calculated column.
\*****************************************************************************/

$insert REACT_EVENT_EQUATES

Declare function SQL_Write, SQL_Write_MV, SQL_Format, SRP_Com
Declare subroutine SRP_Com
Ans = ""

// Parse record into a dimensioned array for speed
Dim Rec(14)
MatParse Record into Rec
					    
// List of key names and their values
Keys  = "REACT_NO" :@VM:SQL_Format(Field(Key, "*", 1), "INT"):@FM
Keys := "EVENT_DTM":@VM:SQL_Format(Field(Key, "*", 2), "DATETIME")

// Special case to skip all this stuff if the record is already in SQL
Common /SQL_Copy_Commons/ SQL_NewRecordsOnly@
If SQL_NewRecordsOnly@ then
    KeyExpression = Keys
    Swap @VM with " = " in KeyExpression
    Swap @FM with " AND " in KeyExpression
    Script = "Select Count(*) From REACT_EVENT where ":KeyExpression
    RecordSetResult = SRP_Com(Connection, "CALL", "Execute", Script)
    RecordExists = SRP_Com(RecordSetResult, "CALL", "GetString", adClipString, 1, @VM, @FM)[1, \0D\]
    SRP_Com(RecordSetResult, "RELEASE")
    If RecordExists EQ "1" then Return ""
end

// List of data fields and their values
DataFields  = "COMMENT"         :@VM:SQL_Format(Rec(REACT_EVENT_COMMENT$),          "STR"):@FM
DataFields := "LOGGED_BY"       :@VM:SQL_Format(Rec(REACT_EVENT_LOGGED_BY$),        "STR"):@FM
DataFields := "EVENT_TYPE"      :@VM:SQL_Format({EVENT_TYPE},                       "STR"):@FM
DataFields := "REACT_MODE"      :@VM:SQL_Format(Rec(REACT_EVENT_REACT_MODE$),       "STR"):@FM
DataFields := "RDS_NO"          :@VM:SQL_Format(Rec(REACT_EVENT_RDS_NO$),           "STR"):@FM
DataFields := "REACT_PROB_CAT"  :@VM:SQL_Format(Rec(REACT_EVENT_REACT_PROB_CAT$),   "STR"):@FM
DataFields := "REACT_ESC_KEY"   :@VM:SQL_Format(Rec(REACT_EVENT_REACT_ESC_KEY$),    "STR"):@FM
DataFields := "STOP_DTM"        :@VM:SQL_Format(Rec(REACT_EVENT_STOP_DTM$),         "DATETIME"):@FM
DataFields := "WO_NO"           :@VM:SQL_Format(Rec(REACT_EVENT_WO_NO$),            "STR")

// Write the data to the SQL database
Ans = SQL_Write(Connection, "REACT_EVENT", Keys, DataFields)

//-------------------------------------------------------------------------------------------------
// Multi-valued Fields

// REACT_EVENT_REACT_UTIL_KEY
If Ans EQ "" AND Rec(REACT_EVENT_REACT_UTIL_KEY$) NE "" then
    
    // List of data fields and their values
    MvFields  = "REACT_UTIL_KEY":@FM:SQL_Format(Rec(REACT_EVENT_REACT_UTIL_KEY$), "STR"):@RM
    MvFields := "MC_MODE"       :@FM:SQL_Format({MC_MODE},                        "STR"):@RM
    MvFields := "MC_USER"       :@FM:SQL_Format({MC_USER},                        "STR")
    
    // Write the data to the SQL database
    Ans = SQL_Write_MV(Connection, "REACT_EVENT_REACT_UTIL_KEY", Keys, MvFields);
    
end

// REACT_EVENT_REACT_LOG_PD_KEY
If Ans EQ "" AND Rec(REACT_EVENT_REACT_LOG_PD_KEY$) NE "" then
    
    // List of data fields and their values
    MvFields  = "REACT_LOG_PD_KEY"  :@FM:SQL_Format(Rec(REACT_EVENT_REACT_LOG_PD_KEY$), "STR"):@RM
    MvFields := "REACT_LOG_NOTES_PD":@FM:SQL_Format({REACT_LOG_NOTES_PD},               "STR")
    
    // Write the data to the SQL database
    Ans = SQL_Write_MV(Connection, "REACT_EVENT_REACT_LOG_PD_KEY", Keys, MvFields);
    
end

// REACT_EVENT_REACT_LOG_KEY
If Ans EQ "" AND Rec(REACT_EVENT_REACT_LOG_KEY$) NE "" then
    
    // List of data fields and their values
    MvFields  = "REACT_LOG_KEY"      :@FM:SQL_Format(Rec(REACT_EVENT_REACT_LOG_KEY$), "STR"):@RM
    MvFields := "REACT_LOG_NOTES_MTC":@FM:SQL_Format({REACT_LOG_NOTES_MTC},           "STR")
    
    // Write the data to the SQL database
    Ans = SQL_Write_MV(Connection, "REACT_EVENT_REACT_LOG_KEY", Keys, MvFields);
    
end

// REACT_EVENT_REACT_MODE_KEYS
If Ans EQ "" AND Rec(REACT_EVENT_REACT_MODE_KEY$) NE "" then
    
    // List of data fields and their values
    MvFields  = "REACT_MODE_KEY":@FM:SQL_Format(Rec(REACT_EVENT_REACT_MODE_KEY$), "STR")
    
    // Write the data to the SQL database
    Ans = SQL_Write_MV(Connection, "REACT_EVENT_REACT_MODE_KEYS", Keys, MvFields);
    
end

Return Ans

