NCBI C++ ToolKit
t0002.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 <stdio.h>
4 #include <string.h>
5 #include <ctpublic.h>
6 #include "common.h"
7 
8 #include <common/test_assert.h>
9 
10 static int sp_who(CS_COMMAND *cmd);
11 
12 /*
13  * ct_send SQL |select name = @@servername|
14  * ct_bind variable
15  * ct_fetch and print results
16  */
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_DATAFMT datafmt;
28  CS_INT datalength;
29  CS_SMALLINT ind;
30  CS_INT count, row_count = 0;
31  CS_INT result_type;
32  CS_CHAR name[256];
33 
34  fprintf(stdout, "%s: Testing bind & select\n", __FILE__);
35  if (verbose) {
36  fprintf(stdout, "Trying login\n");
37  }
38  ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
39  if (ret != CS_SUCCEED) {
40  fprintf(stderr, "Login failed\n");
41  return 1;
42  }
43 
44  ret = ct_command(cmd, CS_LANG_CMD, "select name = @@servername", CS_NULLTERM, CS_UNUSED);
45  if (ret != CS_SUCCEED) {
46  fprintf(stderr, "ct_command() failed\n");
47  return 1;
48  }
49  ret = ct_send(cmd);
50  if (ret != CS_SUCCEED) {
51  fprintf(stderr, "ct_send() failed\n");
52  return 1;
53  }
54  while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
55  switch ((int) result_type) {
56  case CS_CMD_SUCCEED:
57  break;
58  case CS_CMD_DONE:
59  break;
60  case CS_CMD_FAIL:
61  fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
62  return 1;
63  case CS_ROW_RESULT:
64  datafmt.datatype = CS_CHAR_TYPE;
65  datafmt.format = CS_FMT_NULLTERM;
66  datafmt.maxlength = 256;
67  datafmt.count = 1;
68  datafmt.locale = NULL;
69  ret = ct_bind(cmd, 1, &datafmt, name, &datalength, &ind);
70  if (ret != CS_SUCCEED) {
71  fprintf(stderr, "ct_bind() failed\n");
72  return 1;
73  }
74 
75  while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
76  || (ret == CS_ROW_FAIL)) {
77  row_count += count;
78  if (ret == CS_ROW_FAIL) {
79  fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
80  return 1;
81  } else if (ret == CS_SUCCEED) {
82  if (verbose) {
83  fprintf(stdout, "server name = %s\n", name);
84  }
85  } else {
86  break;
87  }
88  }
89  switch ((int) ret) {
90  case CS_END_DATA:
91  break;
92  case CS_FAIL:
93  fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
94  return 1;
95  default:
96  fprintf(stderr, "ct_fetch() unexpected return.\n");
97  return 1;
98  }
99  break;
100  case CS_COMPUTE_RESULT:
101  fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
102  return 1;
103  default:
104  fprintf(stderr, "ct_results() unexpected result_type.\n");
105  return 1;
106  }
107  }
108  switch ((int) results_ret) {
109  case CS_END_RESULTS:
110  break;
111  case CS_FAIL:
112  fprintf(stderr, "ct_results() failed.\n");
113  return 1;
114  break;
115  default:
116  fprintf(stderr, "ct_results() unexpected return.\n");
117  return 1;
118  }
119 
120  /*
121  * test parameter return code processing with sp_who
122  */
123  sp_who(cmd);
124 
125  if (verbose) {
126  fprintf(stdout, "Trying logout\n");
127  }
128  ret = try_ctlogout(ctx, conn, cmd, verbose);
129  if (ret != CS_SUCCEED) {
130  fprintf(stderr, "Logout failed\n");
131  return 1;
132  }
133 
134  return 0;
135 }
136 
137 int
139 {
140  enum {maxcol=10, colsize=260};
141 
142  struct _col {
143  CS_DATAFMT datafmt;
144  CS_INT datalength;
145  CS_SMALLINT ind;
146  CS_CHAR data[colsize];
147  } col[maxcol];
148 
149  CS_INT num_cols;
150  CS_INT count, row_count = 0;
151  CS_INT result_type;
152  CS_RETCODE ret;
153  CS_RETCODE results_ret;
154  int i;
155  int is_status_result=0;
156 
157  ret = ct_command(cmd, CS_LANG_CMD, "exec sp_who", CS_NULLTERM, CS_UNUSED);
158  if (ret != CS_SUCCEED) {
159  fprintf(stderr, "ct_command: \"exec sp_who\" failed with %d\n", ret);
160  return 1;
161  }
162  ret = ct_send(cmd);
163  if (ret != CS_SUCCEED) {
164  fprintf(stderr, "ct_send: \"exec sp_who\" failed with %d\n", ret);
165  return 1;
166  }
167  while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
168  is_status_result = 0;
169  switch ((int) result_type) {
170  case CS_CMD_SUCCEED:
171  break;
172  case CS_CMD_DONE:
173  break;
174  case CS_CMD_FAIL:
175  fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
176  return 1;
177  case CS_STATUS_RESULT:
178  fprintf(stdout, "ct_results: CS_STATUS_RESULT detected for sp_who\n");
179  is_status_result = 1;
180  /* fall through */
181  case CS_ROW_RESULT:
182  ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
183  if (ret != CS_SUCCEED || num_cols > maxcol) {
184  fprintf(stderr, "ct_res_info() failed\n");
185  return 1;
186  }
187 
188  if (num_cols <= 0) {
189  fprintf(stderr, "ct_res_info() return strange values\n");
190  return 1;
191  }
192 
193  if (is_status_result && num_cols != 1) {
194  fprintf(stderr, "CS_STATUS_RESULT return more than 1 column\n");
195  return 1;
196  }
197 
198  for (i=0; i < num_cols; i++) {
199 
200  /* here we can finally test for the return status column */
201  ret = ct_describe(cmd, i+1, &col[i].datafmt);
202 
203  if (ret != CS_SUCCEED) {
204  fprintf(stderr, "ct_describe() failed for column %d\n", i);
205  return 1;
206  }
207 
208  if (col[i].datafmt.status & CS_RETURN) {
209  fprintf(stdout, "ct_describe() indicates a return code in column %d for sp_who\n", i);
210 
211  /*
212  * other possible values:
213  * CS_CANBENULL
214  * CS_HIDDEN
215  * CS_IDENTITY
216  * CS_KEY
217  * CS_VERSION_KEY
218  * CS_TIMESTAMP
219  * CS_UPDATABLE
220  * CS_UPDATECOL
221  */
222  }
223 
224  col[i].datafmt.datatype = CS_CHAR_TYPE;
225  col[i].datafmt.format = CS_FMT_NULLTERM;
226  col[i].datafmt.maxlength = colsize;
227  col[i].datafmt.count = 1;
228  col[i].datafmt.locale = NULL;
229 
230  ret = ct_bind(cmd, i+1, &col[i].datafmt, &col[i].data, &col[i].datalength, &col[i].ind);
231  if (ret != CS_SUCCEED) {
232  fprintf(stderr, "ct_bind() failed\n");
233  return 1;
234  }
235 
236  }
237 
238  row_count = 0;
239  while ((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED) {
240  if( row_count == 0) { /* titles */
241  for (i=0; i < num_cols; i++) {
242  char fmt[40];
243  if (col[i].datafmt.namelen == 0) {
244  printf("unnamed%d ",i+1);
245  continue;
246  }
247  sprintf(fmt, "%%-%d.%ds ", col[i].datafmt.namelen, col[i].datafmt.maxlength);
248  printf(fmt, col[i].datafmt.name);
249  }
250  printf("\n");
251  }
252 
253  for (i=0; i < num_cols; i++) { /* data */
254  char fmt[40];
255  if (col[i].ind)
256  continue;
257  sprintf(fmt, "%%-%d.%ds ", col[i].datalength, col[i].datafmt.maxlength);
258  printf(fmt, col[i].data);
259  if (is_status_result && strcmp(col[i].data,"0")) {
260  fprintf(stderr, "CS_STATUS_RESULT should return 0 as result\n");
261  return 1;
262  }
263  }
264 
265  printf("\n");
266 
267  row_count += count;
268  }
269  if (is_status_result && row_count != 1) {
270  fprintf(stderr, "CS_STATUS_RESULT should return a row\n");
271  return 1;
272  }
273 
274  switch ((int) ret) {
275  case CS_END_DATA:
276  fprintf(stdout, "ct_results fetched %d rows.\n", row_count);
277  break;
278  case CS_ROW_FAIL:
279  fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
280  return 1;
281  case CS_FAIL:
282  fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
283  return 1;
284  default:
285  fprintf(stderr, "ct_fetch() unexpected return.\n");
286  return 1;
287  }
288  break;
289  case CS_COMPUTE_RESULT:
290  fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
291  return 1;
292  default:
293  fprintf(stderr, "ct_results() unexpected result_type.\n");
294  return 1;
295  }
296  }
297 
298  switch ((int) results_ret) {
299  case CS_END_RESULTS:
300  break;
301  case CS_FAIL:
302  fprintf(stderr, "ct_results() failed.\n");
303  return 1;
304  break;
305  default:
306  fprintf(stderr, "ct_results() unexpected return.\n");
307  return 1;
308  }
309 
310  return 0;
311 }
#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_NULLTERM
Definition: cspublic.h:399
#define CS_LANG_CMD
Definition: cspublic.h:441
#define CS_RETURN
Definition: cspublic.h:295
#define CS_STATUS_RESULT
Definition: cspublic.h:542
#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_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
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 try_ctlogout(CS_CONTEXT *ctx, CS_CONNECTION *conn, CS_COMMAND *cmd, int verbose)
Definition: common.c:308
static int sp_who(CS_COMMAND *cmd)
Definition: t0002.c:138
int main(int argc, char **argv)
Definition: t0002.c:18
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
int i
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
true_type verbose
Definition: processing.cpp:890
CS_LOCALE * locale
Definition: cstypes.h:133
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 07:57:20 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 10 07:32:41 2024 by modify_doxy.py rev. 669887
Modified on Thu Apr 11 15:05:24 2024 by modify_doxy.py rev. 669887
Modified on Fri Apr 12 17:17:32 2024 by modify_doxy.py rev. 669887
Modified on Sat Apr 13 11:45:39 2024 by modify_doxy.py rev. 669887
Modified on Sun Apr 14 05:26:07 2024 by modify_doxy.py rev. 669887