2
0

api7.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Program type: API Interface
  3. *
  4. * Description:
  5. * This program selects a blob data type.
  6. * A set of project descriptions is printed.
  7. * The contents of this file are subject to the Interbase Public
  8. * License Version 1.0 (the "License"); you may not use this file
  9. * except in compliance with the License. You may obtain a copy
  10. * of the License at http://www.Inprise.com/IPL.html
  11. *
  12. * Software distributed under the License is distributed on an
  13. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  14. * or implied. See the License for the specific language governing
  15. * rights and limitations under the License.
  16. *
  17. * The Original Code was created by Inprise Corporation
  18. * and its predecessors. Portions created by Inprise Corporation are
  19. * Copyright (C) Inprise Corporation.
  20. *
  21. * All Rights Reserved.
  22. * Contributor(s): ______________________________________.
  23. */
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ibase.h>
  27. #include <stdio.h>
  28. #include "example.h"
  29. #define TYPELEN 12
  30. #define PROJLEN 20
  31. #define BUFLEN 512
  32. /* This macro is used to declare structures representing SQL VARCHAR types */
  33. #define SQL_VARCHAR(len) struct {short vary_length; char vary_string[(len)+1];}
  34. int main (int argc, char** argv)
  35. {
  36. SQL_VARCHAR(PROJLEN + 2) proj_name;
  37. char prod_type[TYPELEN + 2];
  38. char sel_str[BUFLEN + 1];
  39. ISC_QUAD blob_id;
  40. isc_blob_handle blob_handle = NULL;
  41. short blob_seg_len;
  42. char blob_segment[11];
  43. isc_db_handle DB = NULL; /* database handle */
  44. isc_tr_handle trans = NULL; /* transaction handle */
  45. ISC_STATUS_ARRAY status; /* status vector */
  46. isc_stmt_handle stmt = NULL; /* statement handle */
  47. XSQLDA * sqlda;
  48. long fetch_stat, blob_stat;
  49. short flag0 = 0,
  50. flag1 = 0,
  51. flag2 = 0;
  52. char empdb[128];
  53. if (argc > 1)
  54. strcpy(empdb, argv[1]);
  55. else
  56. strcpy(empdb, "employee.fdb");
  57. strcpy(sel_str, "SELECT proj_name, proj_desc, product FROM project WHERE \
  58. product IN ('software', 'hardware', 'other') ORDER BY proj_name");
  59. if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  60. {
  61. ERREXIT(status, 1)
  62. }
  63. if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  64. {
  65. ERREXIT(status, 1)
  66. }
  67. /*
  68. * Allocate and prepare the select statement.
  69. */
  70. if (isc_dsql_allocate_statement(status, &DB, &stmt))
  71. {
  72. ERREXIT(status, 1)
  73. }
  74. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(3));
  75. sqlda->sqln = 3;
  76. sqlda->version = 1;
  77. if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, sqlda))
  78. {
  79. ERREXIT(status, 1)
  80. }
  81. sqlda->sqlvar[0].sqldata = (char *)&proj_name;
  82. sqlda->sqlvar[0].sqltype = SQL_VARYING + 1;
  83. sqlda->sqlvar[0].sqlind = &flag0;
  84. sqlda->sqlvar[1].sqldata = (char *) &blob_id;
  85. sqlda->sqlvar[1].sqltype = SQL_BLOB + 1;
  86. sqlda->sqlvar[1].sqlind = &flag1;
  87. sqlda->sqlvar[2].sqldata = prod_type;
  88. sqlda->sqlvar[2].sqltype = SQL_TEXT + 1;
  89. sqlda->sqlvar[2].sqlind = &flag2;
  90. if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  91. {
  92. ERREXIT(status, 1)
  93. }
  94. /*
  95. * For each project in the select statement, get and display
  96. * project descriptions.
  97. */
  98. while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) == 0)
  99. {
  100. prod_type[TYPELEN] = '\0';
  101. printf("\nPROJECT: %-20.*s TYPE: %-15s\n\n",
  102. proj_name.vary_length, proj_name.vary_string, prod_type);
  103. /* Open the blob with the fetched blob_id. Notice that the
  104. * segment length is shorter than the average segment fetched.
  105. * Each partial fetch should return isc_segment.
  106. */
  107. if (isc_open_blob(status, &DB, &trans, &blob_handle, &blob_id))
  108. {
  109. ERREXIT(status, 1)
  110. }
  111. /* Get blob segments and their lengths and print each segment. */
  112. blob_stat = isc_get_segment(status, &blob_handle,
  113. (unsigned short *) &blob_seg_len,
  114. sizeof(blob_segment), blob_segment);
  115. while (blob_stat == 0 || status[1] == isc_segment)
  116. {
  117. printf("%*.*s", blob_seg_len, blob_seg_len, blob_segment);
  118. blob_stat = isc_get_segment(status, &blob_handle,
  119. (unsigned short *)&blob_seg_len,
  120. sizeof(blob_segment), blob_segment);
  121. }
  122. /* Close the blob. Should be blob_stat to check */
  123. if (status[1] == isc_segstr_eof)
  124. {
  125. if (isc_close_blob(status, &blob_handle))
  126. {
  127. ERREXIT(status, 1)
  128. }
  129. }
  130. else
  131. isc_print_status(status);
  132. printf("\n");
  133. }
  134. if (fetch_stat != 100L)
  135. {
  136. ERREXIT(status, 1)
  137. }
  138. if (isc_dsql_free_statement(status, &stmt, DSQL_close))
  139. {
  140. ERREXIT(status, 1)
  141. }
  142. if (isc_commit_transaction (status, &trans))
  143. {
  144. ERREXIT(status, 1)
  145. }
  146. if (isc_detach_database(status, &DB))
  147. {
  148. ERREXIT(status, 1)
  149. }
  150. free(sqlda);
  151. return 0;
  152. }