NCBI C++ ToolKit
t0009.c
Go to the documentation of this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

1 #include <config.h>
2 
3 #if HAVE_STRING_H
4 #include <string.h>
5 #endif /* HAVE_STRING_H */
6 
7 #include <stdio.h>
8 #include <ctpublic.h>
9 #include "common.h"
10 
11 #include <common/test_assert.h>
12 
13 static CS_RETCODE ex_servermsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_SERVERMSG * errmsg);
14 static int compute_supported = 1;
15 
16 /* Testing: Retrieve compute results */
17 int
18 main(int argc, char *argv[])
19 {
20  CS_CONTEXT *ctx;
22  CS_COMMAND *cmd;
23  int verbose = 0;
24 
25  CS_RETCODE ret;
26  CS_RETCODE results_ret;
27  CS_INT result_type;
28  CS_INT num_cols, compute_id;
29 
30  CS_DATAFMT datafmt;
31  CS_INT datalength;
32  CS_SMALLINT ind;
33  CS_INT count, row_count = 0;
34 
35  CS_CHAR select[1024];
36 
37  CS_INT col1;
38  CS_CHAR col2[2];
39  CS_CHAR col3[32];
40 
41  CS_INT compute_col1;
42  CS_CHAR compute_col3[32];
43 
44  unsigned rows[3] = { 0, 0, 0 };
45 
46  fprintf(stdout, "%s: Retrieve compute results processing\n", __FILE__);
47  if (verbose) {
48  fprintf(stdout, "Trying login\n");
49  }
50  ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
51  if (ret != CS_SUCCEED) {
52  fprintf(stderr, "Login failed\n");
53  return 1;
54  }
55 
56  ret = run_command(cmd, "CREATE TABLE #ctlib0009 (col1 int not null, col2 char(1) not null, col3 datetime not null)");
57  if (ret != CS_SUCCEED)
58  return 1;
59 
60  ret = run_command(cmd, "insert into #ctlib0009 values (1, 'A', 'Jan 1 2002 10:00:00AM')");
61  if (ret != CS_SUCCEED)
62  return 1;
63 
64  ret = run_command(cmd, "insert into #ctlib0009 values (2, 'A', 'Jan 2 2002 10:00:00AM')");
65  if (ret != CS_SUCCEED)
66  return 1;
67 
68  ret = run_command(cmd, "insert into #ctlib0009 values (3, 'A', 'Jan 3 2002 10:00:00AM')");
69  if (ret != CS_SUCCEED)
70  return 1;
71 
72  ret = run_command(cmd, "insert into #ctlib0009 values (8, 'B', 'Jan 4 2002 10:00:00AM')");
73  if (ret != CS_SUCCEED)
74  return 1;
75 
76  ret = run_command(cmd, "insert into #ctlib0009 values (9, 'B', 'Jan 5 2002 10:00:00AM')");
77  if (ret != CS_SUCCEED)
78  return 1;
79 
80 
81  strcpy(select, "select col1, col2, col3 from #ctlib0009 order by col2 ");
82  strcat(select, "compute sum(col1) by col2 ");
83  strcat(select, "compute max(col3)");
84 
86 
87  if (ret != CS_SUCCEED) {
88  fprintf(stderr, "ct_command(%s) failed\n", select);
89  return 1;
90  }
91 
92  ret = ct_send(cmd);
93  if (ret != CS_SUCCEED) {
94  fprintf(stderr, "ct_send() failed\n");
95  return 1;
96  }
97 
99  while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
100  printf("ct_results returned %s type\n", res_type_str(result_type));
101  switch ((int) result_type) {
102  case CS_CMD_SUCCEED:
103  break;
104  case CS_CMD_DONE:
105  break;
106  case CS_CMD_FAIL:
107  if (!compute_supported) {
109  return 0;
110  }
111  fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
112  return 1;
113  case CS_ROW_RESULT:
114  ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
115  if (ret != CS_SUCCEED) {
116  fprintf(stderr, "ct_res_info() failed");
117  return 1;
118  }
119  if (num_cols != 3) {
120  fprintf(stderr, "num_cols %d != 3", num_cols);
121  return 1;
122  }
123 
124  ret = ct_describe(cmd, 1, &datafmt);
125  if (ret != CS_SUCCEED) {
126  fprintf(stderr, "ct_describe() failed");
127  return 1;
128  }
129  datafmt.format = CS_FMT_UNUSED;
130  if (datafmt.maxlength > 1024) {
131  datafmt.maxlength = 1024;
132  }
133  ret = ct_bind(cmd, 1, &datafmt, &col1, &datalength, &ind);
134  if (ret != CS_SUCCEED) {
135  fprintf(stderr, "ct_bind() failed\n");
136  return 1;
137  }
138 
139  ret = ct_describe(cmd, 2, &datafmt);
140  if (ret != CS_SUCCEED) {
141  fprintf(stderr, "ct_describe() failed");
142  return 1;
143  }
144 
145  datafmt.format = CS_FMT_NULLTERM;
146  datafmt.maxlength = 2;
147 
148  ret = ct_bind(cmd, 2, &datafmt, col2, &datalength, &ind);
149  if (ret != CS_SUCCEED) {
150  fprintf(stderr, "ct_bind() failed\n");
151  return 1;
152  }
153 
154  ret = ct_describe(cmd, 3, &datafmt);
155  if (ret != CS_SUCCEED) {
156  fprintf(stderr, "ct_describe() failed");
157  return 1;
158  }
159 
160  datafmt.datatype = CS_CHAR_TYPE;
161  datafmt.format = CS_FMT_NULLTERM;
162  datafmt.maxlength = 32;
163 
164  ret = ct_bind(cmd, 3, &datafmt, col3, &datalength, &ind);
165  if (ret != CS_SUCCEED) {
166  fprintf(stderr, "ct_bind() failed\n");
167  return 1;
168  }
169 
170  while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
171  || (ret == CS_ROW_FAIL)) {
172  row_count += count;
173  if (ret == CS_ROW_FAIL) {
174  fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
175  return 1;
176  } else { /* ret == CS_SUCCEED */
177  fprintf(stdout, "col1 = %d col2= '%s', col3 = '%s'\n", col1, col2, col3);
178  ++rows[0];
179  }
180  }
181 
182 
183  switch ((int) ret) {
184  case CS_END_DATA:
185  break;
186  case CS_FAIL:
187  fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
188  return 1;
189  default:
190  fprintf(stderr, "ct_fetch() unexpected return.\n");
191  return 1;
192  }
193  break;
194 
195  case CS_COMPUTE_RESULT:
196 
197  printf("testing compute_result\n");
198 
199  ret = ct_compute_info(cmd, CS_COMP_ID, CS_UNUSED, &compute_id, CS_UNUSED, NULL);
200  if (ret != CS_SUCCEED) {
201  fprintf(stderr, "ct_compute_info() failed");
202  return 1;
203  }
204 
205  if (compute_id != 1 && compute_id != 2) {
206  fprintf(stderr, "invalid compute_id value");
207  return 1;
208  }
209 
210  if (compute_id == 1) {
211  ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
212  if (ret != CS_SUCCEED) {
213  fprintf(stderr, "ct_res_info() failed");
214  return 1;
215  }
216  if (num_cols != 1) {
217  fprintf(stderr, "compute_id %d num_cols %d != 1", compute_id, num_cols);
218  return 1;
219  }
220 
221  ret = ct_describe(cmd, 1, &datafmt);
222  if (ret != CS_SUCCEED) {
223  fprintf(stderr, "ct_describe() failed");
224  return 1;
225  }
226  datafmt.format = CS_FMT_UNUSED;
227  if (datafmt.maxlength > 1024) {
228  datafmt.maxlength = 1024;
229  }
230  compute_col1 = -1;
231  ret = ct_bind(cmd, 1, &datafmt, &compute_col1, &datalength, &ind);
232  if (ret != CS_SUCCEED) {
233  fprintf(stderr, "ct_bind() failed\n");
234  return 1;
235  }
236  }
237  if (compute_id == 2) {
238  ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
239  if (ret != CS_SUCCEED) {
240  fprintf(stderr, "ct_res_info() failed");
241  return 1;
242  }
243  if (num_cols != 1) {
244  fprintf(stderr, "compute_id %d num_cols %d != 1", compute_id, num_cols);
245  return 1;
246  }
247 
248  ret = ct_describe(cmd, 1, &datafmt);
249  if (ret != CS_SUCCEED) {
250  fprintf(stderr, "ct_describe() failed");
251  return 1;
252  }
253 
254  datafmt.datatype = CS_CHAR_TYPE;
255  datafmt.format = CS_FMT_NULLTERM;
256  datafmt.maxlength = 32;
257 
258  compute_col3[0] = 0;
259  ret = ct_bind(cmd, 1, &datafmt, compute_col3, &datalength, &ind);
260  if (ret != CS_SUCCEED) {
261  fprintf(stderr, "ct_bind() failed\n");
262  return 1;
263  }
264  }
265 
266 
267  while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
268  || (ret == CS_ROW_FAIL)) {
269  row_count += count;
270  if (ret == CS_ROW_FAIL) {
271  fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
272  return 1;
273  } else { /* ret == CS_SUCCEED */
274  if (compute_id == 1) {
275  fprintf(stdout, "compute_col1 = %d \n", compute_col1);
276  if (compute_col1 != 6 && compute_col1 != 17) {
277  fprintf(stderr, "(should be 6 or 17)\n");
278  return 1;
279  }
280  }
281  if (compute_id == 2) {
282  fprintf(stdout, "compute_col3 = '%s'\n", compute_col3);
283  if (strcmp("Jan 5 2002 10:00:00AM", compute_col3)
284  && strcmp("Jan 05 2002 10:00AM", compute_col3)
285  && strcmp("Jan 5 2002 10:00AM", compute_col3)) {
286  fprintf(stderr, "(should be \"Jan 5 2002 10:00:00AM\")\n");
287  return 1;
288  }
289  }
290  ++rows[compute_id];
291  }
292  }
293 
294 
295  switch ((int) ret) {
296  case CS_END_DATA:
297  break;
298  case CS_FAIL:
299  fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
300  return 1;
301  default:
302  fprintf(stderr, "ct_fetch() unexpected return.\n");
303  return 1;
304  }
305  break;
306 
307  default:
308  fprintf(stderr, "ct_results() unexpected result_type.\n");
309  return 1;
310  }
311  }
312  switch ((int) results_ret) {
313  case CS_END_RESULTS:
314  break;
315  case CS_FAIL:
316  fprintf(stderr, "ct_results() failed.\n");
317  return 1;
318  break;
319  default:
320  fprintf(stderr, "ct_results() unexpected return.\n");
321  return 1;
322  }
323 
324  if (rows[0] != 5 || rows[1] != 2 || rows[2] != 1) {
325  fprintf(stderr, "wrong number of rows: normal %u compute_1 %u compute_2 %u, expected 5 2 1\n",
326  rows[0], rows[1], rows[2]);
327  return 1;
328  }
329 
330  if (verbose) {
331  fprintf(stdout, "Trying logout\n");
332  }
333  ret = try_ctlogout(ctx, conn, cmd, verbose);
334  if (ret != CS_SUCCEED) {
335  fprintf(stderr, "Logout failed\n");
336  return 1;
337  }
338 
339  return 0;
340 }
341 
342 static CS_RETCODE
343 ex_servermsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_SERVERMSG * srvmsg)
344 {
345  if (strstr(srvmsg->text, "compute")) {
346  compute_supported = 0;
347  printf("Server does not support compute\n");
348  return CS_SUCCEED;
349  }
350  return servermsg_cb(context, connection, srvmsg);
351 }
static char col1[256]
Definition: compute.c:13
static char col2[256]
Definition: compute.c:13
#define CS_SERVERMSG_CB
Definition: cspublic.h:411
#define CS_CMD_DONE
Definition: cspublic.h:436
#define CS_CMD_SUCCEED
Definition: cspublic.h:437
#define CS_FAIL
Definition: cspublic.h:41
@ CS_FMT_UNUSED
Definition: cspublic.h:397
@ CS_FMT_NULLTERM
Definition: cspublic.h:399
#define CS_LANG_CMD
Definition: cspublic.h:441
#define CS_COMP_ID
Definition: cspublic.h:485
#define CS_CHAR_TYPE
Definition: cspublic.h:550
#define CS_UNUSED
Definition: cspublic.h:425
#define CS_END_DATA
Definition: cspublic.h:55
#define CS_SET
Definition: cspublic.h:429
#define CS_ROW_RESULT
Definition: cspublic.h:541
#define CS_COMPUTE_RESULT
Definition: cspublic.h:538
#define CS_ROW_FAIL
Definition: cspublic.h:54
#define CS_SUCCEED
Definition: cspublic.h:40
#define CS_CMD_FAIL
Definition: cspublic.h:438
#define CS_NULLTERM
Definition: cspublic.h:422
#define CS_END_RESULTS
Definition: cspublic.h:56
#define CS_NUMDATA
Definition: cspublic.h:472
Int4 CS_INT
Definition: cstypes.h:41
Int2 CS_SMALLINT
Definition: cstypes.h:45
void CS_VOID
Definition: cstypes.h:53
CS_INT CS_RETCODE
Definition: cstypes.h:63
char CS_CHAR
Definition: cstypes.h:48
static CS_COMMAND * cmd
Definition: ct_dynamic.c:26
static CS_CONNECTION * conn
Definition: ct_dynamic.c:25
CS_RETCODE try_ctlogin(CS_CONTEXT **ctx, CS_CONNECTION **conn, CS_COMMAND **cmd, int verbose)
Definition: common.c:194
CS_RETCODE run_command(CS_COMMAND *cmd, const char *sql)
Definition: common.c:330
const char * res_type_str(CS_RETCODE ret)
Definition: common.c:432
CS_RETCODE servermsg_cb(CS_CONTEXT *context, CS_CONNECTION *connection, CS_SERVERMSG *srvmsg)
Definition: common.c:412
CS_RETCODE try_ctlogout(CS_CONTEXT *ctx, CS_CONNECTION *conn, CS_COMMAND *cmd, int verbose)
Definition: common.c:308
CS_CONTEXT * ctx
Definition: t0006.c:12
int main(int argc, char *argv[])
Definition: t0009.c:18
static int compute_supported
Definition: t0009.c:14
static CS_RETCODE ex_servermsg_cb(CS_CONTEXT *context, CS_CONNECTION *connection, CS_SERVERMSG *errmsg)
Definition: t0009.c:343
CS_RETCODE ct_compute_info(CS_COMMAND *cmd, CS_INT type, CS_INT colnum, CS_VOID *buffer, CS_INT buflen, CS_INT *outlen)
Definition: ct.c:2747
CS_RETCODE ct_command(CS_COMMAND *cmd, CS_INT type, const CS_VOID *buffer, CS_INT buflen, CS_INT option)
Definition: ct.c:760
CS_RETCODE ct_results(CS_COMMAND *cmd, CS_INT *result_type)
Definition: ct.c:1172
CS_RETCODE ct_res_info(CS_COMMAND *cmd, CS_INT type, CS_VOID *buffer, CS_INT buflen, CS_INT *out_len)
Definition: ct.c:2491
CS_RETCODE ct_callback(CS_CONTEXT *ctx, CS_CONNECTION *con, CS_INT action, CS_INT type, CS_VOID *func)
Definition: ct.c:306
CS_RETCODE ct_fetch(CS_COMMAND *cmd, CS_INT type, CS_INT offset, CS_INT option, CS_INT *rows_read)
Definition: ct.c:1589
CS_RETCODE ct_send(CS_COMMAND *cmd)
Definition: ct.c:913
CS_RETCODE ct_bind(CS_COMMAND *cmd, CS_INT item, CS_DATAFMT *datafmt, CS_VOID *buffer, CS_INT *copied, CS_SMALLINT *indicator)
Definition: ct.c:1531
CS_RETCODE ct_describe(CS_COMMAND *cmd, CS_INT item, CS_DATAFMT *datafmt)
Definition: ct.c:2424
#define NULL
Definition: ncbistd.hpp:225
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
true_type verbose
Definition: processing.cpp:890
#define strcat(s, k)
CS_INT format
Definition: cstypes.h:126
CS_INT maxlength
Definition: cstypes.h:127
CS_INT datatype
Definition: cstypes.h:125
CS_CHAR text[1024]
Definition: cstypes.h:203
Modified on Tue Apr 09 07:56:28 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 10 07:31:53 2024 by modify_doxy.py rev. 669887
Modified on Thu Apr 11 15:03:51 2024 by modify_doxy.py rev. 669887
Modified on Fri Apr 12 17:15:52 2024 by modify_doxy.py rev. 669887
Modified on Sat Apr 13 11:44:18 2024 by modify_doxy.py rev. 669887
Modified on Sun Apr 14 05:25:08 2024 by modify_doxy.py rev. 669887
Modified on Tue Apr 16 20:08:16 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 17 13:08:34 2024 by modify_doxy.py rev. 669887