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

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 char software_version[] = "$Id: t0007.c 80645 2017-12-28 17:20:38Z ucko $";
15 
16 /* Testing: Retrieve CS_TEXT_TYPE using ct_bind() */
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;
29 
30  CS_DATAFMT datafmt[4];
31  CS_INT datalength[4];
32  CS_SMALLINT ind[4];
33  CS_INT count, row_count = 0;
34 
35  CS_CHAR name[4][1024];
36 
37  name[0][0] = 0;
38  name[1][0] = 0;
39  name[2][0] = 0;
40  name[3][0] = 0;
41 
42  fprintf(stdout, "%s: Retrieve CS_CHAR_TYPE using ct_bind()\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 = ct_command(cmd, CS_LANG_CMD, "SELECT CONVERT(VARCHAR(7),'1234') AS test, CONVERT(VARCHAR(7),'') AS test2, CONVERT(VARCHAR(7),NULL) AS test3, CONVERT(NUMERIC(38,2), 123.45) as test4", CS_NULLTERM, CS_UNUSED);
53  if (ret != CS_SUCCEED) {
54  fprintf(stderr, "ct_command() failed\n");
55  return 1;
56  }
57  ret = ct_send(cmd);
58  if (ret != CS_SUCCEED) {
59  fprintf(stderr, "ct_send() failed\n");
60  return 1;
61  }
62  while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
63  switch ((int) result_type) {
64  case CS_CMD_SUCCEED:
65  break;
66  case CS_CMD_DONE:
67  break;
68  case CS_CMD_FAIL:
69  fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
70  return 1;
71  case CS_ROW_RESULT:
72  ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
73  if (ret != CS_SUCCEED) {
74  fprintf(stderr, "ct_res_info() failed");
75  return 1;
76  }
77  if (num_cols != 4) {
78  fprintf(stderr, "num_cols %d != 4", num_cols);
79  return 1;
80  }
81  if (ct_describe(cmd, 1, &datafmt[0]) != CS_SUCCEED) {
82  fprintf(stderr, "ct_describe() failed");
83  return 1;
84  }
85  datafmt[0].format = CS_FMT_NULLTERM;
86  ++datafmt[0].maxlength;
87  if (datafmt[0].maxlength > 1024) {
88  datafmt[0].maxlength = 1024;
89  }
90 
91  if (ct_describe(cmd, 2, &datafmt[1]) != CS_SUCCEED) {
92  fprintf(stderr, "ct_describe() failed");
93  return 1;
94  }
95  datafmt[1].format = CS_FMT_NULLTERM;
96  ++datafmt[1].maxlength;
97  if (datafmt[1].maxlength > 1024) {
98  datafmt[1].maxlength = 1024;
99  }
100 
101  if (ct_describe(cmd, 3, &datafmt[2]) != CS_SUCCEED) {
102  fprintf(stderr, "ct_describe() failed\n");
103  return 1;
104  }
105  datafmt[2].format = CS_FMT_NULLTERM;
106  ++datafmt[2].maxlength;
107  if (datafmt[2].maxlength > 1024) {
108  datafmt[2].maxlength = 1024;
109  }
110 
111  if (ct_describe(cmd, 4, &datafmt[3]) != CS_SUCCEED) {
112  fprintf(stderr, "ct_describe() failed\n");
113  return 1;
114  }
115  datafmt[3].format = CS_FMT_NULLTERM;
116  if (datafmt[3].maxlength != sizeof(CS_NUMERIC)) {
117  fprintf(stderr, "wrong maxlength for numeric\n");
118  return 1;
119  }
120  ++datafmt[3].maxlength;
121  if (datafmt[3].maxlength > 1024) {
122  datafmt[3].maxlength = 1024;
123  }
124 
125  if (ct_bind(cmd, 1, &datafmt[0], name[0], &datalength[0], &ind[0]) != CS_SUCCEED) {
126  fprintf(stderr, "ct_bind() failed\n");
127  return 1;
128  }
129  if (ct_bind(cmd, 2, &datafmt[1], name[1], &datalength[1], &ind[1]) != CS_SUCCEED) {
130  fprintf(stderr, "ct_bind() failed\n");
131  return 1;
132  }
133  if (ct_bind(cmd, 3, &datafmt[2], name[2], &datalength[2], &ind[2]) != CS_SUCCEED) {
134  fprintf(stderr, "ct_bind() failed\n");
135  return 1;
136  }
137  if (ct_bind(cmd, 4, &datafmt[3], name[3], &datalength[3], &ind[3]) != CS_SUCCEED) {
138  fprintf(stderr, "ct_bind() failed\n");
139  return 1;
140  }
141 
142  while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
143  || (ret == CS_ROW_FAIL)) {
144  row_count += count;
145  if (ret == CS_ROW_FAIL) {
146  fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
147  return 1;
148  } else { /* ret == CS_SUCCEED */
149  if (verbose) {
150  fprintf(stdout, "name = '%s'\n", name[0]);
151  }
152  if (ind[0] != 0) {
153  fprintf(stderr, "Returned NULL\n");
154  return 1;
155  }
156  if (strcmp(name[0], "1234")) {
157  fprintf(stderr, "Bad return:\n'%s'\n! =\n'%s'\n", name[0], "1234");
158  return 1;
159  }
160  if (datalength[0] != strlen(name[0]) + 1) {
161  fprintf(stderr, "Bad length:\n'%ld'\n! =\n'%d'\n", (long) strlen(name[0]) + 1, datalength[0]);
162  return 1;
163  }
164  if (ind[1] != 0) {
165  fprintf(stderr, "Returned NULL\n");
166  return 1;
167  }
168  /* empty are retunerd as a single space in TDS4.x and TDS5 */
169  if (strcmp(name[1], "") && strcmp(name[1], " ")) {
170  fprintf(stderr, "Bad return:\n'%s'\n! =\n'%s'\n", name[1], "");
171  return 1;
172  }
173  if (datalength[1] != strlen(name[1]) + 1) {
174  fprintf(stderr, "Col 2 bad length:\n'%ld'\n! =\n'%d'\n", (long) strlen(name[1]) + 1, datalength[1]);
175  return 1;
176  }
177  if (ind[2] == 0) {
178  fprintf(stderr, "Col 3 returned not NULL (ind %d len %d)\n", (int) ind[2], (int) datalength[2]);
179  return 1;
180  }
181  if (strcmp(name[2], "")) {
182  fprintf(stderr, "Col 3 bad return:\n'%s'\n! =\n'%s'\n", name[2], "");
183  return 1;
184  }
185  if (datalength[2] != 0) {
186  fprintf(stderr, "Col 3 bad length:\n'%ld'\n! =\n'%d'\n", (long) 0, datalength[2]);
187  return 1;
188  }
189  }
190  }
191  switch ((int) ret) {
192  case CS_END_DATA:
193  break;
194  case CS_FAIL:
195  fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
196  return 1;
197  default:
198  fprintf(stderr, "ct_fetch() unexpected return.\n");
199  return 1;
200  }
201  break;
202  case CS_COMPUTE_RESULT:
203  fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
204  return 1;
205  default:
206  fprintf(stderr, "ct_results() unexpected result_type.\n");
207  return 1;
208  }
209  }
210  switch ((int) results_ret) {
211  case CS_END_RESULTS:
212  break;
213  case CS_FAIL:
214  fprintf(stderr, "ct_results() failed.\n");
215  return 1;
216  break;
217  default:
218  fprintf(stderr, "ct_results() unexpected return.\n");
219  return 1;
220  }
221 
222  if (verbose) {
223  fprintf(stdout, "Trying logout\n");
224  }
225  ret = try_ctlogout(ctx, conn, cmd, verbose);
226  if (ret != CS_SUCCEED) {
227  fprintf(stderr, "Logout failed\n");
228  return 1;
229  }
230 
231  return 0;
232 }
#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_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
CS_CONTEXT * ctx
Definition: t0006.c:12
int main(int argc, char **argv)
Definition: t0007.c:18
static void * no_unused_var_warn[]
Definition: t0007.c:14
static char software_version[]
Definition: t0007.c:13
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 strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
true_type verbose
Definition: processing.cpp:890
CS_INT format
Definition: cstypes.h:126
CS_INT maxlength
Definition: cstypes.h:127
Modified on Tue Apr 09 07:55:53 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 10 07:31:20 2024 by modify_doxy.py rev. 669887
Modified on Thu Apr 11 15:02:47 2024 by modify_doxy.py rev. 669887
Modified on Fri Apr 12 17:14:54 2024 by modify_doxy.py rev. 669887
Modified on Sat Apr 13 11:43:30 2024 by modify_doxy.py rev. 669887
Modified on Sun Apr 14 05:24:34 2024 by modify_doxy.py rev. 669887
Modified on Tue Apr 16 20:07:17 2024 by modify_doxy.py rev. 669887