2
0

06.fb_message.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. int main()
  38. {
  39. int rc = 0;
  40. setenv("ISC_USER", "sysdba", 0);
  41. setenv("ISC_PASSWORD", "masterkey", 0);
  42. ThrowStatusWrapper status(master->getStatus());
  43. IProvider* prov = master->getDispatcher();
  44. IAttachment* att = NULL;
  45. ITransaction* tra = NULL;
  46. IResultSet* rs = NULL;
  47. const char* dbName = "employee";
  48. try
  49. {
  50. idf16 = master->getUtilInterface()->getDecFloat16(&status);
  51. att = prov->attachDatabase(&status, dbName, 0, NULL);
  52. tra = att->startTransaction(&status, 0, NULL);
  53. // Comment some tables
  54. att->execute(&status, tra, 0, "comment on table employee is 'Employees'",
  55. SAMPLES_DIALECT, NULL, NULL, NULL, NULL);
  56. att->execute(&status, tra, 0, "comment on table customer is 'Customers'",
  57. SAMPLES_DIALECT, NULL, NULL, NULL, NULL);
  58. att->execute(&status, tra, 0, "comment on table country is 'Countries and national currencies'",
  59. SAMPLES_DIALECT, NULL, NULL, NULL, NULL);
  60. tra->commitRetaining(&status);
  61. // Print tables list
  62. FB_MESSAGE(Input, ThrowStatusWrapper,
  63. (FB_INTEGER, systemFlag)
  64. ) input(&status, master);
  65. FB_MESSAGE(Output, ThrowStatusWrapper,
  66. (FB_SMALLINT, relationId)
  67. (FB_VARCHAR(31), relationName)
  68. (FB_VARCHAR(100), description)
  69. (FB_DECFLOAT16, df16)
  70. ) output(&status, master);
  71. input.clear();
  72. input->systemFlag = 0;
  73. rs = att->openCursor(&status, tra, 0,
  74. "select rdb$relation_id, rdb$relation_name, rdb$description,"
  75. " cast (rdb$relation_id as decfloat(16)) * 0.05 as df16"
  76. " from rdb$relations"
  77. " where rdb$system_flag = ?"
  78. " order by rdb$relation_id",
  79. SAMPLES_DIALECT, input.getMetadata(), input.getData(), output.getMetadata(), NULL, 0);
  80. printf(" ID Name/comment\n");
  81. while (rs->fetchNext(&status, output.getData()) == IStatus::RESULT_OK)
  82. {
  83. unsigned lRelName = output->relationNameNull ? 0 : output->relationName.length;
  84. unsigned lDesc = output->descriptionNull ? 0 : output->description.length;
  85. char t16[IDecFloat16::STRING_SIZE];
  86. idf16->toString(&status, &output->df16, sizeof(t16), t16);
  87. printf("%4d %*.*s%c%*.*s (%s)\n", output->relationId,
  88. lRelName, lRelName, output->relationName.str,
  89. lDesc ? '/' : ' ',
  90. lDesc, lDesc, output->description.str,
  91. t16);
  92. }
  93. rs->close(&status);
  94. rs = NULL;
  95. tra->commit(&status);
  96. tra = NULL;
  97. att->detach(&status);
  98. att = NULL;
  99. }
  100. catch (const FbException& error)
  101. {
  102. // handle error
  103. rc = 1;
  104. char buf[256];
  105. master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
  106. fprintf(stderr, "%s\n", buf);
  107. }
  108. // release interfaces after error caught
  109. if (rs)
  110. rs->release();
  111. if (tra)
  112. tra->release();
  113. if (att)
  114. att->release();
  115. // generic cleanup
  116. prov->release();
  117. status.dispose();
  118. }