Compile function Copy_Record_To_SQL(Table, Key, LogError, pKey)

/*****************************************************************************\
	Copies the given record to the MSSQL database. Since every table differs
	from the next, it's not really possible to use the same code base for all
	tables. Therefore, common functionality has been moved into separate
	stored procedures. Supported tables are ones that have a stored procedure
	that handles the copy request. These are called the Handlers. A Handler
	is identified by the following naming convention:
	
	COPY_XYZ_RECORD_TO_SQL
	
	XYZ is a placeholder for a table name. For example, the ASM_PART table has
	a handler called COPY_ASM_PART_RECORD_TO_SQL. If a table does not have a
	handler, then it is not supported by this procedure.
	
	History
	-------
	03/22/2010    KRF   Original Programmer
	04/05/2010    KRF   Modified to lookup handler in SYSOBJ instead of using
						large Locate list 
	09/23/2010    KRF   Added logic to set @DICT, @ID, and @RECORD so handlers
	                    can use symbolics
\*****************************************************************************/
$Insert APP_INSERTS
$Insert MICROSOFT_ADO_EQUATES

Declare subroutine SRP_Com, Sql_Services
Declare function SRP_Com, Environment_Services, Datetime, Sql_Services, Error_Services, Database_Services, Unassigned

Main:
	
	If Unassigned(LogError) then LogError = False$
	If Unassigned(pKey) 	then pKey	  = ''
	Ans  = ""
	Done = False$
	// Make sure table is uppercase
	Convert @LOWER_CASE to @UPPER_CASE in Table

	TablesToSkip = Database_Services('ReadDataRow', 'APP_INFO', 'SQL_TABLES_TO_SKIP')
	Locate Table in TablesToSkip by "AL" using "," setting ListPos else
		
		Open 'SQL_PENDING' to pTable then
			
			If pKey NE '' then Lock pTable, pKey else Done = True$
			
			If Not(Done) then 
				// 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
							ConnectionString = Environment_Services('GetSQLScrapeConnectionString')
							If SRP_Com(Connection, "CALL", "Open", ConnectionString) EQ "" then   
								// Read the record and call the handler
								Open Table to hTable then
									Read @RECORD from hTable, Key then
										If Key NE "" then
											Open "DICT.":Table to @DICT then null
											@ID = Key
											Ans = Function(@Handler(Connection, Key, @RECORD))
											If Ans EQ '' then
												If Assigned(pKey) then
													If pKey NE '' then
														Delete pTable, pKey else Null
														Unlock pTable, pKey else Null	
													end
												end
											end
										end
									end else
										Ans = 'Unable to read ':Key:' from ':Table:' table.'
									end
								end
							end else
								Ans  = 'Unable to open connection to SQL server. SQL connection error: '
								Ans := SRP_Com(Connection, "ERROR")
							end
							SRP_Com(Connection, "CALL", "Close")
							SRP_Com(Connection, "RELEASE")
						end else
							Ans = "Unable to create ADO connection."
						end
					end
					
					If LogError and Len(Ans) then    
						Open "SQL_ERROR" to hSqlError then
							ErrorKey  = OConv(Date(), "D4-J")
							Timestamp = OConv(Datetime(), 'DT')
							Read Errors from hSqlError, ErrorKey then
								Errors := @FM:Table:@VM:Key:@VM:Timestamp:@VM:Ans
								Write Errors to hSqlError, ErrorKey
							end else
								Errors = Table:@VM:Key:@VM:Timestamp:@VM:Ans
								Write Errors to hSqlError, ErrorKey
							end
						end
					end
					
					If Assigned(pKey) then
						If (pKey NE '') then 
							// Always log the result
							Open 'SQL_LOG' to hLog then
								If Ans EQ '' then
									Result = 'PROCESSED'
								end else
									Result = Ans
								end
								CurrDate = Date()
								Read Log from hLog, CurrDate then
									Log := @FM:pKey:@VM:Result
								end else
									Log = pKey:@VM:Result
								end
								Write Log to hLog, CurrDate
							end
						end
					end
				end
			end
		end
	end
    
Return Ans

