api11.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Program type: API Interface
  3. *
  4. * Description:
  5. * This program executes a stored procedure and selects from
  6. * a stored procedure. First, a list of projects an employee
  7. * is involved in is printed. Then the employee is added to
  8. * another project. The new list of projects is printed again.
  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 <ibase.h>
  29. #include <stdio.h>
  30. #include "example.h"
  31. #define PROJLEN 5
  32. #define BUFLEN 128
  33. int select_projects (isc_db_handle db, int emp_no);
  34. int add_emp_proj (isc_db_handle db, int emp_no, char * proj_id);
  35. int get_params (isc_db_handle db, int * emp_no, char * proj_id);
  36. int main (int argc, char** argv)
  37. {
  38. int emp_no;
  39. char proj_id[PROJLEN + 2];
  40. isc_db_handle db = NULL;
  41. ISC_STATUS_ARRAY status;
  42. char empdb[128];
  43. if (argc > 1)
  44. strcpy(empdb, argv[1]);
  45. else
  46. strcpy(empdb, "employee.fdb");
  47. if (isc_attach_database(status, 0, empdb, &db, 0, NULL))
  48. {
  49. ERREXIT(status, 1)
  50. }
  51. /*
  52. * Add employee with id 8 to project 'MAPDB'.
  53. */
  54. if (get_params(db, &emp_no, proj_id))
  55. return 1;
  56. /*
  57. * Display employee's current projects.
  58. */
  59. printf("\nCurrent projects for employee id: %d\n\n", emp_no);
  60. if (select_projects(db, emp_no))
  61. return 1;
  62. /*
  63. * Insert a new employee project row.
  64. */
  65. printf("\nAdd employee id: %d to project: %s\n", emp_no, proj_id);
  66. if (add_emp_proj(db, emp_no, proj_id))
  67. return 1;
  68. /*
  69. * Display employee's new current projects.
  70. */
  71. printf("\nCurrent projects for employee id: %d\n\n", emp_no);
  72. if (select_projects(db, emp_no))
  73. return 1;
  74. if (isc_detach_database(status, &db))
  75. {
  76. ERREXIT(status, 1)
  77. }
  78. return 0 ;
  79. }
  80. /*
  81. * Select from a stored procedure.
  82. * Procedure 'get_emp_proj' gets employee's projects.
  83. */
  84. int select_projects (isc_db_handle db, int emp_no)
  85. {
  86. char proj_id[PROJLEN + 2];
  87. char selstr[BUFLEN];
  88. short flag0 = 0;
  89. isc_tr_handle trans = NULL;
  90. ISC_STATUS_ARRAY status;
  91. isc_stmt_handle stmt = NULL;
  92. XSQLDA *sqlda;
  93. long fetch_stat;
  94. sprintf(selstr, "SELECT proj_id FROM get_emp_proj (%d) ORDER BY proj_id",
  95. emp_no);
  96. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(1));
  97. sqlda->sqln = 1;
  98. sqlda->version = 1;
  99. if (isc_start_transaction(status, &trans, 1, &db, 0, NULL))
  100. {
  101. ERREXIT(status, 1)
  102. }
  103. if (isc_dsql_allocate_statement(status, &db, &stmt))
  104. {
  105. ERREXIT(status, 1)
  106. }
  107. if (isc_dsql_prepare(status, &trans, &stmt, 0, selstr, 1, sqlda))
  108. {
  109. ERREXIT(status, 1)
  110. }
  111. sqlda->sqlvar[0].sqldata = (char *) proj_id;
  112. sqlda->sqlvar[0].sqlind = &flag0;
  113. sqlda->sqlvar[0].sqltype = SQL_TEXT + 1;
  114. if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  115. {
  116. ERREXIT(status, 1)
  117. }
  118. while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) == 0)
  119. {
  120. proj_id[PROJLEN] = '\0';
  121. printf("\t%s\n", proj_id);
  122. }
  123. if (fetch_stat != 100L)
  124. {
  125. ERREXIT(status, 1)
  126. }
  127. if (isc_dsql_free_statement(status, &stmt, DSQL_close))
  128. {
  129. ERREXIT(status, 1)
  130. }
  131. if (isc_commit_transaction(status, &trans))
  132. {
  133. ERREXIT(status, 1)
  134. }
  135. free(sqlda);
  136. return 0;
  137. }
  138. /*
  139. * Execute a stored procedure.
  140. * Procedure 'add_emp_proj' adds an employee to a project.
  141. */
  142. int add_emp_proj (isc_db_handle db, int emp_no, char *proj_id)
  143. {
  144. char exec_str[BUFLEN];
  145. isc_tr_handle trans = NULL;
  146. ISC_STATUS_ARRAY status;
  147. sprintf(exec_str, "EXECUTE PROCEDURE add_emp_proj %d, \"%s\"",
  148. emp_no, proj_id);
  149. if (isc_start_transaction(status, &trans, 1, &db, 0, NULL))
  150. {
  151. ERREXIT(status, 1)
  152. }
  153. if (isc_dsql_execute_immediate(status, &db, &trans, 0, exec_str, 1, NULL))
  154. {
  155. ERREXIT(status, 1)
  156. }
  157. if (isc_commit_transaction(status, &trans))
  158. {
  159. ERREXIT (status, 1)
  160. }
  161. return 0;
  162. }
  163. /*
  164. * Set-up procedure parameters and clean-up old data.
  165. */
  166. int get_params (isc_db_handle db, int *emp_no, char *proj_id)
  167. {
  168. isc_tr_handle trans = NULL;
  169. ISC_STATUS_ARRAY status;
  170. *emp_no = 8;
  171. strcpy(proj_id, "MAPDB");
  172. /* Cleanup: delete row from the previous run. */
  173. if (isc_start_transaction(status, &trans, 1, &db, 0, NULL))
  174. {
  175. ERREXIT(status, 1)
  176. }
  177. if (isc_dsql_execute_immediate(status, &db, &trans, 0,
  178. "DELETE FROM employee_project \
  179. WHERE emp_no = 8 AND proj_id = 'MAPDB'", 1,
  180. NULL))
  181. {
  182. ERREXIT(status, 1)
  183. }
  184. if (isc_commit_transaction(status, &trans))
  185. {
  186. ERREXIT(status, 1)
  187. }
  188. return 0;
  189. }