Compile subroutine Copy_Backlog_Records_To_SQL(VOID)
#pragma precomp SRP_PreCompiler

/*****************************************************************************\
	This is a utility function. For each record found in SQL_BACKLOG, it makes
	sure the identified record is copied to SQL. Then the backlog record is
	removed.
	
	History
	-------
	08/10/2010    KRF    Original Programmer
\*****************************************************************************/

$Insert APP_INSERTS

Declare subroutine Copy_Record_To_SQL, Delete_Record_From_SQL, SRP_TcpClient, Copy_Pending_Records_To_Sql
Declare function   GetTickCount, Database_Services, SRP_TcpClient, Datetime, Environment_Services

hSysLists = Database_Services('GetTableHandle', 'SYSLISTS')
Lock hSysLists, 'Copy_Backlog_Records_To_SQL' then

    Copy_Pending_Records_To_Sql()

    CurrDtm           = Datetime()
    TablesToSkip      = Database_Services('ReadDataRow', 'APP_INFO', 'SQL_TABLES_TO_SKIP')
    NonCriticalTables = Database_Services('ReadDataRow', 'APP_INFO', 'NONCRITICAL_SQL_TABLES')
    
    Open "SQL_PENDING" to pTable then
    
        Open "SQL_BACKLOG" to hTable then
            ClearSelect
            Select hTable
            Done = 0
            Loop
                ReadNext Key else Done = 1
                Command = ''
                Skip    = False$
            Until Done
                Table                  = Key[1, "*"]
                Locate Table in TablesToSkip by "AL" using "," setting ListPos then
                    Delete hTable, Key else null
                end else
                    Locate Table in NonCriticalTables using @FM setting Dummy then
                        ID = Key[Col2() + 1, Len(Key)]
                        
                        Read Dummy From pTable, Key then
                            Skip = True$
                        end
                        
                        If Skip EQ False$ then
                            Begin Case
                                Case ID[-7, 7] EQ "*DELETE"
                                    ID[-7, 7] = ""
                                    Command := 'RUN DELETE_RECORD_FROM_SQL ':Quote(Table):', ':Quote(ID):', 1, ':Quote(Key)
                                    Write CurrDtm to pTable, Key then Delete hTable, Key
                                Case ID[-6, 6] EQ "*WRITE"
                                    ID[-6, 6] = ""
                                    Command := 'RUN COPY_RECORD_TO_SQL ':Quote(Table):', ':Quote(ID):', 1, ':Quote(Key)
                                    Write CurrDtm to pTable, Key then Delete hTable, Key
                            End Case
                        end else
                            // This record is already being processed and we don't need to process it multiple times.
                            Delete hTable, Key else null
                        end
                    end else
                        TempKey                              = Key
                        TempKey[1, Len(Table) + 1]           = ''
                        TransTime                            = TempKey[-1, 'B*']
                        TempKey[-1, Neg(Len(TransTime) + 1)] = ''
                        TransDate                            = TempKey[-1, 'B*']
                        TempKey[-1, Neg(Len(TransDate) + 1)] = ''
                        Process                              = TempKey[-1, 'B*']
                        TempKey[-1, Neg(Len(Process) + 1)]   = ''
                        ID                                   = TempKey
                        If Process EQ 'DELETE' then
                            pKey = Key
                            Command := 'RUN DELETE_RECORD_FROM_SQL ':Quote(Table):', ':Quote(ID):', 1, ':Quote(pKey)
                            Write CurrDtm to pTable, pKey then Delete hTable, Key 
                        end else
                            pKey = Key
                            Command := 'RUN COPY_RECORD_TO_SQL ':Quote(Table):', ':Quote(ID):', 1, ':Quote(pKey)
                            Write CurrDtm to pTable, pKey then Delete hTable, Key 
                        end
                    end

                    If Skip EQ False$ then
                        TcpClientHandle = 0
                        ServerIP        = Environment_Services('GetApplicationRootIP')
                        Convert '\' to '' in ServerIP
                        If SRP_TcpClient(TcpClientHandle, 'CONNECT', ServerIP, '20779') then
                            SRP_TcpClient(TcpClientHandle, 'SEND_SES', Command)
                            SRP_TcpClient(TcpClientHandle, 'CLOSE_SES')
                        end
                    end
                end
            Repeat
        end
    end
    Unlock hSysLists, 'Copy_Backlog_Records_To_SQL' else Null
end

Return


