4 * Description: This module contains only routines related to
5 * implementing SQLDriverConnect.
9 * API functions: SQLDriverConnect
11 * Comments: See "notice.txt" for copyright and license information.
23 #include "connection.h"
26 #include <sys/types.h>
27 #include <sys/socket.h>
37 #define stricmp(s1,s2) strcasecmp(s1,s2)
38 #define strnicmp(s1,s2,n) strncasecmp(s1,s2,n)
53 #include "dlg_specific.h"
56 void dconn_get_connect_attributes(UCHAR FAR *connect_string, ConnInfo *ci);
58 #ifndef UNIX /* should be something like ifdef WINDOWS */
59 BOOL FAR PASCAL dconn_FDriverConnectProc(HWND hdlg, UINT wMsg, WPARAM wParam, LPARAM lParam);
60 RETCODE dconn_DoDialog(HWND hwnd, ConnInfo *ci);
62 extern HINSTANCE NEAR s_hModule; /* Saved module handle. */
65 extern GLOBAL_VALUES globals;
68 RETCODE SQL_API SQLDriverConnect(
71 UCHAR FAR *szConnStrIn,
73 UCHAR FAR *szConnStrOut,
74 SWORD cbConnStrOutMax,
75 SWORD FAR *pcbConnStrOut,
76 UWORD fDriverCompletion)
78 char *func = "SQLDriverConnect";
79 ConnectionClass *conn = (ConnectionClass *) hdbc;
81 RETCODE dialog_result;
82 char connStrIn[MAX_CONNECT_STRING];
83 char connStrOut[MAX_CONNECT_STRING];
85 char password_required = FALSE;
88 CC_log_error(func, "", NULL);
89 return SQL_INVALID_HANDLE;
92 make_string(szConnStrIn, cbConnStrIn, connStrIn);
94 mylog("**** SQLDriverConnect: fDriverCompletion=%d, connStrIn='%s'\n", fDriverCompletion, connStrIn);
95 qlog("conn=%u, SQLDriverConnect( in)='%s', fDriverCompletion=%d\n", conn, connStrIn, fDriverCompletion);
97 ci = &(conn->connInfo);
99 // Parse the connect string and fill in conninfo for this hdbc.
100 dconn_get_connect_attributes(connStrIn, ci);
102 // If the ConnInfo in the hdbc is missing anything,
103 // this function will fill them in from the registry (assuming
104 // of course there is a DSN given -- if not, it does nothing!)
105 getDSNinfo(ci, CONN_DONT_OVERWRITE);
107 // Fill in any default parameters if they are not there.
111 ci->focus_password = password_required;
113 switch(fDriverCompletion) {
114 #ifndef UNIX /* again should be ifdef WINDOWS like */
115 case SQL_DRIVER_PROMPT:
116 dialog_result = dconn_DoDialog(hwnd, ci);
117 if(dialog_result != SQL_SUCCESS) {
118 return dialog_result;
122 case SQL_DRIVER_COMPLETE_REQUIRED:
126 case SQL_DRIVER_COMPLETE:
128 /* Password is not a required parameter. */
129 if( ci->username[0] == '\0' ||
130 ci->server[0] == '\0' ||
131 ci->database[0] == '\0' ||
132 ci->port[0] == '\0' ||
135 dialog_result = dconn_DoDialog(hwnd, ci);
136 if(dialog_result != SQL_SUCCESS) {
137 return dialog_result;
142 case SQL_DRIVER_PROMPT:
143 case SQL_DRIVER_COMPLETE:
144 case SQL_DRIVER_COMPLETE_REQUIRED:
146 case SQL_DRIVER_NOPROMPT:
150 /* Password is not a required parameter unless authentication asks for it.
151 For now, I think its better to just let the application ask over and over until
152 a password is entered (the user can always hit Cancel to get out)
154 if( ci->username[0] == '\0' ||
155 ci->server[0] == '\0' ||
156 ci->database[0] == '\0' ||
157 ci->port[0] == '\0') {
158 // (password_required && ci->password[0] == '\0'))
160 return SQL_NO_DATA_FOUND;
165 /* Return the completed string to the caller.
166 Only construct the connect string if a dialog was put up,
167 otherwise, just copy the connection input string to the output.
169 makeConnectString(connStrOut, ci);
172 *pcbConnStrOut = strlen(connStrOut);
174 strncpy_null(szConnStrOut, connStrOut, cbConnStrOutMax);
177 mylog("szConnStrOut = '%s'\n", szConnStrOut);
178 qlog("conn=%u, SQLDriverConnect(out)='%s'\n", conn, szConnStrOut);
180 // do the actual connect
181 retval = CC_connect(conn, password_required);
182 if (retval < 0) { /* need a password */
183 if (fDriverCompletion == SQL_DRIVER_NOPROMPT) {
184 CC_log_error(func, "Need password but Driver_NoPrompt", conn);
185 return SQL_ERROR; /* need a password but not allowed to prompt so error */
189 password_required = TRUE;
192 return SQL_ERROR; /* until a better solution is found. */
196 else if (retval == 0) {
197 // error msg filled in above
198 CC_log_error(func, "Error from CC_Connect", conn);
202 mylog("SQLDRiverConnect: returning success\n");
206 #ifndef UNIX /* yet another candidate for ifdef WINDOWS */
207 RETCODE dconn_DoDialog(HWND hwnd, ConnInfo *ci)
211 mylog("dconn_DoDialog: ci = %u\n", ci);
214 dialog_result = DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_CONFIG),
215 hwnd, dconn_FDriverConnectProc, (LPARAM) ci);
216 if(!dialog_result || (dialog_result == -1)) {
217 return SQL_NO_DATA_FOUND;
227 BOOL FAR PASCAL dconn_FDriverConnectProc(
237 ci = (ConnInfo *) lParam;
239 /* Change the caption for the setup dialog */
240 SetWindowText(hdlg, "PostgreSQL Connection");
242 SetWindowText(GetDlgItem(hdlg, IDC_DATASOURCE), "Connection");
244 /* Hide the DSN and description fields */
245 ShowWindow(GetDlgItem(hdlg, IDC_DSNAMETEXT), SW_HIDE);
246 ShowWindow(GetDlgItem(hdlg, IDC_DSNAME), SW_HIDE);
247 ShowWindow(GetDlgItem(hdlg, IDC_DESCTEXT), SW_HIDE);
248 ShowWindow(GetDlgItem(hdlg, IDC_DESC), SW_HIDE);
250 SetWindowLong(hdlg, DWL_USER, lParam);// Save the ConnInfo for the "OK"
252 SetDlgStuff(hdlg, ci);
254 if (ci->database[0] == '\0')
255 ; /* default focus */
256 else if (ci->server[0] == '\0')
257 SetFocus(GetDlgItem(hdlg, IDC_SERVER));
258 else if (ci->port[0] == '\0')
259 SetFocus(GetDlgItem(hdlg, IDC_PORT));
260 else if (ci->username[0] == '\0')
261 SetFocus(GetDlgItem(hdlg, IDC_USER));
262 else if (ci->focus_password)
263 SetFocus(GetDlgItem(hdlg, IDC_PASSWORD));
269 switch (GET_WM_COMMAND_ID(wParam, lParam)) {
272 ci = (ConnInfo *) GetWindowLong(hdlg, DWL_USER);
274 GetDlgStuff(hdlg, ci);
278 EndDialog(hdlg, GET_WM_COMMAND_ID(wParam, lParam) == IDOK);
283 DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_OPTIONS_DRV),
284 hdlg, driver_optionsProc, (LPARAM) NULL);
291 ci = (ConnInfo *) GetWindowLong(hdlg, DWL_USER);
292 DialogBoxParam(s_hModule, MAKEINTRESOURCE(DLG_OPTIONS_DS),
293 hdlg, ds_optionsProc, (LPARAM) ci);
304 void dconn_get_connect_attributes(UCHAR FAR *connect_string, ConnInfo *ci)
306 char *our_connect_string;
307 char *pair, *attribute, *value, *equals;
310 memset(ci, 0, sizeof(ConnInfo));
312 our_connect_string = strdup(connect_string);
313 strtok_arg = our_connect_string;
315 mylog("our_connect_string = '%s'\n", our_connect_string);
318 pair = strtok(strtok_arg, ";");
326 equals = strchr(pair, '=');
331 attribute = pair; // ex. DSN
332 value = equals + 1; // ex. 'CEO co1'
334 mylog("attribute = '%s', value = '%s'\n", attribute, value);
336 if( !attribute || !value)
339 // Copy the appropriate value to the conninfo
340 copyAttributes(ci, attribute, value);
345 free(our_connect_string);