-
Notifications
You must be signed in to change notification settings - Fork 84
/
Quick.Logger.Provider.ADODB.pas
265 lines (232 loc) · 9.92 KB
/
Quick.Logger.Provider.ADODB.pas
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
{ ***************************************************************************
Copyright (c) 2016-2018 Kike Pérez
Unit : Quick.Logger.Provider.ADO
Description : Log ADO DB Provider
Author : Kike Pérez
Version : 1.22
Created : 21/05/2018
Modified : 26/05/2018
This file is part of QuickLogger: https://github.com/exilon/QuickLogger
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Logger.Provider.ADODB;
{$i QuickLib.inc}
interface
uses
Classes,
SysUtils,
Data.Win.ADODB,
Winapi.ActiveX,
Quick.Commons,
Quick.Logger;
type
TFieldsMapping = class
private
fEventDate : string;
fEventType : string;
fMsg : string;
fEnvironment : string;
fPlatformInfo : string;
fOSVersion : string;
fAppName : string;
fUserName : string;
fHost : string;
public
property EventDate : string read fEventDate write fEventDate;
property EventType : string read fEventType write fEventType;
property Msg : string read fMsg write fMsg;
property Environment : string read fEnvironment write fEnvironment;
property PlatformInfo : string read fPlatformInfo write fPlatformInfo;
property OSVersion : string read fOSVersion write fOSVersion;
property AppName : string read fAppName write fAppName;
property UserName : string read fUserName write fUserName;
property Host : string read fHost write fHost;
end;
TDBProvider = (dbMSAccess2000, dbMSAccess2007, dbMSSQL, dbMSSQLnc10, dbMSSQLnc11, dbAS400);
const
{$IFDEF DELPHIXE7_UP}
ProviderName : array of string = ['Microsoft.Jet.OLEDB.4.0','Microsoft.ACE.OLEDB.12.0','SQLOLEDB.1','SQLNCLI10','SQLNCLI11','IBMDA400'];
{$ELSE}
ProviderName : array[0..5] of string = ('Microsoft.Jet.OLEDB.4.0','Microsoft.ACE.OLEDB.12.0','SQLOLEDB.1','SQLNCLI10','SQLNCLI11','IBMDA400');
{$ENDIF}
type
{$M+}
TDBConfig = class
private
fDBProvider : TDBProvider;
fServer : string;
fDatabase : string;
fTable : string;
fUserName : string;
fPassword : string;
published
property Provider : TDBProvider read fDBProvider write fDBProvider;
property Server : string read fServer write fServer;
property Database : string read fDatabase write fDatabase;
property Table : string read fTable write fTable;
property UserName : string read fUserName write fUserName;
property Password : string read fPassword write fPassword;
end;
{$M-}
TLogADODBProvider = class (TLogProviderBase)
private
fDBConnection : TADOConnection;
fDBQuery : TADOQuery;
fConnectionString : string;
fDBConfig : TDBConfig;
fFieldsMapping : TFieldsMapping;
function CreateConnectionString : string;
function CreateTable : Boolean;
procedure AddColumnToTable(const aColumnName, aDataType : string);
procedure AddToQuery(var aFields, aValues : string; const aNewField, aNewValue : string); overload;
//procedure AddToQuery(var aFields, aValues : string; const aNewField : string; aNewValue : Integer); overload;
public
constructor Create; override;
destructor Destroy; override;
property ConnectionString : string read fConnectionString write fConnectionString;
property DBConfig : TDBConfig read fDBConfig write fDBConfig;
property FieldsMapping : TFieldsMapping read fFieldsMapping write fFieldsMapping;
procedure Init; override;
procedure Restart; override;
procedure WriteLog(cLogItem : TLogItem); override;
end;
var
GlobalLogADODBProvider : TLogADODBProvider;
implementation
constructor TLogADODBProvider.Create;
begin
inherited;
CoInitialize(nil);
LogLevel := LOG_ALL;
//set default db fields mapping
fFieldsMapping := TFieldsMapping.Create;
fFieldsMapping.EventDate := 'EventDate';
fFieldsMapping.EventType := 'EventType';
fFieldsMapping.Msg := 'Msg';
fFieldsMapping.Environment := 'Environment';
fFieldsMapping.PlatformInfo := 'PlaftormInfo';
fFieldsMapping.OSVersion := 'OSVersion';
fFieldsMapping.AppName := 'AppName';
fFieldsMapping.UserName := 'UserName';
fFieldsMapping.Host := 'Host';
//set default db config
fDBConfig := TDBConfig.Create;
fDBConfig.Provider := dbMSSQL;
fDBConfig.Server := 'localhost';
fDBConfig.Table := 'Logger';
IncludedInfo := [iiAppName,iiHost];
end;
function TLogADODBProvider.CreateConnectionString: string;
begin
Result := Format('Provider=%s;Persist Security Info=False;User ID=%s;Password=%s;Database=%s;Data Source=%s',[
ProviderName[Integer(fDBConfig.Provider)],
fDBConfig.UserName,
fDBConfig.Password,
fDBConfig.Database,
fDBConfig.Server]);
end;
procedure TLogADODBProvider.AddColumnToTable(const aColumnName, aDataType : string);
begin
fDBQuery.SQL.Clear;
//fDBQuery.SQL.Add(Format('IF COL_LENGTH(''%s'', ''%s'') IS NULL',[fDBConfig.Table,aColumnName]));
fDBQuery.SQL.Add('IF COL_LENGTH(:LOGTABLE,:NEWFIELD) IS NULL');
fDBQuery.SQL.Add('BEGIN');
fDBQuery.SQL.Add(Format('ALTER TABLE %s',[fDBConfig.Table]));
fDBQuery.SQL.Add(Format('ADD %s %s',[aColumnName,aDataType]));
fDBQuery.SQL.Add('END');
fDBQuery.Parameters.ParamByName('LOGTABLE').Value := fDBConfig.Table;
fDBQuery.Parameters.ParamByName('NEWFIELD').Value := aColumnName;
if fDBQuery.ExecSQL = 0 then raise Exception.Create('Error creating table fields');
end;
function TLogADODBProvider.CreateTable: Boolean;
begin
fDBQuery.SQL.Clear;
fDBQuery.SQL.Add('IF NOT EXISTS (SELECT name FROM sys.tables WHERE name = :LOGTABLE)');
fDBQuery.SQL.Add(Format('CREATE TABLE %s (',[fDBConfig.Table]));
fDBQuery.SQL.Add(Format('%s DateTime,',[fFieldsMapping.EventDate]));
fDBQuery.SQL.Add(Format('%s varchar(20),',[fFieldsMapping.EventType]));
fDBQuery.SQL.Add(Format('%s varchar(max));',[fFieldsMapping.Msg]));
fDBQuery.Parameters.ParamByName('LOGTABLE').Value := fDBConfig.Table;
if fDBQuery.ExecSQL = 0 then raise Exception.Create('Error creating table!');
if iiEnvironment in IncludedInfo then AddColumnToTable(fFieldsMapping.Environment,'varchar(50)');
if iiPlatform in IncludedInfo then AddColumnToTable(fFieldsMapping.PlatformInfo,'varchar(50)');
if iiOSVersion in IncludedInfo then AddColumnToTable(fFieldsMapping.OSVersion,'varchar(70)');
if iiHost in IncludedInfo then AddColumnToTable(fFieldsMapping.Host,'varchar(20)');
if iiUserName in IncludedInfo then AddColumnToTable(fFieldsMapping.UserName,'varchar(50)');
if iiAppName in IncludedInfo then AddColumnToTable(fFieldsMapping.AppName,'varchar(70)');
Result := True;
end;
destructor TLogADODBProvider.Destroy;
begin
if Assigned(fDBQuery) then fDBQuery.Free;
if Assigned(fDBConnection) then fDBConnection.Free;
fFieldsMapping.Free;
fDBConfig.Free;
CoUninitialize;
inherited;
end;
procedure TLogADODBProvider.Init;
begin
fDBConnection := TADOConnection.Create(nil);
if fConnectionString <> '' then fDBConnection.ConnectionString := fConnectionString
else fConnectionString := CreateConnectionString;
fDBConnection.ConnectionString := fConnectionString;
fDBConnection.LoginPrompt := False;
fDBConnection.Connected := True;
fDBQuery := TADOQuery.Create(nil);
fDBQuery.Connection := fDBConnection;
CreateTable;
inherited;
end;
procedure TLogADODBProvider.Restart;
begin
Stop;
if Assigned(fDBQuery) then fDBQuery.Free;
if Assigned(fDBConnection) then fDBConnection.Free;
Init;
end;
procedure TLogADODBProvider.AddToQuery(var aFields, aValues : string; const aNewField, aNewValue : string);
begin
aFields := Format('%s,%s',[aFields,aNewField]);
aValues := Format('%s,''%s''',[aValues,aNewValue]);
end;
//procedure TLogADODBProvider.AddToQuery(var aFields, aValues : string; const aNewField : string; aNewValue : Integer);
//begin
// aFields := Format('%s,%s',[aFields,aNewField]);
// aValues := Format('%s,%d',[aValues,aNewValue]);
//end;
procedure TLogADODBProvider.WriteLog(cLogItem : TLogItem);
var
fields : string;
values : string;
begin
//prepare fields and values for insert query
fields := fFieldsMapping.EventDate;
values := Format('''%s''',[DateTimeToStr(cLogItem.EventDate,FormatSettings)]);
AddToQuery(fields,values,fFieldsMapping.EventType,EventTypeName[cLogItem.EventType]);
if iiHost in IncludedInfo then AddToQuery(fields,values,fFieldsMapping.Host,SystemInfo.HostName);
if iiAppName in IncludedInfo then AddToQuery(fields,values,fFieldsMapping.AppName,SystemInfo.AppName);
if iiEnvironment in IncludedInfo then AddToQuery(fields,values,fFieldsMapping.Environment,Environment);
if iiOSVersion in IncludedInfo then AddToQuery(fields,values,fFieldsMapping.OSVersion,SystemInfo.OsVersion);
if iiPlatform in IncludedInfo then AddToQuery(fields,values,fFieldsMapping.PlatformInfo,PlatformInfo);
if iiUserName in IncludedInfo then AddToQuery(fields,values,fFieldsMapping.UserName,SystemInfo.UserName);
AddToQuery(fields,values,fFieldsMapping.Msg,StringReplace(cLogItem.Msg,'''','"',[rfReplaceAll]));
fDBQuery.SQL.Clear;
fDBQuery.SQL.Add(Format('INSERT INTO %s (%s)',[fDBConfig.Table,fields]));
fDBQuery.SQL.Add(Format('VALUES (%s)',[values]));
if fDBQuery.ExecSQL = 0 then raise ELogger.Create('[TLogADODBProvider] : Error trying to write to ADODB');
end;
initialization
GlobalLogADODBProvider := TLogADODBProvider.Create;
finalization
if Assigned(GlobalLogADODBProvider) and (GlobalLogADODBProvider.RefCount = 0) then GlobalLogADODBProvider.Free;
end.