2
0

api15.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Program type: API
  3. *
  4. * Description:
  5. * This program demonstrates constructing a database
  6. * parameter buffer and attaching to a database.
  7. * First manually set sweep interval, then
  8. * The user and password is set to "guest" with expand_dpb
  9. * Then get back the sweep interval and other useful numbers
  10. * with an info call.
  11. * This program will accept up to 4 args. All are positional:
  12. * api15 <db_name> <user> <password> <sweep_interval>
  13. *
  14. * Note: The system administrator needs to create the account
  15. * "guest" with password "guest" on the server before this
  16. * example can be run.
  17. * The contents of this file are subject to the Interbase Public
  18. * License Version 1.0 (the "License"); you may not use this file
  19. * except in compliance with the License. You may obtain a copy
  20. * of the License at http://www.Inprise.com/IPL.html
  21. *
  22. * Software distributed under the License is distributed on an
  23. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  24. * or implied. See the License for the specific language governing
  25. * rights and limitations under the License.
  26. *
  27. * The Original Code was created by Inprise Corporation
  28. * and its predecessors. Portions created by Inprise Corporation are
  29. * Copyright (C) Inprise Corporation.
  30. *
  31. * All Rights Reserved.
  32. * Contributor(s): ______________________________________.
  33. */
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include "example.h"
  38. #include <ibase.h>
  39. int main (int argc, char** argv)
  40. {
  41. isc_db_handle db = NULL; /* database handle */
  42. isc_tr_handle trans = NULL; /* transaction handle */
  43. isc_stmt_handle stmt = NULL;
  44. ISC_STATUS_ARRAY status; /* status vector */
  45. char dbname[128];
  46. char user_name[31], uname[81];
  47. short nullind;
  48. char password[31];
  49. /* Query to find current user name */
  50. static char *query = "SELECT USER FROM RDB$DATABASE";
  51. char * dpb = NULL, /* DB parameter buffer */
  52. *d, *p, *copy;
  53. XSQLDA *sqlda;
  54. short dpb_length = 0;
  55. long l,sweep_interval = 16384;
  56. /* All needed for analyzing an info packet */
  57. char buffer[100]; /* Buffer for db info */
  58. char item;
  59. short length;
  60. long value_out;
  61. /* List of items for db_info call */
  62. static char db_items [] = {
  63. isc_info_page_size,
  64. isc_info_allocation,
  65. isc_info_sweep_interval,
  66. isc_info_end
  67. };
  68. strcpy(user_name, "guest");
  69. strcpy(password, "guest");
  70. strcpy(dbname, "employee.fdb");
  71. if (argc > 1)
  72. strcpy(dbname, argv[1]);
  73. if (argc > 2)
  74. strcpy(user_name, argv[2]);
  75. if (argc > 3)
  76. strcpy(password, argv[3]);
  77. if (argc > 4)
  78. sweep_interval = atoi(argv[4]);
  79. /* Adding sweep interval will be done by hand
  80. ** First byte is a version (1), next byte is the isc_dpb_sweep
  81. ** byte, then a length (4) then the byte-swapped int we want
  82. ** to provide -- 7 bytes total
  83. */
  84. copy = dpb = (char *) malloc(7);
  85. p = dpb;
  86. *p++ = '\1';
  87. *p++ = isc_dpb_sweep_interval;
  88. *p++ = '\4';
  89. l = isc_vax_integer((char *) &sweep_interval, 4);
  90. d = (char *) &l;
  91. *p++ = *d++;
  92. *p++ = *d++;
  93. *p++ = *d++;
  94. *p = *d;
  95. dpb_length = 7;
  96. /* Add user and password to dpb, much easier. The dpb will be
  97. ** new memory.
  98. */
  99. isc_expand_dpb(&dpb, (short *) &dpb_length,
  100. isc_dpb_user_name, user_name,
  101. isc_dpb_password, password, NULL);
  102. /*
  103. ** Connect to the database with the given user and pw.
  104. */
  105. printf("Attaching to %s with user name: %s, password: %s\n",
  106. dbname, user_name, password);
  107. printf("Resetting sweep interval to %ld\n", sweep_interval);
  108. if (isc_attach_database(status, 0, dbname, &db, dpb_length, dpb))
  109. isc_print_status(status);
  110. /* Prove we are "guest" . Query a single row with the
  111. ** key word "USER" to find out who we are
  112. */
  113. if (isc_start_transaction(status, &trans, 1, &db, 0, NULL))
  114. isc_print_status(status);
  115. /* Prepare sqlda for singleton fetch */
  116. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(1));
  117. sqlda->sqln = sqlda->sqld = 1;
  118. sqlda->version = 1;
  119. sqlda->sqlvar[0].sqldata = uname;
  120. sqlda->sqlvar[0].sqlind = &nullind;
  121. /* Yes, it is possible to execute a singleton select without
  122. ** a cursor. You must prepare the sqlda by hand.
  123. */
  124. isc_dsql_allocate_statement(status, &db, &stmt);
  125. if (isc_dsql_prepare(status, &trans, &stmt, 0, query, 1, sqlda))
  126. {
  127. ERREXIT(status, 1)
  128. }
  129. /* Force to type sql_text */
  130. sqlda->sqlvar[0].sqltype = SQL_TEXT;
  131. if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  132. {
  133. ERREXIT(status, 1)
  134. }
  135. /* There will only be one row. If it isn't there, something is
  136. ** seriously wrong.
  137. */
  138. if (isc_dsql_fetch(status, &stmt, 1, sqlda))
  139. {
  140. ERREXIT(status, 1)
  141. }
  142. isc_dsql_free_statement(status, &stmt, DSQL_close);
  143. uname[sqlda->sqlvar[0].sqllen] = '\0';
  144. printf ("New user = %s\n", uname);
  145. if (isc_commit_transaction(status, &trans))
  146. {
  147. ERREXIT(status, 1)
  148. }
  149. /* Look at the database to see some parameters (like sweep
  150. ** The database_info call returns a buffer full of type, length,
  151. ** and value sets until info_end is reached
  152. */
  153. if (isc_database_info(status, &db, sizeof(db_items), db_items,
  154. sizeof (buffer), buffer))
  155. {
  156. ERREXIT(status, 1)
  157. }
  158. for (d = buffer; *d != isc_info_end;)
  159. {
  160. value_out = 0;
  161. item = *d++;
  162. length = (short) isc_vax_integer (d, 2);
  163. d += 2;
  164. switch (item)
  165. {
  166. case isc_info_end:
  167. break;
  168. case isc_info_page_size:
  169. value_out = isc_vax_integer (d, length);
  170. printf ("PAGE_SIZE %ld \n", value_out);
  171. break;
  172. case isc_info_allocation:
  173. value_out = isc_vax_integer (d, length);
  174. printf ("Number of DB pages allocated = %ld \n",
  175. value_out);
  176. break;
  177. case isc_info_sweep_interval:
  178. value_out = isc_vax_integer (d, length);
  179. printf ("Sweep interval = %ld \n", value_out);
  180. break;
  181. }
  182. d += length;
  183. }
  184. isc_detach_database(status, &db);
  185. isc_free(dpb);
  186. free(copy);
  187. free(sqlda);
  188. return 0;
  189. }