提交 6c0c319b 编写于 作者: F freemine

implement ConfigDSN for windows'

上级 12d62902
...@@ -2892,16 +2892,8 @@ struct kv_s { ...@@ -2892,16 +2892,8 @@ struct kv_s {
int val; int val;
}; };
static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes) static BOOL get_driver_dll_path(char *buf, size_t len)
{ {
BOOL r = TRUE;
kv_t *kvs = NULL;
kv_t dsn = {0};
kv_t driver = {0};
char driver_dll[MAX_PATH + 1];
HMODULE hm = NULL; HMODULE hm = NULL;
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
...@@ -2911,72 +2903,99 @@ static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes) ...@@ -2911,72 +2903,99 @@ static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes)
LOG("GetModuleHandle failed, error = %d\n", ret); LOG("GetModuleHandle failed, error = %d\n", ret);
return FALSE; return FALSE;
} }
if (GetModuleFileName(hm, driver_dll, sizeof(driver_dll)) == 0) if (GetModuleFileName(hm, buf, len) == 0)
{ {
int ret = GetLastError(); int ret = GetLastError();
LOG("GetModuleFileName failed, error = %d\n", ret); LOG("GetModuleFileName failed, error = %d\n", ret);
return FALSE; return FALSE;
} }
LOG("path: [%s]", driver_dll); return TRUE;
}
const char *p = lpszAttributes; static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes)
int ikvs = 0; {
while (p && *p) { BOOL r = TRUE;
LOG("attr: [%s]", p);
char *line = strdup(p);
if (!line) { r = FALSE; break; }
char *v = strchr(line, '=');
if (v) *v = '\0';
if (stricmp(line, "DSN")==0) { kv_t *kvs = NULL;
if (!v) { r = FALSE; break; }
if (dsn.line) { kv_t dsn = {0};
free(dsn.line); char *line = NULL;
dsn.line = NULL;
dsn.val = 0; do {
} char driver_dll[MAX_PATH + 1];
dsn.line = line; r = get_driver_dll_path(driver_dll, sizeof(driver_dll));
if (v) dsn.val = v - line + 1; LOG("path: [%s]", driver_dll);
} else if (stricmp(line, "Driver")==0) { if (!r) break;
dsn.line = strdup("DSN=TAOS_DEMO");
if (!dsn.line) { r = FALSE; break; }
const char *p = lpszAttributes;
int ikvs = 0;
while (p && *p) {
LOG("attr: [%s]", p);
line = strdup(p);
if (!line) { r = FALSE; break; }
char *v = strchr(line, '=');
if (!v) { r = FALSE; break; } if (!v) { r = FALSE; break; }
if (driver.line) {
free(driver.line); if (strstr(line, "DSN")==line) {
driver.line = NULL; if (dsn.line) {
driver.val = 0; free(dsn.line);
dsn.line = NULL;
dsn.val = 0;
}
dsn.line = line;
line = NULL;
} else {
kv_t *t = (kv_t*)realloc(kvs, (ikvs+1)*sizeof(*t));
if (!t) { r = FALSE; free(line); break; }
t[ikvs].line = line;
*v = '\0';
if (v) t[ikvs].val = v - line + 1;
line = NULL;
kvs = t;
++ikvs;
} }
driver.line = line;
if (v) driver.val = v - line + 1;
} else {
kv_t *t = (kv_t*)realloc(kvs, (ikvs+1)*sizeof(*t));
if (!t) { r = FALSE; free(line); break; }
t[ikvs].line = line;
if (v) t[ikvs].val = v - line + 1;
kvs = t; p += strlen(p) + 1;
++ikvs;
} }
p += strlen(p) + 1; if (hwndParent) {
} MessageBox(hwndParent, "Please use odbcconf to add DSN for TAOS ODBC Driver", "Warning!", MB_OK|MB_ICONEXCLAMATION);
}
if (!r) break;
if (!dsn.line || !driver.line) { char *v = NULL;
LOG("lack of either DSN or Driver"); v = strchr(dsn.line, '=');
} else { if (!v) { r = FALSE; break; }
LOG("Driver[%s]", driver.line+driver.val); *v = '\0';
if (r) r = SQLWritePrivateProfileString("ODBC Data Sources", dsn.line+dsn.val, driver.line+driver.val, "Odbc.ini"); dsn.val = v - dsn.line + 1;
LOG("r:%d", r);
if (r) r = SQLWritePrivateProfileString(dsn.line+dsn.val, "Driver", driver_dll, "Odbc.ini");
LOG("r:%d", r);
}
for (int i=0; r && i<ikvs; ++i) { if ((!dsn.line)) {
const char *k = kvs[i].line; if (!r) LOG("lack of either DSN or Driver");
const char *v = NULL; } else {
if (kvs[i].val) v = kvs[i].line + kvs[i].val; LOG("DSN/Driver[%s/%s]", dsn.line+dsn.val, lpszDriver);
LOG("DSN[%s]/%s/%s", dsn.line+dsn.val, k, v); if (r) r = SQLWritePrivateProfileString("ODBC Data Sources", dsn.line+dsn.val, lpszDriver, "Odbc.ini");
r = SQLWritePrivateProfileString(dsn.line+dsn.val, k, v, "Odbc.ini"); LOG("r:%d", r);
LOG("r:%d", r); LOG("DSN/Driver_dll[%s/%s]", dsn.line+dsn.val, driver_dll);
} if (r) r = SQLWritePrivateProfileString(dsn.line+dsn.val, "Driver", driver_dll, "Odbc.ini");
LOG("r:%d", r);
}
for (int i=0; r && i<ikvs; ++i) {
const char *k = kvs[i].line;
const char *v = NULL;
if (kvs[i].val) v = kvs[i].line + kvs[i].val;
LOG("DSN[%s]/%s/%s", dsn.line+dsn.val, k, v);
r = SQLWritePrivateProfileString(dsn.line+dsn.val, k, v, "Odbc.ini");
LOG("r:%d", r);
}
} while (0);
if (dsn.line) free(dsn.line);
if (line) free(line);
return r; return r;
} }
...@@ -2996,46 +3015,68 @@ static BOOL doDSNRemove(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttribute ...@@ -2996,46 +3015,68 @@ static BOOL doDSNRemove(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttribute
BOOL r = TRUE; BOOL r = TRUE;
kv_t dsn = {0}; kv_t dsn = {0};
char *line = NULL;
const char *p = lpszAttributes; do {
int ikvs = 0; const char *p = lpszAttributes;
while (p && *p) { int ikvs = 0;
LOG("attr: [%s]", p); while (p && *p) {
char *line = strdup(p); LOG("attr: [%s]", p);
if (!line) { r = FALSE; break; } line = strdup(p);
char *v = strchr(line, '='); if (!line) { r = FALSE; break; }
if (v) *v = '\0'; char *v = strchr(line, '=');
if (stricmp(line, "DSN")==0) {
if (!v) { r = FALSE; break; } if (!v) { r = FALSE; break; }
if (dsn.line) { *v = '\0';
free(dsn.line);
dsn.line = NULL; if (strstr(line, "DSN")==line) {
dsn.val = 0; if (dsn.line) {
free(dsn.line);
dsn.line = NULL;
dsn.val = 0;
}
dsn.line = line;
dsn.val = v - line + 1;
line = NULL;
break;
} else {
free(line);
line = NULL;
} }
dsn.line = line;
if (v) dsn.val = v - line + 1; p += strlen(p) + 1;
} }
p += strlen(p) + 1; if (!r) break;
}
if (!dsn.line) { if (!dsn.line) {
LOG("lack of either DSN"); LOG("lack of DSN");
} else { r = FALSE;
LOG("DSN[%s]", dsn.line+dsn.val); break;
if (r) r = SQLWritePrivateProfileString("ODBC Data Sources", dsn.line+dsn.val, NULL, "Odbc.ini"); }
LOG("r:%d", r);
}
char buf[8192]; LOG("delete ODBC Data Sources/[%s]", dsn.line+dsn.val);
LOG("DSN[%s]", dsn.line+dsn.val); r = SQLWritePrivateProfileString("ODBC Data Sources", dsn.line+dsn.val, NULL, "Odbc.ini");
if (r) r = SQLGetPrivateProfileString(dsn.line+dsn.val, NULL, "null", buf, sizeof(buf), "Odbc.ini"); LOG("r:%d", r);
LOG("r:%d", r); if (!r) break;
if (r) LOG("returns: [%s]", buf); char buf[8192];
LOG("fetch DSN[%s]", dsn.line+dsn.val);
r = SQLGetPrivateProfileString(dsn.line+dsn.val, NULL, "null", buf, sizeof(buf), "Odbc.ini");
LOG("r:%d", r);
if (!r) break;
int n = 0;
char *s = buf;
while (s && *s && n++<10) {
LOG("delete %s/[%s]", dsn.line+dsn.val, s);
SQLWritePrivateProfileString(dsn.line+dsn.val, s, NULL, "Odbc.ini");
s += strlen(s) + 1;
}
} while (0);
return FALSE; if (dsn.line) free(dsn.line);
if (line) free(line);
return r;
} }
static BOOL doConfigDSN(HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCSTR lpszAttributes) static BOOL doConfigDSN(HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCSTR lpszAttributes)
...@@ -3048,7 +3089,7 @@ static BOOL doConfigDSN(HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCST ...@@ -3048,7 +3089,7 @@ static BOOL doConfigDSN(HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCST
case ODBC_REMOVE_DSN: sReq = "ODBC_REMOVE_DSN"; break; case ODBC_REMOVE_DSN: sReq = "ODBC_REMOVE_DSN"; break;
default: sReq = "UNKNOWN"; break; default: sReq = "UNKNOWN"; break;
} }
LOG("req:[%s];driver:[%s]", sReq, lpszDriver, lpszAttributes); LOG("req:[%s];Driver:[%s];Attr:[%s]", sReq, lpszDriver, lpszAttributes);
switch(fRequest) { switch(fRequest) {
case ODBC_ADD_DSN: { case ODBC_ADD_DSN: {
r = doDSNAdd(hwndParent, lpszDriver, lpszAttributes); r = doDSNAdd(hwndParent, lpszDriver, lpszAttributes);
...@@ -3070,20 +3111,17 @@ BOOL INSTAPI ConfigDSN(HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCSTR ...@@ -3070,20 +3111,17 @@ BOOL INSTAPI ConfigDSN(HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCSTR
{ {
BOOL r; BOOL r;
r = doConfigDSN(hwndParent, fRequest, lpszDriver, lpszAttributes); r = doConfigDSN(hwndParent, fRequest, lpszDriver, lpszAttributes);
D("return r: %d", r);
return r; return r;
} }
BOOL INSTAPI ConfigTranslator(HWND hwndParent, DWORD *pvOption) BOOL INSTAPI ConfigTranslator(HWND hwndParent, DWORD *pvOption)
{ {
D("====================");
return FALSE; return FALSE;
} }
BOOL INSTAPI ConfigDriver(HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCSTR lpszArgs, BOOL INSTAPI ConfigDriver(HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCSTR lpszArgs,
LPSTR lpszMsg, WORD cbMsgMax, WORD *pcbMsgOut) LPSTR lpszMsg, WORD cbMsgMax, WORD *pcbMsgOut)
{ {
D("x====================");
return FALSE; return FALSE;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册