NCBI C++ ToolKit
ct_diagall.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 #include <stdarg.h>
4 #include <stdio.h>
5 
6 #if HAVE_STRING_H
7 #include <string.h>
8 #endif /* HAVE_STRING_H */
9 
10 #include <ctpublic.h>
11 #include "common.h"
12 
13 #include <common/test_assert.h>
14 
15 /* Testing: Client and server Messages */
16 int
17 main(int argc, char *argv[])
18 {
19  CS_CONTEXT *ctx;
21  CS_COMMAND *cmd;
22  int verbose = 0;
23 
24  CS_RETCODE ret;
25  CS_RETCODE results_ret;
26  CS_INT result_type;
27  CS_INT num_cols;
28 
29  CS_DATAFMT datafmt;
30  CS_INT datalength[2];
31  CS_SMALLINT ind[2];
32  CS_INT count, row_count = 0;
33  CS_INT cv;
34  int i;
35  CS_CHAR select[1024];
36 
37  CS_INT col1[2];
38  CS_CHAR col2[2][5];
39  CS_INT num_msgs, totmsgs;
40  CS_CLIENTMSG client_message;
41  CS_SERVERMSG server_message;
42 
43  fprintf(stdout, "%s: Retrieve data using array binding \n", __FILE__);
44  if (verbose) {
45  fprintf(stdout, "Trying login\n");
46  }
47  ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
48  if (ret != CS_SUCCEED) {
49  fprintf(stderr, "Login failed\n");
50  return 1;
51  }
52 
54  fprintf(stderr, "ct_diag(CS_INIT) failed\n");
55  return 1;
56  }
57 
58  totmsgs = 5;
60  fprintf(stderr, "ct_diag(CS_MSGLIMIT) failed\n");
61  return 1;
62  }
63 
64  fprintf(stdout, "Maximum message limit is set to: %d\n", totmsgs);
65 
66  ret = run_command(cmd, "CREATE TABLE #ctlibarray (col1 int not null, col2 char(4) not null, col3 datetime not null)");
67  if (ret != CS_SUCCEED)
68  return 1;
69 
70  ret = run_command(cmd, "insert into #ctlibarray values (1, 'AAAA', 'Jan 1 2002 10:00:00AM')");
71  if (ret != CS_SUCCEED)
72  return 1;
73 
74  ret = run_command(cmd, "insert into #ctlibarray values (2, 'BBBB', 'Jan 2 2002 10:00:00AM')");
75  if (ret != CS_SUCCEED)
76  return 1;
77 
78  strcpy(select, "select col1, col2 from #ctlibarray order by col1 ");
79 
81 
82  if (ret != CS_SUCCEED) {
83  fprintf(stderr, "ct_command(%s) failed\n", select);
84  return 1;
85  }
86 
87  ret = ct_send(cmd);
88  if (ret != CS_SUCCEED) {
89  fprintf(stderr, "ct_send() failed\n");
90  return 1;
91  }
92 
93  while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
94  switch ((int) result_type) {
95  case CS_CMD_SUCCEED:
96  break;
97  case CS_CMD_DONE:
98  break;
99  case CS_CMD_FAIL:
100  fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
101  return 1;
102  case CS_ROW_RESULT:
103 
104  ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
105  if (ret != CS_SUCCEED) {
106  fprintf(stderr, "ct_res_info() failed");
107  return 1;
108  }
109  if (num_cols != 2) {
110  fprintf(stderr, "num_cols %d != 2", num_cols);
111  return 1;
112  }
113 
114  ret = ct_describe(cmd, 1, &datafmt);
115  if (ret != CS_SUCCEED) {
116  fprintf(stderr, "ct_describe() failed");
117  return 1;
118  }
119  datafmt.format = CS_FMT_UNUSED;
120  if (datafmt.maxlength > 1024) {
121  datafmt.maxlength = 1024;
122  }
123 
124  datafmt.count = 2;
125 
126  ret = ct_bind(cmd, 1, &datafmt, &col1[0], datalength, ind);
127  if (ret != CS_SUCCEED) {
128  fprintf(stderr, "ct_bind() failed\n");
129  return 1;
130  }
131 
132  ret = ct_describe(cmd, 2, &datafmt);
133  if (ret != CS_SUCCEED) {
134  fprintf(stderr, "ct_describe() failed");
135  return 1;
136  }
137 
138  datafmt.format = CS_FMT_NULLTERM;
139  datafmt.maxlength = 5;
140  datafmt.count = 4;
141 
142  ret = ct_bind(cmd, 2, &datafmt, &col2[0], datalength, ind);
143 
144  if (ret != CS_SUCCEED) {
145 
146  datafmt.format = CS_FMT_NULLTERM;
147  datafmt.maxlength = 5;
148  datafmt.count = 2;
149 
150  ret = ct_bind(cmd, 2, &datafmt, &col2[0], datalength, ind);
151 
152  }
153 
154  if (ret != CS_SUCCEED) {
155  return 1;
156  }
157 
158  count = 0;
159  while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
160  || (ret == CS_ROW_FAIL)) {
161  row_count += count;
162  if (ret == CS_ROW_FAIL) {
163  fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
164  return 1;
165  } else { /* ret == CS_SUCCEED */
166  fprintf(stdout, "ct_fetch returned %d rows\n", count);
167  for (cv = 0; cv < count; cv++)
168  fprintf(stdout, "col1 = %d col2= '%s'\n", col1[cv], col2[cv]);
169  }
170  count = 0;
171  }
172 
173 
174  switch ((int) ret) {
175  case CS_END_DATA:
176  break;
177  case CS_FAIL:
178  fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
179  return 1;
180  default:
181  fprintf(stderr, "ct_fetch() unexpected return.\n");
182  return 1;
183  }
184  break;
185 
186  default:
187  fprintf(stderr, "ct_results() unexpected result_type.\n");
188  return 1;
189  }
190  }
191  switch ((int) results_ret) {
192  case CS_END_RESULTS:
193  break;
194  case CS_FAIL:
195  fprintf(stderr, "ct_results() failed.\n");
196  return 1;
197  break;
198  default:
199  fprintf(stderr, "ct_results() unexpected return.\n");
200  return 1;
201  }
202 
203  ret = run_command(cmd, "DROP TABLE #ctlibarray3");
204  ret = run_command(cmd, "DROP TABLE #ctlibarray4");
205  ret = run_command(cmd, "DROP TABLE #ctlibarray5");
206 
207  if (ct_diag(conn, CS_STATUS, CS_ALLMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) {
208  fprintf(stderr, "ct_diag(CS_STATUS) failed\n");
209  return 1;
210  }
211 
212  fprintf(stdout, "Total number of client/server messages = %d \n", num_msgs);
213 
215  fprintf(stderr, "ct_diag(CS_STATUS) failed\n");
216  return 1;
217  }
218 
219  fprintf(stdout, "Number of client messages returned: %d\n", num_msgs);
220 
221  for (i = 0; i < num_msgs; i++) {
222 
223  if (ct_diag(conn, CS_GET, CS_CLIENTMSG_TYPE, i + 1, &client_message) != CS_SUCCEED) {
224  fprintf(stderr, "cs_diag(CS_GET) failed\n");
225  return 1;
226  }
227 
228  clientmsg_cb(ctx, conn, &client_message);
229 
230  }
231 
233  fprintf(stderr, "ct_diag(CS_STATUS) failed\n");
234  return 1;
235  }
236 
237  fprintf(stdout, "Number of server messages returned: %d\n", num_msgs);
238 
239  for (i = 0; i < num_msgs; i++) {
240 
241  if (ct_diag(conn, CS_GET, CS_SERVERMSG_TYPE, i + 1, &server_message) != CS_SUCCEED) {
242  fprintf(stderr, "cs_diag(CS_GET) failed\n");
243  return 1;
244  }
245 
246  servermsg_cb(ctx, conn, &server_message);
247 
248  }
249 
251  fprintf(stderr, "cs_diag(CS_CLEAR) failed\n");
252  return 1;
253  }
254 
255  if (ct_diag(conn, CS_STATUS, CS_ALLMSG_TYPE, CS_UNUSED, &num_msgs) != CS_SUCCEED) {
256  fprintf(stderr, "cs_diag(CS_STATUS) failed\n");
257  return 1;
258  }
259  if (num_msgs != 0) {
260  fprintf(stderr, "cs_diag(CS_CLEAR) failed there are still %d messages on queue\n", num_msgs);
261  return 1;
262  }
263 
264  if (verbose) {
265  fprintf(stdout, "Trying logout\n");
266  }
267  ret = try_ctlogout(ctx, conn, cmd, verbose);
268  if (ret != CS_SUCCEED) {
269  fprintf(stderr, "Logout failed\n");
270  return 1;
271  }
272 
273  return 0;
274 }
static char col1[256]
Definition: compute.c:13
static char col2[256]
Definition: compute.c:13
#define CS_CMD_DONE
Definition: cspublic.h:436
#define CS_SERVERMSG_TYPE
Definition: cspublic.h:728
#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_MSGLIMIT
Definition: cspublic.h:433
#define CS_UNUSED
Definition: cspublic.h:425
#define CS_END_DATA
Definition: cspublic.h:55
#define CS_INIT
Definition: cspublic.h:431
#define CS_ROW_RESULT
Definition: cspublic.h:541
#define CS_ROW_FAIL
Definition: cspublic.h:54
#define CS_SUCCEED
Definition: cspublic.h:40
#define CS_ALLMSG_TYPE
Definition: cspublic.h:729
#define CS_CMD_FAIL
Definition: cspublic.h:438
#define CS_CLIENTMSG_TYPE
Definition: cspublic.h:727
#define CS_NULLTERM
Definition: cspublic.h:422
#define CS_CLEAR
Definition: cspublic.h:430
#define CS_STATUS
Definition: cspublic.h:432
#define CS_END_RESULTS
Definition: cspublic.h:56
#define CS_NUMDATA
Definition: cspublic.h:472
#define CS_GET
Definition: cspublic.h:428
Int4 CS_INT
Definition: cstypes.h:41
Int2 CS_SMALLINT
Definition: cstypes.h:45
CS_INT CS_RETCODE
Definition: cstypes.h:63
char CS_CHAR
Definition: cstypes.h:48
int main(int argc, char *argv[])
Definition: ct_diagall.c:17
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
CS_RETCODE clientmsg_cb(CS_CONTEXT *context, CS_CONNECTION *connection, CS_CLIENTMSG *errmsg)
Definition: common.c:397
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
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_fetch(CS_COMMAND *cmd, CS_INT type, CS_INT offset, CS_INT option, CS_INT *rows_read)
Definition: ct.c:1589
CS_RETCODE ct_diag(CS_CONNECTION *conn, CS_INT operation, CS_INT type, CS_INT idx, CS_VOID *buffer)
Definition: ct.c:4548
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 i
true_type verbose
Definition: processing.cpp:890
CS_INT format
Definition: cstypes.h:126
CS_INT maxlength
Definition: cstypes.h:127
CS_INT count
Definition: cstypes.h:131
Modified on Tue Apr 09 07:56:05 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 10 07:31:32 2024 by modify_doxy.py rev. 669887
Modified on Thu Apr 11 15:03:11 2024 by modify_doxy.py rev. 669887
Modified on Fri Apr 12 17:15:12 2024 by modify_doxy.py rev. 669887
Modified on Sat Apr 13 11:43:45 2024 by modify_doxy.py rev. 669887
Modified on Sun Apr 14 05:24:47 2024 by modify_doxy.py rev. 669887
Modified on Tue Apr 16 20:07:37 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 17 13:08:18 2024 by modify_doxy.py rev. 669887