api5.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Program type: API Interface
  3. *
  4. * Description:
  5. * This program demonstrates the reallocation of SQLDA
  6. * and the 'isc_dsql_describe' statement. After a query
  7. * is examined with 'isc_dsql_describe', an SQLDA of correct
  8. * size is reallocated, and some information is printed about
  9. * the query: its type (select, non-select), the number
  10. * of columns, etc.
  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 <ibase.h>
  31. #include <stdio.h>
  32. #include "example.h"
  33. char *sel_str =
  34. "SELECT department, mngr_no, location, head_dept \
  35. FROM department WHERE head_dept in ('100', '900', '600')";
  36. isc_db_handle DB = 0; /* database handle */
  37. isc_tr_handle trans = 0; /* transaction handle */
  38. ISC_STATUS_ARRAY status; /* status vector */
  39. int main (int argc, char** argv)
  40. {
  41. int num_cols, i;
  42. isc_stmt_handle stmt = NULL;
  43. XSQLDA *sqlda;
  44. char empdb[128];
  45. if (argc > 1)
  46. strcpy(empdb, argv[1]);
  47. else
  48. strcpy(empdb, "employee.fdb");
  49. if (isc_attach_database(status, 0, empdb, &DB, 0, NULL))
  50. {
  51. ERREXIT(status, 1)
  52. }
  53. /* Allocate SQLDA of an arbitrary size. */
  54. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(3));
  55. sqlda->sqln = 3;
  56. sqlda->version = 1;
  57. if (isc_start_transaction(status, &trans, 1, &DB, 0, NULL))
  58. {
  59. ERREXIT(status, 1)
  60. }
  61. /* Allocate a statement. */
  62. if (isc_dsql_allocate_statement(status, &DB, &stmt))
  63. {
  64. ERREXIT(status, 1)
  65. }
  66. /* Prepare the statement. */
  67. if (isc_dsql_prepare(status, &trans, &stmt, 0, sel_str, 1, sqlda))
  68. {
  69. ERREXIT(status, 1)
  70. }
  71. /* Describe the statement. */
  72. if (isc_dsql_describe(status, &stmt, 1, sqlda))
  73. {
  74. ERREXIT(status, 1)
  75. }
  76. /* This is a select statement, print more information about it. */
  77. printf("Query Type: SELECT\n\n");
  78. num_cols = sqlda->sqld;
  79. printf("Number of columns selected: %d\n", num_cols);
  80. /* Reallocate SQLDA if necessary. */
  81. if (sqlda->sqln < sqlda->sqld)
  82. {
  83. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(num_cols));
  84. sqlda->sqln = num_cols;
  85. sqlda->version = 1;
  86. /* Re-describe the statement. */
  87. if (isc_dsql_describe(status, &stmt, 1, sqlda))
  88. {
  89. ERREXIT(status, 1)
  90. }
  91. num_cols = sqlda->sqld;
  92. }
  93. /* List column names, types, and lengths. */
  94. for (i = 0; i < num_cols; i++)
  95. {
  96. printf("\nColumn name: %s\n", sqlda->sqlvar[i].sqlname);
  97. printf("Column type: %d\n", sqlda->sqlvar[i].sqltype);
  98. printf("Column length: %d\n", sqlda->sqlvar[i].sqllen);
  99. }
  100. if (isc_dsql_free_statement(status, &stmt, DSQL_drop))
  101. {
  102. ERREXIT(status, 1)
  103. }
  104. if (isc_commit_transaction(status, &trans))
  105. {
  106. ERREXIT(status, 1)
  107. }
  108. if (isc_detach_database(status, &DB))
  109. {
  110. ERREXIT (status, 1)
  111. }
  112. free(sqlda);
  113. return 0;
  114. }