applyfunc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 "tseries.h"
  29. /*
  30. * ApplyFunc:
  31. * create function ApplyFunc(lvarchar,
  32. * row,
  33. * TimeSeries,
  34. * TimeSeries default NULL,
  35. * datetime year to fraction(5) default NULL,
  36. * datetime year to fraction(5) default NULL,
  37. * integer default NULL)
  38. * returns TimeSeries with (handlesnulls)
  39. * external name '$INFORMIXDIR/funcs/applyfunc.bld(apply_func)'
  40. * language c;
  41. */
  42. /* struct to keep track of state */
  43. typedef struct _apply_state {
  44. MI_CONNECTION *apply_conn;
  45. MI_FUNC_DESC *apply_func;
  46. mi_string *apply_sig;
  47. mi_string *apply_funcname;
  48. mi_string *apply_tssubtypename;
  49. mi_string *apply_rowtypename;
  50. MI_FPARAM *apply_fparam;
  51. } apply_state;
  52. /*
  53. * APPLY_FUNC:
  54. * This function applied a function to a range of the elements within
  55. * the src timeseries. The results are placed in the dst timeseries.
  56. *
  57. * This function is passed:
  58. * funcname: name of SPL or C or JAVA function to call,
  59. * this function must have a signature of
  60. * funcname(rowtype1, rowtype2)
  61. * where rowtype1 is the name of the timeseries
  62. * subtype, and rowtype2 is the name of the
  63. * row type that contains extra params
  64. * data_row: contains the non-timeseries arguments that
  65. * will be passed to function "funcname".
  66. * src_ts: the source timeseries which "funcname" will
  67. * be applied to.
  68. * dst_ts: optional. if supplied results of funcname are
  69. * inserted here. if not supplied the dst_ts is
  70. * created in this function.
  71. * start: where to start the scan.
  72. * end: where to end the scan.
  73. * flags: flags passed to ts_open.
  74. */
  75. #ifdef NT
  76. __declspec(dllexport)
  77. #endif
  78. ts_timeseries *
  79. apply_func(mi_lvarchar *funcname,
  80. MI_ROW *data_row,
  81. ts_timeseries *src_ts,
  82. ts_timeseries *dst_ts,
  83. mi_datetime *start,
  84. mi_datetime *end,
  85. mi_integer flags,
  86. MI_FPARAM *fParam)
  87. {
  88. ts_tselem elem;
  89. MI_ROW *row, *val;
  90. apply_state *state;
  91. MI_MEMORY_DURATION md;
  92. MI_TYPE_DESC *td, *subtype_td;
  93. mi_string *ts_rowtype, *rowtype;
  94. mi_string *sig;
  95. ts_tsdesc *tsdesc, *newdesc;
  96. ts_tscan *tsscan;
  97. mi_integer err, len, nelems, off, ret;
  98. mi_boolean is_irreg;
  99. /* look for NULL values */
  100. if (mi_fp_argisnull(fParam, 0))
  101. mi_db_error_raise(NULL, MI_SQL, "UTSFF", 0);
  102. if (mi_fp_argisnull(fParam, 1))
  103. mi_db_error_raise(NULL, MI_SQL, "UTSFL", 0);
  104. if (mi_fp_argisnull(fParam, 2))
  105. mi_db_error_raise(NULL, MI_SQL, "UTSFN", 0);
  106. if (mi_fp_argisnull(fParam, 3))
  107. dst_ts = NULL;
  108. if (mi_fp_argisnull(fParam, 4))
  109. start = NULL;
  110. if (mi_fp_argisnull(fParam, 5))
  111. end = NULL;
  112. if (mi_fp_argisnull(fParam, 6))
  113. flags = 0;
  114. /*
  115. * check and see if we have already gotten a state from
  116. * a previous row.
  117. */
  118. if (!(state = (apply_state *) mi_fp_funcstate(fParam)) ||
  119. strlen(state->apply_funcname) != mi_get_varlen(funcname) ||
  120. memcmp(state->apply_funcname, mi_get_vardata(funcname), mi_get_varlen(funcname))) {
  121. /*
  122. * we either don't have any state yet, or
  123. * we the parameters to this function have changed.
  124. */
  125. md = mi_switch_mem_duration (PER_COMMAND);
  126. if (state != NULL) {
  127. /* remove previous state info */
  128. mi_free(state->apply_funcname);
  129. mi_routine_end(state->apply_conn, state->apply_func);
  130. } else {
  131. /* allocate space for our state info */
  132. state = mi_alloc(sizeof(apply_state));
  133. state->apply_conn = mi_open(NULL, NULL, NULL);
  134. /* get the timeseries subtype name */
  135. td = mi_type_typedesc(state->apply_conn, mi_fp_argtype(fParam, 2));
  136. subtype_td = mi_type_element_typedesc(td);
  137. ts_rowtype = mi_type_typename(subtype_td);
  138. len = strlen(ts_rowtype) + 1;
  139. state->apply_tssubtypename = (mi_string *) mi_alloc(len);
  140. strcpy(state->apply_tssubtypename, ts_rowtype);
  141. state->apply_tssubtypename[len] = 0;
  142. /* get the argument row name */
  143. td = mi_type_typedesc(state->apply_conn, mi_fp_argtype(fParam, 1));
  144. rowtype = mi_type_typename(td);
  145. len = strlen(rowtype) + 1;
  146. state->apply_rowtypename = (mi_string *) mi_alloc(len);
  147. strcpy(state->apply_rowtypename, rowtype);
  148. state->apply_rowtypename[len] = 0;
  149. }
  150. /* construct the function signature */
  151. state->apply_funcname = mi_lvarchar_to_string(funcname);
  152. len = strlen(state->apply_funcname) +
  153. 1 + /* '(' */
  154. strlen(state->apply_tssubtypename) +
  155. 1 + /* ',' */
  156. strlen(state->apply_rowtypename) +
  157. 1 + /* ')' */
  158. 1; /* '\0' */
  159. state->apply_sig = sig = (mi_string *) mi_alloc(len);
  160. sprintf(sig, "%s(%s,%s)", state->apply_funcname,
  161. state->apply_tssubtypename,
  162. state->apply_rowtypename);
  163. /* get the function */
  164. state->apply_func = mi_routine_get(state->apply_conn, MI_FUNC, sig);
  165. /* get the fparam for the function */
  166. state->apply_fparam = mi_fparam_get(state->apply_conn, state->apply_func);
  167. (void) mi_switch_mem_duration (md);
  168. }
  169. /* open the source timeseries */
  170. tsdesc = ts_open(state->apply_conn, src_ts, mi_fp_argtype(fParam, 2),flags);
  171. /* calculate (guess) the number of elements between start and end */
  172. if (!end)
  173. if (!start)
  174. nelems = ts_nelems(tsdesc);
  175. else
  176. nelems = 100; /* just guess */
  177. else
  178. nelems = ts_cal_index(state->apply_conn,
  179. ts_get_calname(src_ts),
  180. start ? start : ts_get_origin(src_ts),
  181. end);
  182. is_irreg = TS_IS_IRREGULAR(src_ts);
  183. if (dst_ts == NULL) {
  184. /* a destination ts was not passed in, so create one */
  185. dst_ts = ts_create(state->apply_conn,
  186. ts_get_calname(src_ts),
  187. start ? start : ts_get_origin(src_ts),
  188. ts_get_threshold(src_ts),
  189. TS_IS_IRREGULAR(src_ts) ? TS_CREATE_IRR : 0,
  190. mi_fp_argtype(fParam, 2),
  191. nelems,
  192. ts_get_containername(src_ts));
  193. off = 0;
  194. } else if (!is_irreg) {
  195. /*
  196. * we have a destination timeseries, get
  197. * the offset of the starting point
  198. */
  199. off = ts_cal_index(state->apply_conn,
  200. ts_get_calname(dst_ts),
  201. ts_get_origin(dst_ts),
  202. start);
  203. } else
  204. off = 0;
  205. /* open the destination timeseries */
  206. newdesc = ts_open(state->apply_conn, dst_ts, mi_fp_argtype(fParam, 2), 0);
  207. /* start a scan of the source timeseries */
  208. tsscan = ts_begin_scan(tsdesc, 0, start, end);
  209. /* loop through the values in the source timeseries */
  210. while ((ret = ts_next(tsscan, &elem)) != TS_SCAN_EOS) {
  211. if (ret == TS_SCAN_NULL) {
  212. /* this element is missing */
  213. off++;
  214. continue;
  215. }
  216. /* convert this element into a row */
  217. row = ts_elem_to_row(tsdesc, elem, ts_current_offset(tsscan));
  218. /*
  219. * call the function with the element data and
  220. * the argument row data
  221. */
  222. val = (MI_ROW *) mi_routine_exec(state->apply_conn, state->apply_func, &err, row, data_row);
  223. /* if the return was NULL ignore it */
  224. if (mi_fp_returnisnull(state->apply_fparam, 0)) {
  225. off++;
  226. continue;
  227. }
  228. /* turn the returned row into an element */
  229. elem = ts_row_to_elem(newdesc, val, NULL);
  230. /* put the element into the destination timeseries */
  231. if (is_irreg)
  232. (void) ts_put_elem(newdesc, elem, ts_current_timestamp(tsscan));
  233. else
  234. (void) ts_put_nth_elem(newdesc, elem, off++);
  235. }
  236. /* cleanup */
  237. ts_end_scan(tsscan);
  238. ts_close(tsdesc);
  239. ts_close(newdesc);
  240. return(dst_ts);
  241. }
  242. /*
  243. * We don't have a good place to put this currently, so put it here for now.
  244. *
  245. * begin hilow.c
  246. */
  247. #ifdef NT
  248. __declspec(dllexport)
  249. #endif
  250. MI_ROW *
  251. high_low_diff(MI_ROW * row, MI_FPARAM * fp)
  252. {
  253. MI_ROW_DESC *rowdesc;
  254. MI_ROW *result;
  255. void *values[2];
  256. mi_boolean nulls[2];
  257. mi_real *high, *low;
  258. mi_real r;
  259. mi_integer len;
  260. MI_CONNECTION *conn;
  261. mi_integer rc;
  262. nulls[0] = MI_TRUE;
  263. nulls[1] = MI_FALSE;
  264. conn = mi_open(NULL, NULL, NULL);
  265. if ((rc = mi_value(row, 1, (MI_DATUM *) & high, &len)) == MI_ERROR)
  266. mi_db_error_raise(conn, MI_EXCEPTION,
  267. "ts_test_float_sql: corrupted argument row");
  268. if (rc == MI_NULL_VALUE)
  269. goto retisnull;
  270. if ((rc = mi_value(row, 2, (MI_DATUM *) & low, &len)) == MI_ERROR)
  271. mi_db_error_raise(conn, MI_EXCEPTION,
  272. "ts_test_float_sql: corrupted argument row");
  273. if (rc == MI_NULL_VALUE)
  274. goto retisnull;
  275. r = *high - *low;
  276. values[1] = (void *) &r;
  277. rowdesc = mi_row_desc_create(mi_typestring_to_id(conn, "one_real"));
  278. result = mi_row_create(conn, rowdesc, (MI_DATUM *) values, nulls);
  279. mi_close(conn);
  280. return (result);
  281. retisnull:
  282. mi_fp_setreturnisnull(fp, 0, MI_TRUE);
  283. return (MI_ROW *) NULL;
  284. } /* end of applyfunc.c */