api16t.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Program type: API
  3. *
  4. * Description:
  5. * If run from a Windows Client, the winevent program
  6. * should be started before running this program and should
  7. * be terminated upon the completion of this program.
  8. * For other platforms, this program should be run in
  9. * conjunction with api16.
  10. *
  11. * This Program adds some sales records, in order to trigger
  12. * the event that api16 or winevent is waiting for.
  13. * The contents of this file are subject to the Interbase Public
  14. * License Version 1.0 (the "License"); you may not use this file
  15. * except in compliance with the License. You may obtain a copy
  16. * of the License at http://www.Inprise.com/IPL.html
  17. *
  18. * Software distributed under the License is distributed on an
  19. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  20. * or implied. See the License for the specific language governing
  21. * rights and limitations under the License.
  22. *
  23. * The Original Code was created by Inprise Corporation
  24. * and its predecessors. Portions created by Inprise Corporation are
  25. * Copyright (C) Inprise Corporation.
  26. *
  27. * All Rights Reserved.
  28. * Contributor(s): ______________________________________.
  29. */
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include "example.h"
  34. #include <ibase.h>
  35. int main (int argc, char** argv)
  36. {
  37. struct {
  38. short len;
  39. char data [9];
  40. } po_number;
  41. short nullind = 0;
  42. isc_stmt_handle stmt = NULL; /* statement handle */
  43. isc_db_handle DB = NULL; /* database handle */
  44. isc_tr_handle trans = NULL; /* transaction handle */
  45. ISC_STATUS_ARRAY status; /* status vector */
  46. XSQLDA * sqlda;
  47. char empdb[128];
  48. char *delete_str =
  49. "DELETE FROM sales WHERE po_number LIKE 'VNEW%'";
  50. char *insert_str =
  51. "INSERT INTO sales (po_number, cust_no, order_status, total_value) \
  52. VALUES (?, 1015, 'new', 0)";
  53. if (argc > 1)
  54. strcpy(empdb, argv[1]);
  55. else
  56. strcpy(empdb, "employee.fdb");
  57. if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  58. {
  59. ERREXIT(status, 1)
  60. }
  61. if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  62. {
  63. ERREXIT(status, 1)
  64. }
  65. /* Allocate an input SQLDA for po_number in insert string. */
  66. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(1));
  67. sqlda->sqln = 1;
  68. sqlda->sqld = 1;
  69. sqlda->version = 1;
  70. /* Allocate a statement. */
  71. if (isc_dsql_allocate_statement(status, &DB, &stmt))
  72. {
  73. ERREXIT(status, 1)
  74. }
  75. /* Start out by deleting any existing records */
  76. if (isc_dsql_execute_immediate(status, &DB, &trans, 0, delete_str,
  77. 1, NULL))
  78. {
  79. ERREXIT(status, 2)
  80. }
  81. if (isc_commit_transaction(status, &trans))
  82. {
  83. ERREXIT(status, 2)
  84. }
  85. if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  86. {
  87. ERREXIT(status, 3)
  88. }
  89. /* Insert three records in one transaction */
  90. if (isc_dsql_prepare(status, &trans, &stmt, 0, insert_str, 1, NULL))
  91. {
  92. ERREXIT(status, 4)
  93. }
  94. if (isc_dsql_describe_bind(status, &stmt, 1, sqlda))
  95. {
  96. ERREXIT(status, 5)
  97. }
  98. sqlda->sqlvar[0].sqltype = SQL_VARYING + 1;
  99. sqlda->sqlvar[0].sqllen = sizeof (po_number.data);
  100. sqlda->sqlvar[0].sqldata = (char *) &po_number;
  101. sqlda->sqlvar[0].sqlind = &nullind;
  102. /* Add batch 1. */
  103. po_number.len = strlen("VNEW1");
  104. strncpy(po_number.data, "VNEW1", sizeof (po_number.data));
  105. printf("api16t: Adding %s\n", po_number.data);
  106. if (isc_dsql_execute(status, &trans, &stmt, 1, sqlda))
  107. {
  108. ERREXIT(status, 6)
  109. }
  110. po_number.len = strlen("VNEW2");
  111. strncpy(po_number.data, "VNEW2", sizeof (po_number.data));
  112. printf("api16t: Adding %s\n", po_number.data);
  113. if (isc_dsql_execute(status, &trans, &stmt, 1, sqlda))
  114. {
  115. ERREXIT(status, 6)
  116. }
  117. po_number.len = strlen("VNEW3");
  118. strncpy(po_number.data, "VNEW3", sizeof (po_number.data));
  119. printf("api16t: Adding %s\n", po_number.data);
  120. if (isc_dsql_execute(status, &trans, &stmt, 1, sqlda))
  121. {
  122. ERREXIT(status, 6)
  123. }
  124. /* This will fire the triggers for the first three records */
  125. if (isc_commit_transaction(status, &trans))
  126. {
  127. ERREXIT(status, 7)
  128. }
  129. /* Add batch 2. */
  130. if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  131. {
  132. ERREXIT(status, 8)
  133. }
  134. po_number.len = strlen("VNEW4");
  135. strncpy(po_number.data, "VNEW4", sizeof (po_number.data));
  136. printf("api16t: Adding %s\n", po_number.data);
  137. if (isc_dsql_execute(status, &trans, &stmt, 1, sqlda))
  138. {
  139. ERREXIT(status, 9)
  140. }
  141. if (isc_dsql_free_statement(status, &stmt, DSQL_drop))
  142. {
  143. ERREXIT(status, 10)
  144. }
  145. /* This will fire the triggers for the fourth record */
  146. if (isc_commit_transaction(status, &trans))
  147. {
  148. ERREXIT(status, 11)
  149. }
  150. isc_detach_database(status, &DB);
  151. free(sqlda);
  152. return 0;
  153. }