dyn4.e 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Program type: Embedded Dynamic SQL
  3. *
  4. * Description:
  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. * The contents of this file are subject to the Interbase Public
  11. * License Version 1.0 (the "License"); you may not use this file
  12. * except in compliance with the License. You may obtain a copy
  13. * of the License at http://www.Inprise.com/IPL.html
  14. *
  15. * Software distributed under the License is distributed on an
  16. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  17. * or implied. See the License for the specific language governing
  18. * rights and limitations under the License.
  19. *
  20. * The Original Code was created by Inprise Corporation
  21. * and its predecessors. Portions created by Inprise Corporation are
  22. * Copyright (C) Inprise Corporation.
  23. *
  24. * All Rights Reserved.
  25. * Contributor(s): ______________________________________.
  26. */
  27. #include "example.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30. int get_input (char *dept_no, double *percent);
  31. static char *Dept_data[] =
  32. {"622", "100", "116", "900", 0};
  33. static double Percent_data[] =
  34. {0.05, 1.00, 0.075, 0.10, 0};
  35. int Input_ptr = 0;
  36. char Db_name[128];
  37. EXEC SQL
  38. SET DATABASE empdb = "employee.fdb" RUNTIME :Db_name;
  39. char *upd_str =
  40. "UPDATE department SET budget = ? * budget + budget WHERE dept_no = ?";
  41. int main(int argc, char** argv)
  42. {
  43. BASED_ON department.dept_no dept_no;
  44. double percent_inc;
  45. short nullind = 0;
  46. XSQLDA *sqlda;
  47. if (argc > 1)
  48. strcpy(Db_name, argv[1]);
  49. else
  50. strcpy(Db_name, "employee.fdb");
  51. EXEC SQL
  52. WHENEVER SQLERROR GO TO Error;
  53. EXEC SQL
  54. CONNECT empdb;
  55. EXEC SQL
  56. SET TRANSACTION USING empdb;
  57. /* Allocate an input SQLDA. There are two unknown parameters. */
  58. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(2));
  59. sqlda->sqln = 2;
  60. sqlda->sqld = 2;
  61. sqlda->version = 1;
  62. /* Prepare the query. */
  63. EXEC SQL
  64. PREPARE q FROM :upd_str;
  65. /* Prepare the input sqlda, only data and indicator to set */
  66. EXEC SQL
  67. DESCRIBE INPUT q USING SQL DESCRIPTOR sqlda;
  68. sqlda->sqlvar[0].sqldata = (char *) &percent_inc;
  69. sqlda->sqlvar[0].sqlind = &nullind;
  70. sqlda->sqlvar[1].sqldata = dept_no;
  71. /* FOrce the type to char instead of varchar */
  72. sqlda->sqlvar[1].sqltype = SQL_TEXT +1;
  73. sqlda->sqlvar[1].sqlind = &nullind;
  74. /* Expect an error, trap it */
  75. EXEC SQL WHENEVER SQLERROR CONTINUE;
  76. /*
  77. * Get the next department-percent increase input pair.
  78. */
  79. while (get_input(dept_no, &percent_inc))
  80. {
  81. printf("\nIncreasing budget for department: %s by %5.2f percent.\n",
  82. dept_no, percent_inc);
  83. /* Update the budget. */
  84. EXEC SQL
  85. EXECUTE q USING SQL DESCRIPTOR sqlda;
  86. /* Don't save the update, if the new budget exceeds
  87. ** the limit. Detect an integrity violation
  88. */
  89. if (SQLCODE == -625)
  90. {
  91. printf("\tExceeded budget limit -- not updated.\n");
  92. continue;
  93. }
  94. /* Undo all changes, in case of an error. */
  95. else if (SQLCODE)
  96. goto Error;
  97. /* Save each department's update independently. */
  98. EXEC SQL
  99. COMMIT RETAIN;
  100. }
  101. EXEC SQL
  102. COMMIT RELEASE;
  103. return (0);
  104. Error:
  105. isc_print_status(gds__status);
  106. printf("SQLCODE=%d\n", SQLCODE);
  107. EXEC SQL
  108. ROLLBACK RELEASE;
  109. EXEC SQL
  110. DISCONNECT empdb;
  111. free( sqlda);
  112. return(1);
  113. }
  114. /*
  115. * Get the department and percent parameters.
  116. */
  117. int get_input(char* dept_no, double* percent)
  118. {
  119. if (Dept_data[Input_ptr] == 0)
  120. return 0;
  121. strcpy(dept_no, Dept_data[Input_ptr]);
  122. if ((*percent = Percent_data[Input_ptr++]) == 0)
  123. return 0;
  124. return(1);
  125. }