ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/fbintf/testsuite/Test1.pas
Revision: 143
Committed: Fri Feb 23 12:11:21 2018 UTC (6 years, 9 months ago) by tony
Content type: text/x-pascal
File size: 4690 byte(s)
Log Message:
Fixes Merged

File Contents

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