07.blob.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * PROGRAM: Object oriented API samples.
  3. * MODULE: 07.blob.cpp
  4. * DESCRIPTION: A sample of loading data into blob and reading.
  5. * Run second time (when database already exists) to see
  6. * how FbException is caught and handled by this code.
  7. *
  8. * Example for the following interfaces:
  9. * IAttachment - use of open and create blob methods
  10. * IBlob - interface to work with blobs
  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. #include <firebird/Message.h>
  34. static IMaster* master = fb_get_master_interface();
  35. static const char* testData[] = {
  36. "This is test data",
  37. "that is inserted during example execution",
  38. "into blobs_table",
  39. NULL
  40. };
  41. static void errPrint(IStatus* status)
  42. {
  43. char buf[256];
  44. master->getUtilInterface()->formatStatus(buf, sizeof(buf), status);
  45. fprintf(stderr, "%s\n", buf);
  46. }
  47. static void drop(IAttachment** att)
  48. {
  49. // With CheckStatusWrapper passed as status interface noting is thrown on error
  50. // You should check status yourself
  51. CheckStatusWrapper status(master->getStatus());
  52. // drop database (will close interface)
  53. (*att)->dropDatabase(&status);
  54. if (status.getState() & IStatus::STATE_ERRORS)
  55. {
  56. errPrint(&status);
  57. fprintf(stderr, "*** Drop database failed - do it manually before next run ***\n");
  58. }
  59. else
  60. *att = NULL;
  61. // cleanup
  62. status.dispose();
  63. }
  64. int main()
  65. {
  66. int rc = 0;
  67. // set default password if none specified in environment
  68. setenv("ISC_USER", "sysdba", 0);
  69. setenv("ISC_PASSWORD", "masterkey", 0);
  70. // With ThrowStatusWrapper passed as status interface FbException will be thrown on error
  71. ThrowStatusWrapper status(master->getStatus());
  72. // Declare pointers to required interfaces
  73. IProvider* prov = master->getDispatcher();
  74. IAttachment* att = NULL;
  75. ITransaction* tra = NULL;
  76. IBlob* blob = NULL;
  77. try
  78. {
  79. // create database
  80. att = prov->createDatabase(&status, "blob_07.fdb", 0, NULL);
  81. tra = att->startTransaction(&status, 0, NULL);
  82. // create table
  83. att->execute(&status, tra, 0, "create table blobs_table (b blob sub_type text)", SAMPLES_DIALECT,
  84. NULL, NULL, NULL, NULL);
  85. tra->commitRetaining(&status);
  86. // Message for data exchange
  87. FB_MESSAGE(Msg, ThrowStatusWrapper,
  88. (FB_BLOB, b)
  89. ) message(&status, master);
  90. message.clear();
  91. // create blob
  92. blob = att->createBlob(&status, tra, &message->b, 0, NULL);
  93. // populate blob with data
  94. for (const char** seg = testData; *seg; ++seg)
  95. blob->putSegment(&status, strlen(*seg), *seg);
  96. blob->close(&status);
  97. blob = NULL;
  98. // insert blob into the table
  99. att->execute(&status, tra, 0, "insert into blobs_table(b) values(?)", SAMPLES_DIALECT,
  100. message.getMetadata(), message.getData(), NULL, NULL);
  101. tra->commitRetaining(&status);
  102. printf("Test blob inserted into blobs_table\n...\n");
  103. // Read blob from table
  104. message.clear();
  105. att->execute(&status, tra, 0, "select first(1) b from blobs_table", SAMPLES_DIALECT,
  106. NULL, NULL, message.getMetadata(), message.getData());
  107. blob = att->openBlob(&status, tra, &message->b, 0, NULL);
  108. // Read segments from blob
  109. // Use very small segment buffer to show read of incomplete segment
  110. printf("Read inserted blob from blobs_table\n...\n");
  111. int bufOver = 0;
  112. for(bool eof = false; !eof; )
  113. {
  114. const char* lineFeed = "\n";
  115. char buf[32];
  116. unsigned l = 0;
  117. switch (blob->getSegment(&status, sizeof(buf) - 1, buf, &l))
  118. {
  119. case IStatus::RESULT_OK:
  120. break;
  121. case IStatus::RESULT_SEGMENT:
  122. lineFeed = "";
  123. bufOver++;
  124. break;
  125. default:
  126. eof = true;
  127. continue;
  128. }
  129. buf[l] = 0;
  130. printf("%s%s", buf, lineFeed);
  131. }
  132. printf("\nSegment not fit in buffer counter = %d\n\n", bufOver);
  133. // cleanup
  134. blob->close(&status);
  135. blob = NULL;
  136. tra->commit(&status);
  137. tra = NULL;
  138. // uncomment next line to play with errors during drop database
  139. // printf("Attach with any client to blob_07.fdb to prevent it being dropped and press enter"); getchar();
  140. // drop database
  141. drop(&att);
  142. }
  143. catch (const FbException& error)
  144. {
  145. // handle error
  146. rc = 1;
  147. errPrint(error.getStatus());
  148. if (att)
  149. drop(&att);
  150. }
  151. // release interfaces after error caught
  152. if (blob)
  153. blob->release();
  154. if (tra)
  155. tra->release();
  156. if (att)
  157. att->release();
  158. status.dispose();
  159. prov->release();
  160. return rc;
  161. }