NCBI C++ ToolKit
connect2.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.

1 #include "common.h"
2 
3 #include <common/test_assert.h>
4 
5 /*
6  * Test setting current "catalog" before and after connection using
7  * either SQLConnect and SQLDriverConnect
8  */
9 
10 static int failed = 0;
11 
12 static void init_connect(void);
13 
14 static void
16 {
17  CHKAllocEnv(&odbc_env, "S");
19 }
20 
21 static void
23 {
25 }
26 
27 static void
28 driver_connect(const char *conn_str)
29 {
30  SQLTCHAR tmp[1024];
32 
34 }
35 
36 static void
37 check_dbname(const char *dbname)
38 {
40  SQLTCHAR out[512];
41  char sql[1024];
42 
43  len = sizeof(out);
45 
46  if (strcmp(C(out), dbname) != 0) {
47  fprintf(stderr, "Current database (%s) is not %s\n", C(out), dbname);
48  failed = 1;
49  }
50 
51  sprintf(sql, "IF DB_NAME() <> '%s' SELECT 1", dbname);
52  CHKAllocStmt(&odbc_stmt, "S");
56 }
57 
58 static void
59 set_dbname(const char *dbname)
60 {
62  (SQLINTEGER) strlen(dbname)*sizeof(SQLTCHAR), "SI");
63 }
64 
65 int
66 main(int argc, char *argv[])
67 {
68  char tmp[1024];
69 
71  exit(1);
72 
73  /* try setting db name before connect */
74  printf("SQLConnect before 1..\n");
75  init_connect();
76  set_dbname("master");
78  check_dbname("master");
79 
80  /* check change after connection */
81  printf("SQLConnect after..\n");
82  set_dbname("tempdb");
83  check_dbname("tempdb");
84 
85  printf("SQLConnect after not existing..\n");
86  strcpy(tmp, "IDontExist");
88  (SQLINTEGER) strlen(tmp), "E");
89  check_dbname("tempdb");
90 
92 
93  /* try setting db name before connect */
94  printf("SQLConnect before 2..\n");
95  init_connect();
96  set_dbname("tempdb");
98  check_dbname("tempdb");
100 
101  /* try connect string with using DSN */
102  printf("SQLDriverConnect before 1..\n");
103  sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;DATABASE=%s;", odbc_server, odbc_user, odbc_password, odbc_database);
104  init_connect();
105  set_dbname("master");
108  odbc_disconnect();
109 
110  /* try connect string with using DSN */
111  printf("SQLDriverConnect before 2..\n");
112  sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;", odbc_server, odbc_user, odbc_password);
113  init_connect();
114  set_dbname("tempdb");
116  check_dbname("tempdb");
117  odbc_disconnect();
118 
119  if (failed) {
120  printf("Some tests failed\n");
121  return 1;
122  }
123 
124  printf("Done.\n");
125  return 0;
126 }
int main(int argc, char *argv[])
Definition: connect2.c:66
static void check_dbname(const char *dbname)
Definition: connect2.c:37
static void set_dbname(const char *dbname)
Definition: connect2.c:59
static void normal_connect(void)
Definition: connect2.c:22
static void driver_connect(const char *conn_str)
Definition: connect2.c:28
static void init_connect(void)
Definition: connect2.c:15
static int failed
Definition: connect2.c:10
#define C(s)
Definition: common.h:231
#define CHKAllocStmt(a, res)
Definition: common.h:90
#define CHKSetConnectAttr(a, b, c, res)
Definition: common.h:152
#define CHKConnect(a, b, c, d, e, f, res)
Definition: common.h:106
#define CHKAllocConnect(a, res)
Definition: common.h:86
#define T(s)
Definition: common.h:230
#define ODBC_VECTOR_SIZE(x)
Definition: common.h:188
#define CHKAllocEnv(a, res)
Definition: common.h:88
#define CHKDriverConnect(a, b, c, d, e, f, g, res)
Definition: common.h:108
#define CHKGetConnectAttr(a, b, c, d, res)
Definition: common.h:126
std::ofstream out("events_result.xml")
main entry point for tests
#define NULL
Definition: ncbistd.hpp:225
char * dbname(DBPROCESS *dbproc)
Get name of current database.
Definition: dblib.c:6929
exit(2)
int len
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
HSTMT odbc_stmt
Definition: common.c:33
char odbc_database[512]
Definition: common.c:40
int odbc_disconnect(void)
Definition: common.c:290
int odbc_read_login_info(void)
Definition: common.c:88
HENV odbc_env
Definition: common.c:31
char odbc_server[512]
Definition: common.c:38
char odbc_password[512]
Definition: common.c:39
void odbc_check_no_row(const char *query)
Definition: common.c:883
HDBC odbc_conn
Definition: common.c:32
char odbc_user[512]
Definition: common.c:37
static char tmp[2048]
Definition: utf8.c:42
static char sql[1024]
Definition: putdata.c:19
#define SQL_NULL_HSTMT
Definition: sql.h:277
SQLRETURN SQLFreeStmt(SQLHSTMT StatementHandle, SQLUSMALLINT Option)
Definition: odbc.c:4237
#define SQL_DROP
Definition: sql.h:254
#define SQL_NTS
Definition: sql.h:49
#define SQL_ATTR_CURRENT_CATALOG
Definition: sqlext.h:126
#define SQL_DRIVER_NOPROMPT
Definition: sqlext.h:1785
void * SQLPOINTER
Definition: sqltypes.h:195
SQLCHAR SQLTCHAR
Definition: sqltypes.h:463
long SQLINTEGER
Definition: sqltypes.h:176
signed short int SQLSMALLINT
Definition: sqltypes.h:201
Modified on Tue Apr 09 07:56:49 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 10 07:32:11 2024 by modify_doxy.py rev. 669887
Modified on Thu Apr 11 15:04:26 2024 by modify_doxy.py rev. 669887
Modified on Fri Apr 12 17:16:25 2024 by modify_doxy.py rev. 669887