提交 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,46 +2903,57 @@ static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes) ...@@ -2911,46 +2903,57 @@ 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;
} }
return TRUE;
}
static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes)
{
BOOL r = TRUE;
kv_t *kvs = NULL;
kv_t dsn = {0};
char *line = NULL;
do {
char driver_dll[MAX_PATH + 1];
r = get_driver_dll_path(driver_dll, sizeof(driver_dll));
LOG("path: [%s]", driver_dll); LOG("path: [%s]", driver_dll);
if (!r) break;
dsn.line = strdup("DSN=TAOS_DEMO");
if (!dsn.line) { r = FALSE; break; }
const char *p = lpszAttributes; const char *p = lpszAttributes;
int ikvs = 0; int ikvs = 0;
while (p && *p) { while (p && *p) {
LOG("attr: [%s]", p); LOG("attr: [%s]", p);
char *line = strdup(p); line = strdup(p);
if (!line) { r = FALSE; break; } if (!line) { r = FALSE; break; }
char *v = strchr(line, '='); char *v = strchr(line, '=');
if (v) *v = '\0';
if (stricmp(line, "DSN")==0) {
if (!v) { r = FALSE; break; } if (!v) { r = FALSE; break; }
if (strstr(line, "DSN")==line) {
if (dsn.line) { if (dsn.line) {
free(dsn.line); free(dsn.line);
dsn.line = NULL; dsn.line = NULL;
dsn.val = 0; dsn.val = 0;
} }
dsn.line = line; dsn.line = line;
if (v) dsn.val = v - line + 1; line = NULL;
} else if (stricmp(line, "Driver")==0) {
if (!v) { r = FALSE; break; }
if (driver.line) {
free(driver.line);
driver.line = NULL;
driver.val = 0;
}
driver.line = line;
if (v) driver.val = v - line + 1;
} else { } else {
kv_t *t = (kv_t*)realloc(kvs, (ikvs+1)*sizeof(*t)); kv_t *t = (kv_t*)realloc(kvs, (ikvs+1)*sizeof(*t));
if (!t) { r = FALSE; free(line); break; } if (!t) { r = FALSE; free(line); break; }
t[ikvs].line = line; t[ikvs].line = line;
*v = '\0';
if (v) t[ikvs].val = v - line + 1; if (v) t[ikvs].val = v - line + 1;
line = NULL;
kvs = t; kvs = t;
++ikvs; ++ikvs;
...@@ -2959,12 +2962,24 @@ static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes) ...@@ -2959,12 +2962,24 @@ static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes)
p += strlen(p) + 1; p += strlen(p) + 1;
} }
if (!dsn.line || !driver.line) { if (hwndParent) {
LOG("lack of either DSN or Driver"); MessageBox(hwndParent, "Please use odbcconf to add DSN for TAOS ODBC Driver", "Warning!", MB_OK|MB_ICONEXCLAMATION);
}
if (!r) break;
char *v = NULL;
v = strchr(dsn.line, '=');
if (!v) { r = FALSE; break; }
*v = '\0';
dsn.val = v - dsn.line + 1;
if ((!dsn.line)) {
if (!r) LOG("lack of either DSN or Driver");
} else { } else {
LOG("Driver[%s]", driver.line+driver.val); LOG("DSN/Driver[%s/%s]", dsn.line+dsn.val, lpszDriver);
if (r) r = SQLWritePrivateProfileString("ODBC Data Sources", dsn.line+dsn.val, driver.line+driver.val, "Odbc.ini"); if (r) r = SQLWritePrivateProfileString("ODBC Data Sources", dsn.line+dsn.val, lpszDriver, "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"); if (r) r = SQLWritePrivateProfileString(dsn.line+dsn.val, "Driver", driver_dll, "Odbc.ini");
LOG("r:%d", r); LOG("r:%d", r);
} }
...@@ -2977,6 +2992,10 @@ static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes) ...@@ -2977,6 +2992,10 @@ static BOOL doDSNAdd(HWND hwndParent, LPCSTR lpszDriver, LPCSTR lpszAttributes)
r = SQLWritePrivateProfileString(dsn.line+dsn.val, k, v, "Odbc.ini"); r = SQLWritePrivateProfileString(dsn.line+dsn.val, k, v, "Odbc.ini");
LOG("r:%d", r); 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;
do {
const char *p = lpszAttributes; const char *p = lpszAttributes;
int ikvs = 0; int ikvs = 0;
while (p && *p) { while (p && *p) {
LOG("attr: [%s]", p); LOG("attr: [%s]", p);
char *line = strdup(p); line = strdup(p);
if (!line) { r = FALSE; break; } if (!line) { r = FALSE; break; }
char *v = strchr(line, '='); char *v = strchr(line, '=');
if (v) *v = '\0';
if (stricmp(line, "DSN")==0) {
if (!v) { r = FALSE; break; } if (!v) { r = FALSE; break; }
*v = '\0';
if (strstr(line, "DSN")==line) {
if (dsn.line) { if (dsn.line) {
free(dsn.line); free(dsn.line);
dsn.line = NULL; dsn.line = NULL;
dsn.val = 0; dsn.val = 0;
} }
dsn.line = line; dsn.line = line;
if (v) dsn.val = v - line + 1; dsn.val = v - line + 1;
line = NULL;
break;
} else {
free(line);
line = NULL;
} }
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);
} }
LOG("delete ODBC Data Sources/[%s]", dsn.line+dsn.val);
r = SQLWritePrivateProfileString("ODBC Data Sources", dsn.line+dsn.val, NULL, "Odbc.ini");
LOG("r:%d", r);
if (!r) break;
char buf[8192]; char buf[8192];
LOG("DSN[%s]", dsn.line+dsn.val); LOG("fetch DSN[%s]", dsn.line+dsn.val);
if (r) r = SQLGetPrivateProfileString(dsn.line+dsn.val, NULL, "null", buf, sizeof(buf), "Odbc.ini"); 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); 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.
先完成此消息的编辑!
想要评论请 注册