123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- /*
- * Licensed Materials - Property of HCL
- *
- * IBM Informix DataBlade Module
- * (C) Copyright International Business Machines Corporation 2002.
- * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
- *
- * COPYRIGHT LICENSE:
- * This information contains sample application programs in source language,
- * which illustrate programming techniques on various operating platforms.
- * You may copy, modify, and distribute these sample programs in any form
- * without payment to IBM, for the purposes of developing, using, marketing
- * or distributing application programs conforming to the application
- * programming interface for the operating platform for which the sample
- * programs are written. These examples have not been thoroughly tested under
- * all conditions. IBM, therefore, cannot guarantee or imply reliability,
- * serviceability, or function of these programs. You may copy, modify, and
- * distribute these sample programs in any form without payment to IBM for
- * the purposes of developing, using, marketing, or distributing application
- * programs conforming to IBM's application programming interfaces.
- * Each copy or any portion of these sample programs or any derivative work,
- * must include a copyright notice as follows:
- * © (your company name) (year). Portions of this code are derived from
- * IBM Corp. Sample Programs. © Copyright IBM Corp. (enter the year or
- * years). All rights reserved.
- *
- */
- #include <stdio.h>
- #include <string.h>
- #include <mi.h>
- #include "tseries.h"
- void error_callback(MI_EVENT_TYPE type,
- MI_CONNECTION *conn,
- void *s1, void *s2);
- mi_integer get_data(MI_CONNECTION *conn);
- mi_integer ts_scan_test(MI_CONNECTION *conn,
- ts_timeseries *ts,
- MI_TYPEID *ts_typeid);
- /*
- * driver.c:
- *
- * connect to the given database, retrieve a timeseries and count the number
- * of elements in it.
- */
- int
- main(argc, argv)
- int argc;
- char **argv;
- {
- MI_CONNECTION *conn;
- char dbname[48];
-
- /* connect to database */
- if (argc == 1)
- strcpy(dbname, "test");
- else
- strcpy(dbname, argv[1]);
- if ((conn = mi_open(dbname, NULL, NULL)) == NULL) {
- (void) fprintf(stderr, "%s: Cannot open database %s\n", dbname);
- return (-1);
- }
- /* handle server errors */
- if (mi_add_callback(MI_Exception, (MI_VOID) error_callback, NULL) == MI_ERROR) {
- (void) fprintf(stderr, "%s: Cannot setup callback \n");
- return (-1);
- }
- /* execute a query and get the results */
- if (get_data(conn) != 0)
- return (-1);
- /* close database connection */
- (void) printf("\nClosing the database connection...\n");
- if (mi_close(conn) == MI_ERROR) {
- (void) fprintf(stderr,
- "%s: Error closing database %s\n", argv[0], dbname);
- return (-1);
- }
- return (0);
- }
- /*
- * get_data():
- * execute a canned query and get the results.
- */
- mi_integer
- get_data(MI_CONNECTION *conn)
- {
- char cmd[255];
- MI_TYPEID *ts_typeid;
- mi_integer ret, error, mival, i;
- MI_ROW *row;
- mi_integer col_len, num;
- ts_timeseries *ts;
- int cnt;
- /* generate the query */
- strcpy(cmd, "select ts from T where id = 1;");
- printf("Command: %s\n", cmd);
- if ((mi_exec(conn, cmd, MI_QUERY_BINARY)) == MI_ERROR) {
- (void) fprintf(stderr, "mi_exec returns MI_ERROR\n");
- (void) fprintf(stderr, "Command: %s\n", cmd);
- return (-1);
- }
- /* get results of the query */
- cnt = 0;
- while ((ret = mi_get_result(conn)) != MI_NO_MORE_RESULTS) {
- switch (ret) {
- case MI_ERROR:
- (void) fprintf(stderr,
- "get_data: mi_get_result failed!\n");
- return (-1);
- break;
- case MI_DDL:
- case MI_DML:
- break;
- case MI_ROWS:
- /* get the timeseries data from the row(s) */
- while ((row = mi_next_row(conn, &error)) != NULL) {
- /* pull the timeseries from column 0 of the row */
- mival = mi_value(row, 0, (MI_DATUM *) &ts, &col_len);
- if (mival != MI_NORMAL_VALUE) {
- if (mival != MI_NULL_VALUE) {
- (void) fprintf(stderr, "mi_value returned %d\n",
- mival);
- return (-1);
- } else {
- /* timeseries was empty */
- printf("Time Series was empty!\n");
- continue;
- }
- }
- cnt++;
- printf("Scannning....\n");
- /* scan the timeseries */
- ts_typeid = mi_column_type_id(mi_get_row_desc(row), 0);
- num = ts_scan_test(conn, (ts_timeseries *) ts, ts_typeid);
- printf("Done: %d read\n", num);
- }
- }
- }
- if (mi_query_finish(conn) == MI_ERROR) {
- printf("could not finish query");
- return (-1);
- }
- return (0);
- }
- /* setup handlers for exceptions */
- void
- notice_handler(void *s1, void *s2)
- {
- char buf[8192];
- mi_errmsg(s1, buf, 8192);
- if (!(strncmp(buf, "S00X01", 6)))
- printf("\n ( NULL, NULL )");
- }
- void
- warn_handler(void *s1, void *s2)
- {
- char buf[8192];
- mi_errmsg(s1, buf, 8192);
- fprintf(stderr, "%s\n", buf);
- }
- void
- fatal_handler(void *s1, void *s2)
- {
- char buf[8192];
- mi_errmsg(s1, buf, 8192);
- fprintf(stderr, "%s\n", buf);
- }
- void
- error_callback(MI_EVENT_TYPE type, MI_CONNECTION *conn, void *s1, void *s2)
- {
- int ecode;
- switch (type) {
- case MI_Exception:
- ecode = mi_error_level(s1);
- switch (mi_error_level(s1)) {
- case MI_MESSAGE:
- notice_handler(s1, s2);
- break;
- case MI_EXCEPTION:
- warn_handler(s1, s2);
- break;
- case MI_FATAL:
- fatal_handler(s1, s2);
- break;
- }
- break;
- default:
- fprintf(stderr, "Caught an unexpected event type\n");
- break;
- }
- }
- mi_integer
- ts_scan_test(MI_CONNECTION *conn, ts_timeseries *ts, MI_TYPEID *ts_typeid)
- {
- int cnt;
- ts_tsdesc *tsdesc;
- ts_tscan *tsscan;
- ts_tselem elem;
- /* open the timeseries */
- tsdesc = ts_open(conn, ts, ts_typeid, 0);
- /* start a scan from the begining to end of the timeseries */
- tsscan = ts_begin_scan(tsdesc, 0, NULL, NULL);
- /* lets count the number of elements */
- cnt = 0;
- while (ts_next(tsscan, &elem) != TS_SCAN_EOS)
- cnt++;
- /* done with the scan */
- ts_end_scan(tsscan);
- /* close the timeseries */
- ts_close(tsdesc);
- /* return number of elements */
- return(cnt);
- }
|