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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/*
* PROGRAM: Object oriented API samples.
* MODULE: 04.print_table.cpp
* DESCRIPTION: Run SELECT statement without parameters.
* Use attachment method to open cursor.
* Print all fields for selected records in a table.
* Learns how to access blob data and use unprepared statement.
*
* Example for the following interfaces:
*
* IAttachment - database attachment
* 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();
struct MyField
{
const char* name;
unsigned type, length, offset, null;
void print(ThrowStatusWrapper* st, IAttachment* att, ITransaction* tra, unsigned char* buf);
};
int main()
{
int rc = 0;
char s[100];
setenv("ISC_USER", "sysdba", 0);
setenv("ISC_PASSWORD", "masterkey", 0);
ThrowStatusWrapper status(master->getStatus());
IProvider* prov = master->getDispatcher();
IAttachment* att = NULL;
ITransaction* tra = NULL;
IResultSet* curs = NULL;
IMessageMetadata* meta = NULL;
try
{
att = prov->attachDatabase(&status, "employee", 0, NULL);
tra = att->startTransaction(&status, 0, NULL);
// If we are not going to run same SELECT query many times we may do not prepare it,
// opening cursor instead with single API call.
// If statement has input parameters and we know them, appropriate IMetadata may be
// constructed and passed to openCursor() together with data buffer.
// We also may provide out format info and coerce data passing appropriate metadata
// into API call.
// In this sample we have no input parameters and do not coerce anything - just
// print what we get from SQL query.
const char* sql = "select * from rdb$relations where RDB$RELATION_ID < 3 "
"or RDB$VIEW_SOURCE is not null";
// Do not use IStatement - just ask attachment to open cursor
curs = att->openCursor(&status, tra, 0, sql, SAMPLES_DIALECT, NULL, NULL, NULL, NULL, 0);
meta = curs->getMetadata(&status);
unsigned cols = meta->getCount(&status);
MyField* fields = new MyField[cols];
memset(fields, 0, sizeof(MyField) * cols);
unsigned f = 0;
for (unsigned j = 0; j < cols; ++j)
{
unsigned t = meta->getType(&status, j) & ~1;
unsigned sub = meta->getSubType(&status, j);
switch (t)
{
case SQL_BLOB:
if (sub != 1)
continue;
break;
case SQL_TEXT:
case SQL_VARYING:
case SQL_SHORT:
case SQL_DOUBLE:
break;
default:
{
sprintf(s, "Unknown type %d for %s", t, meta->getField(&status, j));
throw s;
}
continue;
}
// we can work with this field - cache metadata info for fast access
fields[f].type = t;
fields[f].name = meta->getField(&status, j);
fields[f].length = meta->getLength(&status, j);
fields[f].offset = meta->getOffset(&status, j);
fields[f].null = meta->getNullOffset(&status, j);
++f;
}
unsigned l = meta->getMessageLength(&status);
unsigned char* buffer = new unsigned char[l];
// fetch records from cursor
while (curs->fetchNext(&status, buffer) == IStatus::RESULT_OK)
{
for (unsigned j = 0; j < f; ++j)
{
// call field's function to print it
fields[j].print(&status, att, tra, buffer);
}
printf("\n");
}
curs->close(&status);
curs = NULL;
meta->release();
meta = NULL;
tra->commit(&status);
tra = NULL;
att->detach(&status);
att = NULL;
}
catch (const FbException& error)
{
// handle error
fflush(stdout);
rc = 1;
char buf[256];
master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
fprintf(stderr, "%s\n", buf);
}
if (meta)
meta->release();
if (curs)
curs->release();
if (tra)
tra->release();
if (att)
att->release();
prov->release();
status.dispose();
return rc;
}
template <typename T>
T as(unsigned char* ptr)
{
return *((T*) ptr);
}
void MyField::print(ThrowStatusWrapper* st, IAttachment* att, ITransaction* tra, unsigned char* buf)
{
printf("%s: ", name);
if (as<short>(buf + null))
{
printf("<Null>\n");
return;
}
// IBlob makes it possible to read/write BLOB data
IBlob* blob = NULL;
switch (type)
{
// text fields
case SQL_TEXT:
printf("%*.*s\n", length, length, buf + offset);
break;
case SQL_VARYING:
{
unsigned l = as<short>(buf + offset);
printf("%*.*s\n", l, l, buf + offset + sizeof(short));
}
break;
// numeric fields
case SQL_SHORT:
printf("%d\n", as<short>(buf + offset));
break;
case SQL_DOUBLE:
printf("%f\n", as<double>(buf + offset));
break;
// blob requires more handling in DB
case SQL_BLOB:
try
{
// use attachment's method to access BLOB object
blob = att->openBlob(st, tra, (ISC_QUAD*) (buf + offset), 0, NULL);
char segbuf[16];
unsigned len;
// read data segment by segment
for (;;)
{
int cc = blob->getSegment(st, sizeof(segbuf), segbuf, &len);
if (cc != IStatus::RESULT_OK && cc != IStatus::RESULT_SEGMENT)
break;
fwrite(segbuf, sizeof(char), len, stdout);
}
// close BLOB after receiving all data
blob->close(st);
blob = NULL;
printf("\n");
}
catch (...)
{
if (blob)
blob->release();
throw;
}
break;
// do not plan to have all types here
default:
throw "Unknown type in MyField::print()";
}
}
|