-
Notifications
You must be signed in to change notification settings - Fork 0
/
iODBC_glue.c
389 lines (328 loc) · 12.1 KB
/
iODBC_glue.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include "iODBCGui.h"
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/listbrowser.h>
#include <string.h>
#include <sys/stat.h>
#include <classes/requester.h>
#include "iODBC_cat.h"
#include "version.h"
#include "debug.h"
extern int32 Requester(uint32 img, TEXT *title, TEXT *reqtxt, TEXT *buttons, struct Window* win);
extern char *GetString(LONG id);
BOOL checkKey(struct List *list, CONST_STRPTR key)
{
BOOL found = FALSE;
STRPTR NodeKey;
struct Node *n = IExec->GetHead(list);
while (n && !found)
{
IListBrowser->GetListBrowserNodeAttrs(n,
LBNA_Column, 0, LBNCA_Text, &NodeKey,
TAG_DONE);
if (strcasecmp((const char*)NodeKey, key) == 0)
found = TRUE;
n = IExec->GetSucc(n);
}
return found;
}
void addRow2(struct List *list, CONST_STRPTR label1, CONST_STRPTR label2)
{
if (NULL == list)
return;
struct Node *node = IListBrowser->AllocListBrowserNode(2,
LBNA_Column, 0, LBNCA_CopyText, TRUE, LBNCA_Text, label1 == NULL ? "" : label1,
LBNA_Column, 1, LBNCA_CopyText, TRUE, LBNCA_Text, label2 == NULL ? "" : label2,
TAG_DONE);
if (node)
IExec->AddTail(list, node);
}
void addRow3(struct List *list, CONST_STRPTR label1, CONST_STRPTR label2, CONST_STRPTR label3)
{
if (NULL == list)
return;
struct Node *node = IListBrowser->AllocListBrowserNode(3,
LBNA_Column, 0, LBNCA_CopyText, TRUE, LBNCA_Text, label1 == NULL ? "" : label1,
LBNA_Column, 1, LBNCA_CopyText, TRUE, LBNCA_Text, label2 == NULL ? "" : label2,
LBNA_Column, 2, LBNCA_CopyText, TRUE, LBNCA_Text, label3 == NULL ? "" : label3,
TAG_DONE);
if (node)
IExec->AddTail(list, node);
}
void addRow4(struct List *list, CONST_STRPTR label1, CONST_STRPTR label2, CONST_STRPTR label3, CONST_STRPTR label4)
{
if (NULL == list)
return;
struct Node *node = IListBrowser->AllocListBrowserNode(4,
LBNA_Column, 0, LBNCA_CopyText, TRUE, LBNCA_Text, label1 == NULL ? "" : label1,
LBNA_Column, 1, LBNCA_CopyText, TRUE, LBNCA_Text, label2 == NULL ? "" : label2,
LBNA_Column, 2, LBNCA_CopyText, TRUE, LBNCA_Text, label3 == NULL ? "" : label3,
LBNA_Column, 3, LBNCA_CopyText, TRUE, LBNCA_Text, label4 == NULL ? "" : label4,
TAG_DONE);
if (node)
IExec->AddTail(list, node);
}
void addRow5(struct List *list, CONST_STRPTR label1, CONST_STRPTR label2, CONST_STRPTR label3, CONST_STRPTR label4, CONST_STRPTR label5)
{
if (NULL == list)
return;
struct Node *node = IListBrowser->AllocListBrowserNode(5,
LBNA_Column, 0, LBNCA_CopyText, TRUE, LBNCA_Text, label1 == NULL ? "" : label1,
LBNA_Column, 1, LBNCA_CopyText, TRUE, LBNCA_Text, label2 == NULL ? "" : label2,
LBNA_Column, 2, LBNCA_CopyText, TRUE, LBNCA_Text, label3 == NULL ? "" : label3,
LBNA_Column, 3, LBNCA_CopyText, TRUE, LBNCA_Text, label4 == NULL ? "" : label4,
LBNA_Column, 4, LBNCA_CopyText, TRUE, LBNCA_Text, label5 == NULL ? "" : label5,
TAG_DONE);
if (node)
IExec->AddTail(list, node);
}
int ODBC_Connect(void)
{
if (SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv) != SQL_SUCCESS)
return -1;
SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_UINTEGER);
if (SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc) != SQL_SUCCESS)
return -1;
SQLSetConnectOption (hdbc, SQL_APPLICATION_NAME, (SQLULEN) TEXT ("iODBC Source Administrator"));
return 0;
}
int ODBC_Disconnect(void)
{
if (hstmt)
{
SQLCloseCursor (hstmt);
SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
}
if (connected)
SQLDisconnect (hdbc);
if (hdbc)
SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
if (henv)
SQLFreeHandle (SQL_HANDLE_ENV, henv);
return 0;
}
int listDSN(Object *LBDSN, struct Window *win)
{
if (ODBC_Connect()==0)
{
SQLTCHAR dsn[33] = {0};
SQLTCHAR desc[255] = {0};
SQLTCHAR driver[1024] = {0};
SQLSMALLINT len1, len2;
int retcode = -1;
IExec->NewList(&DSNlist);
if (SQLDataSources (henv, SQL_FETCH_FIRST, dsn, NUMTCHAR (dsn), &len1, desc, NUMTCHAR (desc), &len2) == SQL_SUCCESS)
{
do
{
char *tempDSN = (char*)calloc(1,len1+1);
char *tempDriver = (char*)calloc(1,1024);
char tempDes[4096] = { 0 };
strncpy(tempDSN, (const char *) dsn, len1);
SQLSetConfigMode(ODBC_SYSTEM_DSN);
SQLGetPrivateProfileString((const char *) dsn, "Driver", "", (char *) driver, sizeof(driver), "odbc.ini");
SQLGetPrivateProfileString((const char *) dsn, "Description", "", (char *) tempDes, sizeof(tempDes), "odbc.ini");
if (driver!=NULL)
strncpy(tempDriver, (const char *) driver, 1023);
else
strcpy(tempDriver, "-");
addRow3(&DSNlist,(CONST_STRPTR) tempDSN, tempDes, tempDriver);
}
while (SQLDataSources (henv, SQL_FETCH_NEXT, dsn, NUMTCHAR (dsn), &len1, desc, NUMTCHAR (desc), &len2) == SQL_SUCCESS);
retcode = 0;
}
IIntuition->RefreshSetGadgetAttrs((struct Gadget*)LBDSN, win, NULL, LISTBROWSER_Labels, &DSNlist, TAG_DONE);
return retcode;
}
else
{
Requester(REQIMAGE_ERROR, PROGRAM_TITLE, GetString(MSG_ERROR_LOADING_ODBC),GetString(MSG_OK), NULL);
return -1;
}
}
int listDrivers(Object *LBDRV, struct Window *win)
{
if (ODBC_Connect()==0)
{
SQLTCHAR drvdesc[1024] = {0};
SQLTCHAR drvattr[1024] = {0};
SQLTCHAR drvattrs[1024] = {0};
SQLTCHAR driver[1024] = {0};
SQLSMALLINT len, len1, len2;
void *handle = NULL;
HENV drv_henv;
HDBC drv_hdbc;
SQLRETURN ret;
pSQLGetInfoFunc funcHdl;
pSQLAllocHandle allocHdl;
pSQLAllocEnv allocEnvHdl = NULL;
pSQLAllocConnect allocConnectHdl = NULL;
pSQLFreeHandle freeHdl;
pSQLFreeEnv freeEnvHdl;
pSQLFreeConnect freeConnectHdl;
int retcode = -1;
IExec->NewList(&DriverList);
if (SQLDrivers(henv, SQL_FETCH_FIRST, drvdesc, sizeof(drvdesc), &len1, drvattr, sizeof(drvattr), &len2) != SQL_SUCCESS)
{
Requester(REQIMAGE_ERROR, GetString(MSG_WINDOW_TITLE), GetString(MSG_ERROR_NODRIVERS_INSTALLED),GetString(MSG_OK), NULL);
return retcode;
}
else
{
do
{
char *tempDesc = (char*)calloc(1,1024);
char *tempDrvVer = (char*)calloc(1,1024);
char *tempDrv = (char*)calloc(1,sizeof(driver));
char *tempSize = (char*)calloc(1,1024);
struct stat _stat;
strncpy(tempDesc, (const char *) drvdesc, len1);
SQLSetConfigMode(ODBC_BOTH_DSN);
SQLGetPrivateProfileString((const char *) drvdesc, "Driver", "", (char *) driver, sizeof(driver), "odbcinst.ini");
if (driver[0] == '\0')
SQLGetPrivateProfileString("Default", "Driver","", (char *) driver, sizeof(driver), "odbcinst.ini");
if (driver[0] != '\0')
{
strncpy(tempDrv, (const char *) driver, 1023);
drv_hdbc = NULL;
drv_henv = NULL;
if ((handle = (void *) DLL_OPEN (driver)) != NULL)
{
if ((allocHdl = (pSQLAllocHandle) DLL_PROC (handle, "SQLAllocHandle")) != NULL)
{
ret = allocHdl (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &drv_henv);
if (ret == SQL_ERROR)
goto nodriverver;
ret = allocHdl (SQL_HANDLE_DBC, drv_henv, &drv_hdbc);
if (ret == SQL_ERROR)
goto nodriverver;
}
else
{
if ((allocEnvHdl = (pSQLAllocEnv) DLL_PROC (handle, "SQLAllocEnv")) != NULL)
{
ret = allocEnvHdl (&drv_henv);
if (ret == SQL_ERROR)
goto nodriverver;
}
else
goto nodriverver;
if ((allocConnectHdl = (pSQLAllocConnect) DLL_PROC (handle, "SQLAllocConnect")) != NULL)
{
ret = allocConnectHdl (drv_henv, &drv_hdbc);
if (ret == SQL_ERROR)
goto nodriverver;
}
else
goto nodriverver;
}
if ((funcHdl = (pSQLGetInfoFunc) DLL_PROC (handle, "SQLGetInfo")) != NULL)
{
/* Retrieve some information */
ret = funcHdl (drv_hdbc, SQL_DRIVER_VER, drvattrs, sizeof (drvattrs), &len);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
{
unsigned int z;
/* Drop the description if one provided */
for (z = 0; ((char *) drvattrs)[z]; z++)
if (((char *) drvattrs)[z] == ' ')
((char *) drvattrs)[z] = '\0';
strncpy(tempDrvVer, (const char *) drvattrs, 1023);
}
else
goto nodriverver;
}
else
goto nodriverver;
}
else
{
nodriverver:
strcpy(tempDrvVer,"-");
}
if (drv_hdbc || drv_henv)
{
if (allocConnectHdl && (freeConnectHdl = (pSQLFreeConnect) DLL_PROC (handle, "SQLFreeConnect")) != NULL)
{
freeConnectHdl (drv_hdbc);
drv_hdbc = NULL;
}
if (allocEnvHdl && (freeEnvHdl = (pSQLFreeEnv) DLL_PROC (handle, "SQLFreeEnv")) != NULL)
{
freeEnvHdl (drv_henv);
drv_henv = NULL;
}
}
if ((drv_hdbc || drv_henv) && (freeHdl = (pSQLFreeHandle) DLL_PROC (handle, "SQLFreeHandle")) != NULL)
{
if (drv_hdbc)
freeHdl (SQL_HANDLE_DBC, drv_hdbc);
if (drv_henv)
freeHdl (SQL_HANDLE_ENV, drv_henv);
}
if (handle)
DLL_CLOSE (handle);
if (!stat((const char *) driver, &_stat))
snprintf(tempSize, 1023, "%d Kb", (int) (_stat.st_size / 1024));
else
strcpy(tempSize, "-");
}
else
{
strcpy(tempDrv,"-");
strcpy(tempDrvVer, "-");
strcpy(tempSize, "-");
}
addRow4(&DriverList, tempDesc, tempDrv, tempDrvVer, tempSize);
}
while (SQLDrivers(henv, SQL_FETCH_NEXT, drvdesc, NUMTCHAR(drvdesc), &len1, drvattr, NUMTCHAR(drvattr), &len2) == SQL_SUCCESS);
}
IIntuition->RefreshSetGadgetAttrs((struct Gadget*)LBDRV, win, NULL, LISTBROWSER_Labels, &DriverList, TAG_DONE);
return retcode;
}
else
{
Requester(REQIMAGE_ERROR, PROGRAM_TITLE, GetString(MSG_ERROR_LOADING_ODBC),GetString(MSG_OK), NULL);
return -1;
}
}
void addAboutList(Object *LBDRV, struct Window *win)
{
char _date[1024] = {0}, _size[1024] = {0};
char *data[6];
struct stat _stat;
void *handle, *proc;
int i;
IExec->NewList(&AboutList);
for (i = 0; i < sizeof(iODBC_Components) / sizeof(iODBC_Components[0]); i++)
{
memset(&_stat,0x0, sizeof(stat));
data[0] = iODBC_Components[i].lib_desc;
data[1] = VERSION;
data[2] = iODBC_Components[i].lib_name;
data[3] = "";
data[4] = "";
data[5] = iODBC_Components[i].lib_path;
if ((handle = dlopen(iODBC_Components[i].lib_name, RTLD_LAZY)))
{
if ((proc = dlsym(handle, iODBC_Components[i].lib_ver_sym)))
data[1] = strdup(*(char **) proc);
if (!stat(iODBC_Components[i].lib_path, &_stat))
{
snprintf(_size, 1023, "%lu Kb", (unsigned long) _stat.st_size / 1024L);
snprintf(_date, 1023, "%s", ctime(&_stat.st_mtime));
_date[strlen(_date) - 1] = '\0';
data[3] = strdup(_date);
data[4] = strdup(_size);
}
else
{
data[3] = strdup("-");
data[4] = strdup("-");
}
dlclose(handle);
addRow5(&AboutList, data[0], data[2], data[3], data[4], data[1]);
}
}
IIntuition->RefreshSetGadgetAttrs((struct Gadget*)LBDRV, win, NULL, LISTBROWSER_Labels, &AboutList, TAG_DONE);
}