This repository has been archived by the owner on Jun 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser3mssql.cpp
289 lines (249 loc) · 9.05 KB
/
parser3mssql.cpp
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
/*!
* \brief The file contains Parser MSSQL driver implementation
* \author \verbatim
Created by: Alexander Egorov
\endverbatim
* \date \verbatim
Creation date: 2010-04-11
\endverbatim
* Copyright 2008-2012 Alexander Egorov <egoroff@gmail.com> (http://www.egoroff.spb.ru)
*/
#include "StdAfx.h"
#include "parser3mssql.h"
#include "ConnectionString.h"
#include "wexception.h"
inline static bool IsNoLimit(unsigned long limit)
{
return limit == SQL_NO_LIMIT;
}
inline void SqlNativeDriver::_throw(
SQL_Driver_services& rServices,
const std::exception& e) const
{
rServices._throw(e.what());
}
inline void SqlNativeDriver::ThrowIfError(
SQL_Driver_services& rServices,
bool isFailure) const
{
SQL_Error sqlError;
if (isFailure) {
rServices._throw(sqlError);
}
}
inline void SqlNativeDriver::ValidateColumns(ULONG* pColumnsCount, SQL_Driver_services& rServices) const
{
if (pColumnsCount == NULL) {
rServices._throw("null column number pointer");
}
if (!(*pColumnsCount)) {
rServices._throw("result contains no columns");
}
if (*pColumnsCount > kMaxColumnsCount) {
*pColumnsCount = kMaxColumnsCount;
}
}
SqlNativeDriver::SqlNativeDriver() : SQL_Driver() {}
int SqlNativeDriver::api_version()
{
return SQL_DRIVER_API_VERSION;
}
const char* SqlNativeDriver::initialize(char* p)
{
UNREFERENCED_PARAMETER(p);
return 0;
}
/** connect
@param pConnString
format: @b server=<server>;database=<database>;Trusted_Connection=Yes (MSSQL connect string)
WARNING: must be used only to connect, for buffer doesn't live long
*/
void SqlNativeDriver::connect(
char* pConnString,
SQL_Driver_services& rServices,
void** pConnection)
{
Connection& rConnection = *static_cast<Connection*>(rServices.malloc(sizeof(Connection)));
*pConnection = &rConnection;
rConnection.services = &rServices;
CStringA codePage(rServices.request_charset());
codePage_ = codePage == "UTF-8" ? CP_UTF8 : CP_ACP;
ConnectionString str(pConnString);
str.Parse();
try {
rConnection.pClient = new Client(CStringW(str.GetOleDBString().c_str()));
} catch (const wexception& e) {
rServices._throw(CW2A(e.GetMessage()->c_str(), codePage_));
} catch (const std::exception& e) {
_throw(rServices, e);
}
}
void SqlNativeDriver::disconnect(void* pConnection)
{
Connection& rConnection = *static_cast<Connection*>(pConnection);
try {
delete rConnection.pClient;
rConnection.pClient = NULL;
} catch (const std::exception&) {
// do nothing
}
}
void SqlNativeDriver::commit(void* p)
{
UNREFERENCED_PARAMETER(p);
// do nothing
}
void SqlNativeDriver::rollback(void* p)
{
UNREFERENCED_PARAMETER(p);
// do nothing
}
bool SqlNativeDriver::ping(void* p)
{
UNREFERENCED_PARAMETER(p);
return true;
}
const char* SqlNativeDriver::quote(
void* pConnection,
const char* from,
unsigned int length)
{
Connection& rConnection = *static_cast<Connection*>(pConnection);
char* result = static_cast<char*>(rConnection.services->malloc_atomic(length * 2 + 1));
char* to = result;
while (length--) {
if (*from == '\'') { // ' -> ''
*to++='\'';
}
*to++=*from++;
}
*to = 0;
return result;
}
void SqlNativeDriver::InsertColumnValue(
const CStringA& rMultibyteStr,
Connection& rConnection,
SQL_Driver_query_event_handlers& rHandlers,
SQL_Error& rSqlError)
{
size_t length = rMultibyteStr.GetLength();
char* pStr = static_cast<char*>(rConnection.services->malloc_atomic(length + 1));
strcpy_s(pStr, length + 1, rMultibyteStr);
ThrowIfError(*rConnection.services, rHandlers.add_row_cell(rSqlError, pStr, length));
}
void SqlNativeDriver::query(
void* pConnection,
const char* pStatement,
size_t nPlaceholders,
Placeholder* pPlaceholders,
unsigned long offset,
unsigned long limit,
SQL_Driver_query_event_handlers& rHandlers)
{
Connection& rConnection = *static_cast<Connection*>(pConnection);
Client* pClient = rConnection.pClient;
SQL_Driver_services& rServices = *rConnection.services;
CStringW sql = CA2W(pStatement, codePage_);
LPCWSTR pSql = sql.GetString();
QueryDecorator decorator(pSql, limit, offset, !IsNoLimit(limit));
try {
// Server cursors are created only for statements that begin with
// SELECT
// EXEC[ute] procedure_name
if (!decorator.IsSelectQuery() && !decorator.IsExecQuery()) {
pClient->ExecuteNonQuery(sql);
return;
}
if (nPlaceholders > 0) {
ExecuteReader(decorator, rConnection, rHandlers, offset, limit, nPlaceholders, pPlaceholders);
} else {
ExecuteReader(decorator, rConnection, rHandlers, offset, limit);
}
} catch (const wexception& e) {
rServices._throw(CW2A(e.GetMessage()->c_str(), codePage_));
} catch (const std::exception& e) {
_throw(rServices, e);
}
}
void SqlNativeDriver::ExecuteReader(
const QueryDecorator& decorator,
Connection& rConnection,
SQL_Driver_query_event_handlers& rHandlers,
unsigned long offset,
unsigned long limit)
{
Client* pClient = rConnection.pClient;
SQL_Driver_services& rServices = *rConnection.services;
CCommand<CDynamicStringAccessorW> cmd;
pClient->ExecuteReader(decorator.GetQuery(), &cmd);
HRESULT result = cmd.MoveFirst();
DBORDINAL nColumns = cmd.GetColumnCount();
ValidateColumns(&nColumns, rServices);
SQL_Error sqlError;
for (DBORDINAL i = 1; i <= nColumns; ++i) {
CStringW name(cmd.GetColumnName(i));
InsertColumn(name, rConnection, rHandlers, sqlError);
}
if (result != DB_S_ENDOFROWSET) {
ReadResults(cmd, rConnection, rHandlers, offset, limit, nColumns, &cmd);
}
}
void SqlNativeDriver::ExecuteReader(
const QueryDecorator& decorator,
Connection& rConnection,
SQL_Driver_query_event_handlers& rHandlers,
unsigned long offset,
unsigned long limit,
size_t nPlaceholders,
Placeholder* pPlaceholders)
{
Client* pClient = rConnection.pClient;
SQL_Driver_services& rServices = *rConnection.services;
CCommand<CManualAccessor> cmd;
std::map<CStringW, CStringW> parameters;
for (size_t i = 0; i < nPlaceholders; ++i) {
Placeholder& ph = pPlaceholders[i];
CStringW sName = CA2W(ph.name, codePage_);
CStringW sValue = CA2W(ph.value, codePage_);
parameters[sName] = sValue;
}
pClient->ExecuteReader(decorator.GetQuery(), &cmd, parameters);
HRESULT result = NULL;
ULONG nColumns = 0;
CComHeapPtr<DBCOLUMNINFO> pColumnInfo;
CComHeapPtr<OLECHAR> pStrings;
result = cmd.GetColumnInfo(&nColumns, &pColumnInfo, &pStrings);
ColumnBind* pBind = static_cast<ColumnBind*>(
rConnection.services->malloc_atomic(sizeof(ColumnBind) * nColumns));
result = cmd.CreateAccessor(nColumns, &pBind[0], sizeof(ColumnBind) * nColumns);
ValidateColumns(&nColumns, rServices);
SQL_Error sqlError;
for (ULONG l = 0; l < nColumns; ++l) {
DBLENGTH sz = Client::CalculateStringLength(pColumnInfo[l].wType, pColumnInfo[l].ulColumnSize) + 1;
pBind[l].nSize = sz;
pBind[l].szValue = static_cast<LPWSTR>(rConnection.services->malloc_atomic(sizeof(WCHAR) * sz));
cmd.AddBindEntry(l + 1, DBTYPE_WSTR, sizeof(WCHAR) * sz, pBind[l].szValue, NULL, &pBind[l].dwStatus);
CStringW name(pColumnInfo[l].pwszName);
InsertColumn(name, rConnection, rHandlers, sqlError);
}
cmd.Bind();
result = cmd.MoveFirst();
if (result != DB_S_ENDOFROWSET) {
ReadResults(cmd, rConnection, rHandlers, offset, limit, nColumns, pBind);
}
}
void SqlNativeDriver::InsertColumn(
const CStringW& rUnicodeStr,
Connection& rConnection,
SQL_Driver_query_event_handlers& rHandlers,
SQL_Error& rSqlError
)
{
char* pStr = 0;
size_t length = rUnicodeStr.GetLength();
if (length) {
pStr = static_cast<char*>(rConnection.services->malloc_atomic(length + 1));
strcpy_s(pStr, length + 1, CW2A(rUnicodeStr, codePage_));
}
ThrowIfError(*rConnection.services, rHandlers.add_column(rSqlError, pStr, length));
}