NCBI C++ ToolKit
string_bind.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /*
2  * Purpose: Test different string binding combinations.
3  * Functions: dbbind
4  */
5 
6 #include "common.h"
7 
8 #include <freetds/test_assert.h>
9 
10 static DBPROCESS *dbproc = NULL;
11 static int bind_len = -1;
12 static int expected_error = 0;
13 static const char *select_cmd = "select 'foo '";
14 
15 static void
16 test_row(int vartype, const char *vartype_name, const char *expected, int line)
17 {
18  char str[11];
19  size_t i;
20 
21  printf("%d: row type %s bind len %d\n", line, vartype_name, bind_len);
22 
23  if (dbcmd(dbproc, select_cmd) != SUCCEED) {
24  fprintf(stderr, "error: dbcmd\n");
25  exit(1);
26  }
28 
29  if (dbresults(dbproc) != SUCCEED) {
30  fprintf(stderr, "error: expected a result set, none returned.\n");
31  exit(1);
32  }
33 
34  memset(str, '$', sizeof(str));
35  str[sizeof(str) - 1] = 0;
36  if (dbbind(dbproc, 1, vartype, bind_len, (BYTE *) str) != SUCCEED) {
37  fprintf(stderr, "Had problem with bind\n");
38  exit(1);
39  }
40  if (dbnextrow(dbproc) != REG_ROW) {
41  fprintf(stderr, "Failed. Expected a row\n");
42  exit(1);
43  }
44 
45  assert(str[sizeof(str) - 1] == 0);
46  if (vartype == CHARBIND) {
47  /* not terminated space padded */
48  char *p = strchr(str, '$');
49  i = p ? p - str : sizeof(str);
50  } else {
51  /* terminated */
52  char *p = strchr(str, 0);
53  i = p - str + 1;
54  }
55  for (; i < sizeof(str)-1; ++i) {
56  assert(str[i] == '$');
57  str[i] = 0;
58  }
59 
60  printf("str '%s'\n", str);
61  if (strcmp(str, expected) != 0) {
62  fprintf(stderr, "Expected '%s' string\n", expected);
63  exit(1);
64  }
65 
66  if (dbnextrow(dbproc) != NO_MORE_ROWS) {
67  fprintf(stderr, "Was expecting no more rows\n");
68  exit(1);
69  }
70  assert(expected_error == 0);
71 }
72 
73 #define row(bind, expected) test_row(bind, #bind, expected, __LINE__)
74 
75 int
76 main(int argc, char **argv)
77 {
78  LOGINREC *login;
79 
81 
82  read_login_info(argc, argv);
83 
84  printf("Starting %s\n", argv[0]);
85 
86  dbinit();
87 
90 
91  printf("About to logon as \"%s\"\n", USER);
92 
93  login = dblogin();
96  DBSETLAPP(login, "spid");
97 
98  printf("About to open \"%s\"\n", SERVER);
99 
101  if (!dbproc) {
102  fprintf(stderr, "Unable to connect to %s\n", SERVER);
103  return 1;
104  }
106 
108 
109  row(NTBSTRINGBIND, "foo");
110  row(STRINGBIND, "foo ");
111  row(CHARBIND, "foo ");
112 
113  bind_len = 4;
114  row(NTBSTRINGBIND, "foo");
116  row(STRINGBIND, "foo");
118  row(CHARBIND, "foo ");
119 
120  bind_len = 5;
121  row(NTBSTRINGBIND, "foo");
123  row(STRINGBIND, "foo ");
124  row(CHARBIND, "foo ");
125 
126  bind_len = 8;
127  row(NTBSTRINGBIND, "foo");
128  row(STRINGBIND, "foo ");
129  row(CHARBIND, "foo ");
130 
131  bind_len = 3;
133  row(NTBSTRINGBIND, "fo");
134 
135  select_cmd = "select 123";
136 
137  bind_len = -1;
138  row(NTBSTRINGBIND, "123");
139  row(STRINGBIND, "123");
140  row(CHARBIND, "123");
141 
142  bind_len = 4;
143  row(NTBSTRINGBIND, "123");
144  row(STRINGBIND, "123");
145  row(CHARBIND, "123 ");
146 
147  bind_len = 6;
148  row(NTBSTRINGBIND, "123");
149  row(STRINGBIND, "123 ");
150  row(CHARBIND, "123 ");
151 
152  bind_len = 3;
154  row(NTBSTRINGBIND, "12");
156  row(STRINGBIND, "12");
157  row(CHARBIND, "123");
158 
159  bind_len = 2;
161  row(CHARBIND, "12");
162 
163  dbclose(dbproc);
164 
165  dbexit();
166  return 0;
167 }
#define USER
Definition: fastme_common.h:43
char PASSWORD[512]
Definition: common.c:31
CS_RETCODE read_login_info(void)
Definition: common.c:71
char SERVER[512]
Definition: common.c:30
int syb_err_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
Definition: common.c:404
int syb_msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate, int severity, char *msgtext, char *srvname, char *procname, int line)
Definition: common.c:334
void set_malloc_options(void)
Definition: common.c:47
static const char * expected[]
Definition: bcp.c:42
static const char * str(char *buf, int n)
Definition: stats.c:84
#define dbopen(x, y)
Definition: sybdb.h:855
#define REG_ROW
Definition: sybdb.h:580
#define CHARBIND
Definition: sybdb.h:547
#define NTBSTRINGBIND
Definition: sybdb.h:549
unsigned char BYTE
Definition: sybdb.h:334
#define NO_MORE_ROWS
Definition: sybdb.h:582
#define SYBECOFL
Definition: sybdb.h:977
#define SUCCEED
Definition: sybdb.h:585
#define STRINGBIND
Definition: sybdb.h:548
static TDSLOGIN * login
Definition: dataread.c:31
#define dbnextrow
#define NULL
Definition: ncbistd.hpp:225
#define DBSETLPWD(x, y)
Set the password in the login packet.
Definition: sybdb.h:1259
#define DBSETLAPP(x, y)
Set the (client) application name in the login packet.
Definition: sybdb.h:1266
#define DBSETLUSER(x, y)
Set the username in the login packet.
Definition: sybdb.h:1256
LOGINREC * dblogin(void)
Allocate a LOGINREC structure.
Definition: dblib.c:719
RETCODE dbresults(DBPROCESS *dbproc)
Set up query results.
Definition: dblib.c:1706
RETCODE dbinit(void)
Initialize db-lib.
Definition: dblib.c:674
RETCODE dbbind(DBPROCESS *dbproc, int column, int vartype, DBINT varlen, BYTE *varaddr)
Tie a host variable to a resultset column.
Definition: dblib.c:2645
RETCODE dbcmd(DBPROCESS *dbproc, const char cmdstring[])
Append SQL to the command buffer.
Definition: dblib.c:1377
RETCODE dbsqlexec(DBPROCESS *dbproc)
send the SQL command to the server and wait for an answer.
Definition: dblib.c:1423
EHANDLEFUNC dberrhandle(EHANDLEFUNC handler)
Set an error handler, for messages from db-lib.
Definition: dblib.c:5030
void dbsetuserdata(DBPROCESS *dbproc, BYTE *ptr)
Associate client-allocated (and defined) data with a DBPROCESS.
Definition: dblib.c:5849
void dbloginfree(LOGINREC *login)
free the LOGINREC
Definition: dblib.c:751
void dbexit()
Close server connections and free all related structures.
Definition: dblib.c:1559
MHANDLEFUNC dbmsghandle(MHANDLEFUNC handler)
Set a message handler, for messages from the server.
Definition: dblib.c:5049
void dbclose(DBPROCESS *dbproc)
Close a connection to the server and free associated resources.
Definition: dblib.c:1492
exit(2)
int i
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
#define assert(x)
Definition: srv_diag.hpp:58
static DBPROCESS * dbproc
Definition: string_bind.c:10
#define row(bind, expected)
Definition: string_bind.c:73
int main(int argc, char **argv)
Definition: string_bind.c:76
static int expected_error
Definition: string_bind.c:12
static int bind_len
Definition: string_bind.c:11
static void test_row(int vartype, const char *vartype_name, const char *expected, int line)
Definition: string_bind.c:16
static const char * select_cmd
Definition: string_bind.c:13
Modified on Fri Sep 20 14:58:21 2024 by modify_doxy.py rev. 669887