Autor Beitrag
matze
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Do 16.03.06 22:42 
Hallo.

Hier möchte ich nun einmal meine Klasse TSQlHelper vorstellen. Diese Hilfsklasse hilft einem, bei langen SQL Querys den Quellcode schlank, sauber und vor allem übersichtlich zu halten.
Ich hatte die Idee für diese Klasse, als ich ein langes SQL Query basteln musste und vor lauter Hochkommas und Funktionen den Überblick im Querystring verloren hab.
Das SQL Query, das man dann aus der Klasse bekommt ist sauber und auch gegen SQL Injections gesichert.

Hier die Unit:
ausblenden volle Höhe Delphi-Quelltext
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
  Copyright:   Matthias Feist [matze]
  Website:    http://www.matf.de
  Mail:        SQlHelper@matf.de
 ===================================}


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

{ TSQLHelper }

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;
{
  function AddSlashes
  Thanks to delfiphan
  The Function was posted at:
    http://www.delphi-forum.de/viewtopic.php?p=341687#341687
}

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, 2else
    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.

Bei der Gelegenheit möchste ich auch nochmal user profile icondelfiphan für seine AddSlashes Funktion danken.

So arbeitet man mit der Klasse:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
var
  SQL: TSQLHelper;
begin
  SQL := TSQLHelper.Create;
  try
    SQL.Add('name','value');
    SQL.Add('name1','value1');
    SQL.database := 'test';
    showmessage (SQL.GetUpdate);
  finally
    SQL.Free;
  end;
end;

Man erhält dann
ausblenden SQL-Anweisung
1:
UPDATE `test` SET  `name` = 'value', `name1` = 'value1'					

oder bei der Verwendung von GetInsert
ausblenden SQL-Anweisung
1:
INSERT INTO `test` ( `name`, `name1`) VALUES ( 'value''value1')					

_________________
In the beginning was the word.
And the word was content-type: text/plain.
oern
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 142

Linux, Win XP
D7
BeitragVerfasst: So 19.03.06 10:58 
Hey sieht schon mal gut aus ich denke das wird mir manchmal helfen leider ist dein Helper etwas unvollständig was ich mir noch wünschen würde wäre SELECT abfragen mit WHERE und LIMIT bedingungen, weil die auch nicht grade einfach sind. :D

_________________
Ich hab eine Signatur
matze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: So 19.03.06 11:32 
Das wäre auch noch eine Idee. Mal schauen, ich werde es evtl noch einbauen.

_________________
In the beginning was the word.
And the word was content-type: text/plain.