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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/*
* PROGRAM: Object oriented API samples.
* MODULE: 05.user_metadata.cpp
* DESCRIPTION: A sample of user-implemented IMessageMetadata.
* Prints firebird user name (SYSDBA by default).
*
* Example for the following interfaces:
*
* IOffsetsCallback - callback for IUtil::setOffsets()
* IMessageMetadata - how to implement it yourself
*
* 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) 2016 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();
/*
* Trivial sample of IMessageMetadata implementation.
* Metadata is created for a fixed output format with single char field.
* Therefore index parameter in all functions is ignored.
* Because the only possible error is index out of bounds status parameter is ignored too.
* Atomic operation is not used in IReferenceCounted cause we do not plan MT support.
* Non-array vars used to represent offset and nullOffset of that single field.
*/
class MyMetadata : public IMessageMetadataImpl<MyMetadata, ThrowStatusWrapper>
{
private:
class Callback : public IOffsetsCallbackImpl<Callback, ThrowStatusWrapper>
{
private:
MyMetadata* metadata;
public:
Callback(MyMetadata* pmeta)
: metadata(pmeta)
{ }
//IOffsetsCallback implementation
void setOffset(ThrowStatusWrapper* status, unsigned /*index*/, unsigned offset, unsigned nullOffset)
{
// Typically setOffset() function should save passed offsets
// in your implementation of message metadata.
metadata->offset = offset;
metadata->nullOffset = nullOffset;
}
};
FbSampleAtomic referenceCounter;
public:
unsigned offset, nullOffset, length;
MyMetadata()
: referenceCounter(0)
{
IUtil* utl = master->getUtilInterface();
ThrowStatusWrapper s(master->getStatus());
try
{
Callback cb(this);
length = utl->setOffsets(&s, this, &cb);
}
catch(...)
{
s.dispose();
throw;
}
s.dispose();
}
void addRef()
{
++referenceCounter;
}
int release()
{
int rc = --referenceCounter;
if (!rc)
delete this;
return rc;
}
// IMessageMetadata implementation
unsigned getCount(ThrowStatusWrapper* /*status*/)
{
return 1;
}
const char* getField(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
{
return NULL;
}
const char* getRelation(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
{
return NULL;
}
const char* getOwner(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
{
return NULL;
}
const char* getAlias(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
{
return NULL;
}
unsigned getType(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
{
return SQL_VARYING;
}
FB_BOOLEAN isNullable(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
{
return false;
}
int getSubType(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
{
return 0;
}
unsigned getLength(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
{
return 20; // Want to make it fit
}
int getScale(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
{
return 0;
}
unsigned getCharSet(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
{
return 0;
}
unsigned getOffset(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
{
return offset;
}
unsigned getNullOffset(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
{
return nullOffset;
}
IMetadataBuilder* getBuilder(ThrowStatusWrapper* status)
{
ISC_STATUS err[] = {isc_arg_gds, isc_wish_list, isc_arg_end};
status->setErrors(err);
return NULL;
}
unsigned getMessageLength(ThrowStatusWrapper* /*status*/)
{
return length;
}
};
template <typename T>
T to(const unsigned char* b, unsigned o)
{
return *((T*) (b + o));
}
int main()
{
int rc = 0;
unsigned char* buffer = NULL;
// 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();
// declare pointers to required interfaces
IAttachment* att = NULL;
ITransaction* tra = NULL;
IResultSet* curs = NULL;
MyMetadata* meta = NULL;
try
{
// Instance of our metadata
meta = new MyMetadata;
meta->addRef();
// allocate output buffer
buffer = new unsigned char[meta->length];
// attach employee db
att = prov->attachDatabase(&status, "employee", 0, NULL);
// start default transaction
tra = att->startTransaction(&status, 0, NULL);
// open cursor
curs = att->openCursor(&status, tra, 0, "select current_user from rdb$database",
SAMPLES_DIALECT, NULL, NULL, meta, NULL, 0);
// fetch record from cursor and print it
curs->fetchNext(&status, buffer);
ISC_SHORT l = to<ISC_SHORT>(buffer, meta->offset);
printf("<%*.*s>\n", l, l, buffer + meta->offset + sizeof(ISC_SHORT));
// close interfaces
curs->close(&status);
curs = 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 (curs)
curs->release();
if (tra)
tra->release();
if (att)
att->release();
// generic cleanup
if (meta)
meta->release();
prov->release();
status.dispose();
delete[] buffer;
return rc;
}
|