2
0

api3.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Program type: API Interface
  3. *
  4. * Description:
  5. * This program displays employee names and phone extensions.
  6. *
  7. * It allocates an output SQLDA, prepares and executes a statement,
  8. * and loops fetching multiple rows.
  9. *
  10. * The SQLCODE returned by fetch is checked.
  11. * The contents of this file are subject to the Interbase Public
  12. * License Version 1.0 (the "License"); you may not use this file
  13. * except in compliance with the License. You may obtain a copy
  14. * of the License at http://www.Inprise.com/IPL.html
  15. *
  16. * Software distributed under the License is distributed on an
  17. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  18. * or implied. See the License for the specific language governing
  19. * rights and limitations under the License.
  20. *
  21. * The Original Code was created by Inprise Corporation
  22. * and its predecessors. Portions created by Inprise Corporation are
  23. * Copyright (C) Inprise Corporation.
  24. *
  25. * All Rights Reserved.
  26. * Contributor(s): ______________________________________.
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdio.h>
  31. #include <ibase.h>
  32. #include "example.h"
  33. #define LASTLEN 20
  34. #define FIRSTLEN 15
  35. #define EXTLEN 4
  36. /* This macro is used to declare structures representing SQL VARCHAR types */
  37. #define SQL_VARCHAR(len) struct {short vary_length; char vary_string[(len)+1];}
  38. int main (int argc, char** argv)
  39. {
  40. SQL_VARCHAR(LASTLEN) last_name;
  41. SQL_VARCHAR(FIRSTLEN) first_name;
  42. char phone_ext[EXTLEN + 2];
  43. short flag0 = 0, flag1 = 0;
  44. short flag2 = 0;
  45. isc_stmt_handle stmt = NULL; /* statement handle */
  46. isc_db_handle DB = NULL; /* database handle */
  47. isc_tr_handle trans = NULL; /* transaction handle */
  48. ISC_STATUS_ARRAY status; /* status vector */
  49. XSQLDA * sqlda;
  50. long fetch_stat;
  51. char empdb[128];
  52. char *sel_str =
  53. "SELECT last_name, first_name, phone_ext FROM phone_list \
  54. WHERE location = 'Monterey' ORDER BY last_name, first_name;";
  55. if (argc > 1)
  56. strcpy(empdb, argv[1]);
  57. else
  58. strcpy(empdb, "employee.fdb");
  59. if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  60. isc_print_status(status);
  61. if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  62. {
  63. ERREXIT(status, 1)
  64. }
  65. /* Allocate an output SQLDA. */
  66. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(3));
  67. sqlda->sqln = 3;
  68. sqlda->sqld = 3;
  69. sqlda->version = 1;
  70. /* Allocate a statement. */
  71. if (isc_dsql_allocate_statement(status, &DB, &stmt))
  72. {
  73. ERREXIT(status, 1)
  74. }
  75. /* Prepare the statement. */
  76. if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, sqlda))
  77. {
  78. ERREXIT(status, 1)
  79. }
  80. /*
  81. * Although all three selected columns are of type varchar, the
  82. * third field's type is changed and printed as type TEXT.
  83. */
  84. sqlda->sqlvar[0].sqldata = (char *)&last_name;
  85. sqlda->sqlvar[0].sqltype = SQL_VARYING + 1;
  86. sqlda->sqlvar[0].sqlind = &flag0;
  87. sqlda->sqlvar[1].sqldata = (char *)&first_name;
  88. sqlda->sqlvar[1].sqltype = SQL_VARYING + 1;
  89. sqlda->sqlvar[1].sqlind = &flag1;
  90. sqlda->sqlvar[2].sqldata = (char *) phone_ext;
  91. sqlda->sqlvar[2].sqltype = SQL_TEXT + 1;
  92. sqlda->sqlvar[2].sqlind = &flag2;
  93. printf("\n%-20s %-15s %-10s\n\n", "LAST NAME", "FIRST NAME", "EXTENSION");
  94. /* Execute the statement. */
  95. if (isc_dsql_execute(status, &trans, &stmt, 1, NULL))
  96. {
  97. ERREXIT(status, 1)
  98. }
  99. /*
  100. * Fetch and print the records.
  101. * Status is 100 after the last row is fetched.
  102. */
  103. while ((fetch_stat = isc_dsql_fetch(status, &stmt, 1, sqlda)) == 0)
  104. {
  105. printf("%-20.*s ", last_name.vary_length, last_name.vary_string);
  106. printf("%-15.*s ", first_name.vary_length, first_name.vary_string);
  107. phone_ext[sqlda->sqlvar[2].sqllen] = '\0';
  108. printf("%s\n", phone_ext);
  109. }
  110. if (fetch_stat != 100L)
  111. {
  112. ERREXIT(status, 1)
  113. }
  114. /* Free statement handle. */
  115. if (isc_dsql_free_statement(status, &stmt, DSQL_close))
  116. {
  117. ERREXIT(status, 1)
  118. }
  119. if (isc_commit_transaction(status, &trans))
  120. {
  121. ERREXIT(status, 1)
  122. }
  123. if (isc_detach_database(status, &DB))
  124. {
  125. ERREXIT(status, 1)
  126. }
  127. free( sqlda);
  128. return 0;
  129. }