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

Go to the SVN repository for this file.

1 /*
2  * Purpose: Test fetching 50 rows in two result sets with a 5000-row buffer.
3  * Functions: dbbind dbcmd dbnextrow dbnumcols dbresults dbsetopt dbsqlexec
4  */
5 
6 #include "common.h"
7 
8 #include <common/test_assert.h>
9 
10 static char teststr[1024];
11 static DBINT testint;
12 
13 static int failed = 0;
14 
15 
16 static void
18 {
19  int current = start - 1;
20 
21  while (REG_ROW == dbnextrow(dbproc)) {
22  char expected[1024];
23 
24  current++;
25  sprintf(expected, "row %04d", current);
26 
27  if (testint != current) {
28  fprintf(stderr, "Failed. Expected i to be %d, was %d\n", current, (int) testint);
29  abort();
30  }
31  if (0 != strncmp(teststr, expected, strlen(expected))) {
32  fprintf(stdout, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
33  abort();
34  }
35  printf("Read a row of data -> %d %s\n", (int) testint, teststr);
36  }
37 }
38 
39 
40 int
41 main(int argc, char **argv)
42 {
43  RETCODE rc;
44  const int rows_to_add = 50;
45  LOGINREC *login;
47  int i;
48 
50 
51  read_login_info(argc, argv);
52  fprintf(stdout, "Starting %s\n", argv[0]);
53 
54  /* Fortify_EnterScope(); */
55  dbinit();
56 
59 
60  fprintf(stdout, "About to logon\n");
61 
62  login = dblogin();
65  DBSETLAPP(login, "t0006");
66  DBSETLHOST(login, "ntbox.dntis.ro");
67 
68  fprintf(stdout, "About to open\n");
69 
71  if (strlen(DATABASE))
74 
75 #ifdef MICROSOFT_DBLIB
76  dbsetopt(dbproc, DBBUFFER, "5000");
77 #else
78  dbsetopt(dbproc, DBBUFFER, "5000", 0);
79 #endif
80 
81  fprintf(stdout, "creating table\n");
82  sql_cmd(dbproc);
84  while (dbresults(dbproc) != NO_MORE_RESULTS) {
85  /* nop */
86  }
87 
88  fprintf(stdout, "insert\n");
89  for (i = 1; i < rows_to_add; i++) {
90  sql_cmd(dbproc);
92  while (dbresults(dbproc) != NO_MORE_RESULTS) {
93  /* nop */
94  }
95  }
96 
97  fprintf(stdout, "first select\n");
98  if (SUCCEED != sql_cmd(dbproc)) {
99  fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
100  failed = 1;
101  }
102  if (SUCCEED != dbsqlexec(dbproc)) {
103  fprintf(stderr, "%s:%d: dbsqlexec failed\n", __FILE__, __LINE__);
104  failed = 1;
105  }
106 
107 
108  if (dbresults(dbproc) != SUCCEED) {
109  fprintf(stdout, "%s:%d: Was expecting a result set.", __FILE__, __LINE__);
110  failed = 1;
111  exit(1);
112  }
113 
114  for (i = 1; i <= dbnumcols(dbproc); i++)
115  printf("col %d is %s\n", i, dbcolname(dbproc, i));
116 
117  dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
118  dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
119 
120  get_results(dbproc, 1);
121 
122  testint = -1;
123  strcpy(teststr, "bogus");
124  fprintf(stdout, "second select\n");
125  sql_cmd(dbproc);
126  dbsqlexec(dbproc);
127 
128  if ((rc = dbresults(dbproc)) != SUCCEED) {
129  fprintf(stdout, "%s:%d: Was expecting a result set. (rc=%d)\n", __FILE__, __LINE__, rc);
130  failed = 1;
131  }
132 
133  if (!failed) {
134  dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
135  dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
136 
137  get_results(dbproc, 25);
138  }
139  dbexit();
140 
141  fprintf(stdout, "%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
142  return failed ? 1 : 0;
143 }
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: t0006.c:118
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 get_results(DBPROCESS *dbproc, int start)
Definition: t0006.c:17
static int failed
Definition: t0006.c:13
STATUS dbnextrow(DBPROCESS *dbproc)
Definition: dblib.c:2076
#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 DBSETLHOST(x, y)
Set the (client) host name in the login packet.
Definition: sybdb.h:1253
#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 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 dbsetopt(DBPROCESS *dbproc, int option, const char *char_param, int int_param)
Set db-lib or server option.
Definition: dblib.c:4383
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 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
exit(2)
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
#define dbopen(x, y)
Definition: sybdb.h:855
#define REG_ROW
Definition: sybdb.h:580
int RETCODE
Definition: sybdb.h:121
unsigned char BYTE
Definition: sybdb.h:334
#define INTBIND
Definition: sybdb.h:554
#define SUCCEED
Definition: sybdb.h:585
#define DBBUFFER
Definition: sybdb.h:414
#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 Thu Dec 07 10:06:48 2023 by modify_doxy.py rev. 669887