TsToList.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /*
  29. * SETUP:
  30. *
  31. * create function TsToList(TimeSeries, integer) returns list
  32. * external name '/foo/bar/TsToList.so(ts_to_list)'
  33. * language c not variant;
  34. *
  35. */
  36. #include <stdio.h>
  37. #include <mi.h>
  38. #include <tseries.h>
  39. /*
  40. * This function converts the first column of a timeseries
  41. * to an array. This assumes the type is mi_real
  42. */
  43. #include <math.h>
  44. #ifdef NT
  45. __declspec(dllexport)
  46. #endif
  47. MI_COLLECTION *
  48. ts_to_list(ts_timeseries *tsPtr, mi_integer col, MI_FPARAM *fParam)
  49. {
  50. ts_tsdesc *descPtr;
  51. ts_tselem tselem;
  52. ts_tscan *scan;
  53. MI_CONNECTION *conn;
  54. int scancode;
  55. MI_COLLECTION *colPtr = NULL;
  56. MI_COLL_DESC *colDesc = NULL;
  57. ts_typeinfo *typePtr;
  58. mi_boolean isNull;
  59. void *rslt;
  60. MI_TYPE_DESC *list_td;
  61. MI_TYPE_DESC *subtype_td;
  62. MI_TYPEID *subtype_id;
  63. char errbuf[100];
  64. /* get a connection for libmi */
  65. conn = mi_open(NULL,NULL,NULL);
  66. /* open a descriptor for the timeseries */
  67. descPtr = ts_open(conn, tsPtr, mi_fp_argtype(fParam, 0), 0);
  68. scan = ts_begin_scan(descPtr, 0, NULL, NULL);
  69. while ((scancode = ts_next(scan, &tselem)) != TS_SCAN_EOS) {
  70. switch(scancode) {
  71. case TS_SCAN_ELEM:
  72. if (colPtr == (MI_COLLECTION *)NULL) {
  73. /* first time */
  74. typePtr = ts_colinfo_number(descPtr, col);
  75. /* make sure type of column agrees with return type */
  76. list_td = mi_type_typedesc(conn, mi_fp_rettype(fParam, 0));
  77. subtype_td = mi_type_element_typedesc(list_td);
  78. subtype_id = mi_typedesc_typeid(subtype_td);
  79. if (!mi_typeid_equals(subtype_id, typePtr->ti_typeid)) {
  80. subtype_td = mi_type_typedesc(conn, typePtr->ti_typeid);
  81. sprintf(errbuf, "Subtype of %s must match column type (%s)",
  82. mi_type_typename(list_td),
  83. mi_type_typename(subtype_td));
  84. mi_db_error_raise(NULL, MI_FATAL, errbuf, 0);
  85. }
  86. colPtr = mi_collection_create(conn, mi_fp_rettype(fParam, 0));
  87. colDesc = mi_collection_open(conn, colPtr);
  88. }
  89. /* insert this ts_elem into the collection */
  90. rslt = ts_get_col_by_number(descPtr,
  91. tselem,
  92. col,
  93. &isNull,
  94. ts_current_offset(scan));
  95. if (!isNull) {
  96. /* we inseret only if it is not NULL. */
  97. mi_collection_insert(conn, colDesc,
  98. (MI_DATUM) rslt, MI_CURSOR_NEXT,0);
  99. }
  100. break;
  101. case TS_SCAN_NULL:
  102. break;
  103. }
  104. }
  105. ts_end_scan(scan);
  106. ts_close(descPtr);
  107. mi_collection_close(conn, colDesc);
  108. return(colPtr);
  109. }