delphi 版 sqlHelper第二版

  1 {
  2   odbc操作sqlite帮助类
  3   author:yywang
  4   date:2013-5-15
  5 }
  6 unit CommUtils;
  7 
  8 interface
  9 
 10 uses
 11   SysUtils, Windows, ActiveX, DB, ADODB, Generics.Collections;
 12 
 13 type
 14   TParameterMap = class
 15 
 16   private
 17     thisKey: string;
 18     thisValue: Variant;
 19     thisDataType: TDataType;
 20     thisSize: Integer;
 21   public
 22     constructor Create(key: string; value: Variant); overload;
 23     constructor Create(key: string; value: Variant; dataType: TFieldType;
 24       size: Integer); overload;
 25 
 26     function GetKey: string;
 27     function GetValue: Variant;
 28     function GetDataType: TDataType;
 29     function GetSize: Integer;
 30 
 31     procedure SetKey(key: string);
 32     procedure SetValue(value: Variant);
 33     procedure SetDataType(dataType: TDataType);
 34     procedure SetSize(size: Integer);
 35 
 36   published
 37     property key: string read GetKey write SetKey;
 38     property value: Variant read GetValue write SetValue;
 39     property dataType: TDataType read GetDataType write SetDataType;
 40     property size: Integer read GetSize write SetSize;
 41 
 42   end;
 43 
 44   TSqlHelper = class
 45 
 46   public
 47     { 执行单个插入更新删除sql }
 48     function ExecSQL(sql: string): Integer; overload;
 49 
 50     { 执行单个插入更新删除sql 参数化 }
 51     function ExceSQL(sql: string; parms: TList<TParameterMap>): Integer;
 52       overload;
 53 
 54     { 执行批量的sql插入更新删除 }
 55     function ButchExecSQL(sqls: TList<string>): Integer;
 56 
 57     { 获取单个值得查询 }
 58     function GetSingle(sql: string): Variant; overload;
 59 
 60     { 获取单个值得查询 参数化 }
 61     function GetSingle(sql: string; parms: TList<TParameterMap>): Variant;
 62       overload;
 63 
 64     { 获取一个连接 }
 65     function GetConnection(): TADOConnection;
 66 
 67     { 获取一个查询query对象 }
 68     function GetQuery(sql: string; connection: TADOConnection): TADOQuery;
 69       overload;
 70 
 71     { 获取一个query对象,参数化 }
 72     function GetQuery(sql: string; parms: TList<TParameterMap>;
 73       connection: TADOConnection): TADOQuery; overload;
 74 
 75     { 关闭连接 }
 76     procedure CloseConnection(conn: TADOConnection);
 77 
 78     { 关闭Query }
 79     procedure CloseQuery(query: TADOQuery);
 80 
 81     { 关闭连接及Query }
 82     procedure Close(query: TADOQuery; connection: TADOConnection);
 83 
 84   private
 85     { 私有 }
 86 
 87   end;
 88 
 89 implementation
 90 
 91 const
 92   constr: string =
 93     'Provider=MSDASQL.1;Persist Security Info=False;Data Source=SQLite3 Datasource';
 94 
 95 function TSqlHelper.ExecSQL(sql: string): Integer;
 96 begin
 97   Result := ExceSQL(sql, nil);
 98 end;
 99 
100 function TSqlHelper.ExceSQL(sql: string; parms: TList<TParameterMap>): Integer;
101 
102 var
103   command: TADOCommand;
104   conn: TADOConnection;
105   parm: TParameterMap;
106 begin
107   try
108     conn := GetConnection;
109     command := TADOCommand.Create(nil);
110     with command do
111     begin
112       connection := conn;
113       CommandText := sql;
114       if (parms <> nil) and (parms.Count > 0) then
115       begin
116         for parm in parms do
117         begin
118           Parameters.FindParam(parm.key).value := parm.value;
119           if (parm.size <> -1) then
120           begin
121             Parameters.FindParam(parm.key).dataType := parm.dataType;
122             Parameters.FindParam(parm.key).size := parm.size;
123           end;
124         end;
125       end;
126       Execute;
127     end;
128     Result := 1;
129   finally
130     command.Cancel;
131     CloseConnection(conn);
132   end;
133 end;
134 
135 function TSqlHelper.ButchExecSQL(sqls: TList<string>): Integer;
136 var
137   command: TADOCommand;
138   sql: string;
139   conn: TADOConnection;
140 begin
141   try
142     try
143       conn := GetConnection;
144       command := TADOCommand.Create(nil);
145       conn.BeginTrans;
146       with command do
147       begin
148         connection := conn;
149         for sql in sqls do
150         begin
151           if sql <> '' then
152           begin
153             CommandText := sql;
154             Execute;
155           end;
156         end;
157       end;
158       Result := 1;
159       conn.CommitTrans;
160     except
161       Result := 0;
162       conn.RollbackTrans;
163     end;
164   finally
165     command.Cancel;
166     CloseConnection(conn);
167   end;
168 end;
169 
170 function TSqlHelper.GetSingle(sql: string): Variant;
171 begin
172   Result := GetSingle(sql, nil);
173 end;
174 
175 function TSqlHelper.GetSingle(sql: string; parms: TList<TParameterMap>)
176   : Variant;
177 var
178   query: TADOQuery;
179   conn: TADOConnection;
180 begin
181   try
182     conn := GetConnection;
183     query := GetQuery(sql, parms, conn);
184     if query.RecordCount < 0 then
185       Result := '';
186     query.First;
187     Result := query.Fields.Fields[0].AsVariant;
188   finally
189     CloseQuery(query);
190     CloseConnection(conn);
191   end;
192 end;
193 
194 function TSqlHelper.GetQuery(sql: string;
195   connection: TADOConnection): TADOQuery;
196 begin
197   Result := GetQuery(sql, nil, connection);
198 end;
199 
200 function TSqlHelper.GetQuery(sql: string; parms: TList<TParameterMap>;
201   connection: TADOConnection): TADOQuery;
202 var
203   query: TADOQuery;
204   parm: TParameterMap;
205 begin
206   query := TADOQuery.Create(nil);
207   query.connection := connection;
208   query.sql.Add(sql);
209   if (parms <> nil) and (parms.Count > 0) then
210   begin
211     for parm in parms do
212     begin
213       query.Parameters.FindParam(parm.key).value := parm.value;
214       if (parm.size <> -1) then
215       begin
216         query.Parameters.FindParam(parm.key).dataType := parm.dataType;
217         query.Parameters.FindParam(parm.key).size := parm.size;
218       end;
219     end;
220   end;
221   query.Open;
222   Result := query;
223 end;
224 
225 function TSqlHelper.GetConnection: TADOConnection;
226 var
227   conn: TADOConnection;
228 begin
229   conn := TADOConnection.Create(nil);
230   conn.ConnectionString := constr;
231   conn.Open();
232   Result := conn;
233 end;
234 
235 procedure TSqlHelper.CloseQuery(query: TADOQuery);
236 begin
237   { if query.Active then }
238   query.Close;
239 end;
240 
241 procedure TSqlHelper.CloseConnection(conn: TADOConnection);
242 begin
243   if conn.Connected then
244     conn.Close;
245 end;
246 
247 procedure TSqlHelper.Close(query: TADOQuery; connection: TADOConnection);
248 begin
249   if query <> nil then
250     CloseQuery(query);
251   if connection <> nil then
252     CloseConnection(connection);
253 end;
254 
255 function TParameterMap.GetKey;
256 begin
257   Result := thisKey;
258 end;
259 
260 function TParameterMap.GetValue;
261 begin
262   Result := thisValue;
263 end;
264 
265 function TParameterMap.GetDataType;
266 begin
267   Result := thisDataType;
268 end;
269 
270 function TParameterMap.GetSize;
271 begin
272   Result := thisSize;
273 end;
274 
275 procedure TParameterMap.SetKey(key: string);
276 begin
277   thisKey := key;
278 end;
279 
280 procedure TParameterMap.SetValue(value: Variant);
281 begin
282   thisValue := value;
283 end;
284 
285 procedure TParameterMap.SetDataType(dataType: TFieldType);
286 begin
287   thisDataType := dataType;
288 end;
289 
290 procedure TParameterMap.SetSize(size: Integer);
291 begin
292   thisSize := size;
293 end;
294 
295 constructor TParameterMap.Create(key: string; value: Variant);
296 begin
297   thisValue := value;
298   thisKey := key;
299   thisDataType := ftUnknown;
300   thisSize := -1;
301 end;
302 
303 constructor TParameterMap.Create(key: string; value: Variant;
304   dataType: TFieldType; size: Integer);
305 begin
306   thisKey := key;
307   thisValue := value;
308   thisDataType := dataType;
309   thisSize := size;
310 end;
311 
312 initialization
313 
314 CoInitialize(nil);
315 
316 finalization
317 
318 CoUnInitialize;
319 
320 end.

 

你可能感兴趣的:(delphi 版 sqlHelper第二版)