05.user_metadata.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * PROGRAM: Object oriented API samples.
  3. * MODULE: 05.user_metadata.cpp
  4. * DESCRIPTION: A sample of user-implemented IMessageMetadata.
  5. * Prints firebird user name (SYSDBA by default).
  6. *
  7. * Example for the following interfaces:
  8. *
  9. * IOffsetsCallback - callback for IUtil::setOffsets()
  10. * IMessageMetadata - how to implement it yourself
  11. *
  12. * The contents of this file are subject to the Initial
  13. * Developer's Public License Version 1.0 (the "License");
  14. * you may not use this file except in compliance with the
  15. * License. You may obtain a copy of the License at
  16. * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
  17. *
  18. * Software distributed under the License is distributed AS IS,
  19. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing rights
  21. * and limitations under the License.
  22. *
  23. * The Original Code was created by Alexander Peshkoff
  24. * for the Firebird Open Source RDBMS project.
  25. *
  26. * Copyright (c) 2016 Alexander Peshkoff <[email protected]>
  27. * and all contributors signed below.
  28. *
  29. * All Rights Reserved.
  30. * Contributor(s): ______________________________________.
  31. */
  32. #include "ifaceExamples.h"
  33. static IMaster* master = fb_get_master_interface();
  34. /*
  35. * Trivial sample of IMessageMetadata implementation.
  36. * Metadata is created for a fixed output format with single char field.
  37. * Therefore index parameter in all functions is ignored.
  38. * Because the only possible error is index out of bounds status parameter is ignored too.
  39. * Atomic operation is not used in IReferenceCounted cause we do not plan MT support.
  40. * Non-array vars used to represent offset and nullOffset of that single field.
  41. */
  42. class MyMetadata : public IMessageMetadataImpl<MyMetadata, ThrowStatusWrapper>
  43. {
  44. private:
  45. class Callback : public IOffsetsCallbackImpl<Callback, ThrowStatusWrapper>
  46. {
  47. private:
  48. MyMetadata* metadata;
  49. public:
  50. Callback(MyMetadata* pmeta)
  51. : metadata(pmeta)
  52. { }
  53. //IOffsetsCallback implementation
  54. void setOffset(ThrowStatusWrapper* status, unsigned /*index*/, unsigned offset, unsigned nullOffset)
  55. {
  56. // Typically setOffset() function should save passed offsets
  57. // in your implementation of message metadata.
  58. metadata->offset = offset;
  59. metadata->nullOffset = nullOffset;
  60. }
  61. };
  62. FbSampleAtomic referenceCounter;
  63. // we are using only single field (varchar) in a sample, therefore no strong alignment requirements.
  64. // In general message alignment is the maximum field alignment in that message.
  65. static const unsigned messageAlignment = sizeof(ISC_SHORT);
  66. public:
  67. unsigned offset, nullOffset, length;
  68. MyMetadata()
  69. : referenceCounter(0)
  70. {
  71. IUtil* utl = master->getUtilInterface();
  72. ThrowStatusWrapper s(master->getStatus());
  73. try
  74. {
  75. Callback cb(this);
  76. length = utl->setOffsets(&s, this, &cb);
  77. }
  78. catch(...)
  79. {
  80. s.dispose();
  81. throw;
  82. }
  83. s.dispose();
  84. }
  85. void addRef()
  86. {
  87. ++referenceCounter;
  88. }
  89. int release()
  90. {
  91. int rc = --referenceCounter;
  92. if (!rc)
  93. delete this;
  94. return rc;
  95. }
  96. // IMessageMetadata implementation
  97. unsigned getCount(ThrowStatusWrapper* /*status*/)
  98. {
  99. return 1;
  100. }
  101. const char* getField(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  102. {
  103. return NULL;
  104. }
  105. const char* getRelation(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  106. {
  107. return NULL;
  108. }
  109. const char* getOwner(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  110. {
  111. return NULL;
  112. }
  113. const char* getAlias(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  114. {
  115. return NULL;
  116. }
  117. unsigned getType(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  118. {
  119. return SQL_VARYING;
  120. }
  121. FB_BOOLEAN isNullable(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  122. {
  123. return false;
  124. }
  125. int getSubType(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  126. {
  127. return 0;
  128. }
  129. unsigned getLength(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  130. {
  131. return 20; // Want to make it fit
  132. }
  133. int getScale(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  134. {
  135. return 0;
  136. }
  137. unsigned getCharSet(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  138. {
  139. return 0;
  140. }
  141. unsigned getOffset(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  142. {
  143. return offset;
  144. }
  145. unsigned getNullOffset(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  146. {
  147. return nullOffset;
  148. }
  149. IMetadataBuilder* getBuilder(ThrowStatusWrapper* status)
  150. {
  151. ISC_STATUS err[] = {isc_arg_gds, isc_wish_list, isc_arg_end};
  152. status->setErrors(err);
  153. return NULL;
  154. }
  155. unsigned getMessageLength(ThrowStatusWrapper* /*status*/)
  156. {
  157. return length;
  158. }
  159. unsigned getAlignment(ThrowStatusWrapper* /*status*/)
  160. {
  161. return messageAlignment;
  162. }
  163. unsigned getAlignedLength(ThrowStatusWrapper* /*status*/)
  164. {
  165. return ((length / messageAlignment) + (length % messageAlignment ? 1 : 0)) * messageAlignment;
  166. }
  167. };
  168. template <typename T>
  169. T to(const unsigned char* b, unsigned o)
  170. {
  171. return *((T*) (b + o));
  172. }
  173. int main()
  174. {
  175. int rc = 0;
  176. unsigned char* buffer = NULL;
  177. // set default password if none specified in environment
  178. setenv("ISC_USER", "sysdba", 0);
  179. setenv("ISC_PASSWORD", "masterkey", 0);
  180. // status vector and main dispatcher
  181. ThrowStatusWrapper status(master->getStatus());
  182. IProvider* prov = master->getDispatcher();
  183. // declare pointers to required interfaces
  184. IAttachment* att = NULL;
  185. ITransaction* tra = NULL;
  186. IResultSet* curs = NULL;
  187. MyMetadata* meta = NULL;
  188. try
  189. {
  190. // Instance of our metadata
  191. meta = new MyMetadata;
  192. meta->addRef();
  193. // allocate output buffer
  194. buffer = new unsigned char[meta->length];
  195. // attach employee db
  196. att = prov->attachDatabase(&status, "employee", 0, NULL);
  197. // start default transaction
  198. tra = att->startTransaction(&status, 0, NULL);
  199. // open cursor
  200. curs = att->openCursor(&status, tra, 0, "select current_user from rdb$database",
  201. SAMPLES_DIALECT, NULL, NULL, meta, NULL, 0);
  202. // fetch record from cursor and print it
  203. curs->fetchNext(&status, buffer);
  204. ISC_SHORT l = to<ISC_SHORT>(buffer, meta->offset);
  205. printf("<%*.*s>\n", l, l, buffer + meta->offset + sizeof(ISC_SHORT));
  206. // close interfaces
  207. curs->close(&status);
  208. curs = NULL;
  209. tra->commit(&status);
  210. tra = NULL;
  211. att->detach(&status);
  212. att = NULL;
  213. }
  214. catch (const FbException& error)
  215. {
  216. // handle error
  217. rc = 1;
  218. char buf[256];
  219. master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
  220. fprintf(stderr, "%s\n", buf);
  221. }
  222. // release interfaces after error caught
  223. if (curs)
  224. curs->release();
  225. if (tra)
  226. tra->release();
  227. if (att)
  228. att->release();
  229. // generic cleanup
  230. if (meta)
  231. meta->release();
  232. prov->release();
  233. status.dispose();
  234. delete[] buffer;
  235. return rc;
  236. }