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

Go to the SVN repository for this file.

1 /*
2  * Purpose: Test binding strings and ints, attempt 2nd query with results pending.
3  * Functions: dbbind dbcmd dbnextrow dbopen dbresults dbsqlexec
4  */
5 
6 #include "common.h"
7 
8 #include <common/test_assert.h>
9 
10 static void
11 create_tables(DBPROCESS * dbproc, int rows_to_add)
12 {
13  int i;
14 
15  fprintf(stdout, "creating table\n");
16  sql_cmd(dbproc);
18  while (dbresults(dbproc) != NO_MORE_RESULTS) {
19  /* nop */
20  }
21 
22  fprintf(stdout, "insert\n");
23  for (i = 1; i < rows_to_add; i++) {
24  sql_cmd(dbproc);
26  while (dbresults(dbproc) != NO_MORE_RESULTS) {
27  /* nop */
28  }
29  }
30 }
31 
32 
33 static int
35 {
36  int i;
37 
38  if (SUCCEED != sql_cmd(dbproc)) {
39  return 0;
40  }
41  if (SUCCEED != dbsqlexec(dbproc)) {
42  return 0;
43  }
44 
45  if (dbresults(dbproc) != SUCCEED)
46  return 0;
47 
48  for (i = 1; i <= dbnumcols(dbproc); i++)
49  printf("col %d is named \"%s\"\n", i, dbcolname(dbproc, i));
50 
51  return 1;
52 }
53 
54 int
55 main(int argc, char **argv)
56 {
57  LOGINREC *login;
59  int i;
60  char teststr[1024];
61  DBINT testint;
62  DBVARYBIN testvbin;
63  DBVARYCHAR testvstr;
64  int failed = 0;
65  int expected_error;
66 
68 
69  read_login_info(argc, argv);
70 
71  fprintf(stdout, "Starting %s\n", argv[0]);
72 
73  dbinit();
74 
77 
78  fprintf(stdout, "About to logon\n");
79 
80  login = dblogin();
83  DBSETLAPP(login, "t0007");
84 
85  fprintf(stdout, "About to open\n");
86 
88  if (strlen(DATABASE))
91 
92  create_tables(dbproc, 10);
93 
94  if (!start_query(dbproc)) {
95  fprintf(stderr, "%s:%d: start_query failed\n", __FILE__, __LINE__);
96  failed = 1;
97  }
98 
99  dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
100  dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
101 
102  for (i = 1; i <= 2; i++) {
103  char expected[1024];
104 
105  sprintf(expected, "row %07d", i);
106 
107  if (i % 5 == 0) {
108  dbclrbuf(dbproc, 5);
109  }
110 
111  testint = -1;
112  strcpy(teststr, "bogus");
113 
114  if (REG_ROW != dbnextrow(dbproc)) {
115  fprintf(stderr, "Failed. Expected a row\n");
116  abort();
117  }
118  if (testint != i) {
119  fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
120  abort();
121  }
122  if (0 != strncmp(teststr, expected, strlen(expected))) {
123  fprintf(stdout, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
124  abort();
125  }
126  printf("Read a row of data -> %d %s\n", (int) testint, teststr);
127  }
128 
129 
130  fprintf(stdout, "second select. Should fail.\n");
131 
132  expected_error = 20019;
133  dbsetuserdata(dbproc, (BYTE*) &expected_error);
134 
135  if (start_query(dbproc)) {
136  fprintf(stderr, "%s:%d: start_query should have failed but didn't\n", __FILE__, __LINE__);
137  failed = 1;
138  }
139 
140  dbcancel(dbproc);
141 
142  /*
143  * Test Binary binding
144  */
145  if (!start_query(dbproc)) {
146  fprintf(stderr, "%s:%d: start_query failed\n", __FILE__, __LINE__);
147  failed = 1;
148  }
149 
150  dbbind(dbproc, 1, VARYBINBIND, sizeof(testvbin), (BYTE *) &testvbin);
151  dbbind(dbproc, 2, VARYCHARBIND, sizeof(testvstr), (BYTE *) &testvstr);
152  dbbind(dbproc, 3, BINARYBIND, sizeof(testint), (BYTE *) &testint);
153 
154  for (i = 1; i <= 2; i++) {
155  char expected[1024];
156 
157  sprintf(expected, "row %07d ", i);
158 
159  testint = -1;
160  memset(&testvbin, '*', sizeof(testvbin));
161  memset(&testvstr, '*', sizeof(testvstr));
162 
163  if (REG_ROW != dbnextrow(dbproc)) {
164  fprintf(stderr, "Failed. Expected a row\n");
165  abort();
166  }
167  if (testint != i) {
168  fprintf(stderr, "Failed, line %d. Expected i to be %d, was %d (0x%x)\n", __LINE__, i, (int) testint, (int) testint);
169  abort();
170  }
171  if (testvbin.len != sizeof(testint)) {
172  fprintf(stderr, "Failed, line %d. Expected bin lenght to be %d, was %d\n", __LINE__, (int) sizeof(testint), (int) testvbin.len);
173  abort();
174  }
175  memcpy(&testint, testvbin.array, sizeof(testint));
176  if (testint != i) {
177  fprintf(stderr, "Failed, line %d. Expected i to be %d, was %d (0x%x)\n", __LINE__, i, (int) testint, (int) testint);
178  abort();
179  }
180  if (testvstr.len != strlen(expected) || 0 != strncmp(testvstr.str, expected, strlen(expected))) {
181  fprintf(stdout, "Failed, line %d. Expected s to be |%s|, was |%s|\n", __LINE__, expected, testvstr.str);
182  abort();
183  }
184  testvstr.str[testvstr.len] = 0;
185  printf("Read a row of data -> %d %s\n", (int) testint, testvstr.str);
186  }
187 
188 
189  dbexit();
190 
191  fprintf(stdout, "%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
192  return failed ? 1 : 0;
193 }
char PASSWORD[512]
Definition: common.c:31
char DATABASE[512]
Definition: common.c:32
CS_RETCODE read_login_info(void)
Definition: common.c:71
char SERVER[512]
Definition: common.c:30
int main(int argc, char **argv)
Definition: t0007.c:18
static TDSLOGIN * login
Definition: dataread.c:31
int syb_err_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
Definition: common.c:404
RETCODE sql_cmd(DBPROCESS *dbproc)
Definition: common.c:280
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 DBINT testint
Definition: t0006.c:11
static char teststr[1024]
Definition: t0006.c:10
static void create_tables(DBPROCESS *dbproc, int rows_to_add)
Definition: t0007.c:11
static int start_query(DBPROCESS *dbproc)
Definition: t0007.c:34
STATUS dbnextrow(DBPROCESS *dbproc)
Definition: dblib.c:2076
int failed
Definition: dbmorecmds.c:10
#define USER
Definition: fastme_common.h:43
#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
void dbclrbuf(DBPROCESS *dbproc, DBINT n)
Clear n rows from the row buffer.
Definition: dblib.c:2855
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 dbcancel(DBPROCESS *dbproc)
Cancel the current command batch.
Definition: dblib.c:3279
RETCODE dbuse(DBPROCESS *dbproc, const char *name)
Change current database.
Definition: dblib.c:1449
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 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
char * dbcolname(DBPROCESS *dbproc, int column)
Return name of a regular result column.
Definition: dblib.c:1884
int dbnumcols(DBPROCESS *dbproc)
Return number of regular columns in a result set.
Definition: dblib.c:1860
MHANDLEFUNC dbmsghandle(MHANDLEFUNC handler)
Set a message handler, for messages from the server.
Definition: dblib.c:5049
int i
int strncmp(const char *str1, const char *str2, size_t count)
Definition: odbc_utils.hpp:133
void abort()
static const char * expected[]
Definition: bcp.c:42
unsigned char array[256]
Definition: sybdb.h:273
DBSMALLINT len
Definition: sybdb.h:272
char str[256]
Definition: sybdb.h:267
DBSMALLINT len
Definition: sybdb.h:266
#define dbopen(x, y)
Definition: sybdb.h:855
#define VARYBINBIND
Definition: sybdb.h:551
#define REG_ROW
Definition: sybdb.h:580
unsigned char BYTE
Definition: sybdb.h:334
#define INTBIND
Definition: sybdb.h:554
#define SUCCEED
Definition: sybdb.h:585
#define BINARYBIND
Definition: sybdb.h:561
#define VARYCHARBIND
Definition: sybdb.h:550
#define STRINGBIND
Definition: sybdb.h:548
Int4 DBINT
Definition: sybdb.h:255
#define NO_MORE_RESULTS
Definition: sybdb.h:584
DBPROCESS * dbproc
Definition: t0013.c:18
Modified on Tue Feb 27 05:53:34 2024 by modify_doxy.py rev. 669887