06.fb_message.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * PROGRAM: Object oriented API samples.
  3. * MODULE: 06.fb_message.cpp
  4. * DESCRIPTION: A sample of using static messages.
  5. * Prints user-defined tables with comments.
  6. *
  7. * Example for the following interface:
  8. * IDecFloat16 - support for IEEE-754 64-bit decimal float numbers
  9. *
  10. * Example for the following macro:
  11. *
  12. * FB_MESSAGE - defines static messages
  13. * C++ specific sample!
  14. *
  15. * The contents of this file are subject to the Mozilla Public License Version
  16. * 1.1 (the "License"); you may not use this file except in compliance with
  17. * the License. You may obtain a copy of the License at
  18. * http://www.mozilla.org/MPL/
  19. *
  20. * Software distributed under the License is distributed on an "AS IS" basis,
  21. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  22. * for the specific language governing rights and limitations under the
  23. * License.
  24. *
  25. * The Initial Developer of the Original Code is Adriano dos Santos Fernandes.
  26. * Portions created by the Initial Developer are Copyright (C) 2011 the Initial Developer.
  27. * All Rights Reserved.
  28. *
  29. * Contributor(s):
  30. * Alexander Peshkov
  31. *
  32. */
  33. #include "ifaceExamples.h"
  34. #include <firebird/Message.h>
  35. static IMaster* master = fb_get_master_interface();
  36. static IDecFloat16* idf16 = NULL;
  37. static IInt128* ii128 = NULL;
  38. int main()
  39. {
  40. int rc = 0;
  41. setenv("ISC_USER", "sysdba", 0);
  42. setenv("ISC_PASSWORD", "masterkey", 0);
  43. ThrowStatusWrapper status(master->getStatus());
  44. IProvider* prov = master->getDispatcher();
  45. IAttachment* att = NULL;
  46. ITransaction* tra = NULL;
  47. IResultSet* rs = NULL;
  48. const char* dbName = "employee";
  49. try
  50. {
  51. idf16 = master->getUtilInterface()->getDecFloat16(&status);
  52. ii128 = master->getUtilInterface()->getInt128(&status);
  53. att = prov->attachDatabase(&status, dbName, 0, NULL);
  54. tra = att->startTransaction(&status, 0, NULL);
  55. // Comment some tables
  56. att->execute(&status, tra, 0, "comment on table employee is 'Employees'",
  57. SAMPLES_DIALECT, NULL, NULL, NULL, NULL);
  58. att->execute(&status, tra, 0, "comment on table customer is 'Customers'",
  59. SAMPLES_DIALECT, NULL, NULL, NULL, NULL);
  60. att->execute(&status, tra, 0, "comment on table country is 'Countries and national currencies'",
  61. SAMPLES_DIALECT, NULL, NULL, NULL, NULL);
  62. tra->commitRetaining(&status);
  63. // Print tables list
  64. FB_MESSAGE(Input, ThrowStatusWrapper,
  65. (FB_INTEGER, systemFlag)
  66. ) input(&status, master);
  67. FB_MESSAGE(Output, ThrowStatusWrapper,
  68. (FB_SMALLINT, relationId)
  69. (FB_VARCHAR(31), relationName)
  70. (FB_VARCHAR(100), description)
  71. (FB_DECFLOAT16, df16)
  72. (FB_INT128, iHuge)
  73. ) output(&status, master);
  74. input.clear();
  75. input->systemFlag = 0;
  76. rs = att->openCursor(&status, tra, 0,
  77. "select rdb$relation_id, rdb$relation_name, rdb$description,"
  78. " cast (rdb$relation_id as decfloat(16)) * 0.05 as df16,"
  79. " cast (rdb$relation_id as int128) * 212778764464767 as iHuge"
  80. " from rdb$relations"
  81. " where rdb$system_flag = ?"
  82. " order by rdb$relation_id",
  83. SAMPLES_DIALECT, input.getMetadata(), input.getData(), output.getMetadata(), NULL, 0);
  84. printf(" ID Name datatype-tests (perform some arithmetics) /comment\n");
  85. while (rs->fetchNext(&status, output.getData()) == IStatus::RESULT_OK)
  86. {
  87. unsigned lRelName = output->relationNameNull ? 0 : output->relationName.length;
  88. unsigned lDesc = output->descriptionNull ? 0 : output->description.length;
  89. char t16[IDecFloat16::STRING_SIZE];
  90. idf16->toString(&status, &output->df16, sizeof(t16), t16);
  91. char huge[IInt128::STRING_SIZE];
  92. ii128->toString(&status, &output->iHuge, -3, sizeof(huge), huge);
  93. printf("%4d %*.*s [Decfloat16:%s Int128:%s] %c%*.*s\n", output->relationId,
  94. lRelName, lRelName, output->relationName.str,
  95. t16, huge,
  96. lDesc ? '/' : ' ',
  97. lDesc, lDesc, output->description.str);
  98. }
  99. rs->close(&status);
  100. rs = NULL;
  101. tra->commit(&status);
  102. tra = NULL;
  103. att->detach(&status);
  104. att = NULL;
  105. }
  106. catch (const FbException& error)
  107. {
  108. // handle error
  109. rc = 1;
  110. char buf[256];
  111. master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
  112. fprintf(stderr, "%s\n", buf);
  113. }
  114. // release interfaces after error caught
  115. if (rs)
  116. rs->release();
  117. if (tra)
  118. tra->release();
  119. if (att)
  120. att->release();
  121. // generic cleanup
  122. prov->release();
  123. status.dispose();
  124. }