driver.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Licensed Materials - Property of HCL
  3. *
  4. * IBM Informix DataBlade Module
  5. * (C) Copyright International Business Machines Corporation 2002.
  6. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  7. *
  8. * COPYRIGHT LICENSE:
  9. * This information contains sample application programs in source language,
  10. * which illustrate programming techniques on various operating platforms.
  11. * You may copy, modify, and distribute these sample programs in any form
  12. * without payment to IBM, for the purposes of developing, using, marketing
  13. * or distributing application programs conforming to the application
  14. * programming interface for the operating platform for which the sample
  15. * programs are written. These examples have not been thoroughly tested under
  16. * all conditions. IBM, therefore, cannot guarantee or imply reliability,
  17. * serviceability, or function of these programs. You may copy, modify, and
  18. * distribute these sample programs in any form without payment to IBM for
  19. * the purposes of developing, using, marketing, or distributing application
  20. * programs conforming to IBM's application programming interfaces.
  21. * Each copy or any portion of these sample programs or any derivative work,
  22. * must include a copyright notice as follows:
  23. * © (your company name) (year). Portions of this code are derived from
  24. * IBM Corp. Sample Programs. © Copyright IBM Corp. (enter the year or
  25. * years). All rights reserved.
  26. *
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <mi.h>
  31. #include "tseries.h"
  32. void error_callback(MI_EVENT_TYPE type,
  33. MI_CONNECTION *conn,
  34. void *s1, void *s2);
  35. mi_integer get_data(MI_CONNECTION *conn);
  36. mi_integer ts_scan_test(MI_CONNECTION *conn,
  37. ts_timeseries *ts,
  38. MI_TYPEID *ts_typeid);
  39. /*
  40. * driver.c:
  41. *
  42. * connect to the given database, retrieve a timeseries and count the number
  43. * of elements in it.
  44. */
  45. int
  46. main(argc, argv)
  47. int argc;
  48. char **argv;
  49. {
  50. MI_CONNECTION *conn;
  51. char dbname[48];
  52. /* connect to database */
  53. if (argc == 1)
  54. strcpy(dbname, "test");
  55. else
  56. strcpy(dbname, argv[1]);
  57. if ((conn = mi_open(dbname, NULL, NULL)) == NULL) {
  58. (void) fprintf(stderr, "%s: Cannot open database %s\n", dbname);
  59. return (-1);
  60. }
  61. /* handle server errors */
  62. if (mi_add_callback(MI_Exception, (MI_VOID) error_callback, NULL) == MI_ERROR) {
  63. (void) fprintf(stderr, "%s: Cannot setup callback \n");
  64. return (-1);
  65. }
  66. /* execute a query and get the results */
  67. if (get_data(conn) != 0)
  68. return (-1);
  69. /* close database connection */
  70. (void) printf("\nClosing the database connection...\n");
  71. if (mi_close(conn) == MI_ERROR) {
  72. (void) fprintf(stderr,
  73. "%s: Error closing database %s\n", argv[0], dbname);
  74. return (-1);
  75. }
  76. return (0);
  77. }
  78. /*
  79. * get_data():
  80. * execute a canned query and get the results.
  81. */
  82. mi_integer
  83. get_data(MI_CONNECTION *conn)
  84. {
  85. char cmd[255];
  86. MI_TYPEID *ts_typeid;
  87. mi_integer ret, error, mival, i;
  88. MI_ROW *row;
  89. mi_integer col_len, num;
  90. ts_timeseries *ts;
  91. int cnt;
  92. /* generate the query */
  93. strcpy(cmd, "select ts from T where id = 1;");
  94. printf("Command: %s\n", cmd);
  95. if ((mi_exec(conn, cmd, MI_QUERY_BINARY)) == MI_ERROR) {
  96. (void) fprintf(stderr, "mi_exec returns MI_ERROR\n");
  97. (void) fprintf(stderr, "Command: %s\n", cmd);
  98. return (-1);
  99. }
  100. /* get results of the query */
  101. cnt = 0;
  102. while ((ret = mi_get_result(conn)) != MI_NO_MORE_RESULTS) {
  103. switch (ret) {
  104. case MI_ERROR:
  105. (void) fprintf(stderr,
  106. "get_data: mi_get_result failed!\n");
  107. return (-1);
  108. break;
  109. case MI_DDL:
  110. case MI_DML:
  111. break;
  112. case MI_ROWS:
  113. /* get the timeseries data from the row(s) */
  114. while ((row = mi_next_row(conn, &error)) != NULL) {
  115. /* pull the timeseries from column 0 of the row */
  116. mival = mi_value(row, 0, (MI_DATUM *) &ts, &col_len);
  117. if (mival != MI_NORMAL_VALUE) {
  118. if (mival != MI_NULL_VALUE) {
  119. (void) fprintf(stderr, "mi_value returned %d\n",
  120. mival);
  121. return (-1);
  122. } else {
  123. /* timeseries was empty */
  124. printf("Time Series was empty!\n");
  125. continue;
  126. }
  127. }
  128. cnt++;
  129. printf("Scannning....\n");
  130. /* scan the timeseries */
  131. ts_typeid = mi_column_type_id(mi_get_row_desc(row), 0);
  132. num = ts_scan_test(conn, (ts_timeseries *) ts, ts_typeid);
  133. printf("Done: %d read\n", num);
  134. }
  135. }
  136. }
  137. if (mi_query_finish(conn) == MI_ERROR) {
  138. printf("could not finish query");
  139. return (-1);
  140. }
  141. return (0);
  142. }
  143. /* setup handlers for exceptions */
  144. void
  145. notice_handler(void *s1, void *s2)
  146. {
  147. char buf[8192];
  148. mi_errmsg(s1, buf, 8192);
  149. if (!(strncmp(buf, "S00X01", 6)))
  150. printf("\n ( NULL, NULL )");
  151. }
  152. void
  153. warn_handler(void *s1, void *s2)
  154. {
  155. char buf[8192];
  156. mi_errmsg(s1, buf, 8192);
  157. fprintf(stderr, "%s\n", buf);
  158. }
  159. void
  160. fatal_handler(void *s1, void *s2)
  161. {
  162. char buf[8192];
  163. mi_errmsg(s1, buf, 8192);
  164. fprintf(stderr, "%s\n", buf);
  165. }
  166. void
  167. error_callback(MI_EVENT_TYPE type, MI_CONNECTION *conn, void *s1, void *s2)
  168. {
  169. int ecode;
  170. switch (type) {
  171. case MI_Exception:
  172. ecode = mi_error_level(s1);
  173. switch (mi_error_level(s1)) {
  174. case MI_MESSAGE:
  175. notice_handler(s1, s2);
  176. break;
  177. case MI_EXCEPTION:
  178. warn_handler(s1, s2);
  179. break;
  180. case MI_FATAL:
  181. fatal_handler(s1, s2);
  182. break;
  183. }
  184. break;
  185. default:
  186. fprintf(stderr, "Caught an unexpected event type\n");
  187. break;
  188. }
  189. }
  190. mi_integer
  191. ts_scan_test(MI_CONNECTION *conn, ts_timeseries *ts, MI_TYPEID *ts_typeid)
  192. {
  193. int cnt;
  194. ts_tsdesc *tsdesc;
  195. ts_tscan *tsscan;
  196. ts_tselem elem;
  197. /* open the timeseries */
  198. tsdesc = ts_open(conn, ts, ts_typeid, 0);
  199. /* start a scan from the begining to end of the timeseries */
  200. tsscan = ts_begin_scan(tsdesc, 0, NULL, NULL);
  201. /* lets count the number of elements */
  202. cnt = 0;
  203. while (ts_next(tsscan, &elem) != TS_SCAN_EOS)
  204. cnt++;
  205. /* done with the scan */
  206. ts_end_scan(tsscan);
  207. /* close the timeseries */
  208. ts_close(tsdesc);
  209. /* return number of elements */
  210. return(cnt);
  211. }