-
Notifications
You must be signed in to change notification settings - Fork 0
/
datadb.pas
325 lines (297 loc) · 11.5 KB
/
datadb.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
unit datadb;
{$mode delphi}
interface
uses
Classes,
SysUtils,
sqlite3conn,
sqldb,
db,
FileUtil,
oslog,
opsiconnection,
//progresswindow,
variants,
dialogs,
lazfileutils;
type
{ TDataModuleOCK }
TDataModuleOCK = class(TDataModule)
SQLite3Connection: TSQLite3Connection;
SQLQueryProductData: TSQLQuery;
SQLQueryProductDependencies: TSQLQuery;
SQLTransaction: TSQLTransaction;
procedure CreateDatabaseAndTables;
procedure LoadTableProductsIntoMemory;
procedure RemoveTableProductsFromMemory;
procedure OpsiProductsToDataset;
function SetActionRequestToDataset(fSelectedProduct: string; fActionRequest:string): boolean;
procedure SQLQueryProductDataAfterPost(Dataset: TDataset);
function GoToProduct(fSelectedProduct:string):boolean;
private
{ private declarations }
procedure CreateDatabaseTables(Connection: TSQLite3Connection);
public
{ public declarations }
constructor Create(AOwner: TComponent);override;
destructor Destroy;override;
end;
var
DataModuleOCK: TDataModuleOCK;
implementation
uses
opsiclientkioskgui;
{$R *.lfm}
{ TDataModuleOCK }
procedure TDataModuleOCK.SQLQueryProductDataAfterPost(Dataset: TDataset);
begin
try
TSQLQuery(Dataset).ApplyUpdates;
//TsqlQuery(dataset).Refresh;
//TSQLQuery(Dataset).Edit;
//SQLQueryProductData.Edit
except
on e: Exception do
begin
logdatei.log('Exception SQLQuery1AfterPost', LLError);
logdatei.log('Exception: ' + E.message, LLError);
logdatei.log('Exception handled at: ' + getCallAddrStr, LLError);
logdatei.log_exception(E,LLError);
end;
end;
end;
function TDataModuleOCK.GoToProduct(fSelectedProduct: string):boolean;
begin
with SQLQueryProductData do
begin
First;
//gefunden := Locate('ProductID', VarArrayOf([SelectedProduct]),[loCaseInsensitive]);
Result := Locate('ProductID',VarArrayOf([fSelectedProduct]),[loCaseInsensitive]);
end;
end;
constructor TDataModuleOCK.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
//InitDatabase;
//DataSourceProductData.Dataset := SQLQueryProductData;
FormOpsiClientKiosk.DataSourceProductData.Dataset := SQLQueryProductData;
FormOpsiClientKiosk.DataSourceProductDependencies.Dataset := SQLQueryProductDependencies;
//SQLQueryProductDependencies.DataSource := DataSourceProductData;
SQLQueryProductDependencies.DataSource := FormOpsiClientKiosk.DataSourceProductData;
end;
destructor TDataModuleOCK.Destroy;
begin
SQLite3Connection.Close;
inherited Destroy;
end;
procedure TDataModuleOCK.CreateDatabaseAndTables;
var
newFile: boolean;
begin
logdatei.log('Started CreateDatabaseAndTables ', LLNotice);
SQLite3Connection.Close; // Ensure the connection is closed when we start
try
{ Since we're making this database for the first time,
check whether the file already exists }
SQLite3Connection.DatabaseName := GetTempDir + 'opsikiosk.db';
logdatei.log('db is : ' + SQLite3Connection.DatabaseName, LLInfo);
if FileExists(SQLite3Connection.DatabaseName) then
DeleteFileUTF8(SQLite3Connection.DatabaseName);
newFile := not FileExists(SQLite3Connection.DatabaseName);
{ Create the database and the tables }
if newFile then
begin
try
logdatei.log('Creating new database ', LLNotice);
SQLite3Connection.Open;
SQLTransaction.StartTransaction;
CreateDatabaseTables(SQLite3Connection);
//ShowMessage('Succesfully created database.');
except
//ShowMessage('Unable to Create new Database');
on e: Exception do
begin
logdatei.log('Exception Unable to Create new Database', LLError);
logdatei.log('Exception: ' + E.message, LLError);
logdatei.log('Exception handled at: ' + getCallAddrStr, LLError);
logdatei.log_exception(E,LLError);
end;
end;
end;
except
//ShowMessage('Unable to check if database file exists');
on e: Exception do
begin
logdatei.log('Exception check if database file exists', LLError);
logdatei.log('Exception: ' + E.message, LLError);
logdatei.log('Exception handled at: ' + getCallAddrStr, LLError);
logdatei.log_exception(E,LLError);
end;
end;
{ Commit the transactions }
try
SQLTransaction.Commit;
except
on e: Exception do
begin
logdatei.log('Exception commit', LLError);
logdatei.log('Exception: ' + E.message, LLError);
logdatei.log('Exception handled at: ' + getCallAddrStr, LLError);
logdatei.log_exception(E,LLError);
end;
end;
logdatei.log('Finished CeateDatabaseAndTables', LLNotice);
end;
procedure TDataModuleOCK.LoadTableProductsIntoMemory;
begin
logdatei.log('Loading TABLE products into memory ordered by upper product name', LLInfo);
SQLQueryProductData.SQL.Text := 'SELECT * FROM products ORDER BY UPPER (ProductName)';
SQLTransaction.StartTransaction;
SQLQueryProductData.Open;
//SQLQueryProductData.First;
end;
procedure TDataModuleOCK.RemoveTableProductsFromMemory;
begin
logdatei.log('Removing TABLE products from memory', LLDebug);
DataModuleOCK.SQLQueryProductData.Close;
DataModuleOCK.SQLTransaction.Commit;
end;
procedure TDataModuleOCK.CreateDatabaseTables(Connection: TSQLite3Connection);
begin
{ CREATE TABLE products }
try
Connection.ExecuteDirect(
'CREATE TABLE products ('
+ 'ProductID STRING NOT NULL PRIMARY KEY, '
+ 'ProductName STRING, '
+ 'Description STRING, '
+ 'Advice STRING, '
+ 'ProductVersion STRING, '
+ 'PackageVersion STRING, '
+ 'VersionStr STRING, '
+ 'Priority INTEGER, '
+ 'ProductType STRING, '
+ 'InstallationStatus STRING, '
+ 'InstalledProdVer STRING, '
+ 'InstalledPackVer STRING, '
+ 'InstalledVerStr STRING, '
+ 'ActionRequest STRING, '
+ 'ActionResult STRING, '
+ 'UpdatePossible BOOLEAN, '
+ 'hasSetup BOOLEAN, '
+ 'hasUninstall BOOLEAN, '
+ 'possibleAction STRING'
+ ');'
);
logdatei.log('Success: CREATE TABLE products', LLInfo);
except
on e: Exception do
begin
logdatei.log('Exception CREATE TABLE products', LLError);
logdatei.log('Exception: ' + E.message, LLError);
logdatei.log('Exception handled at: ' + getCallAddrStr, LLError);
logdatei.log_exception(E,LLError);
end;
end;
{ CREATE TABLE dependencies}
try
Connection.ExecuteDirect(
'CREATE TABLE dependencies ('
+ 'ProductID STRING NOT NULL, '
+ 'RequiredProductID STRING, '
+ 'Required STRING, '
+ 'PreRequired STRING, '
+ 'PostRequired STRING, '
+ 'PRIMARY KEY(ProductID,RequiredProductID)'
+ ');'
);
logdatei.log('Success: CREATE TABLE dependencies ', LLInfo);
except
on e: Exception do
begin
logdatei.log('Exception CREATE TABLE dependencies', LLError);
logdatei.log('Exception: ' + E.message, LLError);
logdatei.log('Exception handled at: ' + getCallAddrStr, LLError);
logdatei.log_exception(E,LLError);
end;
end;
end;
procedure TDataModuleOCK.OpsiProductsToDataset;
var
i: integer;
SQLStatment: String;
//JSONObjectProduct: TJSONObject;
begin
//if SQLTransaction.Active then SQLTransaction.Active:=FALSE;
logdatei.log('Starting OpsiProductToDataset', LLNotice);
{ prepare database }
SQLStatment := 'SELECT * FROM products'; //ORDER BY UPPER(ProductName)';
SQLQueryProductData.SQL.Text := SQLStatment;
SQLTransaction.StartTransaction;
SQLQueryProductData.Open;
{ JSON to TABLE products }
logdatei.log('Insert product into local database:', LLInfo);
for i := 0 to OCKOpsiConnection.ProductCount - 1 do
begin
//JSONObjectProduct := TJSONObject(OCKOpsiConnection.JSONObjectProducts.Items[i]);
//logdatei.log('read: ' + JSONObjectProduct.Strings['productId'], LLInfo);
//with SQLQueryProductData do
//begin
OCKOpsiConnection.SelectProduct(i);
SQLQueryProductData.Append;
//if not JSONObjectProduct.Nulls['productId'] then
SQLQueryProductData.FieldByName('ProductID').AsString := OCKOpsiConnection.GetProductValueAsString('productId');
SQLQueryProductData.FieldByName('ProductVersion').AsString := OCKOpsiConnection.GetProductValueAsString('productVersion');
SQLQueryProductData.FieldByName('PackageVersion').AsString := OCKOpsiConnection.GetProductValueAsString('packageVersion');
SQLQueryProductData.FieldByName('VersionStr').AsString :=
OCKOpsiConnection.GetProductValueAsString('productVersion') + '-'
+ OCKOpsiConnection.GetProductValueAsString('packageVersion');
SQLQueryProductData.FieldByName('ProductName').AsString := OCKOpsiConnection.GetProductValueAsString('productName');
SQLQueryProductData.FieldByName('Description').AsString := OCKOpsiConnection.GetProductValueAsString('description');
SQLQueryProductData.FieldByName('Advice').AsString := OCKOpsiConnection.GetProductValueAsString('advice');
SQLQueryProductData.FieldByName('Priority').AsInteger := OCKOpsiConnection.GetProductValueAsInteger('priority');
SQLQueryProductData.FieldByName('ProductType').AsString := OCKOpsiConnection.GetProductValueAsString('productType');
SQLQueryProductData.FieldByName('hasSetup').AsBoolean := OCKOpsiConnection.GetProductValueAsBoolean('hasSetup');
SQLQueryProductData.FieldByName('hasUninstall').AsBoolean := OCKOpsiConnection.GetProductValueAsBoolean('hasUninstall');
if OCKOpsiConnection.GetProductValueAsString('installationStatus') = 'not_installed' then
SQLQueryProductData.FieldByName('InstallationStatus').AsString := ''
else
SQLQueryProductData.FieldByName('InstallationStatus').AsString := OCKOpsiConnection.GetProductValueAsString('installationStatus');
SQLQueryProductData.FieldByName('InstalledProdVer').AsString := OCKOpsiConnection.GetProductValueAsString('installedProdVer');
SQLQueryProductData.FieldByName('InstalledPackVer').AsString := OCKOpsiConnection.GetProductValueAsString('installedPackVer');
SQLQueryProductData.FieldByName('InstalledVerStr').AsString := OCKOpsiConnection.GetProductValueAsString('installedVerStr');
if OCKOpsiConnection.GetProductValueAsString('actionRequest') = 'none' then
SQLQueryProductData.FieldByName('ActionRequest').AsString := ''
else
SQLQueryProductData.FieldByName('ActionRequest').AsString := OCKOpsiConnection.GetProductValueAsString('actionRequest');
SQLQueryProductData.FieldByName('ActionResult').AsString := OCKOpsiConnection.GetProductValueAsString('actionResult');
SQLQueryProductData.FieldByName('UpdatePossible').AsBoolean := OCKOpsiConnection.GetProductValueAsBoolean('updatePossible');
SQLQueryProductData.FieldByName('PossibleAction').AsString := OCKOpsiConnection.GetProductValueAsString('possibleAction');
logdatei.log(' - ' + OCKOpsiConnection.GetProductValueAsString('productId') , LLInfo);
//end;
end;
SQLQueryProductData.ApplyUpdates;
SQLQueryProductData.Close;
SQLTransaction.Commit;
logdatei.log('Finished OpsiProductToDataset', LLNotice);
end;
function TDataModuleOCK.SetActionRequestToDataset(fSelectedProduct: string;
fActionRequest:string): boolean;
begin
with SQLQueryProductData do
begin
First;
//gefunden := Locate('ProductID', VarArrayOf([SelectedProduct]),[loCaseInsensitive]);
if Locate('ProductID',VarArrayOf([fSelectedProduct]),[loCaseInsensitive])
then
begin
Edit;
//FieldByName('ActionRequest').AsString:= fActionRequest;
SQLQueryProductData.FieldByName('ActionRequest').AsString := fActionRequest;
Post;
Result := True;
end;
Open;
end;
end;
end.