1 |
unit test2; |
2 |
{$IFDEF MSWINDOWS} |
3 |
{$DEFINE WINDOWS} |
4 |
{$ENDIF} |
5 |
|
6 |
{$IFDEF FPC} |
7 |
{$mode delphi} |
8 |
{$codepage utf8} |
9 |
{$ENDIF} |
10 |
|
11 |
{Test 2: Open the employee database and run a query} |
12 |
|
13 |
{ This test opens the employee example databases with the supplied user name/password |
14 |
and runs a simple query with no parameters. Note that the password parameter |
15 |
is first supplied empty and then updated. |
16 |
|
17 |
Both the output metadata and the query plan are printed out, followed by the results of the query. |
18 |
|
19 |
A specific employee record is then queried, first using a positional parameter |
20 |
and then a parameter by name. In each case, the SQL Parameter metadata is also |
21 |
printed followed by the query results. |
22 |
|
23 |
Finally, the database is explicitly disconnected. |
24 |
} |
25 |
|
26 |
interface |
27 |
|
28 |
uses |
29 |
Classes, SysUtils, TestManager, IB; |
30 |
|
31 |
type |
32 |
|
33 |
{ TTest2 } |
34 |
|
35 |
TTest2 = class(TTestBase) |
36 |
private |
37 |
procedure DoQuery(Attachment: IAttachment); |
38 |
public |
39 |
function TestTitle: AnsiString; override; |
40 |
procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override; |
41 |
end; |
42 |
|
43 |
|
44 |
implementation |
45 |
|
46 |
{ TTest2 } |
47 |
|
48 |
procedure TTest2.DoQuery(Attachment: IAttachment); |
49 |
var Transaction: ITransaction; |
50 |
Statement: IStatement; |
51 |
ResultSet: IResultSet; |
52 |
begin |
53 |
Transaction := Attachment.StartTransaction([isc_tpb_read,isc_tpb_nowait,isc_tpb_concurrency],taCommit); |
54 |
Statement := Attachment.Prepare(Transaction,'Select First 3 * from EMPLOYEE',3); |
55 |
PrintMetaData(Statement.GetMetaData); |
56 |
writeln(OutFile,'Plan = ' ,Statement.GetPlan); |
57 |
writeln(OutFile,Statement.GetSQLText); |
58 |
writeln(OutFile); |
59 |
ReportResults(Statement); |
60 |
Statement := Attachment.Prepare(Transaction,'Select * from EMPLOYEE Where EMP_NO = ?',3); |
61 |
writeln(OutFile,Statement.GetSQLText); |
62 |
ParamInfo(Statement.SQLParams); |
63 |
Statement.GetSQLParams[0].AsInteger := 8; |
64 |
ReportResults(Statement); |
65 |
writeln(OutFile,'With param names'); |
66 |
Statement := Attachment.PrepareWithNamedParameters(Transaction,'Select * from EMPLOYEE Where EMP_NO = :EMP_NO',3); |
67 |
Statement.SetRetainInterfaces(true); |
68 |
try |
69 |
writeln(OutFile,Statement.GetSQLText); |
70 |
ParamInfo(Statement.SQLParams); |
71 |
Statement.GetSQLParams.ByName('EMP_NO').AsInteger := 8; |
72 |
ReportResults(Statement); |
73 |
finally |
74 |
Statement.SetRetainInterfaces(false); |
75 |
end; |
76 |
end; |
77 |
|
78 |
function TTest2.TestTitle: AnsiString; |
79 |
begin |
80 |
Result := 'Test 2: Open the employee database and run a query'; |
81 |
end; |
82 |
|
83 |
procedure TTest2.RunTest(CharSet: AnsiString; SQLDialect: integer); |
84 |
var Attachment: IAttachment; |
85 |
DPB: IDPB; |
86 |
begin |
87 |
DPB := FirebirdAPI.AllocateDPB; |
88 |
DPB.Add(isc_dpb_user_name).setAsString(Owner.GetUserName); |
89 |
DPB.Add(isc_dpb_password).setAsString(' '); |
90 |
DPB.Add(isc_dpb_lc_ctype).setAsString(CharSet); |
91 |
DPB.Add(isc_dpb_set_db_SQL_dialect).setAsByte(SQLDialect); |
92 |
DPB.Find(isc_dpb_password).setAsString(Owner.GetPassword); |
93 |
try |
94 |
Attachment := FirebirdAPI.OpenDatabase(Owner.GetEmployeeDatabaseName,DPB); |
95 |
except on e: Exception do |
96 |
writeln(OutFile,'Open Database fails ',E.Message); |
97 |
end; |
98 |
writeln(OutFile,'Opening ',Owner.GetEmployeeDatabaseName); |
99 |
Attachment := FirebirdAPI.OpenDatabase(Owner.GetEmployeeDatabaseName,DPB); |
100 |
writeln(OutFile,'Database Open'); |
101 |
DoQuery(Attachment); |
102 |
Attachment.Disconnect; |
103 |
end; |
104 |
|
105 |
initialization |
106 |
RegisterTest(TTest2); |
107 |
|
108 |
end. |
109 |
|