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

Go to the SVN repository for this file.

1 /*
2  * Purpose: Log in, create a table, insert a few rows, select them, and log out.
3  * Functions: dbbind dbcmd dbcolname dberrhandle dbisopt dbmsghandle dbnextrow dbnumcols dbopen dbresults dbsetlogintime dbsqlexec dbuse
4  */
5 
6 #include "common.h"
7 
8 #include <common/test_assert.h>
9 
10 int failed = 0;
11 
12 
13 int
14 main(int argc, char **argv)
15 {
16  const int rows_to_add = 50;
17  LOGINREC *login;
19  int i;
20  char teststr[1024];
21  DBINT testint, erc;
22 
24 
25  read_login_info(argc, argv);
26  if (argc > 1) {
27  argc -= optind;
28  argv += optind;
29  }
30 
31  fprintf(stdout, "Starting %s\n", argv[0]);
32 
33  /* Fortify_EnterScope(); */
34  dbinit();
35 
38 
39  fprintf(stdout, "About to logon as \"%s\"\n", USER);
40 
41  login = dblogin();
44  DBSETLAPP(login, "t0001");
45 
46  if (argc > 1) {
47  printf("server and login timeout overrides (%s and %s) detected\n", argv[0], argv[1]);
48  strcpy(SERVER, argv[0]);
49  i = atoi(argv[1]);
50  if (i) {
51  i = dbsetlogintime(i);
52  printf("dbsetlogintime returned %s.\n", (i == SUCCEED)? "SUCCEED" : "FAIL");
53  }
54  }
55 
56  fprintf(stdout, "About to open \"%s\"\n", SERVER);
57 
59  if (!dbproc) {
60  fprintf(stderr, "Unable to connect to %s\n", SERVER);
61  return 1;
62  }
64 
65  fprintf(stdout, "Using database \"%s\"\n", DATABASE);
66  if (strlen(DATABASE)) {
67  erc = dbuse(dbproc, DATABASE);
68  assert(erc == SUCCEED);
69  }
70 
71 #ifdef DBQUOTEDIDENT
72  fprintf(stdout, "QUOTED_IDENTIFIER is %s\n", (dbisopt(dbproc, DBQUOTEDIDENT, NULL))? "ON":"OFF");
73 #endif
74  sql_cmd(dbproc);
76  while (dbresults(dbproc) == SUCCEED) {
77  /* nop */
78  }
79 
80  for (i = 0; i < rows_to_add && sql_cmd(dbproc) == SUCCEED; i++) {
82  while (dbresults(dbproc) == SUCCEED) {
83  /* nop */
84  }
85  }
86 
87  sql_cmd(dbproc);
89 
90  if (dbresults(dbproc) != SUCCEED) {
91  failed = 1;
92  fprintf(stderr, "error: expected a result set, none returned.\n");
93  exit(1);
94  }
95 
96  for (i = 1; i <= dbnumcols(dbproc); i++)
97  printf("col %d is %s\n", i, dbcolname(dbproc, i));
98 
99  if (SUCCEED != dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint)) {
100  failed = 1;
101  fprintf(stderr, "Had problem with bind\n");
102  abort();
103  }
104  if (SUCCEED != dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr)) {
105  failed = 1;
106  fprintf(stderr, "Had problem with bind\n");
107  abort();
108  }
109 
110  for (i = 0; i < rows_to_add; i++) {
111  char expected[1024];
112 
113  sprintf(expected, "row %03d", i);
114 
115  memset(teststr, 'x', sizeof(teststr));
116  teststr[0] = 0;
117  teststr[sizeof(teststr) - 1] = 0;
118  if (REG_ROW != dbnextrow(dbproc)) {
119  failed = 1;
120  fprintf(stderr, "Failed. Expected a row\n");
121  exit(1);
122  }
123  if (testint != i) {
124  failed = 1;
125  fprintf(stderr, "Failed. Expected i to be %d, was %d\n", i, (int) testint);
126  abort();
127  }
128  if (0 != strncmp(teststr, expected, strlen(expected))) {
129  failed = 1;
130  fprintf(stdout, "Failed. Expected s to be |%s|, was |%s|\n", expected, teststr);
131  abort();
132  }
133  printf("Read a row of data -> %d |%s|\n", (int) testint, teststr);
134  }
135 
136  if (dbnextrow(dbproc) != NO_MORE_ROWS) {
137  failed = 1;
138  fprintf(stderr, "Was expecting no more rows\n");
139  exit(1);
140  }
141 
142  dbexit();
143 
144  fprintf(stdout, "%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
145  return failed ? 1 : 0;
146 }
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: t0001.c:13
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
int failed
Definition: t0001.c:10
static DBINT testint
Definition: t0006.c:11
static char teststr[1024]
Definition: t0006.c:10
STATUS dbnextrow(DBPROCESS *dbproc)
Definition: dblib.c:2076
#define USER
Definition: fastme_common.h:43
#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 dbsetlogintime(int seconds)
Set maximum seconds db-lib waits for a server response to a login attempt.
Definition: dblib.c:4137
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 dbloginfree(LOGINREC *login)
free the LOGINREC
Definition: dblib.c:751
DBBOOL dbisopt(DBPROCESS *dbproc, int option, const char param[])
Get value of an option.
Definition: dblib.c:6076
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 optind
#define assert(x)
Definition: srv_diag.hpp:58
#define DBQUOTEDIDENT
Definition: sybdb.h:435
#define dbopen(x, y)
Definition: sybdb.h:855
#define REG_ROW
Definition: sybdb.h:580
unsigned char BYTE
Definition: sybdb.h:334
#define NO_MORE_ROWS
Definition: sybdb.h:582
#define INTBIND
Definition: sybdb.h:554
#define SUCCEED
Definition: sybdb.h:585
#define STRINGBIND
Definition: sybdb.h:548
Int4 DBINT
Definition: sybdb.h:255
DBPROCESS * dbproc
Definition: t0013.c:18
Modified on Fri Jan 05 07:22:42 2024 by modify_doxy.py rev. 669887