-
Notifications
You must be signed in to change notification settings - Fork 2
/
GettingStarted.Main.DataModule.pas
289 lines (238 loc) · 7.46 KB
/
GettingStarted.Main.DataModule.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
unit GettingStarted.Main.DataModule;
interface
uses
System.SysUtils,
System.Classes,
Data.DB,
FireDAC.Stan.Intf,
FireDAC.DatS,
FireDAC.Stan.Option,
FireDAC.Stan.Param,
FireDAC.Phys.Intf,
FireDAC.DApt.Intf,
FireDAC.Comp.Client,
FireDAC.Comp.DataSet,
FireDAC.Comp.UI,
FireDAC.Stan.Consts,
FireDAC.VCLUI.Controls,
FireDAC.Stan.Error,
FireDAC.Stan.Async,
FireDAC.DApt,
FireDAC.Phys,
FireDAC.UI.Intf,
FireDAC.Stan.Def,
FireDAC.Stan.Pool,
FireDAC.VCLUI.Wait,
FireDAC.VCLUI.Error,
FireDAC.VCLUI.Login,
FireDAC.Stan.ExprFuncs,
FireDAC.Phys.SQLite,
FireDAC.Phys.SQLiteWrapper,
FireDAC.Phys.SQLiteDef;
type
TDatabaseAction = (
SetPassword,
RemovePassword,
ChangePassword
);
TDatabaseValidation = (
Analyze,
CheckOnly,
Sweep
);
TOnDatabaseLog = reference to procedure(const Text: string);
TMainDataModule = class(TDataModule)
BackupSQLite: TFDSQLiteBackup;
CategoriesQuery: TFDQuery;
DatabaseConnection: TFDConnection;
DriverLinkSQLite: TFDPhysSQLiteDriverLink;
ErrorDialogFireDAC: TFDGUIxErrorDialog;
FunctionSQLite: TFDSQLiteFunction;
LoginDialogFireDAC: TFDGUIxLoginDialog;
ProductsQuery: TFDQuery;
SecuritySQLite: TFDSQLiteSecurity;
ValidateSQLite: TFDSQLiteValidate;
WaitCursorFireDAC: TFDGUIxWaitCursor;
CategoriesDataSource: TDataSource;
///
procedure DatabaseConnectionBeforeConnect(Sender: TObject);
procedure DatabaseConnectionBeforeDisconnect(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
procedure FunctionSQLiteCalculate(AFunc: TSQLiteFunctionInstance; AInputs: TSQLiteInputs; AOutput: TSQLiteOutput; var AUserData: TObject);
procedure ValidateSQLiteProgress(ASender: TFDPhysDriverService; const AMessage: String);
private
FOnDatabaseLog: TOnDatabaseLog;
procedure WriteLog(const Text: string);
public
function GetConnectionDefFileName: string;
function IsConnected: Boolean;
///
function Insert(const Categoria, Descricao: string; Picture: Integer): Integer;
procedure Delete(const Categoria: string);
procedure Update(Valor1, Valor2: Integer);
///
procedure Backup(const Database, Password, BackupDatabase, BackupPassword: string);
procedure OpenDatabase(const FileName: string);
procedure SetDatabasePassword(const Database, Password, NewPassword: string; Action: TDatabaseAction);
procedure ValidateDatabase(const Database, Password: string; ValidationAction: TDatabaseValidation);
procedure OpenDefaultDatabase;
///
property OnDatabaseLog: TOnDatabaseLog read FOnDatabaseLog write FOnDatabaseLog;
end;
EDataValidation = class(Exception);
var
MainDataModule: TMainDataModule;
implementation
{%CLASSGROUP 'Vcl.Controls.TControl'}
{$R *.dfm}
uses
GettingStarted.Consts,
GettingStarted.Sql,
GettingStarted.Config;
procedure TMainDataModule.DataModuleDestroy(Sender: TObject);
begin
CategoriesQuery .Close;
ProductsQuery .Close;
DatabaseConnection .Close;
end;
procedure TMainDataModule.Backup(const Database, Password, BackupDatabase, BackupPassword: string);
begin
if Database = '' then
Exit;
BackupSQLite.Database := Database;
BackupSQLite.Password := Password;
BackupSQLite.DestDatabase := BackupDatabase;
BackupSQLite.DestPassword := BackupPassword;
BackupSQLite.Backup;
end;
procedure TMainDataModule.DatabaseConnectionBeforeConnect(Sender: TObject);
begin
FunctionSQLite.Active := True;
end;
procedure TMainDataModule.DatabaseConnectionBeforeDisconnect(Sender: TObject);
begin
FunctionSQLite.Active := False;
end;
procedure TMainDataModule.Delete(const Categoria: string);
begin
if not DatabaseConnection.Connected then
Exit;
// Delete a record
DatabaseConnection.ExecSQL(DELETE_CATEGORIES_SQL, [Categoria]);
CategoriesQuery.Refresh;
end;
function TMainDataModule.GetConnectionDefFileName: string;
begin
Result := FDManager.ActualConnectionDefFileName;
end;
function TMainDataModule.Insert(const Categoria, Descricao: string; Picture: Integer): Integer;
var
CategoryID: Integer;
begin
if not IsConnected then
Exit(-1);
if Categoria = '' then
begin
raise EDataValidation.Create(SREQUIRED_CATEGORY_ERROR);
end;
if Descricao = '' then
begin
raise EDataValidation.Create(SREQUIRED_DESCRIPTION_ERROR);
end;
// Insert a record
DatabaseConnection.ExecSQL(INSERT_CATEGORIES_SQL, [Categoria, Descricao, Picture]);
CategoriesQuery.Refresh;
// Get a scalar value from DB
CategoryID := DatabaseConnection.ExecSQLScalar(SELECT_MAX_CATEGORYID_SQL);
Result := CategoryID;
end;
function TMainDataModule.IsConnected: Boolean;
begin
Result := DatabaseConnection.Connected;
end;
procedure TMainDataModule.OpenDatabase(const FileName: string);
begin
DatabaseConnection.Close;
// create temporary connection definition
DatabaseConnection.Params.Clear;
DatabaseConnection.Params.Add(DRIVERID_SQLITE_PARAM);
DatabaseConnection.Params.Add(SDATABASE_PARAM + FileName);
DatabaseConnection.Open;
CategoriesQuery.Open;
ProductsQuery.Open;
end;
procedure TMainDataModule.SetDatabasePassword(const Database, Password, NewPassword: string; Action: TDatabaseAction);
begin
if Database = '' then
Exit;
SecuritySQLite.Database := Database;
SecuritySQLite.Password := Password;
SecuritySQLite.ToPassword := Password;
case Action of
TDatabaseAction.SetPassword:
SecuritySQLite.SetPassword;
TDatabaseAction.RemovePassword:
SecuritySQLite.RemovePassword;
TDatabaseAction.ChangePassword:
SecuritySQLite.ChangePassword;
end;
end;
procedure TMainDataModule.FunctionSQLiteCalculate(AFunc: TSQLiteFunctionInstance; AInputs: TSQLiteInputs; AOutput: TSQLiteOutput; var AUserData: TObject);
begin
AOutput.AsCurrency := AInputs[0].AsCurrency * AInputs[1].AsInteger;
end;
procedure TMainDataModule.OpenDefaultDatabase;
var
Config: TConfig;
begin
Config := TConfig.Create;
try
if not Config.FileExists then
begin
Config.DatabaseFile := TConfig.GetDefaultDatabaseFileName;
Config.Save;
end;
Config.Load;
OpenDatabase(Config.DatabaseFile );
finally
Config.Free;
end;
end;
procedure TMainDataModule.ValidateSQLiteProgress(ASender: TFDPhysDriverService; const AMessage: String);
begin
WriteLog(AMessage);
end;
procedure TMainDataModule.Update(Valor1, Valor2: Integer);
begin
if not IsConnected then
Exit;
// Update records
DatabaseConnection.ExecSQL(UPDATE_PRODUCTS_SQL, [Valor1, Valor2]);
ProductsQuery.Refresh;
end;
procedure TMainDataModule.ValidateDatabase(const Database, Password: string; ValidationAction: TDatabaseValidation);
begin
if Database = '' then
Exit;
ValidateSQLite.Database := Database;
ValidateSQLite.Password := Password;
case ValidationAction of
TDatabaseValidation.Analyze:
ValidateSQLite.Analyze;
TDatabaseValidation.CheckOnly:
if not ValidateSQLite.CheckOnly then
WriteLog(SDATABASE_HAS_PROBLEMS_ERROR)
else
WriteLog(SDATABASE_IS_VALID_MESSAGE);
TDatabaseValidation.Sweep:
ValidateSQLite.Sweep;
end;
end;
procedure TMainDataModule.WriteLog(const Text: string);
begin
if Assigned(FOnDatabaseLog) then
begin
FOnDatabaseLog(Text);
end;
end;
end.