dyn5.e 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Program type: Embedded Dynamic SQL
  3. *
  4. * Description:
  5. * This program demonstrates the reallocation of SQLDA and
  6. * the 'describe' statement. After a query is examined with
  7. * 'describe', an SQLDA of correct size is reallocated, and some
  8. * information is printed about the query: its type (select,
  9. * non-select), the number of columns, etc.
  10. * The contents of this file are subject to the Interbase Public
  11. * License Version 1.0 (the "License"); you may not use this file
  12. * except in compliance with the License. You may obtain a copy
  13. * of the License at http://www.Inprise.com/IPL.html
  14. *
  15. * Software distributed under the License is distributed on an
  16. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  17. * or implied. See the License for the specific language governing
  18. * rights and limitations under the License.
  19. *
  20. * The Original Code was created by Inprise Corporation
  21. * and its predecessors. Portions created by Inprise Corporation are
  22. * Copyright (C) Inprise Corporation.
  23. *
  24. * All Rights Reserved.
  25. * Contributor(s): ______________________________________.
  26. */
  27. #include "example.h"
  28. #include <stdlib.h>
  29. #include <string.h>
  30. char *sel_str =
  31. "SELECT department, mngr_no, location, head_dept \
  32. FROM department WHERE head_dept in ('100', '900', '600')";
  33. char Db_name[128];
  34. EXEC SQL
  35. SET DATABASE empdb = "employee.fdb" RUNTIME :Db_name;
  36. int main(int argc, char** argv)
  37. {
  38. short num_cols, i;
  39. XSQLDA *sqlda;
  40. if (argc > 1)
  41. strcpy(Db_name, argv[1]);
  42. else
  43. strcpy(Db_name, "employee.fdb");
  44. EXEC SQL
  45. WHENEVER SQLERROR GO TO Error;
  46. EXEC SQL
  47. CONNECT empdb;
  48. EXEC SQL
  49. SET TRANSACTION;
  50. /* Allocate SQLDA of an arbitrary size. */
  51. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(4));
  52. sqlda->sqln = 4;
  53. sqlda->sqld = 4;
  54. sqlda->version = 1;
  55. /* Prepare an unknown statement. */
  56. EXEC SQL
  57. PREPARE q INTO SQL DESCRIPTOR sqlda FROM :sel_str;
  58. /* Describe the statement. */
  59. EXEC SQL
  60. DESCRIBE q INTO SQL DESCRIPTOR sqlda;
  61. /* This is a non-select statement, which can now be executed. */
  62. if (sqlda->sqld == 0)
  63. return(0);
  64. /* If this is a select statement, print more information about it. */
  65. else
  66. printf("Query Type: SELECT\n\n");
  67. num_cols = sqlda->sqld;
  68. printf("Number of columns selected: %d\n", num_cols);
  69. /* Reallocate SQLDA if necessary. */
  70. if (sqlda->sqln < sqlda->sqld)
  71. {
  72. free(sqlda);
  73. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(num_cols));
  74. sqlda->sqln = num_cols;
  75. sqlda->sqld = num_cols;
  76. /* Re-describe the statement. */
  77. EXEC SQL
  78. DESCRIBE q INTO SQL DESCRIPTOR sqlda;
  79. num_cols = sqlda->sqld;
  80. }
  81. /* List column names, types, and lengths. */
  82. for (i = 0; i < num_cols; i++)
  83. {
  84. printf("\nColumn name: %s\n", sqlda->sqlvar[i].sqlname);
  85. printf("Column type: %d\n", sqlda->sqlvar[i].sqltype);
  86. printf("Column length: %d\n", sqlda->sqlvar[i].sqllen);
  87. }
  88. EXEC SQL
  89. COMMIT;
  90. EXEC SQL
  91. DISCONNECT empdb;
  92. free( sqlda);
  93. return(0);
  94. Error:
  95. isc_print_status(gds__status);
  96. printf("SQLCODE=%d\n", SQLCODE);
  97. return(1);
  98. }