05.user_metadata.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. public:
  64. unsigned offset, nullOffset, length;
  65. MyMetadata()
  66. : referenceCounter(0)
  67. {
  68. IUtil* utl = master->getUtilInterface();
  69. ThrowStatusWrapper s(master->getStatus());
  70. try
  71. {
  72. Callback cb(this);
  73. length = utl->setOffsets(&s, this, &cb);
  74. }
  75. catch(...)
  76. {
  77. s.dispose();
  78. throw;
  79. }
  80. s.dispose();
  81. }
  82. void addRef()
  83. {
  84. ++referenceCounter;
  85. }
  86. int release()
  87. {
  88. int rc = --referenceCounter;
  89. if (!rc)
  90. delete this;
  91. return rc;
  92. }
  93. // IMessageMetadata implementation
  94. unsigned getCount(ThrowStatusWrapper* /*status*/)
  95. {
  96. return 1;
  97. }
  98. const char* getField(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  99. {
  100. return NULL;
  101. }
  102. const char* getRelation(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  103. {
  104. return NULL;
  105. }
  106. const char* getOwner(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  107. {
  108. return NULL;
  109. }
  110. const char* getAlias(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  111. {
  112. return NULL;
  113. }
  114. unsigned getType(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  115. {
  116. return SQL_VARYING;
  117. }
  118. FB_BOOLEAN isNullable(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  119. {
  120. return false;
  121. }
  122. int getSubType(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  123. {
  124. return 0;
  125. }
  126. unsigned getLength(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  127. {
  128. return 20; // Want to make it fit
  129. }
  130. int getScale(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  131. {
  132. return 0;
  133. }
  134. unsigned getCharSet(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  135. {
  136. return 0;
  137. }
  138. unsigned getOffset(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  139. {
  140. return offset;
  141. }
  142. unsigned getNullOffset(ThrowStatusWrapper* /*status*/, unsigned /*index*/)
  143. {
  144. return nullOffset;
  145. }
  146. IMetadataBuilder* getBuilder(ThrowStatusWrapper* status)
  147. {
  148. ISC_STATUS err[] = {isc_arg_gds, isc_wish_list, isc_arg_end};
  149. status->setErrors(err);
  150. return NULL;
  151. }
  152. unsigned getMessageLength(ThrowStatusWrapper* /*status*/)
  153. {
  154. return length;
  155. }
  156. };
  157. template <typename T>
  158. T to(const unsigned char* b, unsigned o)
  159. {
  160. return *((T*) (b + o));
  161. }
  162. int main()
  163. {
  164. int rc = 0;
  165. unsigned char* buffer = NULL;
  166. // set default password if none specified in environment
  167. setenv("ISC_USER", "sysdba", 0);
  168. setenv("ISC_PASSWORD", "masterkey", 0);
  169. // status vector and main dispatcher
  170. ThrowStatusWrapper status(master->getStatus());
  171. IProvider* prov = master->getDispatcher();
  172. // declare pointers to required interfaces
  173. IAttachment* att = NULL;
  174. ITransaction* tra = NULL;
  175. IResultSet* curs = NULL;
  176. MyMetadata* meta = NULL;
  177. try
  178. {
  179. // Instance of our metadata
  180. meta = new MyMetadata;
  181. meta->addRef();
  182. // allocate output buffer
  183. buffer = new unsigned char[meta->length];
  184. // attach employee db
  185. att = prov->attachDatabase(&status, "employee", 0, NULL);
  186. // start default transaction
  187. tra = att->startTransaction(&status, 0, NULL);
  188. // open cursor
  189. curs = att->openCursor(&status, tra, 0, "select current_user from rdb$database",
  190. SAMPLES_DIALECT, NULL, NULL, meta, NULL, 0);
  191. // fetch record from cursor and print it
  192. curs->fetchNext(&status, buffer);
  193. ISC_SHORT l = to<ISC_SHORT>(buffer, meta->offset);
  194. printf("<%*.*s>\n", l, l, buffer + meta->offset + sizeof(ISC_SHORT));
  195. // close interfaces
  196. curs->close(&status);
  197. curs = NULL;
  198. tra->commit(&status);
  199. tra = NULL;
  200. att->detach(&status);
  201. att = NULL;
  202. }
  203. catch (const FbException& error)
  204. {
  205. // handle error
  206. rc = 1;
  207. char buf[256];
  208. master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
  209. fprintf(stderr, "%s\n", buf);
  210. }
  211. // release interfaces after error caught
  212. if (curs)
  213. curs->release();
  214. if (tra)
  215. tra->release();
  216. if (att)
  217. att->release();
  218. // generic cleanup
  219. if (meta)
  220. meta->release();
  221. prov->release();
  222. status.dispose();
  223. delete[] buffer;
  224. return rc;
  225. }