api10.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Program type: API
  3. *
  4. * Description:
  5. * This program selects and updates an array type.
  6. * Projected head count is displayed and updated for
  7. * a set of projects.
  8. * The contents of this file are subject to the Interbase Public
  9. * License Version 1.0 (the "License"); you may not use this file
  10. * except in compliance with the License. You may obtain a copy
  11. * of the License at http://www.Inprise.com/IPL.html
  12. *
  13. * Software distributed under the License is distributed on an
  14. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  15. * or implied. See the License for the specific language governing
  16. * rights and limitations under the License.
  17. *
  18. * The Original Code was created by Inprise Corporation
  19. * and its predecessors. Portions created by Inprise Corporation are
  20. * Copyright (C) Inprise Corporation.
  21. *
  22. * All Rights Reserved.
  23. * Contributor(s): ______________________________________.
  24. */
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <ibase.h>
  28. #include <stdio.h>
  29. #include "example.h"
  30. char *sel_str =
  31. "SELECT dept_no, quart_head_cnt FROM proj_dept_budget p \
  32. WHERE fiscal_year = 1994 AND proj_id = 'VBASE' \
  33. FOR UPDATE of quart_head_cnt";
  34. char *upd_str =
  35. "UPDATE proj_dept_budget SET quart_head_cnt = ? WHERE CURRENT OF S";
  36. int main (int argc, char** argv)
  37. {
  38. long hcnt[4];
  39. ISC_QUAD array_id;
  40. ISC_ARRAY_DESC desc;
  41. long len;
  42. char dept_no[6];
  43. isc_db_handle DB = NULL;
  44. isc_tr_handle trans = NULL;
  45. ISC_STATUS_ARRAY status;
  46. short flag0 = 0, flag1 = 0;
  47. isc_stmt_handle stmt = NULL;
  48. isc_stmt_handle ustmt = NULL;
  49. char * cursor = "S";
  50. XSQLDA * osqlda, *isqlda;
  51. long fetch_stat;
  52. short i;
  53. char empdb[128];
  54. if (argc > 1)
  55. strcpy(empdb, argv[1]);
  56. else
  57. strcpy(empdb, "employee.fdb");
  58. if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  59. {
  60. ERREXIT(status, 1)
  61. }
  62. if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  63. {
  64. ERREXIT(status, 1)
  65. }
  66. /*
  67. * Set up the array description structure
  68. */
  69. if (isc_array_lookup_bounds(status, &DB, &trans,
  70. "PROJ_DEPT_BUDGET", "QUART_HEAD_CNT", &desc))
  71. {
  72. ERREXIT(status, 1)
  73. }
  74. /*
  75. * Set-up the select statement.
  76. */
  77. osqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(2));
  78. osqlda->sqln = 2;
  79. osqlda->version = 1;
  80. osqlda->sqlvar[0].sqldata = (char *) dept_no;
  81. osqlda->sqlvar[0].sqltype = SQL_TEXT + 1;
  82. osqlda->sqlvar[0].sqlind = &flag0;
  83. osqlda->sqlvar[1].sqldata = (char *) &array_id;
  84. osqlda->sqlvar[1].sqltype = SQL_ARRAY + 1;
  85. osqlda->sqlvar[1].sqlind = &flag1;
  86. isc_dsql_allocate_statement(status, &DB, &stmt);
  87. isc_dsql_allocate_statement(status, &DB, &ustmt);
  88. /* Prepare and execute query */
  89. if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, osqlda))
  90. {
  91. ERREXIT(status, 1)
  92. }
  93. if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  94. {
  95. ERREXIT(status, 1)
  96. }
  97. /* Needed for update current */
  98. isc_dsql_set_cursor_name(status, &stmt, cursor, 0);
  99. /*
  100. * Set-up the update statement.
  101. */
  102. isqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(1));
  103. isqlda->sqln = 1;
  104. isqlda->version = 1;
  105. /* Use describe_bind to set up input sqlda */
  106. if (isc_dsql_prepare(status, &trans, &ustmt, 0, upd_str, 1, NULL))
  107. {
  108. ERREXIT(status, 1)
  109. }
  110. isc_dsql_describe_bind(status, &ustmt, 1, isqlda);
  111. isqlda->sqlvar[0].sqldata = (char *) &array_id;
  112. isqlda->sqlvar[0].sqlind = &flag1;
  113. /*
  114. * Fetch the head count for each department's 4 quarters;
  115. * increase the head count by 1 for each quarter;
  116. * and save the new head count.
  117. */
  118. while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, osqlda)) == 0)
  119. {
  120. /* Get the current array values. */
  121. if (!flag1)
  122. {
  123. len = sizeof(hcnt);;
  124. if (isc_array_get_slice(status, &DB, &trans,
  125. (ISC_QUAD *) &array_id,
  126. (ISC_ARRAY_DESC *) &desc,
  127. (void *) hcnt,
  128. (long *) &len))
  129. {ERREXIT (status, 1)}
  130. dept_no [osqlda->sqlvar[0].sqllen] = '\0';
  131. printf("Department #: %s\n\n", dept_no);
  132. printf("\tCurrent counts: %ld %ld %ld %ld\n",
  133. hcnt[0], hcnt[1], hcnt[2], hcnt[3]);
  134. /* Add 1 to each count. */
  135. for (i = 0; i < 4; i++)
  136. hcnt[i] = hcnt[i] + 1;
  137. /* Save new array values. */
  138. if (isc_array_put_slice(status, &DB, &trans,
  139. (ISC_QUAD *) &array_id,
  140. (ISC_ARRAY_DESC *) &desc,
  141. (void *) hcnt,
  142. (long *) &len))
  143. {ERREXIT (status, 1)}
  144. /* Update the array handle. */
  145. if (isc_dsql_execute(status, &trans, &ustmt, 1, isqlda))
  146. {
  147. ERREXIT(status, 1)
  148. }
  149. printf("\tNew counts : %ld %ld %ld %ld\n\n",
  150. hcnt[0], hcnt[1], hcnt[2], hcnt[3]);
  151. }
  152. }
  153. if (fetch_stat != 100L)
  154. {
  155. ERREXIT(status, 1)
  156. }
  157. isc_dsql_free_statement(status, &stmt, DSQL_close);
  158. isc_dsql_free_statement(status, &ustmt, DSQL_close);
  159. /* Do a rollback to keep from updating the sample db */
  160. if (isc_rollback_transaction(status, &trans))
  161. {
  162. ERREXIT(status, 1)
  163. }
  164. if (isc_detach_database(status, &DB))
  165. {
  166. ERREXIT(status, 1)
  167. }
  168. free(osqlda);
  169. free(isqlda);
  170. return 0;
  171. }