-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbmodule.pas
254 lines (237 loc) · 8.4 KB
/
dbmodule.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
unit dbmodule;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, sqlite3conn, sqldb, db,
FileUtil,
lazfileutils,
oslog,
opsiconnection;
type
{ TDataModuleOCK1 }
TDataModuleOCK1 = class(TDataModule)
DataSourceProductData: TDataSource;
SQLite3Connection: TSQLite3Connection;
SQLQueryProductData: TSQLQuery;
SQLTransaction: TSQLTransaction;
private
procedure CreateDatabaseTables(Connection: TSQLite3Connection);
public
procedure InitDatabase;
procedure OpsiProductsToDataset(SQLQuery: TSQLQuery);
end;
var
DataModuleOCK1: TDataModuleOCK1;
implementation
{$R *.lfm}
procedure TDataModuleOCK1.InitDatabase;
var
newFile: boolean;
begin
logdatei.log('startinitdb ', LLInfo);
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 ', LLInfo);
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 InitDatabase', LLInfo);
end;
procedure TDataModuleOCK1.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 STRING, '
+ 'hasSetup STRING, '
+ 'hasUninstall STRING, '
+ 'possibleAction STRING'
+ ');'
);
logdatei.log('Finished 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('Finished 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 TDataModuleOCK1.OpsiProductsToDataset(SQLQuery:TSQLQuery);
var
i: integer;
Product: TProduct;
SQLStatment: String;
//productdatarecord: TProductData;
begin
//if SQLTransaction.Active then SQLTransaction.Active:=FALSE;
logdatei.log('starting OpsiProductToDataset ....', LLInfo);
{ product data to database }
SQLStatment := 'INSERT INTO products VALUES ('
+ ':ProductID, '
+ ':ProductName, '
+ ':Description, '
+ ':Advice, '
+ ':ProductVersion, '
+ ':PackageVersion, '
+ ':VersionStr, '
+ ':Priority, '
+ ':ProductType, '
+ ':InstallationStatus, '
+ ':InstalledProdVer, '
+ ':InstalledPackVer, '
+ ':InstalledVerStr, '
+ ':ActionRequest, '
+ ':ActionResult, '
+ ':UpdatePossible, '
+ ':hasSetup, '
+ ':hasUninstall, '
+ ':PossibleAction'
+ ');';
//SQLStatment := 'SELECT * FROM products ORDER BY UPPER(ProductName)';
SQLQueryProductData.SQL.Text := SQLStatment;
SQLTransaction.StartTransaction;
//SQLQueryProductData.Open;
//SQLQueryProductData.SQL.Clear;
//SQLQueryProductData.SQL.Add(SQLStatment);
for i := 0 to OCKOpsiConnection.LengthOpsiProducts - 1 do
begin
Product := OCKOpsiConnection.fetchProduct(i);
logdatei.log('read: ' + Product.ProductID, LLInfo);
{SQLQueryProductData.AppendRecord([
Product.ProductID,
Product.ProductName,
Product.Description,
Product.Advice,
Product.ProductVersion,
Product.PackageVersion,
Product.VersionStr,
Product.Priority,
Product.ProductType,
Product.InstallationStatus,
Product.InstalledProdVer,
Product.InstalledPackVer,
Product.InstalledVerStr,
Product.ActionRequest,
Product.ActionResult,
Product.UpdatePossible,
Product.hasSetup,
Product.hasUninstall,
Product.PossibleAction
]);}
//SQLQueryProductData.ExecSQL;
//SQLQueryProductData.Close;
with SQLQueryProductData do
begin
ParamByName('ProductID').AsString := Product.ProductID;
ParamByName('ProductVersion').AsString := Product.ProductVersion;
ParamByName('PackageVersion').AsString := Product.PackageVersion;
ParamByName('VersionStr').AsString := Product.VersionStr;
ParamByName('ProductName').AsString := Product.ProductName;
ParamByName('Description').AsString := Product.Description;
ParamByName('Advice').AsString := Product.Advice;
ParamByName('Priority').AsInteger := Product.Priority;
ParamByName('ProductType').AsString := Product.ProductType;
ParamByName('hasSetup').AsString := Product.hasSetup;
ParamByName('hasUninstall').AsString := Product.hasUninstall;
ParamByName('InstallationStatus').AsString := Product.InstallationStatus;
ParamByName('Installedprodver').AsString := Product.InstalledProdVer;
ParamByName('Installedpackver').AsString := Product.InstalledPackVer;
ParamByName('Installedverstr').AsString := Product.InstalledVerStr;
ParamByName('ActionRequest').AsString := Product.ActionRequest;
ParamByName('ActionResult').AsString := Product.ActionResult;
ParamByName('UpdatePossible').AsString := Product.UpdatePossible;
ParamByName('PossibleAction').AsString := Product.PossibleAction;
end;
SQLQueryProductData.ExecSQL;
end;
//SQLQueryProductData.ExecSQL;
SQLQueryProductData.Close;
SQLTransaction.Commit;
end;
end.