2
0

api8.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Program type: API Interface
  3. *
  4. * Description:
  5. * This program updates a blob data type.
  6. * Project descriptions are added for a set of projects.
  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 PROJLEN 5
  30. #define BUFLEN 512
  31. char * get_line (void);
  32. static char *Proj_data[] =
  33. {
  34. "VBASE",
  35. "Design a video data base management system for ",
  36. "controlling on-demand video distribution.",
  37. 0,
  38. "DGPII",
  39. "Develop second generation digital pizza maker ",
  40. "with flash-bake heating element and ",
  41. "digital ingredient measuring system.",
  42. 0,
  43. "GUIDE",
  44. "Develop a prototype for the automobile version of ",
  45. "the hand-held map browsing device.",
  46. 0,
  47. "MAPDB",
  48. "Port the map browsing database software to run ",
  49. "on the automobile model.",
  50. 0,
  51. "HWRII",
  52. "Integrate the hand-writing recognition module into the ",
  53. "universal language translator.",
  54. 0,
  55. 0
  56. };
  57. int Inp_ptr = 0;
  58. int main (int argc, char** argv)
  59. {
  60. char proj_id[PROJLEN + 1];
  61. char upd_stmt[BUFLEN + 1];
  62. ISC_QUAD blob_id;
  63. isc_blob_handle blob_handle = NULL;
  64. isc_db_handle DB = NULL; /* database handle */
  65. isc_tr_handle trans = NULL; /* transaction handle */
  66. ISC_STATUS_ARRAY status; /* status vector */
  67. XSQLDA * sqlda;
  68. unsigned short len;
  69. char * line;
  70. int rec_cnt = 0;
  71. char empdb[128];
  72. if (argc > 1)
  73. strcpy(empdb, argv[1]);
  74. else
  75. strcpy(empdb, "employee.fdb");
  76. strcpy(upd_stmt, "UPDATE project SET proj_desc = ? WHERE proj_id = ?");
  77. if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  78. isc_print_status(status);
  79. /*
  80. * Set-up the SQLDA for the update statement.
  81. */
  82. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(2));
  83. sqlda->sqln = 2;
  84. sqlda->sqld = 2;
  85. sqlda->version = 1;
  86. sqlda->sqlvar[0].sqldata = (char *) &blob_id;
  87. sqlda->sqlvar[0].sqltype = SQL_BLOB;
  88. sqlda->sqlvar[0].sqllen = sizeof(ISC_QUAD);
  89. sqlda->sqlvar[1].sqldata = proj_id;
  90. sqlda->sqlvar[1].sqltype = SQL_TEXT;
  91. sqlda->sqlvar[1].sqllen = 5;
  92. if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  93. isc_print_status(status);
  94. /*
  95. * Get the next project id and update the project description.
  96. */
  97. line = get_line();
  98. while (line)
  99. {
  100. strcpy(proj_id, line);
  101. printf("\nUpdating description for project: %s\n\n", proj_id);
  102. blob_handle = 0;
  103. if (isc_create_blob(status, &DB, &trans, &blob_handle, &blob_id))
  104. {
  105. ERREXIT(status, 1)
  106. }
  107. line = get_line();
  108. while (line)
  109. {
  110. printf(" Inserting segment: %s\n", line);
  111. len = (unsigned short)strlen(line);
  112. if (isc_put_segment(status, &blob_handle, len, line))
  113. {
  114. ERREXIT (status, 1)
  115. }
  116. line = get_line();
  117. }
  118. if (isc_close_blob(status, &blob_handle))
  119. {
  120. ERREXIT(status, 1)
  121. }
  122. if (isc_dsql_execute_immediate(status, &DB, &trans, 0, upd_stmt, 1,
  123. sqlda))
  124. {
  125. ERREXIT (status, 1)
  126. }
  127. if (isc_sqlcode(status) == 0)
  128. rec_cnt++;
  129. else
  130. printf("Input error -- no project record with key: %s\n", proj_id);
  131. line = get_line();
  132. }
  133. if (isc_rollback_transaction (status, &trans))
  134. {
  135. ERREXIT(status, 1)
  136. }
  137. printf("\n\nAdded %d project descriptions.\n", rec_cnt);
  138. if (isc_detach_database(status, &DB))
  139. {
  140. ERREXIT(status, 1)
  141. }
  142. free(sqlda);
  143. return 0;
  144. }
  145. /*
  146. * Get the next input line, which is either a project id
  147. * or a project description segment.
  148. */
  149. char *get_line (void)
  150. {
  151. return Proj_data[Inp_ptr++];
  152. }