NCBI C++ ToolKit
array_bind.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.

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: array binding of result set */
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 
35  CS_CHAR select[1024];
36 
37  CS_INT col1[2];
38  CS_CHAR col2[2][5];
39  CS_CHAR col3[2][32];
40 
41 
42  fprintf(stdout, "%s: Retrieve data using array binding \n", __FILE__);
43  if (verbose) {
44  fprintf(stdout, "Trying login\n");
45  }
46  ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
47  if (ret != CS_SUCCEED) {
48  fprintf(stderr, "Login failed\n");
49  return 1;
50  }
51 
52  ret = run_command(cmd, "CREATE TABLE #ctlibarray (col1 int not null, col2 char(4) not null, col3 datetime not null)");
53  if (ret != CS_SUCCEED)
54  return 1;
55 
56  ret = run_command(cmd, "insert into #ctlibarray values (1, 'AAAA', 'Jan 1 2002 10:00:00AM')");
57  if (ret != CS_SUCCEED)
58  return 1;
59 
60  ret = run_command(cmd, "insert into #ctlibarray values (2, 'BBBB', 'Jan 2 2002 10:00:00AM')");
61  if (ret != CS_SUCCEED)
62  return 1;
63 
64  ret = run_command(cmd, "insert into #ctlibarray values (3, 'CCCC', 'Jan 3 2002 10:00:00AM')");
65  if (ret != CS_SUCCEED)
66  return 1;
67 
68  ret = run_command(cmd, "insert into #ctlibarray values (8, 'DDDD', 'Jan 4 2002 10:00:00AM')");
69  if (ret != CS_SUCCEED)
70  return 1;
71 
72  ret = run_command(cmd, "insert into #ctlibarray values (9, 'EEEE', 'Jan 5 2002 10:00:00AM')");
73  if (ret != CS_SUCCEED)
74  return 1;
75 
76 
77  strcpy(select, "select col1, col2, col3 from #ctlibarray order by col1 ");
78 
80 
81  if (ret != CS_SUCCEED) {
82  fprintf(stderr, "ct_command(%s) failed\n", select);
83  return 1;
84  }
85 
86  ret = ct_send(cmd);
87  if (ret != CS_SUCCEED) {
88  fprintf(stderr, "ct_send() failed\n");
89  return 1;
90  }
91 
92  while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
93  switch ((int) result_type) {
94  case CS_CMD_SUCCEED:
95  break;
96  case CS_CMD_DONE:
97  break;
98  case CS_CMD_FAIL:
99  fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
100  return 1;
101  case CS_ROW_RESULT:
102 
103  ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
104  if (ret != CS_SUCCEED) {
105  fprintf(stderr, "ct_res_info() failed");
106  return 1;
107  }
108  if (num_cols != 3) {
109  fprintf(stderr, "num_cols %d != 3", num_cols);
110  return 1;
111  }
112 
113  ret = ct_describe(cmd, 1, &datafmt);
114  if (ret != CS_SUCCEED) {
115  fprintf(stderr, "ct_describe() failed");
116  return 1;
117  }
118  datafmt.format = CS_FMT_UNUSED;
119  if (datafmt.maxlength > 1024) {
120  datafmt.maxlength = 1024;
121  }
122 
123  datafmt.count = 2;
124 
125  ret = ct_bind(cmd, 1, &datafmt, &col1[0], datalength, ind);
126  if (ret != CS_SUCCEED) {
127  fprintf(stderr, "ct_bind() failed\n");
128  return 1;
129  }
130 
131  ret = ct_describe(cmd, 2, &datafmt);
132  if (ret != CS_SUCCEED) {
133  fprintf(stderr, "ct_describe() failed");
134  return 1;
135  }
136 
137  datafmt.format = CS_FMT_NULLTERM;
138  datafmt.maxlength = 5;
139  datafmt.count = 2;
140 
141  ret = ct_bind(cmd, 2, &datafmt, &col2[0], datalength, ind);
142  if (ret != CS_SUCCEED) {
143  fprintf(stderr, "ct_bind() failed\n");
144  return 1;
145  }
146 
147  ret = ct_describe(cmd, 3, &datafmt);
148  if (ret != CS_SUCCEED) {
149  fprintf(stderr, "ct_describe() failed");
150  return 1;
151  }
152 
153  datafmt.datatype = CS_CHAR_TYPE;
154  datafmt.format = CS_FMT_NULLTERM;
155  datafmt.maxlength = 32;
156  datafmt.count = 2;
157 
158  ret = ct_bind(cmd, 3, &datafmt, &col3[0], datalength, ind);
159  if (ret != CS_SUCCEED) {
160  fprintf(stderr, "ct_bind() failed\n");
161  return 1;
162  }
163 
164  count = 0;
165  while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
166  || (ret == CS_ROW_FAIL)) {
167  row_count += count;
168  if (ret == CS_ROW_FAIL) {
169  fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
170  return 1;
171  } else { /* ret == CS_SUCCEED */
172  fprintf(stdout, "ct_fetch returned %d rows\n", count);
173  for (cv = 0; cv < count; cv++)
174  fprintf(stdout, "col1 = %d col2= '%s', col3 = '%s'\n", col1[cv], col2[cv],
175  col3[cv]);
176  }
177  count = 0;
178  }
179 
180 
181  switch ((int) ret) {
182  case CS_END_DATA:
183  break;
184  case CS_FAIL:
185  fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
186  return 1;
187  default:
188  fprintf(stderr, "ct_fetch() unexpected return.\n");
189  return 1;
190  }
191  break;
192 
193  default:
194  fprintf(stderr, "ct_results() unexpected result_type.\n");
195  return 1;
196  }
197  }
198  switch ((int) results_ret) {
199  case CS_END_RESULTS:
200  break;
201  case CS_FAIL:
202  fprintf(stderr, "ct_results() failed.\n");
203  return 1;
204  break;
205  default:
206  fprintf(stderr, "ct_results() unexpected return.\n");
207  return 1;
208  }
209 
210  if (verbose) {
211  fprintf(stdout, "Trying logout\n");
212  }
213  ret = try_ctlogout(ctx, conn, cmd, verbose);
214  if (ret != CS_SUCCEED) {
215  fprintf(stderr, "Logout failed\n");
216  return 1;
217  }
218 
219  return 0;
220 }
int main(int argc, char *argv[])
Definition: array_bind.c:17
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_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_CHAR_TYPE
Definition: cspublic.h:550
#define CS_UNUSED
Definition: cspublic.h:425
#define CS_END_DATA
Definition: cspublic.h:55
#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_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
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
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_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
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
CS_INT datatype
Definition: cstypes.h:125
Modified on Tue Apr 09 08:00:30 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 10 07:35:51 2024 by modify_doxy.py rev. 669887
Modified on Thu Apr 11 15:21:50 2024 by modify_doxy.py rev. 669887
Modified on Fri Apr 12 17:23:50 2024 by modify_doxy.py rev. 669887
Modified on Sat Apr 13 11:50:53 2024 by modify_doxy.py rev. 669887
Modified on Sun Apr 14 05:29:44 2024 by modify_doxy.py rev. 669887