-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathfmMain.pas
303 lines (281 loc) · 8.83 KB
/
fmMain.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
unit fmMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
System.Types, System.UITypes,
ComCtrls, ExtCtrls, StdCtrls, PythonEngine, Vcl.PythonGUIInputOutput, Db,
Grids, DBGrids, Datasnap.DBClient, FireDAC.Stan.Intf, FireDAC.Stan.Option,
FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.FMXUI.Wait,
FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.DApt,
FireDAC.Comp.Client, FireDAC.Comp.DataSet, FireDAC.Comp.UI,
FireDAC.Phys.SQLite, FireDAC.Phys.SQLiteDef, FireDAC.Stan.ExprFuncs,
WrapDelphi, WrapFireDac, SynEditHighlighter, SynHighlighterPython, SynEdit,
SynEditCodeFolding, sqlTimSt, FireDAC.VCLUI.Wait,
FireDAC.Phys.SQLiteWrapper.Stat;
type
TMain = class(TForm)
RichEdit1: TRichEdit;
Splitter1: TSplitter;
PythonGUIInputOutput: TPythonGUIInputOutput;
modDBFireDac: TPythonModule;
dsrcCustomer: TDataSource;
DataSource2: TDataSource;
Panel6: TPanel;
btnSQLTest: TButton;
cobxConnSQLServer: TComboBox;
Label5: TLabel;
Connection: TFDConnection;
mqSrcTables: TFDMetaInfoQuery;
SynPythonSyn: TSynPythonSyn;
PythonEngine: TPythonEngine;
tblCustomer: TFDTable;
PageControl: TPageControl;
TabSheet1: TTabSheet;
Splitter7: TSplitter;
SynEditScript1: TSynEdit;
Panel7: TPanel;
Label1: TLabel;
Label2: TLabel;
btnExecuteExample1: TButton;
TabSheet2: TTabSheet;
Splitter3: TSplitter;
Panel1: TPanel;
btnExecuteExample2: TButton;
DBGrid1: TDBGrid;
SynEditScript2: TSynEdit;
TabSheet4: TTabSheet;
Splitter4: TSplitter;
Panel3: TPanel;
btnExecuteExample3: TButton;
DBGrid2: TDBGrid;
SynEditScript3: TSynEdit;
TabSheet5: TTabSheet;
Splitter2: TSplitter;
Panel2: TPanel;
btnExecuteExample4: TButton;
SynEditScript4: TSynEdit;
PyDelphiWrapper: TPyDelphiWrapper;
procedure btnExecuteExample1Click(Sender: TObject);
procedure btnExecuteExample2Click(Sender: TObject);
procedure Table1CalcFields(DataSet: TDataSet);
procedure btnExecuteExample4Click(Sender: TObject);
procedure btnExecuteExample3Click(Sender: TObject);
procedure modDBFireDacInitialization(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btnSQLTestClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure PageControlChange(Sender: TObject);
private
function DBConnectionClosedCheck(aConnectionDefName: String): Boolean;
public
end;
var
Main: TMain;
implementation
{$R *.DFM}
procedure TMain.FormShow(Sender: TObject);
var
i: Integer;
l_sStr: String;
begin
SynEditScript1.Lines.LoadFromFile( 'Example1.py' );
SynEditScript2.Lines.LoadFromFile( 'Example2.py' );
SynEditScript3.Lines.LoadFromFile( 'Example3.py' );
SynEditScript4.Lines.LoadFromFile( 'Example4.py' );
//
PageControl.ActivePage := TabSheet1;
//
RichEdit1.Lines.Clear;
cobxConnSQLServer.ItemIndex := -1;
if FileExists('FDDrivers.ini') then begin
FDManager.DriverDefFileName := 'FDDrivers.ini';
FDManager.DriverDefFileAutoLoad := True;
end
else begin
l_sStr := 'Die Ini-Datei "FDDrivers.ini" existiert nicht!';
RichEdit1.Lines.Add(l_sStr);
MessageDlg(l_sStr, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0);
Close;
end;
if FileExists('Demo.ini') then begin
FDManager.ConnectionDefFileName := 'Demo.ini';
try
FDManager.LoadConnectionDefFile;
FDManager.GetConnectionNames(cobxConnSQLServer.Items);
for i := 0 to cobxConnSQLServer.Items.Count-1 do begin
l_sStr := UpperCase(cobxConnSQLServer.Items[i]);
if (Pos('SQLITE',l_sStr) > 0) and (cobxConnSQLServer.ItemIndex < 0) then begin
cobxConnSQLServer.ItemIndex := i;
break;
end;
end;
except
on E: Exception do begin
l_sStr := 'Exception: ' + E.Message;
RichEdit1.Lines.Add(l_sStr);
MessageDlg(l_sStr, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0);
end;
end;
end
else begin
l_sStr := 'Die Ini-Datei "Demo.ini" existiert nicht!';
RichEdit1.Lines.Add(l_sStr);
MessageDlg(l_sStr, TMsgDlgType.mtError, [TMsgDlgBtn.mbOK], 0);
Close;
end;
end;
procedure TMain.FormDestroy(Sender: TObject);
begin
if Connection.Connected then
Connection.Close;
end;
procedure TMain.btnSQLTestClick(Sender: TObject);
var
l_sStr, l_sConnName: String;
begin
RichEdit1.Lines.Clear;
l_sConnName := cobxConnSQLServer.Items[cobxConnSQLServer.ItemIndex];
l_sStr := UpperCase(l_sConnName);
if Pos('ORA',l_sStr) > 0 then begin
RichEdit1.Lines.Add('Oracle Parameter:');
end
else if Pos('MSSQL',l_sStr) > 0 then begin
RichEdit1.Lines.Add('MS SQL Server Parameter:');
end
else if Pos('SQLITE',l_sStr) > 0 then begin
RichEdit1.Lines.Add('SQLite Parameter:');
end
else begin
RichEdit1.Lines.Add('Unkown database type!');
exit;
end;
if self.DBConnectionClosedCheck(l_sConnName) then
RichEdit1.Lines.Add('Database was connected!')
else
RichEdit1.Lines.Add('Database was not connected!');
end;
function TMain.DBConnectionClosedCheck(aConnectionDefName: String): Boolean;
var
l_sStr: String;
l_oDef: IFDStanConnectionDef;
begin
Result := True;
if Connection.Connected then
Connection.Close;
if Connection.ConnectionDefName <> aConnectionDefName then begin
l_oDef := FDManager.ConnectionDefs.ConnectionDefByName(aConnectionDefName);
Connection.Params.Clear;
Connection.Params.AddStrings(l_oDef.Params);
Connection.ConnectionDefName := aConnectionDefName;
//
try
Connection.Connected := True;
if Connection.Connected then begin
Result := True;
Connection.Close;
end;
except
on E: Exception do begin
Result := False;
l_sStr := 'Exception: ' + E.Message;
RichEdit1.Lines.Add(l_sStr);
end;
end;
end;
end;
procedure TMain.Table1CalcFields(DataSet: TDataSet);
begin
Dataset.FieldByName('Demo').AsString := Dataset.FieldByName('State').AsString + ' - ' +
Dataset.FieldByName('Zip').AsString;
end;
procedure TMain.modDBFireDacInitialization(Sender: TObject);
begin
// pyDBFireDac.g_oDBModule := Sender as TPythonModule;
with Sender as TPythonModule do begin
with DocString do begin
Add( 'This module contains several Object Types that' );
Add( 'will let you work with FireDAC and access' );
Add( 'a database.' );
Add( '' );
Add( 'CreateDBTable() -> creates a TFDTable instance' );
Add( 'CreateDBQuery() -> creates a TFDQuery instance' );
end;
with Errors.Add do begin
Name := 'DBError';
ErrorType := etClass; // <- !!! Must ...
end;
end;
end;
procedure TMain.PageControlChange(Sender: TObject);
begin
if PageControl.TabIndex = 1 then begin
// Example2
if not tblCustomer.Active then
tblCustomer.Active := True;
end
else begin
if tblCustomer.Active then
tblCustomer.Active := False;
end;
end;
procedure TMain.btnExecuteExample1Click(Sender: TObject);
var
l_sConnName: String;
begin
l_sConnName := cobxConnSQLServer.Items[cobxConnSQLServer.ItemIndex];
if self.DBConnectionClosedCheck(l_sConnName) then begin
with GetPythonEngine do begin
ExecStrings( SynEditScript1.Lines );
end;
end;
end;
procedure TMain.btnExecuteExample2Click(Sender: TObject);
var
pyObj : PPyObject;
begin
// Instantiate a new Python object TPyTable
pyObj := PyDelphiWrapper.Wrap(tblCustomer);
with GetPythonEngine do begin
// Define a new variable "T" in the DB module
modDBFireDac.SetVar( 'T', pyObj );
Py_XDecRef(pyObj);
// Excecute the script
ExecStrings( SynEditScript2.Lines );
end;
end;
procedure TMain.btnExecuteExample3Click(Sender: TObject);
var
pyObj : PPyObject;
l_oTable: TFDTable;
begin
// connect the Datasource2 to the Python Table
l_oTable := TFDTable.Create(Self);
l_oTable.TableName := 'Customer';
l_oTable.Connection := Connection;
Datasource2.Dataset := l_oTable;
l_oTable.Open();
pyObj := PyDelphiWrapper.Wrap(l_oTable, soOwned);
with GetPythonEngine do begin
// Define a new variable "T" in the DB module
modDBFireDac.SetVar( 'T', pyObj );
Py_XDecRef(pyObj);
// Excecute the script
ExecStrings( SynEditScript3.Lines );
end;
end;
procedure TMain.btnExecuteExample4Click(Sender: TObject);
var
l_sConnName: String;
begin
l_sConnName := cobxConnSQLServer.Items[cobxConnSQLServer.ItemIndex];
if self.DBConnectionClosedCheck(l_sConnName) then begin
with PythonEngine do
ExecStrings( SynEditScript4.Lines );
end;
end;
initialization
ReportMemoryLeaksOnShutdown := True;
finalization
CheckSynchronize;
end.