NCBI C++ ToolKit
lang_ct_param.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_STDLIB_H
7 #include <stdlib.h>
8 #endif /* HAVE_STDLIB_H */
9 
10 #if HAVE_STRING_H
11 #include <string.h>
12 #endif /* HAVE_STRING_H */
13 
14 #include <ctpublic.h>
15 #include "common.h"
16 
17 #include <common/test_assert.h>
18 
19 static const char *query =
20  "insert into #ctparam_lang (name,name2,age,cost,bdate,fval) values (@in1, @in2, @in3, @moneyval, @dateval, @floatval)";
21 
22 CS_RETCODE ex_servermsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_SERVERMSG * errmsg);
23 CS_RETCODE ex_clientmsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_CLIENTMSG * errmsg);
24 
25 static int insert_test(CS_CONNECTION *conn, CS_COMMAND *cmd, int useNames);
26 
27 /* Testing: binding of data via ct_param */
28 int
29 main(int argc, char *argv[])
30 {
31  int errCode = 0;
32 
33  CS_CONTEXT *ctx;
35  CS_COMMAND *cmd;
36  int verbose = 0;
37 
38  CS_RETCODE ret;
39  CS_CHAR cmdbuf[4096];
40 
41  if (argc > 1 && (0 == strcmp(argv[1], "-v")))
42  verbose = 1;
43 
44  printf("%s: submit language query with variables using ct_param\n", __FILE__);
45  if (verbose) {
46  fprintf(stdout, "Trying login\n");
47  }
48  ret = try_ctlogin(&ctx, &conn, &cmd, verbose);
49  if (ret != CS_SUCCEED) {
50  fprintf(stderr, "Login failed\n");
51  return 1;
52  }
53 
55 
57 
58  strcpy(cmdbuf, "create table #ctparam_lang (id numeric identity not null, \
59  name varchar(30), name2 varchar(20), age int, cost money, bdate datetime, fval float) ");
60 
61  ret = run_command(cmd, cmdbuf);
62 
63  if (ret != CS_SUCCEED) {
64  fprintf(stderr, "create table failed\n");
65  errCode = 1;
66  goto ERR;
67  }
68 
69  /* test by name */
70  errCode = insert_test(conn, cmd, 1);
71  /* if worked, test by position */
72  if (0 == errCode)
73  errCode = insert_test(conn, cmd, 0);
74  query = "insert into #ctparam_lang (name,name2,age,cost,bdate,fval) values (?, ?, ?, ?, ?, ?)";
75  if (0 == errCode)
76  errCode = insert_test(conn, cmd, 0);
77 
78  if (verbose && (0 == errCode))
79  fprintf(stdout, "lang_ct_param tests successful\n");
80 
81 ERR:
82  if (verbose) {
83  fprintf(stdout, "Trying logout\n");
84  }
85  ret = try_ctlogout(ctx, conn, cmd, verbose);
86  if (ret != CS_SUCCEED) {
87  fprintf(stderr, "Logout failed\n");
88  return 1;
89  }
90 
91  return errCode;
92 }
93 
94 static int
96 {
97  CS_CONTEXT *ctx;
98 
99  CS_RETCODE ret;
100  CS_INT res_type;
101 
102  CS_DATAFMT datafmt;
103  CS_DATAFMT srcfmt;
104  CS_DATAFMT destfmt;
105  CS_INT intvar;
106  CS_FLOAT floatvar;
107  CS_MONEY moneyvar;
108  CS_DATEREC datevar;
109  char moneystring[10];
110  char dummy_name[30];
111  char dummy_name2[20];
112  CS_INT destlen;
113  CS_INT rowsAffected = -1;
114  int rows_found = 0;
115 
116  /* clear table */
117  run_command(cmd, "delete #ctparam_lang");
118 
119  /*
120  * Assign values to the variables used for parameter passing.
121  */
122 
123  intvar = 2;
124  floatvar = 0.12;
125  strcpy(dummy_name, "joe blow");
126  strcpy(dummy_name2, "");
127  strcpy(moneystring, "300.90");
128 
129  /*
130  * Clear and setup the CS_DATAFMT structures used to convert datatypes.
131  */
132 
133  memset(&srcfmt, 0, sizeof(CS_DATAFMT));
134  srcfmt.datatype = CS_CHAR_TYPE;
135  srcfmt.maxlength = (CS_INT) strlen(moneystring);
136  srcfmt.precision = 5;
137  srcfmt.scale = 2;
138  srcfmt.locale = NULL;
139 
140  memset(&destfmt, 0, sizeof(CS_DATAFMT));
141  destfmt.datatype = CS_MONEY_TYPE;
142  destfmt.maxlength = sizeof(CS_MONEY);
143  destfmt.precision = 5;
144  destfmt.scale = 2;
145  destfmt.locale = NULL;
146 
147  /*
148  * Convert the string representing the money value
149  * to a CS_MONEY variable. Since this routine does not have the
150  * context handle, we use the property functions to get it.
151  */
153  fprintf(stderr, "ct_cmd_props() failed\n");
154  return 1;
155  }
157  fprintf(stderr, "ct_con_props() failed\n");
158  return 1;
159  }
160  ret = cs_convert(ctx, &srcfmt, (CS_VOID *) moneystring, &destfmt, &moneyvar, &destlen);
161  if (ret != CS_SUCCEED) {
162  fprintf(stderr, "cs_convert() failed\n");
163  return 1;
164  }
165 
166  /*
167  * create the command
168  */
169  if ((ret = ct_command(cmd, CS_LANG_CMD, query, (CS_INT) strlen(query),
170  CS_UNUSED)) != CS_SUCCEED)
171  {
172  fprintf(stderr, "ct_command(CS_LANG_CMD) failed\n");
173  return 1;
174  }
175 
176  /*
177  * Clear and setup the CS_DATAFMT structure, then pass
178  * each of the parameters for the query.
179  */
180  memset(&datafmt, 0, sizeof(datafmt));
181  if (useNames)
182  strcpy(datafmt.name, "@in1");
183  else
184  datafmt.name[0] = 0;
185  datafmt.maxlength = 255;
186  datafmt.namelen = CS_NULLTERM;
187  datafmt.datatype = CS_CHAR_TYPE;
188  datafmt.status = CS_INPUTVALUE;
189 
190  /*
191  * The character string variable is filled in by the RPC so pass NULL
192  * for the data 0 for data length, and -1 for the indicator arguments.
193  */
194  ret = ct_param(cmd, &datafmt, dummy_name, (CS_INT) strlen(dummy_name),
195  0);
196  if (CS_SUCCEED != ret) {
197  fprintf(stderr, "ct_param(char) failed\n");
198  return 1;
199  }
200 
201  if (useNames)
202  strcpy(datafmt.name, "@in2");
203  else
204  datafmt.name[0] = 0;
205  datafmt.maxlength = 255;
206  datafmt.namelen = CS_NULLTERM;
207  datafmt.datatype = CS_CHAR_TYPE;
208  datafmt.status = CS_INPUTVALUE;
209 
210  ret = ct_param(cmd, &datafmt, dummy_name2, (CS_INT) strlen(dummy_name2),
211  0);
212  if (CS_SUCCEED != ret) {
213  fprintf(stderr, "ct_param(char) failed\n");
214  return 1;
215  }
216 
217  if (useNames)
218  strcpy(datafmt.name, "@in3");
219  else
220  datafmt.name[0] = 0;
221  datafmt.namelen = CS_NULLTERM;
222  datafmt.datatype = CS_INT_TYPE;
223  datafmt.status = CS_INPUTVALUE;
224 
225  ret = ct_param(cmd, &datafmt, (CS_VOID *) & intvar, CS_SIZEOF(CS_INT), 0);
226  if (CS_SUCCEED != ret) {
227  fprintf(stderr, "ct_param(int) failed\n");
228  return 1;
229  }
230 
231  if (useNames)
232  strcpy(datafmt.name, "@moneyval");
233  else
234  datafmt.name[0] = 0;
235  datafmt.namelen = CS_NULLTERM;
236  datafmt.datatype = CS_MONEY_TYPE;
237  datafmt.status = CS_INPUTVALUE;
238 
239  ret = ct_param(cmd, &datafmt, (CS_VOID *) & moneyvar, CS_SIZEOF(CS_MONEY), 0);
240  if (CS_SUCCEED != ret) {
241  fprintf(stderr, "ct_param(money) failed\n");
242  return 1;
243  }
244 
245  if (useNames)
246  strcpy(datafmt.name, "@dateval");
247  else
248  datafmt.name[0] = 0;
249  datafmt.namelen = CS_NULLTERM;
250  datafmt.datatype = CS_DATETIME_TYPE;
251  datafmt.status = CS_INPUTVALUE;
252  memset(&datevar, 0, sizeof(CS_DATEREC));
253  datevar.dateyear = 2003;
254  datevar.datemonth = 2;
255  datevar.datedmonth = 1;
256 
257  ret = ct_param(cmd, &datafmt, &datevar, 0, 0);
258  if (CS_SUCCEED != ret) {
259  fprintf(stderr, "ct_param(datetime) failed");
260  return 1;
261  }
262 
263  if (useNames)
264  strcpy(datafmt.name, "@floatval");
265  else
266  datafmt.name[0] = 0;
267  datafmt.namelen = CS_NULLTERM;
268  datafmt.datatype = CS_FLOAT_TYPE;
269  datafmt.status = CS_INPUTVALUE;
270 
271  ret = ct_param(cmd, &datafmt, &floatvar, 0, 0);
272  if (CS_SUCCEED != ret) {
273  fprintf(stderr, "ct_param(float) failed");
274  return 1;
275  }
276 
277  /*
278  * Send the command to the server
279  */
280  if (ct_send(cmd) != CS_SUCCEED) {
281  fprintf(stderr, "ct_send(CS_LANG_CMD) failed\n");
282  return 1;
283  }
284 
285  /*
286  * Process the results.
287  */
288  while ((ret = ct_results(cmd, &res_type)) == CS_SUCCEED) {
289  switch ((int) res_type) {
290 
291  case CS_CMD_SUCCEED:
292  case CS_CMD_DONE:
293  {
294  CS_INT rowsAffected=0;
295  ct_res_info(cmd, CS_ROW_COUNT, &rowsAffected, CS_UNUSED, NULL);
296  if (1 != rowsAffected)
297  fprintf(stderr, "insert touched %d rows instead of 1\n", rowsAffected);
298  else
299  rows_found = 1;
300  }
301  break;
302 
303  case CS_CMD_FAIL:
304  /*
305  * The server encountered an error while
306  * processing our command.
307  */
308  fprintf(stderr, "ct_results returned CS_CMD_FAIL.\n");
309  break;
310 
311  case CS_STATUS_RESULT:
312  /*
313  * The server encountered an error while
314  * processing our command.
315  */
316  fprintf(stderr, "ct_results returned CS_STATUS_RESULT.\n");
317  break;
318 
319  default:
320  /*
321  * We got something unexpected.
322  */
323  fprintf(stderr, "ct_results returned unexpected result type %d\n", res_type);
324  return 1;
325  }
326  }
327  if (ret != CS_END_RESULTS) {
328  fprintf(stderr, "ct_results returned unexpected result %d.\n", (int) ret);
329  exit(1);
330  }
331 
332  if (!rows_found) {
333  fprintf(stderr, "expected 1 rows inserted, inserted %d.\n", (int) rowsAffected);
334  exit(1);
335  }
336 
337  /* test row inserted */
338  ret = run_command(cmd, "if not exists(select * from #ctparam_lang where name = 'joe blow' and name2 is not null and age = 2) select 1");
339  if (ret != CS_SUCCEED) {
340  fprintf(stderr, "check row inserted failed\n");
341  exit(1);
342  }
343 
344  return 0;
345 }
346 
348 ex_clientmsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_CLIENTMSG * errmsg)
349 {
350  fprintf(stdout, "\nOpen Client Message:\n");
351  fprintf(stdout, "Message number: LAYER = (%d) ORIGIN = (%d) ", CS_LAYER(errmsg->msgnumber), CS_ORIGIN(errmsg->msgnumber));
352  fprintf(stdout, "SEVERITY = (%d) NUMBER = (%d)\n", CS_SEVERITY(errmsg->msgnumber), CS_NUMBER(errmsg->msgnumber));
353  fprintf(stdout, "Message String: %s\n", errmsg->msgstring);
354  if (errmsg->osstringlen > 0) {
355  fprintf(stdout, "Operating System Error: %s\n", errmsg->osstring);
356  }
357  fflush(stdout);
358 
359  return CS_SUCCEED;
360 }
361 
363 ex_servermsg_cb(CS_CONTEXT * context, CS_CONNECTION * connection, CS_SERVERMSG * srvmsg)
364 {
365  fprintf(stdout, "\nServer message:\n");
366  fprintf(stdout, "Message number: %d, Severity %d, ", srvmsg->msgnumber, srvmsg->severity);
367  fprintf(stdout, "State %d, Line %d\n", srvmsg->state, srvmsg->line);
368 
369  if (srvmsg->svrnlen > 0) {
370  fprintf(stdout, "Server '%s'\n", srvmsg->svrname);
371  }
372 
373  if (srvmsg->proclen > 0) {
374  fprintf(stdout, " Procedure '%s'\n", srvmsg->proc);
375  }
376 
377  fprintf(stdout, "Message String: %s\n", srvmsg->text);
378  fflush(stdout);
379 
380  return CS_SUCCEED;
381 }
#define CS_SERVERMSG_CB
Definition: cspublic.h:411
#define CS_CMD_DONE
Definition: cspublic.h:436
#define CS_CLIENTMSG_CB
Definition: cspublic.h:412
@ CS_PARENT_HANDLE
Definition: cspublic.h:227
#define CS_CMD_SUCCEED
Definition: cspublic.h:437
#define CS_INPUTVALUE
Definition: cspublic.h:293
#define CS_LANG_CMD
Definition: cspublic.h:441
#define CS_FLOAT_TYPE
Definition: cspublic.h:560
#define CS_NUMBER(x)
Definition: cspublic.h:66
#define CS_STATUS_RESULT
Definition: cspublic.h:542
#define CS_CHAR_TYPE
Definition: cspublic.h:550
#define CS_DATETIME_TYPE
Definition: cspublic.h:562
#define CS_INT_TYPE
Definition: cspublic.h:558
#define CS_LAYER(x)
Definition: cspublic.h:63
#define CS_SIZEOF(x)
Definition: cspublic.h:61
#define CS_UNUSED
Definition: cspublic.h:425
#define CS_ROW_COUNT
Definition: cspublic.h:469
#define CS_MONEY_TYPE
Definition: cspublic.h:564
#define CS_SET
Definition: cspublic.h:429
#define CS_SEVERITY(x)
Definition: cspublic.h:65
#define CS_SUCCEED
Definition: cspublic.h:40
#define CS_CMD_FAIL
Definition: cspublic.h:438
#define CS_NULLTERM
Definition: cspublic.h:422
#define CS_ORIGIN(x)
Definition: cspublic.h:64
#define CS_END_RESULTS
Definition: cspublic.h:56
CS_RETCODE cs_convert(CS_CONTEXT *ctx, CS_DATAFMT *srcfmt, CS_VOID *srcdata, CS_DATAFMT *destfmt, CS_VOID *destdata, CS_INT *resultlen)
Definition: cs.c:494
#define CS_GET
Definition: cspublic.h:428
Int4 CS_INT
Definition: cstypes.h:41
double CS_FLOAT
Definition: cstypes.h:51
void CS_VOID
Definition: cstypes.h:53
CS_INT CS_RETCODE
Definition: cstypes.h:63
char CS_CHAR
Definition: cstypes.h:48
struct _cs_money CS_MONEY
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_callback(CS_CONTEXT *ctx, CS_CONNECTION *con, CS_INT action, CS_INT type, CS_VOID *func)
Definition: ct.c:306
CS_RETCODE ct_cmd_props(CS_COMMAND *cmd, CS_INT action, CS_INT property, CS_VOID *buffer, CS_INT buflen, CS_INT *outlen)
Definition: ct.c:2661
CS_RETCODE ct_param(CS_COMMAND *cmd, CS_DATAFMT *datafmt, CS_VOID *data, CS_INT datalen, CS_SMALLINT indicator)
Definition: ct.c:3365
CS_RETCODE ct_send(CS_COMMAND *cmd)
Definition: ct.c:913
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
#define NULL
Definition: ncbistd.hpp:225
static int insert_test(CS_CONNECTION *conn, CS_COMMAND *cmd, int useNames)
Definition: lang_ct_param.c:95
int main(int argc, char *argv[])
Definition: lang_ct_param.c:29
CS_RETCODE ex_servermsg_cb(CS_CONTEXT *context, CS_CONNECTION *connection, CS_SERVERMSG *errmsg)
CS_RETCODE ex_clientmsg_cb(CS_CONTEXT *context, CS_CONNECTION *connection, CS_CLIENTMSG *errmsg)
static const char * query
Definition: lang_ct_param.c:19
exit(2)
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
true_type verbose
Definition: processing.cpp:890
CS_CHAR osstring[1024]
Definition: cstypes.h:191
CS_MSGNUM msgnumber
Definition: cstypes.h:187
CS_CHAR msgstring[1024]
Definition: cstypes.h:188
CS_INT osstringlen
Definition: cstypes.h:192
CS_LOCALE * locale
Definition: cstypes.h:133
CS_INT maxlength
Definition: cstypes.h:127
CS_INT precision
Definition: cstypes.h:129
CS_INT datatype
Definition: cstypes.h:125
CS_INT scale
Definition: cstypes.h:128
CS_INT namelen
Definition: cstypes.h:124
CS_CHAR name[132]
Definition: cstypes.h:123
CS_INT status
Definition: cstypes.h:130
CS_INT datemonth
Definition: cstypes.h:169
CS_INT datedmonth
Definition: cstypes.h:170
CS_INT dateyear
Definition: cstypes.h:168
CS_CHAR proc[132]
Definition: cstypes.h:207
CS_CHAR text[1024]
Definition: cstypes.h:203
CS_MSGNUM msgnumber
Definition: cstypes.h:200
CS_INT line
Definition: cstypes.h:209
CS_INT svrnlen
Definition: cstypes.h:206
CS_CHAR svrname[132]
Definition: cstypes.h:205
CS_INT severity
Definition: cstypes.h:202
CS_INT proclen
Definition: cstypes.h:208
CS_INT state
Definition: cstypes.h:201
Modified on Tue Apr 09 07:56:41 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:18 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
Modified on Tue Apr 16 20:08:38 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 17 13:08:44 2024 by modify_doxy.py rev. 669887