Compile function SQL_Insert(Connection, TableName, Keys, DataFields)

/*****************************************************************************\
	Writes a set of field-value pairs to the SQL connection.
	
	History
	-------
	04/05/2010    KRF   Original Programmer
	09/24/2010    KRF   Added special check for special case where insert fails
	                    because record already exists
\*****************************************************************************/

$insert Microsoft_Ado_Equates

If Assigned(Connection) else Connection = 0
If Assigned(TableName) else TableName = ""
If Assigned(Keys) else Keys = ""
If Assigned(DataFields) else DataFields = ""

Declare subroutine SRP_Com
Declare function SRP_Com, SRP_Rotate_Array
Ans = ""

// Rebuild the data field array omitting ones with blank values
NumDataFields = Count(DataFields, @FM) + (DataFields NE "")
LenDataFields = Len(DataFields)
DataFieldsSource = DataFields
DataFields = ""
DataFieldsPos = 1
LastVal = "B":@VM
Loop
    DataField = DataFieldsSource[DataFieldsPos, @FM]
    DataFieldsPos = Col2() + 1
    If DataField[-1, LastVal] NE "" then
        DataFields := DataField:@FM
    end
Until DataFieldsPos GE LenDataFields
Repeat
DataFields[-1, 1] = ""

// Create the where clause expression from the array of keys
KeyExpression = Keys
Swap @VM with " = " in KeyExpression
Swap @FM with " AND " in KeyExpression

Operation = "Insert"
GoSub Do_Insert
GoSub Run_Script

Return Ans

Run_Script:

    // Run the script
    If Script then
        RecordSetResult = SRP_Com(Connection, "CALL", "Execute", Script)
        If RecordSetResult NE 0 then
            SRP_Com(RecordSetResult, "RELEASE")
        end else
            DataOutput = ""
            Pos = 1
            Loop
                DataField = DataFields[Pos, @FM]
                Pos = Col2() + 1
            Until DataField EQ ""
                DataFieldName = DataField[1, @VM]
                DataFieldData = DataField[Col2() + 1, @FM]
                LenDataFieldData = Len(DataFieldData)
                Swap \0D0A\ with @TM in DataFieldData
                DataOutput := DataFieldName:", ":DataFieldData:", Len=":LenDataFieldData:@SVM
            Repeat
            DataOutput[-1, 1] = ""
            Swap \0D0A\ with @TM in Script
            Ans = "Error writing to ":TableName:" where ":KeyExpression:@VM:Operation:@VM:SRP_Com(Connection, "ERROR"):@VM:Script:@VM:DataOutput
        end
    end

return

Do_Insert:

    // Since an INSERT statement lists names separate from value, rotate the arrays
    // of keys and data fields so that it's easier to extract them separately
	
	// Initialize the list of names and values
	NameList = ""
	ValueList = ""
	
	// Separate the array of keys into parallel lists of key names and key values
	If Keys NE "" then
        KeyArray = SRP_Rotate_Array(Keys)
        NameList = KeyArray<1>
        ValueList = KeyArray<2>
	end
		
	// Separate the array of data fields into parallel lists of fields names and values
	If DataFields NE "" AND ValueList NE "" then
        DataFieldsRotated = SRP_Rotate_Array(DataFields)
        If NameList EQ "" then
            NameList = DataFieldsRotated<1>
        end else
            NameList := @VM:DataFieldsRotated<1>
        end
        If ValueList EQ "" then
            ValueList = DataFieldsRotated<2>
        end else
            ValueList := @VM:DataFieldsRotated<2>
        end
	end
	
	// Convert the multivalue lists into comma delimited ones
    Swap @VM with ", " in NameList
    Swap @VM with ", " in ValueList
	
	// Continue only if the lists are not empty
	If NameList NE "" AND ValueList NE "" then
	    Script = "Insert into ":TableName:" (":NameList:") values (":ValueList:")"
	end
	
return
