-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuidelines.pas
248 lines (220 loc) · 5.85 KB
/
Guidelines.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
// Copyright 2019 Navid Madani
// Distributed under GNU General Public License Version 3.0, June 29, 2007
unit Guidelines;
interface
uses
Windows,
ShLwApi,
System.SysUtils,
System.Classes,
System.Generics.Collections,
FireDAC.Comp.Client,
FireDAC.Comp.DataSet,
FireDAC.Comp.UI,
FireDAC.Phys.Intf,
FireDAC.Phys,
FireDAC.Phys.SQLite,
FireDAC.Phys.SQLiteDef,
FireDAC.Stan.Async,
FireDAC.Stan.Def,
FireDAC.Stan.Error,
FireDAC.Stan.ExprFuncs,
FireDAC.Stan.Intf,
FireDAC.Stan.Option,
FireDAC.Stan.Param,
FireDAC.Stan.Pool,
FireDAC.DApt,
FireDAC.UI.Intf,
FireDAC.VCLUI.Wait,
{$IFDEF FireDACMonitor}
FireDAC.Moni.RemoteClient,
{$ENDIF}
Data.DB, Vcl.Graphics;
type
TGuideline = class
private
fOptionName: string;
fDefaultValue: string;
fGuideline: string;
public
constructor Create(const OptName, DefValue, GLn: string);
destructor Destroy; override;
property OptionName: string read fOptionName;
property DefaultValue: string read fDefaultValue;
property Guideline: string read fGuideline;
end;
TGuidelines = class
private
fGuideLines: TObjectList<TGuideline>;
function GetCount: Integer;
function GetOption(Index: integer): string;
function GetDefaultValue(Index: integer): string;
function GetGuideline(Option: string): string; overload;
procedure LoadDatabase;
public
constructor Create;
destructor Destroy; override;
function GetMaxOptionWidth(const Canvas: TCanvas): Integer;
property Guideline[Option: string]: string read GetGuideline; default;
property Count: Integer read GetCount;
property Option[Index: Integer]: string read GetOption;
property DefaultValue[Index: Integer]: string read GetDefaultValue;
end;
var
gGuidelines: TGuidelines;
implementation
const
CSQLitePooledName = 'SQLitePooled';
CSQLiteDriverName = 'SQLite';
CDatabaseName = 'RodentIIIOptions.db3';
var
lDatabaseName: string;
SQLiteConnectionPool: TFDManager;
oParams: TStrings;
{$IFDEF FireDACMonitor}
MoniLink: TFDMoniRemoteClientLink;
{$ENDIF}
{ TGuidelines }
constructor TGuidelines.Create;
begin
fGuideLines := TObjectList<TGuideline>.Create(True);
LoadDatabase;
end;
destructor TGuidelines.Destroy;
begin
fGuideLines.Free;
inherited;
end;
function TGuidelines.GetCount: Integer;
begin
Result := fGuideLines.Count;
end;
function TGuidelines.GetDefaultValue(Index: integer): string;
begin
if (Index < 0) or (Index >= fGuideLines.Count) then
raise EArgumentOutOfRangeException.Create('Index out of bounds');
Result := fGuideLines[Index].fDefaultValue;
end;
function TGuidelines.GetGuideline(Option: string): string;
var
gl: TGuideline;
begin
Result := '';
for gl in fGuideLines do
begin
if gl.OptionName = Option then
Exit(gl.Guideline);
end;
end;
function TGuidelines.GetMaxOptionWidth(const Canvas: TCanvas): Integer;
var
i: Integer;
MaxWidth: Integer;
begin
if Canvas = nil then
raise Exception.Create('Nil Canvas reference.');
Result := 0;
for i := 0 to fGuideLines.Count - 1 do
begin
MaxWidth := Canvas.TextWidth(fGuideLines[i].fOptionName);
if MaxWidth > Result then
Result := MaxWidth;
end;
end;
function TGuidelines.GetOption(Index: integer): string;
begin
if (Index < 0) or (Index >= fGuideLines.Count) then
raise EArgumentOutOfRangeException.Create('Index out of bounds');
Result := fGuideLines[Index].fOptionName;
end;
procedure TGuidelines.LoadDatabase;
var
lConn: TFDConnection;
lTable: TFDQuery;
begin
lTable := nil;
lConn := TFDConnection.Create(nil);
try
lConn.ConnectionDefName := CSQLitePooledName;
lTable := TFDQuery.Create(nil);
lTable.Connection := lConn;
lTable.Open('SELECT * FROM options o ORDER BY o.category_fk');
lTable.First;
while not lTable.Eof do
begin
fGuideLines.Add(TGuideline.Create(
lTable.Fields[1].AsString,
lTable.Fields[2].AsString,
lTable.Fields[3].AsString
));
lTable.Next;
end;
finally
lTable.Free;
lConn.Free;
end;
end;
{ TGuideline }
constructor TGuideline.Create(const OptName, DefValue, GLn: string);
begin
fOptionName := OptName;
fDefaultValue := DefValue;
fGuideline := GLn;
end;
destructor TGuideline.Destroy;
begin
fOptionName := '';
fDefaultValue := '';
fGuideline := '';
inherited;
end;
// Credit to:
// https://stackoverflow.com/questions/5329472/conversion-between-absolute-and-relative-paths-in-delphi
function RelativeToAbsolutePath(const RelativePath, BasePath: string): string;
var
Destination: array[0..MAX_PATH-1] of char;
begin
PathCanonicalize(@Destination[0], PChar(IncludeTrailingBackslash(BasePath) + RelativePath));
result := Destination;
end;
initialization
{$IFDEF FireDACMonitor}
MoniLink := TFDMoniRemoteClientLink.Create(nil);
MoniLink.Tracing := True;
{$ENDIF}
{ TODO -oNM -cImplementation : Ensure database is accessible and handle gracefully if not }
oParams := TSTringList.Create;
try
oParams.Add('DriverID=SQLite');
{$IFDEF DEBUG}
lDatabaseName := RelativeToAbsolutePath('..\..\Db',
ExtractFilePath(System.ParamStr(0))) + PathDelim + CDatabaseName;
{$ELSE}
lDatabaseName := CDatabaseName;
{$ENDIF}
oParams.Add('Database=' + lDatabaseName);
{$IFDEF FireDACMonitor}
oParams.Add('MonitorBy=Remote');
{$ENDIF}
oParams.Add('Pooled=True');
oParams.Add('LockingMode=Normal');
oParams.Add('Synchronous=Full');
oParams.Add('UpdateOptions.LockWait=True');
oParams.Add('BusyTimeout=10000');
oParams.Add('JournalMode=WAL');
oParams.Add('SharedCache=False');
oParams.Add('BusyTimeout=Normal');
oParams.Add('TxOptions.Isolation=xiSnapshot');
SQLiteConnectionPool := TFDManager.Create(nil);
SQLiteConnectionPool.AddConnectionDef(CSQLitePooledName, CSQLiteDriverName, oParams);
finally
oParams.Free;
end;
gGuidelines := TGuidelines.Create;
finalization
gGuidelines.Free;
SQLiteConnectionPool.Free;
{$IFDEF FireDACMonitor}
MoniLink.Free;
{$ENDIF}
end.