06.fb_message.cpp 3.5 KB

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