-
Notifications
You must be signed in to change notification settings - Fork 9
/
FbApiTest.dpr
120 lines (100 loc) · 3.09 KB
/
FbApiTest.dpr
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
{
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Adriano dos Santos Fernandes.
*
* Copyright (c) 2015 Adriano dos Santos Fernandes <adrianosf at gmail.com>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
}
program FbApiTest;
{$ifndef FPC}
{$APPTYPE CONSOLE}
{$endif}
uses FbApi;
function fb_get_master_interface: Master; cdecl; external 'fbclient';
function fb_interpret(s: PChar; n: Cardinal; var statusVector: NativeIntPtr): Integer; cdecl;
external 'fbclient';
type
InMessage = record
n: SmallInt;
nNull: SmallInt;
end;
OutMessage = record
relationId: SmallInt;
relationIdNull: SmallInt;
relationName: array[0..93] of char;
relationNameNull: SmallInt;
end;
var
master: FbApi.Master;
status: FbApi.Status;
dispatcher: Provider;
attachment: FbApi.Attachment;
transaction: FbApi.Transaction;
stmt: Statement;
inMetadata, outMetadata: MessageMetadata;
inBuffer: InMessage;
outBuffer: OutMessage;
rs: ResultSet;
statusVector: NativeIntPtr;
s: string;
begin
try
master := fb_get_master_interface();
status := master.getStatus();
dispatcher := master.getDispatcher();
attachment := dispatcher.createDatabase(status, '/tmp/pascal.fdb', 0, nil);
transaction := attachment.startTransaction(status, 0, nil);
stmt := attachment.prepare(status, transaction, 0,
'select rdb$relation_id relid, rdb$relation_name csname ' +
' from rdb$relations ' +
' where rdb$relation_id < ?',
3, 0);
s := stmt.getPlan(status, false);
WriteLn('Plan: ', s);
WriteLn;
s := stmt.getPlan(status, true);
WriteLn('Detailed plan: ', s);
WriteLn;
inMetadata := stmt.getInputMetadata(status);
outMetadata := stmt.getOutputMetadata(status);
// It's not good to:
// - assume metadata descriptions matches our buffer types
// - work with CHAR (SQL_TEXT) descriptors
inBuffer.nNull := 0;
inBuffer.n := 10;
rs := stmt.openCursor(status, transaction, inMetadata, @inBuffer, outMetadata, 0);
while (rs.fetchNext(status, @outBuffer) = FbApi.Status.RESULT_OK)
do
begin
outBuffer.relationName[92] := #0; // workaround: null terminate it
WriteLn('Fetch: ', outBuffer.relationId, ' ', outBuffer.relationName);
end;
rs.release();
inMetadata.release();
outMetadata.release();
stmt.free(status);
transaction.commit(status);
attachment.dropDatabase(status);
except
on e: FbException do
begin
SetLength(s, 2000);
statusVector := e.getStatus().getErrors();
SetLength(s, fb_interpret(PChar(s), 2000, statusVector));
WriteLn('Exception: ', string(s));
end
end
end.