DON'T USE THIS REPO: IT'S OBSOLETE.
[versaplex.git] / vxodbc / psqlodbc.cc
blob397a27fdb1915188b4924524a2adc8c5bde66471
1 /*
2 * Description: This module contains the main entry point (DllMain)
3 * for the library. It also contains functions to get
4 * and set global variables for the driver in the registry.
5 */
7 #ifdef WIN32
8 #ifdef _DEBUG
9 #include <crtdbg.h>
10 #endif /* _DEBUG */
11 #endif /* WIN32 */
12 #include "psqlodbc.h"
13 #include "dlg_specific.h"
14 #include "environ.h"
15 #include "wvlogger.h"
17 #ifdef WIN32
18 #ifdef _WSASTARTUP_IN_DLLMAIN_
19 #include <winsock2.h>
20 #endif /* _WSASTARTUP_IN_DLLMAIN_ */
21 int platformId = 0;
22 #endif
24 int exepgm = 0;
25 GLOBAL_VALUES globals;
27 EXTERN_C RETCODE SQL_API SQLDummyOrdinal(void);
29 #if defined(WIN_MULTITHREAD_SUPPORT)
30 extern CRITICAL_SECTION conns_cs, common_cs;
31 #elif defined(POSIX_MULTITHREAD_SUPPORT)
32 extern pthread_mutex_t conns_cs, common_cs;
34 #ifdef POSIX_THREADMUTEX_SUPPORT
35 #ifdef PG_RECURSIVE_MUTEXATTR
36 static pthread_mutexattr_t recur_attr;
37 const pthread_mutexattr_t *getMutexAttr(void)
39 static int init = 1;
41 if (init)
43 if (0 != pthread_mutexattr_init(&recur_attr))
44 return NULL;
45 if (0 !=
46 pthread_mutexattr_settype(&recur_attr,
47 PG_RECURSIVE_MUTEXATTR))
48 return NULL;
50 init = 0;
52 return &recur_attr;
54 #else
55 const pthread_mutexattr_t *getMutexAttr(void)
57 return NULL;
59 #endif /* PG_RECURSIVE_MUTEXATTR */
60 #endif /* POSIX_THREADMUTEX_SUPPORT */
61 #endif /* WIN_MULTITHREAD_SUPPORT */
63 int initialize_global_cs(void)
65 static int init = 1;
67 if (!init)
68 return 0;
69 init = 0;
70 #ifdef WIN32
71 #ifdef _DEBUG
72 #ifdef _MEMORY_DEBUG_
73 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
74 #endif /* _MEMORY_DEBUG_ */
75 #endif /* _DEBUG */
76 #endif /* WIN32 */
77 #ifdef POSIX_THREADMUTEX_SUPPORT
78 getMutexAttr();
79 #endif /* POSIX_THREADMUTEX_SUPPORT */
80 InitializeLogging();
81 INIT_CONNS_CS;
82 INIT_COMMON_CS;
84 return 0;
87 static void finalize_global_cs(void)
89 DELETE_COMMON_CS;
90 DELETE_CONNS_CS;
91 FinalizeLogging();
92 #ifdef _DEBUG
93 #ifdef _MEMORY_DEBUG_
94 // _CrtDumpMemoryLeaks();
95 #endif /* _MEMORY_DEBUG_ */
96 #endif /* _DEBUG */
99 #ifdef WIN32
100 HINSTANCE NEAR s_hModule; /* Saved module handle. */
101 /* This is where the Driver Manager attaches to this Driver */
102 EXTERN_C BOOL WINAPI
103 DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
105 #ifdef _WSASTARTUP_IN_DLLMAIN_
106 WORD wVersionRequested;
107 WSADATA wsaData;
108 #endif /* _WSASTARTUP_IN_DLLMAIN_ */
110 switch (ul_reason_for_call)
112 case DLL_PROCESS_ATTACH:
113 s_hModule = (HINSTANCE)hInst; /* Save for dialog boxes */
115 #ifdef _WSASTARTUP_IN_DLLMAIN_
116 /* Load the WinSock Library */
117 wVersionRequested = MAKEWORD(1, 1);
119 if (WSAStartup(wVersionRequested, &wsaData))
120 return FALSE;
122 /* Verify that this is the minimum version of WinSock */
123 if (LOBYTE(wsaData.wVersion) != 1 ||
124 HIBYTE(wsaData.wVersion) != 1)
126 WSACleanup();
127 return FALSE;
129 #endif /* _WSASTARTUP_IN_DLLMAIN_ */
130 if (initialize_global_cs() == 0)
132 char pathname[_MAX_PATH], fname[_MAX_FNAME];
133 OSVERSIONINFO osversion;
135 getCommonDefaults(DBMS_NAME, ODBCINST_INI, NULL);
136 if (GetModuleFileName(NULL, pathname, sizeof(pathname)) > 0)
138 _splitpath(pathname, NULL, NULL, fname, NULL);
139 if (stricmp(fname, "msaccess") == 0)
140 exepgm = 1;
141 else if (strnicmp(fname, "msqry", 5) == 0)
142 exepgm = 2;
144 osversion.dwOSVersionInfoSize = sizeof(osversion);
145 if (GetVersionEx(&osversion))
147 platformId = osversion.dwPlatformId;
149 mylog("exe name=%s plaformId=%d\n", fname, platformId);
151 break;
153 case DLL_THREAD_ATTACH:
154 break;
156 case DLL_PROCESS_DETACH:
157 mylog("DETACHING PROCESS\n");
158 /* my(q)log is unavailable from here */
159 finalize_global_cs();
160 #ifdef _WSASTARTUP_IN_DLLMAIN_
161 WSACleanup();
162 #endif /* _WSASTARTUP_IN_DLLMAIN_ */
163 return TRUE;
165 case DLL_THREAD_DETACH:
166 break;
168 default:
169 break;
172 return TRUE;
174 UNREFERENCED_PARAMETER(lpReserved);
177 #else /* not WIN32 */
179 #ifdef __GNUC__
181 /* This function is called at library initialization time. */
183 static BOOL __attribute__ ((constructor)) init(void)
185 initialize_global_cs();
186 getCommonDefaults(DBMS_NAME, ODBCINST_INI, NULL);
187 return TRUE;
190 #else /* not __GNUC__ */
193 * These two functions do shared library initialziation on UNIX, well at least
194 * on Linux. I don't know about other systems.
196 BOOL _init(void)
198 initialize_global_cs();
199 getCommonDefaults(DBMS_NAME, ODBCINST_INI, NULL);
200 return TRUE;
203 BOOL _fini(void)
205 finalize_global_cs();
206 return TRUE;
208 #endif /* not __GNUC__ */
209 #endif /* not WIN32 */
213 * This function is used to cause the Driver Manager to
214 * call functions by number rather than name, which is faster.
215 * The ordinal value of this function must be 199 to have the
216 * Driver Manager do this. Also, the ordinal values of the
217 * functions must match the value of fFunction in SQLGetFunctions()
219 RETCODE SQL_API SQLDummyOrdinal(void)
221 return SQL_SUCCESS;