1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129:
|
unit SQLHelper;
interface
uses SysUtils;
type TSQLHelperValue = record Name: String; Value: String; end;
type TSQLHelper = class(TObject) private ValueList: Array of TSQLHelperValue; function AddSlashes(S: String): String; public database: String; Constructor Create; Procedure Add(Name:String; Value:String); Procedure Clear; Function GetInsert:string; Function GetUpdate:string; end;
implementation
procedure TSQLHelper.Add(Name, Value: String); begin setlength (ValueList,length(ValueList)+1); ValueList[high(ValueList)].Name := Name; ValueList[high(ValueList)].Value := Value; end;
function TSQLHelper.AddSlashes(S: String): String;
const EscapeChars = [#0,#26,#10,#13,'\','''','"']; var I, Size, L: Integer; R, W: PChar; begin R := PChar(S); if R=nil then exit; L := Length(S); Size := 0; for I := L downto 1 do begin if R^ in EscapeChars then inc(Size, 2) else inc(Size); inc(R); end; SetLength(Result, Size); R := PChar(S); W := PChar(Result); for I := L downto 1 do begin if R^ in EscapeChars then begin W^ := '\'; inc(W); end; W^ := R^; inc(R); inc(W); end; end;
procedure TSQLHelper.Clear; begin setlength (ValueList,0); end;
constructor TSQLHelper.Create; begin setlength (ValueList,0); database := ''; end;
function TSQLHelper.GetInsert: string; var i: Integer; tempNames: String; tempValues: String; begin if database <> '' then result := 'INSERT INTO `' + database + '` '; for i := 0 to high(ValueList) do begin tempNames := tempNames + ', `' + ValueList[i].Name + '`'; tempValues := tempValues + ', ''' + AddSlashes(ValueList[i].Value) + ''''; end; Delete (tempNames,1,1); Delete (tempValues,1,1); result := result + '(' + tempNames + ') VALUES (' + tempValues + ')'; end;
function TSQLHelper.GetUpdate: string; var i: Integer; temp: String; begin if database <> '' then result := 'UPDATE `' + database + '` SET '; for i := 0 to high(ValueList) do begin temp := temp + ', `' + ValueList[i].Name + '` = ''' + AddSlashes(ValueList[i].Value) + ''''; end; Delete (temp,1,1); result := result + temp; end;
end. |