Compile subroutine Copy_Table_To_SQL(Table, OperationCode, NumToCopy)
#pragma precomp SRP_PreCompiler

/*****************************************************************************\
	This is a utility function. For every record in the table, it calls the
	Copy_Record_To_SQL method. Use this for new tables or when you want to
	re-sync the SQL database.
	
	History
	-------
	04/06/2010    KRF    Original Programmer
\*****************************************************************************/

Common /SQL_Copy_Commons/ SQL_NewRecordsOnly@

Equ ErrorsOnly$ to 1
Equ NewRecordsOnly$ to 2
Equ CalledFromCopyAllTables$ to 3

If Assigned(OperationCode) else OperationCode = 0
If Assigned(NumToCopy) else NumToCopy = 0
Errors = ""

Declare function Copy_Record_To_SQL, GetTickCount, Get.RecCount, GetASyncKeyState, SRP_Com
Declare subroutine SRP_Com

// Make sure table is uppercase
Convert @LOWER_CASE to @UPPER_CASE in Table

// Create the SYSLIST key
ErrorKey = "COPY_TABLE_TO_SQL_ERRORS*":Table

NumCopied = 0

StartTime = GetTickCount()
BeginTime = StartTime

SQL_NewRecordsOnly@ = (OperationCode EQ NewRecordsOnly$)

TablesToSkip = "ANNUAL_CONTRACTS,DESIG_ERR_GRP,INVOICE,MFC_LOC,NAMES,ORDER,ORDER_CHANGE,QUOTE,RDS,REACT_ESC,SURFACE_SCAN,TW_PRIME_USAGE,TW_USAGE,VISION_LOG,VISION_ORDER"
Locate Table in TablesToSkip by "AL" using "," setting ListPos then
    //Call Msg(@Window, Table:" skipped.":@FM:@FM:@FM:"!")
end else
 
    // The expected name of the handler
    Handler = "COPY_":Table:"_RECORD_TO_SQL"
    
    // Find the stored procedure that handles this table
    Open "SYSOBJ" to hSysObj then
        Read ObjCode from hSysObj, "$":Handler:"*LSL2" then
        
            // Connect to the ODBC
            If SRP_Com(Connection, "CREATE", "ADODB.Connection") then
                
                // Connect to the database via ODBC    
                SRP_Com(Connection, "SET", "Mode", adModeShareDenyNone)
                SRP_Com(Connection, "SET", "CursorLocation", adUseClient)
                ConnectionString    = 'Provider=SQLOLEDB.1;Password="0okm9ijn";Persist Security Info=True;User ID=srpadmin;Initial Catalog=LSL2SQL;Data Source=10.95.128.28\PROD1,53959'
                If SRP_Com(Connection, "CALL", "Open", ConnectionString) EQ "" then        
                    Open "SQL_BACKLOG" to hSqlBacklog then
                        If OperationCode EQ ErrorsOnly$ then
                            Open Table to hTable then
                                OldErrors = Xlate("SYSLISTS", ErrorKey, "", "X")
                                NumOldErrors = Count(OldErrors, @FM) + (OldErrors NE "")
                                RecCount = NumOldErrors
                                For iOldError = 1 to NumOldErrors
                                    Key = OldErrors<iOldError, 1>
                                    GoSub Copy_Key
                                Until (GetASyncKeyState(27) LT 0)
                                Next iOldError
                            end
                        end else
                            Open Table to hTable then
                                ClearSelect
                                Select hTable
                                RecCount = Get.RecCount(hTable, Status, 0)
                                Done = 0
                                Loop
                                    ReadNext Key else Done = 1
                                Until Done OR (GetASyncKeyState(27) LT 0)
                                    GoSub Copy_Key
                                    If NumToCopy GT 0 AND NumCopied GE NumToCopy then Done = 1
                                Repeat
                            end
                        end
                    end
    
                end else
                    Call Msg(@Window, SRP_Com(Connection, "ERROR"):@FM:@FM:@FM:"!")
                end
                
                SRP_Com(Connection, "CALL", "Close")
                SRP_Com(Connection, "RELEASE")
                
            end else
                Call Msg(@Window, "Unable to create ADO Object.":@FM:@FM:@FM:"!")
            end
        end else
            Call Msg(@Window, Handler:" not found.":@FM:@FM:@FM:"!")
        end
    end
end

Open "SYSLISTS" to hSYSLISTS then
    If Errors[-1, 1] EQ @FM then Errors[-1, 1] = ""
    If Len(Errors) then
        Write Errors to hSYSLISTS, ErrorKey
        Call SRP_Editor_Open("Record", "SYSLISTS":@FM:ErrorKey)
    end else
        Delete hSYSLISTS, ErrorKey else null
        If OperationCode NE CalledFromCopyAllTables$ then
            Call Msg(@Window, "No Errors")
        end
    end
end

Return

Copy_Key:

    // Read the record and call the handler
    Read @RECORD from hTable, Key then
        If Key NE "" then
            Open "DICT.":Table to @DICT then null
            @ID = Key
            Error = Function(@Handler(Connection, Key, @RECORD))
        end
    end

    If Len(Error) then
        Errors := Key:@VM:Error:@FM
    end else
        Delete hSqlBacklog, Table:"*":Key then null
    end
        
    NumCopied += 1
    CurrTime = GetTickCount()
    If CurrTime NE StartTime then
        TotalToCopy = If NumToCopy GT 0 then NumToCopy else RecCount
        AveTime = Int((CurrTime - BeginTime) / NumCopied)
        TimeRemaining = INT((AveTime * (TotalToCopy - NumCopied)) / 1000)
        Hours = Int(TimeRemaining / 3600) ; TimeRemaining = Mod(TimeRemaining, 3600)
        Minutes = Int(TimeRemaining / 60) ; TimeRemaining = Mod(TimeRemaining, 60)
        Countdown = Hours:"h ":Minutes:"m ":TimeRemaining:"s"
        Call SRP_Editor_Send_Info(Table:": ":NumCopied:" / ":TotalToCopy:" -- Time Remaining: ":Countdown)
        StartTime = GetTickCount()
    end
        
return

