NCBI C++ ToolKit
get_send_data.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 
16 
17 /* Testing: Retrieve CS_TEXT_TYPE using ct_bind() */
18 int
19 main(int argc, char **argv)
20 {
21  CS_CONTEXT *ctx;
22  CS_COMMAND *cmd;
23  int i, verbose = 0;
24 
25  CS_RETCODE ret;
26  CS_RETCODE ret2;
27  CS_RETCODE results_ret;
28  CS_INT result_type;
29  CS_INT num_cols;
30 
31  CS_DATAFMT datafmt;
32  CS_INT datalength;
33  CS_SMALLINT ind;
34  CS_INT count, row_count = 0;
35 
36  CS_INT id;
37  CS_CHAR name[600];
38  CS_CHAR *nameptr;
39  CS_INT getlen;
40 
41  char large_sql[1024];
42  char len600[601];
43  char len800[801];
44  char temp[11];
45 
46  char *textptr;
47  CS_IODESC iodesc;
48 
49  int tds_version;
50 
51  len600[0] = 0;
52  name[0] = 0;
53  for (i = 0; i < 60; i++) {
54  sprintf(temp, "_abcde_%03d", (i + 1) * 10);
55  strcat(len600, temp);
56  }
57  len600[600] = '\0';
58 
59  len800[0] = 0;
60  for (i = 0; i < 80; i++) {
61  sprintf(temp, "_zzzzz_%03d", (i + 1) * 10);
62  strcat(len800, temp);
63  }
64  len800[800] = '\0';
65 
66 
67  fprintf(stdout, "%s: Retrieve CS_TEXT_TYPE using ct_bind()\n", __FILE__);
68  if (verbose) {
69  fprintf(stdout, "Trying login\n");
70  }
71  ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
72  if (ret != CS_SUCCEED) {
73  fprintf(stderr, "Login failed\n");
74  return 1;
75  }
76 
78  if (ret == CS_SUCCEED) {
79  if (tds_version >= CS_TDS_72) {
80  printf("Protocol TDS7.2+ detected, test not supported\n");
82  return 0;
83  }
84  }
85 
86  ret = run_command(cmd, "CREATE TABLE #test_table (id int, name text)");
87  if (ret != CS_SUCCEED)
88  return 1;
89 
90  sprintf(large_sql, "INSERT #test_table (id, name) VALUES (2, '%s')", len600);
91  ret = run_command(cmd, large_sql);
92  if (ret != CS_SUCCEED)
93  return 1;
94 
95  ret = ct_command(cmd, CS_LANG_CMD, "SELECT id, name FROM #test_table", CS_NULLTERM, CS_UNUSED);
96  if (ret != CS_SUCCEED) {
97  fprintf(stderr, "ct_command() failed\n");
98  return 1;
99  }
100  ret = ct_send(cmd);
101  if (ret != CS_SUCCEED) {
102  fprintf(stderr, "ct_send() failed\n");
103  return 1;
104  }
105  while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
106  switch ((int) result_type) {
107  case CS_CMD_SUCCEED:
108  break;
109  case CS_CMD_DONE:
110  break;
111  case CS_CMD_FAIL:
112  fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
113  return 1;
114  case CS_ROW_RESULT:
115  ret = ct_res_info(cmd, CS_NUMDATA, &num_cols, CS_UNUSED, NULL);
116  if (ret != CS_SUCCEED) {
117  fprintf(stderr, "ct_res_info() failed");
118  return 1;
119  }
120  if (num_cols != 2) {
121  fprintf(stderr, "num_cols %d != 2", num_cols);
122  return 1;
123  }
124 
125  ret = ct_describe(cmd, 1, &datafmt);
126  if (ret != CS_SUCCEED) {
127  fprintf(stderr, "ct_describe() failed");
128  return 1;
129  }
130  datafmt.format = CS_FMT_UNUSED;
131  if (datafmt.maxlength > 1024) {
132  datafmt.maxlength = 1024;
133  }
134  ret = ct_bind(cmd, 1, &datafmt, &id, &datalength, &ind);
135  if (ret != CS_SUCCEED) {
136  fprintf(stderr, "ct_bind() failed\n");
137  return 1;
138  }
139 
140  while (((ret = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count)) == CS_SUCCEED)
141  || (ret == CS_ROW_FAIL)) {
142  row_count += count;
143  if (ret == CS_ROW_FAIL) {
144  fprintf(stderr, "ct_fetch() CS_ROW_FAIL on row %d.\n", row_count);
145  return 1;
146  } else { /* ret == CS_SUCCEED */
147  if (verbose) {
148  fprintf(stdout, "id = '%d'\n", id);
149  }
150 
151  nameptr = name;
152  while ((ret2 = ct_get_data(cmd, 2 , nameptr, 200, &getlen )) == CS_SUCCEED) {
153  nameptr += getlen;
154  }
155  if (ret2 != CS_END_DATA) {
156  fprintf(stderr, "ct_get_data() failed\n");
157  return 1;
158  }
159 
160  if (memcmp(name, len600, 600)) {
161  fprintf(stderr, "Bad return data\n");
162  return 1;
163  }
164  fprintf(stdout, "%s: Trying ct_data_info on text column\n", __FILE__);
165 
166  if (ct_data_info(cmd, CS_GET, 2, &iodesc) != CS_SUCCEED) {
167  fprintf(stderr, "ct_data_info() failed\n");
168  return 1;
169  } else {
170  fprintf(stdout, "datatype = %d\n", iodesc.datatype);
171  fprintf(stdout, "usertype = %d\n", iodesc.usertype);
172  fprintf(stdout, "text len = %d\n", iodesc.total_txtlen);
173  fprintf(stdout, "name = %*.*s\n", iodesc.namelen, iodesc.namelen, iodesc.name);
174  }
175  }
176  }
177  switch ((int) ret) {
178  case CS_END_DATA:
179  break;
180  case CS_FAIL:
181  fprintf(stderr, "ct_fetch() returned CS_FAIL.\n");
182  return 1;
183  default:
184  fprintf(stderr, "ct_fetch() unexpected return.\n");
185  return 1;
186  }
187  break;
188  case CS_COMPUTE_RESULT:
189  fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
190  return 1;
191  default:
192  fprintf(stderr, "ct_results() unexpected result_type.\n");
193  return 1;
194  }
195  }
196  switch ((int) results_ret) {
197  case CS_END_RESULTS:
198  break;
199  case CS_FAIL:
200  fprintf(stderr, "ct_results() failed.\n");
201  return 1;
202  break;
203  default:
204  fprintf(stderr, "ct_results() unexpected return.\n");
205  return 1;
206  }
207 
209  fprintf(stderr, "ct_command(CS_SEND_DATA_CMD) failed.\n");
210  return 1;
211  }
212 
213  iodesc.total_txtlen = 800;
214  iodesc.log_on_update = CS_TRUE;
215 
216  if (ct_data_info(cmd, CS_SET, CS_UNUSED, &iodesc) != CS_SUCCEED) {
217  fprintf(stderr, "ct_data_info() failed\n");
218  return 1;
219  }
220 
221  for ( i = 0 ; i < 800 ; i += 200 ) {
222  textptr = &len800[i];
223 
224  ret = ct_send_data(cmd, textptr, (CS_INT) 200);
225  if (ret != CS_SUCCEED) {
226  fprintf(stderr, "ct_send_data failed\n");
227  return 1;
228  }
229  }
230  ret = ct_send(cmd);
231 
232  if (ret != CS_SUCCEED) {
233  fprintf(stderr, "ct_send failed\n");
234  return 1;
235  }
236 
237  while ((results_ret = ct_results(cmd, &result_type)) == CS_SUCCEED) {
238  switch ((int) result_type) {
239  case CS_CMD_SUCCEED:
240  break;
241  case CS_CMD_DONE:
242  break;
243  case CS_CMD_FAIL:
244  fprintf(stderr, "ct_results() result_type CS_CMD_FAIL.\n");
245  return 1;
246  case CS_ROW_RESULT:
247  break;
248  case CS_PARAM_RESULT:
249  break;
250  case CS_COMPUTE_RESULT:
251  fprintf(stderr, "ct_results() unexpected CS_COMPUTE_RESULT.\n");
252  return 1;
253  default:
254  fprintf(stderr, "ct_results() unexpected result_type.\n");
255  return 1;
256  }
257  }
258  switch ((int) results_ret) {
259  case CS_END_RESULTS:
260  break;
261  case CS_FAIL:
262  fprintf(stderr, "ct_results() failed.\n");
263  return 1;
264  break;
265  default:
266  fprintf(stderr, "ct_results() unexpected return.\n");
267  return 1;
268  }
269 
270  if (verbose) {
271  fprintf(stdout, "Trying logout\n");
272  }
273  ret = try_ctlogout(ctx, conn, cmd, verbose);
274  if (ret != CS_SUCCEED) {
275  fprintf(stderr, "Logout failed\n");
276  return 1;
277  }
278 
279  return 0;
280 }
#define CS_CMD_DONE
Definition: cspublic.h:436
@ CS_TDS_VERSION
Definition: cspublic.h:193
#define CS_CMD_SUCCEED
Definition: cspublic.h:437
#define CS_FAIL
Definition: cspublic.h:41
#define CS_PARAM_RESULT
Definition: cspublic.h:540
@ CS_FMT_UNUSED
Definition: cspublic.h:397
#define CS_LANG_CMD
Definition: cspublic.h:441
#define CS_SEND_DATA_CMD
Definition: cspublic.h:443
@ CS_TDS_72
Definition: cspublic.h:276
#define CS_UNUSED
Definition: cspublic.h:425
#define CS_END_DATA
Definition: cspublic.h:55
#define CS_SET
Definition: cspublic.h:429
#define CS_TRUE
Definition: cspublic.h:722
#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
@ CS_COLUMN_DATA
Definition: cspublic.h:387
#define CS_NULLTERM
Definition: cspublic.h:422
#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
static CS_COMMAND * cmd
Definition: ct_dynamic.c:26
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_get_data(CS_COMMAND *cmd, CS_INT item, CS_VOID *buffer, CS_INT buflen, CS_INT *outlen)
Definition: ct.c:2835
CS_RETCODE ct_send(CS_COMMAND *cmd)
Definition: ct.c:913
CS_RETCODE ct_data_info(CS_COMMAND *cmd, CS_INT action, CS_INT colnum, CS_IODESC *iodesc)
Definition: ct.c:3051
CS_RETCODE ct_send_data(CS_COMMAND *cmd, CS_VOID *buffer, CS_INT buflen)
Definition: ct.c:2987
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_con_props(CS_CONNECTION *con, CS_INT action, CS_INT property, CS_VOID *buffer, CS_INT buflen, CS_INT *out_len)
Definition: ct.c:351
CS_RETCODE ct_describe(CS_COMMAND *cmd, CS_INT item, CS_DATAFMT *datafmt)
Definition: ct.c:2424
int main(int argc, char **argv)
Definition: get_send_data.c:19
static CS_CONNECTION * conn
Definition: get_send_data.c:15
#define NULL
Definition: ncbistd.hpp:225
int i
static int tds_version
Definition: bcp.c:59
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 usertype
Definition: cstypes.h:109
CS_INT total_txtlen
Definition: cstypes.h:110
CS_INT namelen
Definition: cstypes.h:114
CS_CHAR name[400]
Definition: cstypes.h:113
CS_INT datatype
Definition: cstypes.h:107
CS_BOOL log_on_update
Definition: cstypes.h:112
Modified on Tue Apr 09 07:56:42 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 10 07:32:06 2024 by modify_doxy.py rev. 669887
Modified on Thu Apr 11 15:04:19 2024 by modify_doxy.py rev. 669887
Modified on Fri Apr 12 17:16:18 2024 by modify_doxy.py rev. 669887
Modified on Sat Apr 13 11:44:45 2024 by modify_doxy.py rev. 669887
Modified on Sun Apr 14 05:25:29 2024 by modify_doxy.py rev. 669887