stat10.e 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Program type: Embedded Static SQL
  3. *
  4. * Description:
  5. * This program demonstrates 'set database', 'connect',
  6. * and 'set transaction' statements.
  7. * Each time a database is connected to, a sample table
  8. * is accessed as a test.
  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 "example.h"
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. int count_types (void);
  30. int count_records (void);
  31. long pr_error (void);
  32. char *dbname = "employee.fdb";
  33. EXEC SQL INCLUDE SQLCA;
  34. int main (void)
  35. {
  36. /*
  37. * Declare 2 database handles for future use.
  38. */
  39. EXEC SQL
  40. SET DATABASE db1 = "employee.fdb";
  41. EXEC SQL
  42. SET DATABASE db2 = "employe2.fdb";
  43. /*
  44. * Open a single database.
  45. */
  46. printf("\n1. Opening database employee.fdb.\n");
  47. EXEC SQL
  48. CONNECT db1;
  49. if( pr_error())
  50. return 1;;
  51. EXEC SQL
  52. SET TRANSACTION USING db1;
  53. if (count_types())
  54. return 1;
  55. EXEC SQL
  56. COMMIT RELEASE;
  57. EXEC SQL
  58. DISCONNECT db1;
  59. /*
  60. * Use a database name supplied at run-time.
  61. * Connect to this database using an existing database handle.
  62. */
  63. printf("\n2. Opening database with name: %s supplied at run-time.\n",
  64. dbname);
  65. EXEC SQL
  66. CONNECT TO :dbname AS db1;
  67. if( pr_error())
  68. return 1;;
  69. EXEC SQL
  70. SET TRANSACTION USING db1;
  71. if( count_types())
  72. return 1;
  73. EXEC SQL
  74. COMMIT RELEASE;
  75. EXEC SQL
  76. DISCONNECT DEFAULT;
  77. /*
  78. * Open the second database within the same program,
  79. * while the first database remains disconnected.
  80. */
  81. printf("\n3. Opening a second database after closing the first one.\n");
  82. EXEC SQL
  83. CONNECT db2;
  84. if( pr_error())
  85. return 1;;
  86. EXEC SQL
  87. SET TRANSACTION USING db2;
  88. if (count_records())
  89. return 1;
  90. EXEC SQL
  91. COMMIT RELEASE;
  92. EXEC SQL
  93. DISCONNECT db2;
  94. /*
  95. * Open two databases simultaneously.
  96. */
  97. printf("\n4. Opening two databases simultaneously.\n");
  98. EXEC SQL
  99. CONNECT TO db1, db2;
  100. if( pr_error())
  101. return 1;;
  102. EXEC SQL
  103. SET TRANSACTION USING db1, db2;
  104. if (count_types())
  105. return 1;;
  106. if (count_records())
  107. return 1;
  108. EXEC SQL
  109. COMMIT RELEASE;
  110. EXEC SQL
  111. DISCONNECT db1, db2;
  112. /*
  113. * Open all databases (in this case just two) at the same time.
  114. */
  115. printf("\n5. Opening all databases.\n");
  116. EXEC SQL
  117. CONNECT TO ALL;
  118. if( pr_error())
  119. return 1;;
  120. EXEC SQL
  121. SET TRANSACTION;
  122. if (count_types())
  123. return 1;
  124. if (count_records())
  125. return 1;
  126. EXEC SQL
  127. COMMIT RELEASE;
  128. EXEC SQL
  129. DISCONNECT ALL;
  130. return (0);
  131. }
  132. /*
  133. * Access a table in database 1.
  134. */
  135. int count_types (void)
  136. {
  137. int cnt;
  138. EXEC SQL
  139. SELECT COUNT(DISTINCT currency) INTO :cnt FROM country;
  140. if (SQLCODE == 0)
  141. printf("\tNumber of currency types : %d\n", cnt);
  142. else
  143. if( pr_error())
  144. return 1;;
  145. return (0);
  146. }
  147. /*
  148. * Access a table in database 2.
  149. */
  150. int count_records (void)
  151. {
  152. int cnt;
  153. /* Use the database handle along with the table name. */
  154. EXEC SQL
  155. SELECT COUNT(DISTINCT to_currency) INTO :cnt FROM db2.cross_rate;
  156. if (SQLCODE == 0)
  157. printf("\tNumber of conversion records: %d\n", cnt);
  158. else
  159. if( pr_error())
  160. return 1;;
  161. return (0);
  162. }
  163. /*
  164. * Print an error message.
  165. */
  166. long pr_error (void)
  167. {
  168. if (SQLCODE)
  169. {
  170. isc_print_sqlerror(SQLCODE, isc_status);
  171. printf("\n");
  172. }
  173. return (SQLCODE);
  174. }