Compile function SQL_Format(Value, Conversion, Aux)

/*****************************************************************************\
	To keep from having to copy/paste conversion logic everywhere, all logic
	needed to correctly format data for use within SQL is in this format
	routine. This includes wrapping the data in quotes when and if
	appropriate.
	
	In all cases, when InVal is null, null is returned without any changes.
	This helps the SQL transfer system better identify fields that are being
	omitted.
	
	History
	-------
	04/06/2010    KRF    Original Programmer
	09/12/2012    KRF    Fixed DATETIME formatting
\*****************************************************************************/

If Assigned(Aux) else Aux = ""

// Copy the value so we don't trash it
ReturnVal = ""

// If we have a value, jump to the appropriate conversion
If Len(Value) then
    
    Conversions = "BIT,DATE,DATETIME,DEC,INT,STR,TIME"
    Locate Conversion in Conversions by "AL" using "," setting Pos then
        
        If Conversion EQ "STR" AND Assigned(Aux) then
            If Aux EQ 1 then
                Swap @VM with \0D0A\ in Value
                Swap @SVM with \0D0A\ in Value
            end
        end

        ValuePos = 1
        LenValue = Len(Value)
        Loop
            WorkingValue = Value[ValuePos, @VM]
            ValuePos = Col2() + 1
            On Pos GoSub BIT, DATE, DATETIME, DEC, INT, STR, TIME
            ReturnVal := Ans:@VM
        Until ValuePos GT LenValue
        Repeat
        ReturnVal[-1, 1] = ""
        
    end
    
end else
    
    If Conversion NE "BIT" AND Conversion NE "INT" AND Conversion NE "DEC" then
        ReturnVal = "''"
    end
    
end

Return ReturnVal

// BIT types must be 1 or 0, so this just makes sure we don't have anything other than that
BIT:
    If (WorkingValue NE 0 AND WorkingValue NE "") then
        Ans = -1
    end else
	    Ans = 0
    end
return

// DATE types must be in the format YYYY-MM-DD and are quoted
DATE:
	Ans = "'":OConv(WorkingValue, "D4-J"):"'"
return

// DATETIME types must be in the format YYYY-MM-DD HH:MM:SS and are quoted
DATETIME:
	Ans = "'":OConv(WorkingValue, "DTJ-^S"):"'"
return

// DEC are quoted and must have decimal positions specified
DEC:
    If WorkingValue then
        Swap "," with "" in WOrkingValue
        If Len(Aux) then
            Ans = "'":OConv(WorkingValue, "MD":Aux):"'"
        end else
            Ans = "'":WorkingValue"'"
        end
    end else
        Ans = ""
    end
return

// INT types are not quoted and can remain as-is (but we'll trim spaces out, incase of all blanks)
INT:
    Convert Char(9):" " to "" in WorkingValue
	Ans = WorkingValue
return

// STR types need to be quotes, apostrophies need to be escaped, and delimiters need to be dealt with
STR:
    Swap @TM with \0D0A\ in WorkingValue
    Swap "'" with "''" in WorkingValue
    Ans = "'":WorkingValue:"'"
return

// TIME types must be in the format HH:MM:SS and are quoted
TIME:
	Ans = "'":OConv(WorkingValue, "MTS"):"'"
return
