-
Notifications
You must be signed in to change notification settings - Fork 2
/
ma_environment.c
188 lines (166 loc) · 5.08 KB
/
ma_environment.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
/************************************************************************************
Copyright (C) 2013,2016 MariaDB Corporation AB
2021 SingleStore, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not see <http://www.gnu.org/licenses>
or write to the Free Software Foundation, Inc.,
51 Franklin St., Fifth Floor, Boston, MA 02110, USA
*************************************************************************************/
#include <ma_odbc.h>
extern Client_Charset utf8;
extern MARIADB_CHARSET_INFO* DmUnicodeCs;
extern MARIADB_CHARSET_INFO dummyUtf16le;
extern MARIADB_CHARSET_INFO dummyUtf16be;
extern MARIADB_CHARSET_INFO dummyUtf32le;
extern MARIADB_CHARSET_INFO dummyUtf32be;
Client_Charset SourceAnsiCs= {0, 0}; /* Basically it should be initialized with 0 anyway */
char* DefaultPluginLocation= NULL;
#ifndef _MAX_PATH
# define _MAX_PATH 260
#endif
static char PluginLocationBuf[_MAX_PATH];
MARIADB_CHARSET_INFO * mysql_find_charset_name(const char *name);
#ifdef _WIN32
# pragma comment(lib, "ws2_32.lib")
#endif
static my_bool little_endian()
{
int x= 1;
char *c= (char*)&x;
return *c;
}
/* {{{ MADB_EnvFree */
SQLRETURN MADB_EnvFree(MADB_Env *Env)
{
if (!Env)
return SQL_ERROR;
DeleteCriticalSection(&Env->cs);
free(Env);
#ifdef _WIN32
WSACleanup();
#endif
return SQL_SUCCESS;
}
/* }}} */
const char* GetDefaultLogDir();
int GetSourceAnsiCs(Client_Charset *cc);
/* {{{ MADB_EnvInit */
MADB_Env *MADB_EnvInit()
{
MADB_Env *Env= NULL;
#ifdef _WIN32
/* Since we can't determine if WSAStartup has been called, we need
to call it again
*/
WORD VersionRequested;
int err;
WSADATA WsaData;
/* if possible use latest supported version (2.2) */
const unsigned int MajorVersion=2,
MinorVersion=2;
VersionRequested= MAKEWORD(MajorVersion, MinorVersion);
/* Load WinSock library */
if ((err= WSAStartup(VersionRequested, &WsaData)))
{
/* todo: optional debug output */
return Env;
}
/* make sure 2.2 or higher is supported */
if ((LOBYTE(WsaData.wVersion) * 10 + HIBYTE(WsaData.wVersion)) < 22)
{
/* todo: optional debug output */
goto cleanup;
}
#endif
mysql_library_init(0, NULL, NULL);
if (!(Env= (MADB_Env *)MADB_CALLOC(sizeof(MADB_Env))))
{
/* todo: optional debug output */
goto cleanup;
}
MADB_PutErrorPrefix(NULL, &Env->Error);
InitializeCriticalSection(&Env->cs);
Env->OdbcVersion= SQL_OV_ODBC3;
/* This is probably is better todo with thread_once */
if (DmUnicodeCs == NULL)
{
if (sizeof(SQLWCHAR) == 2)
{
DmUnicodeCs= little_endian() ? &dummyUtf16le : &dummyUtf16be;
}
else
{
DmUnicodeCs= little_endian() ? &dummyUtf32le : &dummyUtf32be;
}
}
utf8.cs_info= mariadb_get_charset_by_name("utf8");
GetDefaultLogDir();
GetSourceAnsiCs(&SourceAnsiCs);
/* If we have something in the buffer - then we've already tried to get default location w/out much success */
if (DefaultPluginLocation == NULL && strlen(PluginLocationBuf) == 0)
{
DefaultPluginLocation= MADB_GetDefaultPluginsDir(PluginLocationBuf, sizeof(PluginLocationBuf));
}
cleanup:
#ifdef _WIN32
if (!Env)
WSACleanup();
#endif
return Env;
}
/* }}} */
/* {{{ MADB_EnvSetAttr */
SQLRETURN MADB_EnvSetAttr(MADB_Env* Env, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER StringLength)
{
MADB_CLEAR_ERROR(&Env->Error);
switch (Attribute) {
case SQL_ATTR_ODBC_VERSION:
if (Env->Dbcs)
{
MADB_SetError(&Env->Error, MADB_ERR_HYC00, NULL, 0);
return Env->Error.ReturnValue;
}
Env->OdbcVersion= (SQLINTEGER)(SQLLEN)ValuePtr;
break;
case SQL_ATTR_OUTPUT_NTS:
if ((SQLINTEGER)(SQLLEN)ValuePtr != SQL_TRUE)
MADB_SetError(&Env->Error, MADB_ERR_S1C00, NULL, 0);
break;
default:
MADB_SetError(&Env->Error, MADB_ERR_HYC00, NULL, 0);
break;
}
// LeaveCriticalSection(&Env->cs);
return Env->Error.ReturnValue;
}
/* }}} */
/* {{{ MADB_EnvGetAttr */
SQLRETURN MADB_EnvGetAttr(MADB_Env *Env, SQLINTEGER Attribute, SQLPOINTER ValuePtr, SQLINTEGER BufferLength,
SQLINTEGER *StringLengthPtr)
{
MADB_CLEAR_ERROR(&Env->Error);
switch (Attribute) {
case SQL_ATTR_CONNECTION_POOLING:
*(SQLUINTEGER *)ValuePtr = SQL_CP_OFF;
break;
case SQL_ATTR_ODBC_VERSION:
*(SQLINTEGER *)ValuePtr= Env->OdbcVersion;
break;
case SQL_ATTR_OUTPUT_NTS:
*(SQLINTEGER *)ValuePtr= SQL_TRUE;
break;
default:
MADB_SetError(&Env->Error, MADB_ERR_HYC00, NULL, 0);
break;
}
return Env->Error.ReturnValue;
}
/* }}} */