-
-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy path03.select.cpp
More file actions
209 lines (178 loc) · 5.34 KB
/
03.select.cpp
File metadata and controls
209 lines (178 loc) · 5.34 KB
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
/*
* PROGRAM: Object oriented API samples.
* MODULE: 03.select.cpp
* DESCRIPTION: A sample of running SELECT statement without parameters.
* Prints string fields in a table, coercing VARCHAR to CHAR.
* Learns how to coerce output data in prepared statement
* and execute it.
*
* Example for the following interfaces:
*
* IStatement - SQL statement execution
* IMessageMetadata - describe input and output data format
* IResultSet - fetch data returned by statement after execution
*
* 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 Alexander Peshkoff
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2013 Alexander Peshkoff <peshkoff@mail.ru>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include "ifaceExamples.h"
static IMaster* master = fb_get_master_interface();
int main()
{
int rc = 0;
// set default password if none specified in environment
setenv("ISC_USER", "sysdba", 0);
setenv("ISC_PASSWORD", "masterkey", 0);
// status vector and main dispatcher
ThrowStatusWrapper status(master->getStatus());
IProvider* prov = master->getDispatcher();
IUtil* utl = master->getUtilInterface();
// declare pointers to required interfaces
IAttachment* att = NULL;
ITransaction* tra = NULL;
IStatement* stmt = NULL;
IMessageMetadata* meta = NULL;
IMetadataBuilder* builder = NULL;
IXpbBuilder* tpb = NULL;
// Interface provides access to data returned by SELECT statement
IResultSet* curs = NULL;
try
{
// attach employee db
att = prov->attachDatabase(&status, "employee", 0, NULL);
// start read only transaction
tpb = utl->getXpbBuilder(&status, IXpbBuilder::TPB, NULL, 0);
tpb->insertTag(&status, isc_tpb_read_committed);
tpb->insertTag(&status, isc_tpb_no_rec_version);
tpb->insertTag(&status, isc_tpb_wait);
tpb->insertTag(&status, isc_tpb_read);
tra = att->startTransaction(&status, tpb->getBufferLength(&status), tpb->getBuffer(&status));
// prepare statement
stmt = att->prepare(&status, tra, 0, "select last_name, first_name, phone_ext from phone_list "
"where location = 'Monterey' order by last_name, first_name",
SAMPLES_DIALECT, IStatement::PREPARE_PREFETCH_METADATA);
// get list of columns
meta = stmt->getOutputMetadata(&status);
builder = meta->getBuilder(&status);
unsigned cols = meta->getCount(&status);
// struct to cache received metadata
struct MyField
{
const char* name;
unsigned length, offset;
};
MyField* fields = new MyField[cols];
memset(fields, 0, sizeof(MyField) * cols);
// parse columns list & coerce datatype(s)
for (unsigned j = 0; j < cols; ++j)
{
unsigned t = meta->getType(&status, j);
if (t == SQL_VARYING || t == SQL_TEXT)
{
builder->setType(&status, j, SQL_TEXT);
fields[j].name = meta->getField(&status, j);
}
}
// release automatically created metadata
// metadata is not database object, therefore no specific call to close it
meta->release();
// get metadata with coerced datatypes
meta = builder->getMetadata(&status);
// builder not needed any more
builder->release();
builder = NULL;
// now we may also get offsets info
for (unsigned j = 0; j < cols; ++j)
{
if (fields[j].name)
{
fields[j].length = meta->getLength(&status, j);
fields[j].offset = meta->getOffset(&status, j);
}
}
// open cursor
curs = stmt->openCursor(&status, tra, NULL, NULL, meta, 0);
// allocate output buffer
unsigned l = meta->getMessageLength(&status);
unsigned char* buffer = new unsigned char[l];
// fetch records from cursor and print them
for (int line = 0; curs->fetchNext(&status, buffer) == IStatus::RESULT_OK; ++line)
{
if (line % 10 == 0)
{
printf("\n");
for (unsigned j = 0; j < cols; ++j)
{
if (fields[j].name)
{
printf("%-*.*s ", fields[j].length, fields[j].length, fields[j].name);
}
}
printf("\n");
}
for (unsigned j = 0; j < cols; ++j)
{
if (fields[j].name)
{
printf("%*.*s ", fields[j].length, fields[j].length, buffer + fields[j].offset);
}
}
printf("\n");
}
printf("\n");
// close interfaces
curs->close(&status);
curs = NULL;
stmt->free(&status);
stmt = NULL;
meta->release();
meta = NULL;
tra->commit(&status);
tra = NULL;
att->detach(&status);
att = NULL;
}
catch (const FbException& error)
{
// handle error
rc = 1;
char buf[256];
master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
fprintf(stderr, "%s\n", buf);
}
// release interfaces after error caught
if (meta)
meta->release();
if (builder)
builder->release();
if (curs)
curs->release();
if (stmt)
stmt->release();
if (tra)
tra->release();
if (att)
att->release();
if (tpb)
tpb->dispose();
prov->release();
status.dispose();
return rc;
}