api4.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Program type: API Interface
  3. *
  4. * Desription:
  5. * This program updates departments' budgets, given
  6. * the department and the new budget information parameters.
  7. *
  8. * An input SQLDA is allocated for the update query
  9. * with parameter markers.
  10. * Note that all updates are rolled back in this version.
  11. * The contents of this file are subject to the Interbase Public
  12. * License Version 1.0 (the "License"); you may not use this file
  13. * except in compliance with the License. You may obtain a copy
  14. * of the License at http://www.Inprise.com/IPL.html
  15. *
  16. * Software distributed under the License is distributed on an
  17. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  18. * or implied. See the License for the specific language governing
  19. * rights and limitations under the License.
  20. *
  21. * The Original Code was created by Inprise Corporation
  22. * and its predecessors. Portions created by Inprise Corporation are
  23. * Copyright (C) Inprise Corporation.
  24. *
  25. * All Rights Reserved.
  26. * Contributor(s): ______________________________________.
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ibase.h>
  31. #include <stdio.h>
  32. #include "example.h"
  33. int get_input (char* , double*);
  34. static char *Dept_data[] =
  35. {"622", "100", "116", "900", 0};
  36. static double Percent_data[] =
  37. {0.05, 1.00, 0.075, 0.10, 0};
  38. int Input_ptr = 0;
  39. char *updstr =
  40. "UPDATE department SET budget = ? * budget + budget WHERE dept_no = ?";
  41. isc_db_handle DB = NULL; /* database handle */
  42. isc_tr_handle trans = NULL; /* transaction handle */
  43. ISC_STATUS_ARRAY status; /* status vector */
  44. int main (int argc, char** argv)
  45. {
  46. char dept_no[4];
  47. double percent_inc;
  48. short flag0 = 0, flag1 = 0;
  49. XSQLDA *sqlda;
  50. long sqlcode;
  51. char empdb[128];
  52. if (argc > 1)
  53. strcpy(empdb, argv[1]);
  54. else
  55. strcpy(empdb, "employee.fdb");
  56. if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  57. {
  58. ERREXIT(status, 1)
  59. }
  60. /* Allocate an input SQLDA. There are two unknown parameters. */
  61. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(2));
  62. sqlda->sqln = 2;
  63. sqlda->sqld = 2;
  64. sqlda->version = 1;
  65. sqlda->sqlvar[0].sqldata = (char *) &percent_inc;
  66. sqlda->sqlvar[0].sqltype = SQL_DOUBLE + 1;
  67. sqlda->sqlvar[0].sqllen = sizeof(percent_inc);
  68. sqlda->sqlvar[0].sqlind = &flag0;
  69. flag0 = 0;
  70. sqlda->sqlvar[1].sqldata = dept_no;
  71. sqlda->sqlvar[1].sqltype = SQL_TEXT + 1;
  72. sqlda->sqlvar[1].sqllen = 3;
  73. sqlda->sqlvar[1].sqlind = &flag1;
  74. flag1 = 0;
  75. /*
  76. * Get the next department-percent increase input pair.
  77. */
  78. while (get_input(dept_no, &percent_inc))
  79. {
  80. printf("\nIncreasing budget for department: %s by %5.2lf percent.\n",
  81. dept_no, percent_inc);
  82. if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  83. {
  84. ERREXIT(status, 1)
  85. }
  86. /* Update the budget. */
  87. isc_dsql_execute_immediate(status, &DB, &trans, 0, updstr, 1, sqlda);
  88. sqlcode = isc_sqlcode(status);
  89. if (sqlcode)
  90. {
  91. /* Don't save the update, if the new budget exceeds the limit. */
  92. if (sqlcode == -625)
  93. {
  94. printf("\tExceeded budget limit -- not updated.\n");
  95. if (isc_rollback_transaction(status, &trans))
  96. {
  97. ERREXIT(status, 1)
  98. }
  99. continue;
  100. }
  101. /* Undo all changes, in case of an error. */
  102. else
  103. {
  104. isc_print_status(status);
  105. printf("SQLCODE=%d\n", sqlcode);
  106. isc_rollback_transaction(status, &trans);
  107. ERREXIT(status, 1)
  108. }
  109. }
  110. /* Save each department's update independently.
  111. ** Change to isc_commit_transaction to see changes
  112. */
  113. if (isc_rollback_transaction (status, &trans))
  114. {
  115. ERREXIT(status, 1)
  116. }
  117. }
  118. if (isc_detach_database(status, &DB))
  119. {
  120. ERREXIT(status, 1)
  121. }
  122. free(sqlda);
  123. return 0;
  124. }
  125. /*
  126. * Get the department and percent parameters.
  127. */
  128. int get_input (char *dept_no, double *percent)
  129. {
  130. if (Dept_data[Input_ptr] == 0)
  131. return 0;
  132. strcpy(dept_no, Dept_data[Input_ptr]);
  133. if ((*percent = Percent_data[Input_ptr++]) == 0)
  134. return 0;
  135. return 1;
  136. }