api9.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Program type: API Interface
  3. *
  4. * Description:
  5. * This program uses a filter to display a blob data type.
  6. * Job descriptions are read through a filter, which formats
  7. * the text into 40-character-wide paragraphs.
  8. *
  9. * IMPORTANT NOTE!
  10. * The server side file, api9f.c, must have been compiled
  11. * and linked and must be running on the server before
  12. * this example can be run.
  13. * The contents of this file are subject to the Interbase Public
  14. * License Version 1.0 (the "License"); you may not use this file
  15. * except in compliance with the License. You may obtain a copy
  16. * of the License at http://www.Inprise.com/IPL.html
  17. *
  18. * Software distributed under the License is distributed on an
  19. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  20. * or implied. See the License for the specific language governing
  21. * rights and limitations under the License.
  22. *
  23. * The Original Code was created by Inprise Corporation
  24. * and its predecessors. Portions created by Inprise Corporation are
  25. * Copyright (C) Inprise Corporation.
  26. *
  27. * All Rights Reserved.
  28. * Contributor(s): ______________________________________.
  29. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include <ibase.h>
  34. #include "example.h"
  35. #define COUNTRYLEN 15
  36. #define CODELEN 5
  37. char *sel_str =
  38. "SELECT job_requirement, job_code, job_grade, job_country \
  39. FROM job WHERE job_code = 'SRep'";
  40. /* Blob parameter buffer. */
  41. char bpb[] = {1,2,2,-4,-1,1,2,1,0};
  42. int main (int argc, char** argv)
  43. {
  44. char job_code[CODELEN + 2];
  45. short job_grade;
  46. char job_country[COUNTRYLEN + 2];
  47. short flag0 = 0, flag1 = 0,
  48. flag2 = 0, flag3 = 0;
  49. ISC_QUAD blob_id;
  50. isc_blob_handle blob_handle = NULL;
  51. short blob_seg_len;
  52. char blob_segment[401];
  53. isc_db_handle DB = NULL;
  54. isc_tr_handle trans = NULL;
  55. ISC_STATUS_ARRAY status;
  56. isc_stmt_handle stmt = NULL;
  57. XSQLDA * sqlda;
  58. long fetch_stat;
  59. char empdb[128];
  60. if (argc > 1)
  61. strcpy(empdb, argv[1]);
  62. else
  63. strcpy(empdb, "employee.fdb");
  64. if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  65. {
  66. ERREXIT(status, 1)
  67. }
  68. if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  69. {
  70. ERREXIT(status, 1)
  71. }
  72. /*
  73. * Allocate and prepare the select statement.
  74. */
  75. if (isc_dsql_allocate_statement(status, &DB, &stmt))
  76. {
  77. ERREXIT(status, 1)
  78. }
  79. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(4));
  80. sqlda->sqln = 4;
  81. sqlda->version = 1;
  82. if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, sqlda))
  83. {
  84. ERREXIT(status, 1)
  85. }
  86. sqlda->sqlvar[0].sqldata = (char *) &blob_id;
  87. sqlda->sqlvar[0].sqltype = SQL_BLOB + 1;
  88. sqlda->sqlvar[0].sqlind = &flag0;
  89. sqlda->sqlvar[1].sqldata = (char *) job_code;
  90. sqlda->sqlvar[1].sqltype = SQL_TEXT + 1;
  91. sqlda->sqlvar[1].sqlind = &flag1;
  92. sqlda->sqlvar[2].sqldata = (char *) &job_grade;
  93. sqlda->sqlvar[2].sqltype = SQL_SHORT + 1;
  94. sqlda->sqlvar[2].sqlind = &flag2;
  95. sqlda->sqlvar[3].sqldata = (char *) job_country;
  96. sqlda->sqlvar[3].sqltype = SQL_TEXT + 1;
  97. sqlda->sqlvar[3].sqlind = &flag3;
  98. if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  99. {
  100. ERREXIT(status, 1)
  101. }
  102. /*
  103. * Display job descriptions.
  104. */
  105. while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) == 0)
  106. {
  107. job_code[CODELEN] = '\0';
  108. job_country[COUNTRYLEN] = '\0';
  109. printf("\nJOB CODE: %5s GRADE: %d", job_code, job_grade);
  110. printf(" COUNTRY: %-20s\n\n", job_country);
  111. /* Open the blob with the fetched blob_id. */
  112. if (isc_open_blob2(status, &DB, &trans, &blob_handle, &blob_id, 9, bpb))
  113. {
  114. ERREXIT(status, 1)
  115. }
  116. /* Get blob segments and their lengths and print each segment. */
  117. while (isc_get_segment(status, &blob_handle,
  118. (unsigned short *) &blob_seg_len, sizeof(blob_segment),
  119. blob_segment) == 0)
  120. printf(" %*.*s", blob_seg_len, blob_seg_len, blob_segment);
  121. /* Close the blob. */
  122. if (status[1] == isc_segstr_eof)
  123. {
  124. if (isc_close_blob(status, &blob_handle))
  125. {
  126. ERREXIT(status, 1)
  127. }
  128. }
  129. else
  130. isc_print_status(status);
  131. printf("\n");
  132. }
  133. if (fetch_stat != 100L)
  134. isc_print_status(status);
  135. if (isc_dsql_free_statement(status, &stmt, DSQL_drop))
  136. {
  137. ERREXIT(status, 1)
  138. }
  139. if (isc_commit_transaction(status, &trans))
  140. {
  141. ERREXIT(status, 1)
  142. }
  143. if (isc_detach_database(status, &DB))
  144. {
  145. ERREXIT(status, 1)
  146. }
  147. free(sqlda);
  148. return 0;
  149. }