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

/*****************************************************************************\
	Deletes the given record from 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:
	
	DELETE_XYZ_RECORD_TO_SQL
	
	XYZ is a placeholder for a table name. For example, the ASM_PART table has
	a handler called DELETE_ASM_PART_RECORD_TO_SQL. If a table does not have a
	handler, then it is not supported by this procedure.
	
	History
	-------
	09/15/2010    KRF    Original Programmer
\*****************************************************************************/
$Insert APP_INSERTS
$insert MICROSOFT_ADO_EQUATES

Declare subroutine Sql_Services, SRP_COM
Declare function Sql_Services, Error_Services, SRP_COM, Environment_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
	
	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 = "DELETE_":Table:"_RECORD_FROM_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/ADO
					If SRP_Com(Connection, "CREATE", "ADODB.Connection") then
						// Connect to the database via ODBC    
						ConnectionString    = Environment_Services('GetSQLScrapeConnectionString')
						If SRP_Com(Connection, "CALL", "Open", ConnectionString) EQ "" then
							// Read the record and call the handler
							If Key NE "" then
								Ans = Function(@Handler(Connection, Key))
								If (Ans EQ '') then
									If Assigned(pKey) then
									    If (pKey NE '') then
									        Delete pTable, pKey else Null
									    end
									end
								end
							end
						end else
							Ans = 'Unable to open connection to SQL server. SQL connection error: ':SRP_Com(Connection, "ERROR")
						end
						SRP_Com(Connection, "CALL", "Close")
						SRP_Com(Connection, "RELEASE")
					end else
						Ans = "Unable to create ADO connection."
					end
				end
			end

			If LogError AND Len(Ans) then
				Open "SQL_ERROR" to hSqlError then
					Read Errors from hSqlError, Date() then
						Errors := @FM:Table:@VM:Key:@VM:Ans
						Write Errors to hSqlError, Date()
					end else
						Errors = Table:@VM:Key:@VM:Ans
						Write Errors to hSqlError, Date()
					end
				end
			end

			// Always log the result

			If Assigned(pKey) then
				If (pKey NE '') then
				    // Call unlock here in case an error was encountered above
				    Unlock pTable, pKey else Null
					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
	
Return Ans

