api2.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Program type: API Interface
  3. *
  4. * Description:
  5. * This program adds several departments with small default
  6. * budgets, using 'execute immediate' statement.
  7. * Then, a prepared statement, which doubles budgets for
  8. * departments with low budgets, is executed.
  9. * The contents of this file are subject to the Interbase Public
  10. * License Version 1.0 (the "License"); you may not use this file
  11. * except in compliance with the License. You may obtain a copy
  12. * of the License at http://www.Inprise.com/IPL.html
  13. *
  14. * Software distributed under the License is distributed on an
  15. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  16. * or implied. See the License for the specific language governing
  17. * rights and limitations under the License.
  18. *
  19. * The Original Code was created by Inprise Corporation
  20. * and its predecessors. Portions created by Inprise Corporation are
  21. * Copyright (C) Inprise Corporation.
  22. *
  23. * All Rights Reserved.
  24. * Contributor(s): ______________________________________.
  25. */
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <stdio.h>
  29. #include "example.h"
  30. #include <ibase.h>
  31. #define MAXLEN 256
  32. #define DNAMELEN 25
  33. #define DNOLEN 3
  34. int getins (char *, int);
  35. int cleanup (void);
  36. isc_db_handle DB = 0; /* database handle */
  37. isc_tr_handle trans = 0; /* transaction handle */
  38. ISC_STATUS_ARRAY status; /* status vector */
  39. char Db_name[128];
  40. int main (int argc, char** argv)
  41. {
  42. int n = 0;
  43. char exec_str[MAXLEN];
  44. char prep_str[MAXLEN];
  45. isc_stmt_handle double_budget = 0; /* statement handle */
  46. if (argc > 1)
  47. strcpy(Db_name, argv[1]);
  48. else
  49. strcpy(Db_name, "employee.fdb");
  50. if (isc_attach_database(status, 0, Db_name, &DB, 0, NULL))
  51. {
  52. ERREXIT(status, 1)
  53. }
  54. cleanup();
  55. /*
  56. * Prepare a statement, which may be executed more than once.
  57. */
  58. strcpy(prep_str,
  59. "UPDATE DEPARTMENT SET budget = budget * 2 WHERE budget < 100000");
  60. /* Allocate a statement. */
  61. if (isc_dsql_allocate_statement(status, &DB, &double_budget))
  62. {
  63. ERREXIT(status, 1)
  64. }
  65. if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  66. {
  67. ERREXIT(status, 1)
  68. }
  69. /* Prepare the statement. */
  70. if (isc_dsql_prepare(status, &trans, &double_budget, 0, prep_str, 3, NULL))
  71. {
  72. ERREXIT(status, 1)
  73. }
  74. /*
  75. * Add new departments, using 'execute immediate'.
  76. * Build each 'insert' statement, using the supplied parameters.
  77. * Since these statements will not be needed after they are executed,
  78. * use 'execute immediate'.
  79. */
  80. while (getins(exec_str, n++))
  81. {
  82. printf("\nExecuting statement:\n%d:\t%s;\n", n, exec_str);
  83. if (isc_dsql_execute_immediate(status, &DB, &trans, 0, exec_str, 3, NULL))
  84. {
  85. ERREXIT(status, 1)
  86. }
  87. }
  88. /*
  89. * Execute a previously prepared statement.
  90. */
  91. printf("\nExecuting a prepared statement:\n\t%s;\n\n", prep_str);
  92. if (isc_dsql_execute(status, &trans, &double_budget, 3, NULL))
  93. {
  94. ERREXIT(status, 1)
  95. }
  96. if (isc_dsql_free_statement(status, &double_budget, DSQL_drop))
  97. {
  98. ERREXIT(status, 1)
  99. }
  100. if (isc_commit_transaction(status, &trans))
  101. {
  102. ERREXIT(status, 1)
  103. }
  104. if (isc_detach_database(status, &DB))
  105. {
  106. ERREXIT(status, 1)
  107. }
  108. return 0;
  109. }
  110. /*
  111. * Construct an 'insert' statement from the supplied parameters.
  112. */
  113. int getins (char* line, int line_no)
  114. {
  115. static char * Dept_data[] =
  116. {
  117. "117", "Field Office: Hong Kong", "110",
  118. "118", "Field Office: Australia", "110",
  119. "119", "Field Office: New Zealand", "110",
  120. 0
  121. };
  122. char dept_no[4];
  123. char department[30];
  124. char dept_head[4];
  125. if (Dept_data[3 * line_no] == 0)
  126. return 0;
  127. strcpy(dept_no, Dept_data[3 * line_no]);
  128. strcpy(department, Dept_data[3 * line_no + 1]);
  129. strcpy(dept_head, Dept_data[3 * line_no + 2]);
  130. sprintf(line, "INSERT INTO DEPARTMENT (dept_no, department, head_dept) \
  131. VALUES ('%s', '%s', '%s')", dept_no, department, dept_head);
  132. return 1;
  133. }
  134. /*
  135. * Delete old data.
  136. */
  137. int cleanup (void)
  138. {
  139. if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  140. {
  141. ERREXIT(status, 1)
  142. }
  143. if (isc_dsql_execute_immediate(status, &DB, &trans, 0,
  144. "DELETE FROM department WHERE dept_no IN ('117', '118', '119')", 1, 0L))
  145. {
  146. ERREXIT(status, 1)
  147. }
  148. if (isc_commit_transaction(status, &trans))
  149. {
  150. ERREXIT(status, 1)
  151. }
  152. return 0;
  153. }