-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect.c
248 lines (195 loc) · 6.93 KB
/
connect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "driver.h"
#if defined(__IODBC__)
# include <iodbcinst.h>
#else
# include <odbcinst.h>
#endif
/**
* Connects to the data source.
* @phdbc must have its connection info filled out before being passed in.
*/
SQLRETURN comdb2_SQLConnect(dbc_t *phdbc)
{
cdb2_hndl_tp *sqlh;
conn_info_t *ci = &phdbc->ci;
int rc;
__debug("enters method");
if(ci->database[0] == '\0' || ci->cluster[0] == '\0') {
__debug("incomplete database information db %s cluster %s", ci->database, ci->cluster);
return DBC_ODBC_ERR(ERROR_NO_CONF);
}
__debug("cdb2_open(%s, %s)", ci->database, ci->cluster);
if((rc = cdb2_open(&sqlh, ci->database, ci->cluster, ci->flag)) != 0) {
return set_dbc_error(phdbc, ERROR_UNABLE_TO_CONN, NULL, rc);
}
phdbc->sqlh = sqlh;
phdbc->sqlh_status = SQLH_IDLE;
phdbc->connected = true;
__debug("leaves method");
return SQL_SUCCESS;
}
/**
* Reads connection information from odbc.ini.
* SQLGetPrivateProfileString, which is a builtin in the driver manager, is used to read configuration.
*/
static void complete_conn_info_by_dm(conn_info_t *ci)
{
char flag_in_ini[MAX_CONN_ATTR_LEN];
flag_in_ini[0] = '\0';
if(ci->database[0] == '\0')
SQLGetPrivateProfileString(ci->dsn, "DATABASE", "", ci->database, MAX_CONN_ATTR_LEN, ODBC_INI);
if(ci->cluster[0] == '\0')
SQLGetPrivateProfileString(ci->dsn, "CLUSTER", "", ci->cluster, MAX_CONN_ATTR_LEN, ODBC_INI);
if(ci->flag == 0) {
SQLGetPrivateProfileString(ci->dsn, "FLAG", "0", flag_in_ini, MAX_CONN_ATTR_LEN, ODBC_INI);
ci->flag = atoi(flag_in_ini);
}
}
SQLRETURN __SQLConnect(
SQLHDBC hdbc,
SQLCHAR *dsn,
SQLSMALLINT dsn_len,
SQLCHAR *uid,
SQLSMALLINT uid_len,
SQLCHAR *auth,
SQLSMALLINT auth_len)
{
dbc_t *phdbc = (dbc_t *)hdbc;
conn_info_t *ci;
__debug("enters method.");
__info("Connecting to %s.", dsn);
if(!phdbc)
return SQL_INVALID_HANDLE;
if(phdbc->connected)
return DBC_ODBC_ERR(ERROR_CONN_IN_USE);
ci = &phdbc->ci;
strncpy(ci->dsn, (char *)dsn, MAX_CONN_ATTR_LEN - 1);
complete_conn_info_by_dm(ci);
__debug("leaves method.");
return comdb2_SQLConnect(phdbc);
}
SQLRETURN SQL_API SQLConnect(
SQLHDBC hdbc,
SQLCHAR *dsn,
SQLSMALLINT dsn_len,
SQLCHAR *uid,
SQLSMALLINT uid_len,
SQLCHAR *auth,
SQLSMALLINT auth_len)
{
return __SQLConnect(hdbc, dsn, dsn_len, uid, uid_len, auth, auth_len);
}
/**
* Parses values from the connection string.
*/
static void get_conn_attrs(char *str, conn_info_t *ci)
{
char *pch, *equal, *key, *value, *tail_of_value, *last;
char flag[MAX_INT64_STR_LEN];
pch = strtok_r(str, ";", &last);
while(pch != NULL) {
if((equal = strchr(pch, '=')) != NULL) {
*equal = '\0';
key = pch;
value = equal + 1;
tail_of_value = &value[strlen(value) - 1];
for( ; *value == '{'; ++value) ;
for( ; *tail_of_value == '}'; --tail_of_value) ;
/* We cannot simply set *tail_of_value to NIL coz it may make strtok() terminate unexpectedly. */
if(!strcasecmp(key, "dsn"))
my_strncpy_in(ci->dsn, MAX_CONN_ATTR_LEN, value, tail_of_value - value + 1);
else if(!strcasecmp(key, "driver"))
my_strncpy_in(ci->driver, MAX_CONN_ATTR_LEN, value, tail_of_value - value + 1);
else if(!strcasecmp(key, "database"))
my_strncpy_in(ci->database, MAX_CONN_ATTR_LEN, value, tail_of_value - value + 1);
else if(!strcasecmp(key, "cluster"))
my_strncpy_in(ci->cluster, MAX_CONN_ATTR_LEN, value, tail_of_value - value + 1);
else if(!strcasecmp(key, "flag")) {
my_strncpy_in(flag, MAX_INT64_STR_LEN, value, tail_of_value - value + 1);
ci->flag = atoi(flag);
}
}
pch=strtok_r(NULL, ";", &last);
}
__info("dsn=%s; driver=%s; database=%s; cluster=%s; flag=%d.",
ci->dsn, ci->driver, ci->database, ci->cluster, ci->flag);
}
SQLRETURN __SQLDriverConnect(
SQLHDBC hdbc,
SQLHWND hwnd,
SQLCHAR *in_conn_str,
SQLSMALLINT in_conn_strlen,
SQLCHAR *out_conn_str,
SQLSMALLINT out_conn_str_max,
SQLSMALLINT *out_conn_strlen,
SQLUSMALLINT drv_completion)
{
dbc_t *phdbc = (dbc_t *)hdbc;
char _instr[MAX_CONN_INFO_LEN];
char _outstr[MAX_CONN_INFO_LEN];
conn_info_t *ci;
printf("my dbc is %p\n", hdbc);
__debug("enters method.");
(void)hwnd;
if(!hdbc)
return SQL_INVALID_HANDLE;
if(phdbc->connected)
return DBC_ODBC_ERR(ERROR_CONN_IN_USE);
ci = &phdbc->ci;
my_strncpy_in_fn(_instr, MAX_CONN_INFO_LEN, (char *)in_conn_str, in_conn_strlen);
get_conn_attrs(_instr, ci);
switch(drv_completion) {
case SQL_DRIVER_PROMPT:
case SQL_DRIVER_COMPLETE:
case SQL_DRIVER_COMPLETE_REQUIRED:
case SQL_DRIVER_NOPROMPT:
break;
}
if(ci->database[0] == '\0' || ci->cluster[0] == '\0')
/* Partial information provided, use .odbc.ini to complete connection info. */
complete_conn_info_by_dm(ci);
if(SQL_FAILED(comdb2_SQLConnect(phdbc)))
return SQL_ERROR;
snprintf(_outstr, MAX_CONN_INFO_LEN, "dsn=%s;driver=%s;database=%s;cluster=%s;flag=%d",
ci->dsn, ci->driver, ci->database, ci->cluster, ci->flag);
if(out_conn_str)
my_strncpy_out_fn((char *)out_conn_str, _outstr, out_conn_str_max);
if(out_conn_strlen)
*out_conn_strlen = (SQLSMALLINT)strlen(_outstr);
__debug("leaves method.");
return SQL_SUCCESS;
}
SQLRETURN SQL_API SQLDriverConnect(
SQLHDBC hdbc,
SQLHWND hwnd,
SQLCHAR *in_conn_str,
SQLSMALLINT in_conn_strlen,
SQLCHAR *out_conn_str,
SQLSMALLINT out_conn_str_max,
SQLSMALLINT *out_conn_strlen,
SQLUSMALLINT drv_completion)
{
return __SQLDriverConnect(hdbc, hwnd, in_conn_str, in_conn_strlen, out_conn_str, out_conn_str_max, out_conn_strlen, drv_completion);
}
SQLRETURN SQL_API SQLDisconnect(SQLHDBC hdbc)
{
dbc_t *phdbc = (dbc_t *)hdbc;
__debug("enters method.");
if(!hdbc)
return SQL_INVALID_HANDLE;
if(!phdbc->connected)
return SQL_SUCCESS;
if(phdbc->sqlh) {
if(phdbc->in_txn || phdbc->sqlh_status == SQLH_EXECUTING)
return DBC_ODBC_ERR(ERROR_INVALID_TRANS_STATE);
if(phdbc->sqlh_status == SQLH_FINISHED)
phdbc->sqlh_status = SQLH_IDLE;
cdb2_close(phdbc->sqlh);
}
phdbc->connected = false;
__debug("leaves method.");
return SQL_SUCCESS;
}
#ifdef __UNICODE__
#include "connectw.c"
#endif