Guest User

Untitled

a guest
Sep 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <oci.h>
  5.  
  6. /*
  7. Run the following SQL in advance.
  8.  
  9. drop table clob_test_1755 purge;
  10. create table clob_test_1755 (clob_col clob);
  11. insert into clob_test_1755 values (empty_clob());
  12. commit;
  13.  
  14. */
  15.  
  16. /* Change the following three values */
  17. const char *username = "username";
  18. const char *password = "password";
  19. const char *database = "";
  20.  
  21. static void checkerr(void *handle, ub4 htype, sword status, const char *func)
  22. {
  23. text errbuf[512];
  24. sb4 errcode = 0;
  25.  
  26. switch (status) {
  27. case OCI_SUCCESS:
  28. case OCI_SUCCESS_WITH_INFO:
  29. return;
  30. break;
  31. case OCI_NEED_DATA:
  32. printf(" %s => OCI_NEED_DATA\n", func);
  33. break;
  34. case OCI_NO_DATA:
  35. printf(" %s => OCI_NO_DATA\n", func);
  36. break;
  37. case OCI_ERROR:
  38. OCIErrorGet(handle, 1, NULL, &errcode, errbuf, sizeof(errbuf), htype);
  39. printf(" %s => OCI_ERROR\n %.*s\n", func, (int)sizeof(errbuf), errbuf);
  40. break;
  41. case OCI_INVALID_HANDLE:
  42. printf(" %s => OCI_INVALID_HANDLE\n", func);
  43. break;
  44. case OCI_STILL_EXECUTING:
  45. printf(" %s => OCI_STILL_EXECUTE\n", func);
  46. break;
  47. case OCI_CONTINUE:
  48. printf(" %s => OCI_CONTINUE\n", func);
  49. break;
  50. default:
  51. printf(" %s => Unknown %d\n", func, status);
  52. }
  53. exit(1);
  54. }
  55.  
  56. #define chke(func) do { \
  57. sword status = (func); \
  58. if (status != OCI_SUCCESS) { \
  59. checkerr(envhp, OCI_HTYPE_ENV, status, #func); \
  60. } \
  61. } while (0)
  62.  
  63. #define chk(func) do { \
  64. sword status = (func); \
  65. if (status != OCI_SUCCESS) { \
  66. checkerr(errhp, OCI_HTYPE_ERROR, status, #func); \
  67. } \
  68. } while (0)
  69.  
  70. static void read_lob_using_OCILobRead2(OCISvcCtx *svchp, OCIError *errhp, OCILobLocator *lob);
  71. static void read_lob_using_OCILobArrayRead(OCISvcCtx *svchp, OCIError *errhp, OCILobLocator *lob);
  72.  
  73. int main()
  74. {
  75. OCIEnv *envhp;
  76. OCISvcCtx *svchp;
  77. OCIServer *srvhp;
  78. OCISession *seshp;
  79. OCIError *errhp;
  80. OCIStmt *stmtp;
  81. OCIDefine *defnp;
  82. const char *sql = "select clob_col from clob_test_1755";
  83. OCILobLocator *lob;
  84.  
  85. /* allocate handles */
  86. chke(OCIEnvCreate(&envhp, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL));
  87. chke(OCIHandleAlloc(envhp, (dvoid**)&svchp, OCI_HTYPE_SVCCTX, 0, NULL));
  88. chke(OCIHandleAlloc(envhp, (dvoid**)&srvhp, OCI_HTYPE_SERVER, 0, NULL));
  89. chke(OCIHandleAlloc(envhp, (dvoid**)&seshp, OCI_HTYPE_SESSION, 0, NULL));
  90. chke(OCIHandleAlloc(envhp, (dvoid**)&errhp, OCI_HTYPE_ERROR, 0, NULL));
  91. chke(OCIDescriptorAlloc(envhp, (dvoid**)&lob, OCI_DTYPE_LOB, 0, NULL));
  92.  
  93. /* connect */
  94. chk(OCIServerAttach(srvhp, errhp, (const text*)database, strlen(database), OCI_DEFAULT));
  95. chk(OCIAttrSet(seshp, OCI_HTYPE_SESSION, (void*)username, strlen(username), OCI_ATTR_USERNAME, errhp));
  96. chk(OCIAttrSet(seshp, OCI_HTYPE_SESSION, (void*)password, strlen(password), OCI_ATTR_PASSWORD, errhp));
  97. chk(OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, srvhp, 0, OCI_ATTR_SERVER, errhp));
  98. chk(OCIAttrSet(svchp, OCI_HTYPE_SVCCTX, seshp, 0, OCI_ATTR_SESSION, errhp));
  99. chk(OCISessionBegin(svchp, errhp, seshp, OCI_CRED_RDBMS, OCI_DEFAULT));
  100.  
  101. /* prepare, execute and fetch */
  102. chk(OCIStmtPrepare2(svchp, &stmtp, errhp, (const text*)sql, strlen(sql), NULL, 0, OCI_NTV_SYNTAX, OCI_DEFAULT));
  103. chk(OCIDefineByPos(stmtp, &defnp, errhp, 1, &lob, 0, SQLT_CLOB, NULL, NULL, NULL, OCI_DEFAULT));
  104. chk(OCIStmtExecute(svchp, stmtp, errhp, 0, 0, NULL, NULL, OCI_DEFAULT));
  105. chk(OCIStmtFetch(stmtp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT));
  106.  
  107. read_lob_using_OCILobRead2(svchp, errhp, lob);
  108. read_lob_using_OCILobArrayRead(svchp, errhp, lob);
  109. return 0;
  110. }
  111.  
  112. static void read_lob_using_OCILobRead2(OCISvcCtx *svchp, OCIError *errhp, OCILobLocator *lob)
  113. {
  114. oraub8 byte_amt = 0;
  115. oraub8 char_amt = UB4MAXVAL;
  116. char buf[50];
  117.  
  118. chk(OCILobRead2(svchp, errhp, lob, &byte_amt, &char_amt, 1, buf, sizeof(buf), OCI_ONE_PIECE, NULL, NULL, 0, SQLCS_IMPLICIT));
  119. printf("len=%d, data='%.*s'\n", (int)byte_amt, (int)byte_amt, buf);
  120. }
  121.  
  122. static void read_lob_using_OCILobArrayRead(OCISvcCtx *svchp, OCIError *errhp, OCILobLocator *lob)
  123. {
  124. ub4 array_iter = 1;
  125. OCILobLocator *lobp_arr[1];
  126. oraub8 byte_amt_arr[1];
  127. oraub8 char_amt_arr[1];
  128. oraub8 offset_arr[1];
  129. void *bufp_arr[1];
  130. oraub8 bufl_arr[1];
  131. char buf[50];
  132.  
  133. lobp_arr[0] = lob;
  134. byte_amt_arr[0] = 0;
  135. char_amt_arr[0] = UB4MAXVAL;
  136. offset_arr[0] = 1;
  137. bufp_arr[0] = buf;
  138. bufl_arr[0] = sizeof(buf);
  139.  
  140. chk(OCILobArrayRead(svchp, errhp, &array_iter, lobp_arr, byte_amt_arr, char_amt_arr, offset_arr, bufp_arr, bufl_arr, OCI_ONE_PIECE, NULL, NULL, 0, SQLCS_IMPLICIT));
  141. printf("len=%d, data='%.*s'\n", (int)byte_amt_arr[0], (int)byte_amt_arr[0], buf);
  142. }
Add Comment
Please, Sign In to add comment