Merge commit 'origin/master'
[versaplex.git] / vxodbc / dlg_specific.cc
blob24eb09a4421312458361f081dd94a3aa31db9ac8
1 /*
2 * Description: This module contains any specific code for handling
3 * dialog boxes such as driver/datasource options. Both the
4 * ConfigDSN() and the SQLDriverConnect() functions use
5 * functions in this module. If you were to add a new option
6 * to any dialog box, you would most likely only have to change
7 * things in here rather than in 2 separate places as before.
8 */
9 /* Multibyte support Eiji Tokuya 2001-03-15 */
11 #include <ctype.h>
12 #include "dlg_specific.h"
14 #include "convert.h"
16 #include "multibyte.h"
17 #include "pgapifunc.h"
19 #include "wvlogger.h"
21 extern GLOBAL_VALUES globals;
23 void makeConnectString(char *connect_string, const ConnInfo * ci, UWORD len)
25 char got_dsn = (ci->dsn[0] != '\0');
26 ssize_t hlen, nlen, olen;
27 /*BOOL abbrev = (len <= 400); */
28 BOOL abbrev = (len < 1024) || 0 < ci->force_abbrev_connstr;
30 inolog("force_abbrev=%d abbrev=%d\n", ci->force_abbrev_connstr,
31 abbrev);
32 /* fundamental info */
33 nlen = MAX_CONNECT_STRING;
34 olen =
35 snprintf(connect_string, nlen,
36 "%s=%s;DATABASE=%s;SERVER=%s;PORT=%s;UID=%s;PWD=%s",
37 got_dsn ? "DSN" : "DRIVER",
38 got_dsn ? ci->dsn : ci->drivername, ci->database,
39 ci->server, ci->port, ci->username, ci->password);
40 if (olen < 0 || olen >= nlen)
42 connect_string[0] = '\0';
43 return;
46 /* extra info */
47 hlen = strlen(connect_string);
48 nlen = MAX_CONNECT_STRING - hlen;
49 inolog("hlen=%d", hlen);
50 if (!abbrev)
52 char protocol_and[16];
54 if (ci->rollback_on_error >= 0)
55 snprintf(protocol_and, sizeof(protocol_and), "%s-%d",
56 ci->protocol, ci->rollback_on_error);
57 else
58 strncpy(protocol_and, ci->protocol, sizeof(protocol_and));
59 olen = snprintf(&connect_string[hlen], nlen, ";"
60 INI_READONLY "=%s;", ci->onlyread
63 /* Abbreviation is needed ? */
64 if (abbrev || olen >= nlen || olen < 0)
66 hlen = strlen(connect_string);
67 nlen = MAX_CONNECT_STRING - hlen;
68 olen = snprintf(&connect_string[hlen], nlen, ";");
69 if (olen < nlen
70 && (PROTOCOL_74(ci) || ci->rollback_on_error >= 0))
72 hlen = strlen(connect_string);
73 nlen = MAX_CONNECT_STRING - hlen;
76 if (olen < 0 || olen >= nlen) /* failed */
77 connect_string[0] = '\0';
80 BOOL
81 copyAttributes(ConnInfo * ci, const char *attribute, const char *value)
83 CSTR func = "copyAttributes";
84 BOOL found = TRUE;
86 if (stricmp(attribute, "DSN") == 0)
87 strcpy(ci->dsn, value);
89 else if (stricmp(attribute, "driver") == 0)
90 strcpy(ci->drivername, value);
92 else if (stricmp(attribute, INI_DATABASE) == 0)
93 strcpy(ci->database, value);
95 else if (stricmp(attribute, INI_SERVER) == 0
96 || stricmp(attribute, SPEC_SERVER) == 0)
97 strcpy(ci->server, value);
99 else if (stricmp(attribute, INI_USER) == 0
100 || stricmp(attribute, INI_UID) == 0)
101 strcpy(ci->username, value);
103 else if (stricmp(attribute, INI_PASSWORD) == 0
104 || stricmp(attribute, "pwd") == 0)
105 strcpy(ci->password, value);
107 else if (stricmp(attribute, INI_PORT) == 0)
108 strcpy(ci->port, value);
110 else if (stricmp(attribute, INI_READONLY) == 0)
111 strcpy(ci->onlyread, value);
113 else if (stricmp(attribute, INI_DBUS) == 0)
114 strcpy(ci->dbus_moniker, value);
116 else
117 found = FALSE;
119 mylog
120 ("%s: DSN='%s',server='%s',dbase='%s',user='%s',passwd='%s',port='%s'"
121 ",onlyread='%s',protocol='%s',conn_settings='%s',disallow_premature=%d,"
122 "dbus_moniker='%s')\n",
123 func, ci->dsn, ci->server, ci->database, ci->username,
124 ci->password ? "xxxxx" : "", ci->port, ci->onlyread,
125 ci->protocol, ci->conn_settings, ci->disallow_premature,
126 ci->dbus_moniker);
128 return found;
131 BOOL copyCommonAttributes(ConnInfo * ci, const char *attribute,
132 const char *value)
134 BOOL found = FALSE;
135 return found;
139 void getDSNdefaults(ConnInfo * ci)
141 mylog("calling getDSNdefaults\n");
143 if (ci->port[0] == '\0')
144 strcpy(ci->port, DEFAULT_PORT);
146 if (ci->onlyread[0] == '\0')
147 sprintf(ci->onlyread, "%d", globals.onlyread);
149 if (ci->protocol[0] == '\0')
150 strcpy(ci->protocol, DEFAULT_PROTOCOL);
152 if (ci->fake_oid_index[0] == '\0')
153 sprintf(ci->fake_oid_index, "%d", DEFAULT_FAKEOIDINDEX);
155 if (ci->show_oid_column[0] == '\0')
156 sprintf(ci->show_oid_column, "%d", DEFAULT_SHOWOIDCOLUMN);
158 if (ci->show_system_tables[0] == '\0')
159 sprintf(ci->show_system_tables, "%d", DEFAULT_SHOWSYSTEMTABLES);
161 if (ci->row_versioning[0] == '\0')
162 sprintf(ci->row_versioning, "%d", DEFAULT_ROWVERSIONING);
164 if (ci->disallow_premature < 0)
165 ci->disallow_premature = DEFAULT_DISALLOWPREMATURE;
166 if (ci->allow_keyset < 0)
167 ci->allow_keyset = DEFAULT_UPDATABLECURSORS;
168 if (ci->lf_conversion < 0)
169 ci->lf_conversion = DEFAULT_LFCONVERSION;
170 if (ci->true_is_minus1 < 0)
171 ci->true_is_minus1 = DEFAULT_TRUEISMINUS1;
172 if (ci->int8_as < -100)
173 ci->int8_as = DEFAULT_INT8AS;
174 if (ci->bytea_as_longvarbinary < 0)
175 ci->bytea_as_longvarbinary = DEFAULT_BYTEAASLONGVARBINARY;
176 if (ci->use_server_side_prepare < 0)
177 ci->use_server_side_prepare = DEFAULT_USESERVERSIDEPREPARE;
178 if (ci->lower_case_identifier < 0)
179 ci->lower_case_identifier = DEFAULT_LOWERCASEIDENTIFIER;
180 if (ci->sslmode[0] == '\0')
181 strcpy(ci->sslmode, DEFAULT_SSLMODE);
182 if (ci->force_abbrev_connstr < 0)
183 ci->force_abbrev_connstr = 0;
184 if (ci->fake_mss < 0)
185 ci->fake_mss = 0;
186 if (ci->bde_environment < 0)
187 ci->bde_environment = 0;
188 if (ci->cvt_null_date_string < 0)
189 ci->cvt_null_date_string = 0;
193 getDriverNameFromDSN(const char *dsn, char *driver_name, int namelen)
195 return SQLGetPrivateProfileString(ODBC_DATASOURCES, dsn, "",
196 driver_name, namelen, ODBC_INI);
199 void getDSNinfo(ConnInfo * ci, char overwrite)
201 CSTR func = "getDSNinfo";
202 char *DSN = ci->dsn;
205 * If a driver keyword was present, then dont use a DSN and return.
206 * If DSN is null and no driver, then use the default datasource.
208 mylog("%s: DSN=%s overwrite=%d\n", func, DSN, overwrite);
209 if (DSN[0] == '\0')
211 if (ci->drivername[0] != '\0')
212 return;
213 else
214 strncpy_null(DSN, INI_DSN, sizeof(ci->dsn));
217 /* brute-force chop off trailing blanks... */
218 while (*(DSN + strlen(DSN) - 1) == ' ')
219 *(DSN + strlen(DSN) - 1) = '\0';
221 if (ci->drivername[0] == '\0' || overwrite)
223 getDriverNameFromDSN(DSN, ci->drivername, sizeof(ci->drivername));
224 if (ci->drivername[0] && stricmp(ci->drivername, DBMS_NAME))
225 getCommonDefaults(ci->drivername, ODBCINST_INI, ci);
228 /* Proceed with getting info for the given DSN. */
230 if (ci->server[0] == '\0' || overwrite)
231 SQLGetPrivateProfileString(DSN, INI_SERVER, "", ci->server,
232 sizeof(ci->server), ODBC_INI);
234 if (ci->database[0] == '\0' || overwrite)
235 SQLGetPrivateProfileString(DSN, INI_DATABASE, "", ci->database,
236 sizeof(ci->database), ODBC_INI);
238 if (ci->username[0] == '\0' || overwrite)
239 SQLGetPrivateProfileString(DSN, INI_USER, "", ci->username,
240 sizeof(ci->username), ODBC_INI);
242 if (ci->password[0] == '\0' || overwrite)
243 SQLGetPrivateProfileString(DSN, INI_PASSWORD, "", ci->password,
244 sizeof(ci->password), ODBC_INI);
246 if (ci->port[0] == '\0' || overwrite)
247 SQLGetPrivateProfileString(DSN, INI_PORT, "", ci->port,
248 sizeof(ci->port), ODBC_INI);
250 if (ci->onlyread[0] == '\0' || overwrite)
251 SQLGetPrivateProfileString(DSN, INI_READONLY, "", ci->onlyread,
252 sizeof(ci->onlyread), ODBC_INI);
254 if (ci->dbus_moniker[0] == '\0' || overwrite)
255 SQLGetPrivateProfileString(DSN, INI_DBUS, "dbus:session",
256 ci->dbus_moniker, sizeof(ci->dbus_moniker), ODBC_INI);
258 char llbuf[2] = {0, 0};
259 if (!log_level || overwrite)
260 SQLGetPrivateProfileString(DSN, "LogLevel", "4", llbuf,
261 sizeof(llbuf), ODBC_INI);
262 log_level = atoi(llbuf);
263 if (!wvlog_isset() || overwrite) {
264 struct pstring log_moniker = wvlog_get_moniker();
265 SQLGetPrivateProfileString(DSN, "LogMoniker", "", log_moniker.string,
266 log_moniker.length, ODBC_INI);
268 wvlog_open();
270 /* Allow override of odbcinst.ini parameters here */
271 getCommonDefaults(DSN, ODBC_INI, ci);
273 qlog("DSN info: DSN='%s',server='%s',port='%s',dbase='%s',user='%s'"
274 ",passwd='%s',dbus='%s',ini='%s'\n",
275 DSN, ci->server, ci->port, ci->database, ci->username,
276 ci->password ? "xxxxx" : "", ci->dbus_moniker, ODBC_INI);
277 qlog(" onlyread='%s',protocol='%s',showoid='%s',fakeoidindex='%s'"
278 ",showsystable='%s'\n",
279 ci->onlyread, ci->protocol, ci->show_oid_column, ci->fake_oid_index,
280 ci->show_system_tables);
282 if (get_qlog())
284 UCHAR *enc = check_client_encoding((const UCHAR *)ci->conn_settings);
286 qlog(" conn_settings='%s',conn_encoding='%s'\n",
287 ci->conn_settings, enc ? enc : (UCHAR*)"(null)");
288 if (enc)
289 free(enc);
290 qlog(" translation_dll='%s',translation_option='%s'\n",
291 ci->translation_dll, ci->translation_option);
296 * This function writes any global parameters (that can be manipulated)
297 * to the ODBCINST.INI portion of the registry
299 int writeDriverCommoninfo(const char *fileName, const char *sectionName,
300 const GLOBAL_VALUES * comval)
302 char tmp[128];
303 int errc = 0;
305 if (ODBCINST_INI == fileName && NULL == sectionName)
306 sectionName = DBMS_NAME;
308 if (stricmp(ODBCINST_INI, fileName) == 0)
309 return errc;
312 * Never update the onlyread from this module.
314 if (stricmp(ODBCINST_INI, fileName) == 0)
316 sprintf(tmp, "%d", comval->onlyread);
317 SQLWritePrivateProfileString(sectionName, INI_READONLY, tmp,
318 fileName);
321 return errc;
324 /* This is for datasource based options only */
325 void writeDSNinfo(const ConnInfo * ci)
327 const char *DSN = ci->dsn;
329 SQLWritePrivateProfileString(DSN,
330 INI_DATABASE, ci->database, ODBC_INI);
332 SQLWritePrivateProfileString(DSN, INI_SERVER, ci->server, ODBC_INI);
334 SQLWritePrivateProfileString(DSN, INI_PORT, ci->port, ODBC_INI);
336 SQLWritePrivateProfileString(DSN, INI_USER, ci->username, ODBC_INI);
337 SQLWritePrivateProfileString(DSN, INI_UID, ci->username, ODBC_INI);
339 SQLWritePrivateProfileString(DSN,
340 INI_PASSWORD, ci->password, ODBC_INI);
342 SQLWritePrivateProfileString(DSN,
343 INI_READONLY, ci->onlyread, ODBC_INI);
348 * This function reads the ODBCINST.INI portion of
349 * the registry and gets any driver defaults.
351 void
352 getCommonDefaults(const char *section, const char *filename,
353 ConnInfo * ci)
355 char temp[256];
356 GLOBAL_VALUES *comval;
357 BOOL inst_position = (stricmp(filename, ODBCINST_INI) == 0);
359 if (ci)
360 comval = &(ci->drivers);
361 else
362 comval = &globals;
363 if (!ci)
364 logs_on_off(0, 0, 0);
366 /* Dont allow override of an override! */
367 if (inst_position)
369 /* Default state for future DSN's Readonly attribute */
370 SQLGetPrivateProfileString(section, INI_READONLY, "",
371 temp, sizeof(temp), filename);
372 if (temp[0])
373 comval->onlyread = atoi(temp);
374 else
375 comval->onlyread = DEFAULT_READONLY;