1 |
unit Test1; |
2 |
{$IFDEF MSWINDOWS} |
3 |
{$DEFINE WINDOWS} |
4 |
{$IF defined(CompilerVersion) and (CompilerVersion >= 28)} |
5 |
{Delphi XE7 onwards}} |
6 |
{$define HASREQEX} |
7 |
{$IFEND} |
8 |
{$ENDIF} |
9 |
|
10 |
{Create and Drop a Database} |
11 |
{ |
12 |
This test first attempts to create a database without specifying any parameters |
13 |
(should fail). It then goes on to create and drop a database, print out the |
14 |
parameters and then creates it again (will fail if the dropdatabase failed silently. |
15 |
|
16 |
Some basic database info is then accessed and printed. |
17 |
|
18 |
A basic query is performed and finally the database dropped. |
19 |
} |
20 |
|
21 |
{$IFDEF FPC} |
22 |
{$mode delphi} |
23 |
{$codepage utf8} |
24 |
{$define HASREQEX} |
25 |
{$ENDIF} |
26 |
|
27 |
interface |
28 |
|
29 |
uses |
30 |
Classes, SysUtils, TestManager, IB; |
31 |
|
32 |
type |
33 |
|
34 |
{ TTest1 } |
35 |
|
36 |
TTest1 = class(TTestBase) |
37 |
private |
38 |
procedure DoQuery(Attachment: IAttachment); |
39 |
public |
40 |
function TestTitle: AnsiString; override; |
41 |
procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override; |
42 |
end; |
43 |
|
44 |
implementation |
45 |
|
46 |
{ TTest1 } |
47 |
|
48 |
procedure TTest1.DoQuery(Attachment: IAttachment); |
49 |
var Transaction: ITransaction; |
50 |
Statement: IStatement; |
51 |
ResultSet: IResultSet; |
52 |
i: integer; |
53 |
begin |
54 |
Transaction := Attachment.StartTransaction([isc_tpb_read,isc_tpb_nowait,isc_tpb_concurrency],taCommit); |
55 |
Statement := Attachment.Prepare(Transaction,'Select * from RDB$Database',3); |
56 |
ResultSet := Statement.OpenCursor; |
57 |
ResultSet.SetRetainInterfaces(true); |
58 |
try |
59 |
while ResultSet.FetchNext do |
60 |
begin |
61 |
for i := 0 to ResultSet.getCount - 1 do |
62 |
writeln(OutFile,ResultSet[i].Name,' = ',ResultSet[i].AsString); |
63 |
end; |
64 |
finally |
65 |
ResultSet.Close; |
66 |
ResultSet.SetRetainInterfaces(false); |
67 |
end; |
68 |
end; |
69 |
|
70 |
function TTest1.TestTitle: AnsiString; |
71 |
begin |
72 |
Result := 'Test 1: Create and Drop a Database'; |
73 |
end; |
74 |
|
75 |
procedure TTest1.RunTest(CharSet: AnsiString; SQLDialect: integer); |
76 |
var DPB: IDPB; |
77 |
Attachment: IAttachment; |
78 |
createSQL: AnsiString; |
79 |
begin |
80 |
writeln(OutFile,'Creating a Database with empty parameters'); |
81 |
Attachment := FirebirdAPI.CreateDatabase('',nil,false); |
82 |
if Attachment = nil then |
83 |
writeln(OutFile,'Create Database fails (as expected): ',FirebirdAPI.GetStatus.GetMessage) |
84 |
else |
85 |
Attachment.DropDatabase; |
86 |
|
87 |
writeln(OutFile,'Creating a Database using an SQL Statement'); |
88 |
createSQL := Format('create database ''%s'' USER ''%s'' PASSWORD ''%s'' DEFAULT CHARACTER SET %s', |
89 |
[Owner.GetNewDatabaseName, Owner.GetUserName, Owner.GetPassword, CharSet]); |
90 |
Attachment := FirebirdAPI.CreateDatabase(createSQL,SQLDialect); |
91 |
WriteDBInfo(Attachment.GetDBInformation([isc_info_db_id,isc_info_db_SQL_Dialect])); |
92 |
writeln(outfile,'DB Connect String = ',Attachment.GetConnectString); |
93 |
writeln(outfile,'DB Charset ID = ',Attachment.GetDefaultCharSetID); |
94 |
writeln(outfile,'DB SQL Dialect = ',Attachment.GetSQLDialect); |
95 |
writeln(outfile,'DB Remote Protocol = ', Attachment.GetRemoteProtocol); |
96 |
writeln(outfile,'DB ODS Major Version = ',Attachment.GetODSMajorVersion); |
97 |
writeln(outfile,'DB ODS Minor Version = ',Attachment.GetODSMinorVersion); |
98 |
PrintDPB(Attachment.getDPB); |
99 |
|
100 |
{$IFDEF HASREQEX} |
101 |
{Demonstrate reconnect when database created with SQL Statement} |
102 |
try |
103 |
Attachment.Disconnect; |
104 |
Attachment.Connect; |
105 |
except on E:Exception do |
106 |
writeln(OutFile,'Error reconnecting to Database: ',E.Message); |
107 |
end; |
108 |
{$ENDIF} |
109 |
|
110 |
writeln(OutFile,'Dropping Database'); |
111 |
if Attachment <> nil then |
112 |
Attachment.DropDatabase; |
113 |
|
114 |
writeln(OutFile,'Creating a Database with a DPD'); |
115 |
DPB := FirebirdAPI.AllocateDPB; |
116 |
DPB.Add(isc_dpb_user_name).setAsString(Owner.GetUserName); |
117 |
DPB.Add(isc_dpb_password).setAsString(Owner.GetPassword); |
118 |
DPB.Add(isc_dpb_lc_ctype).setAsString(CharSet); |
119 |
DPB.Add(isc_dpb_set_db_SQL_dialect).setAsByte(SQLDialect); |
120 |
|
121 |
Attachment := FirebirdAPI.CreateDatabase(Owner.GetNewDatabaseName,DPB); |
122 |
|
123 |
writeln(outfile,'DB Connect String = ',Attachment.GetConnectString); |
124 |
writeln(outfile,'DB Charset ID = ',Attachment.GetDefaultCharSetID); |
125 |
writeln(outfile,'DB SQL Dialect = ',Attachment.GetSQLDialect); |
126 |
writeln(outfile,'DB Remote Protocol = ', Attachment.GetRemoteProtocol); |
127 |
writeln(outfile,'DB ODS Major Version = ',Attachment.GetODSMajorVersion); |
128 |
writeln(outfile,'DB ODS Minor Version = ',Attachment.GetODSMinorVersion); |
129 |
|
130 |
writeln(OutFile,'Dropping Database'); |
131 |
if Attachment <> nil then |
132 |
Attachment.DropDatabase; |
133 |
|
134 |
{Open Database} |
135 |
|
136 |
PrintDPB(DPB); |
137 |
writeln(OutFile,'Creating a Database with a DPD'); |
138 |
Attachment := FirebirdAPI.CreateDatabase(Owner.GetNewDatabaseName,DPB); |
139 |
if Attachment = nil then |
140 |
begin |
141 |
writeln(OutFile,'Create Database Failed'); |
142 |
Exit; |
143 |
end; |
144 |
WriteDBInfo(Attachment.GetDBInformation([isc_info_db_id,isc_info_ods_version,isc_info_ods_minor_version])); |
145 |
writeln(outfile,'DB Connect String = ',Attachment.GetConnectString); |
146 |
writeln(outfile,'DB Charset ID = ',Attachment.GetDefaultCharSetID); |
147 |
writeln(outfile,'DB SQL Dialect = ',Attachment.GetSQLDialect); |
148 |
writeln(outfile,'DB Remote Protocol = ', Attachment.GetRemoteProtocol); |
149 |
writeln(outfile,'DB ODS Major Version = ',Attachment.GetODSMajorVersion); |
150 |
writeln(outfile,'DB ODS Minor Version = ',Attachment.GetODSMinorVersion); |
151 |
|
152 |
{Querying Database} |
153 |
DoQuery(Attachment); |
154 |
|
155 |
writeln(OutFile,'Dropping Database'); |
156 |
Attachment.DropDatabase; |
157 |
end; |
158 |
|
159 |
|
160 |
initialization |
161 |
RegisterTest(TTest1); |
162 |
|
163 |
end. |
164 |
|