-
Notifications
You must be signed in to change notification settings - Fork 15
/
odbcapi30w.c
495 lines (462 loc) · 12.3 KB
/
odbcapi30w.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*-------
* Module: odbcapi30w.c
*
* Description: This module contains UNICODE routines
*
* Classes: n/a
*
* API functions: SQLColAttributeW, SQLGetStmtAttrW, SQLSetStmtAttrW,
SQLSetConnectAttrW, SQLGetConnectAttrW,
SQLGetDescFieldW, SQLGetDescRecW, SQLGetDiagFieldW,
SQLGetDiagRecW,
*-------
*/
#include "psqlodbc.h"
#include "unicode_support.h"
#include <stdio.h>
#include <string.h>
#include "pgapifunc.h"
#include "connection.h"
#include "statement.h"
#include "misc.h"
RETCODE SQL_API
SQLGetStmtAttrW(SQLHSTMT hstmt,
SQLINTEGER fAttribute,
PTR rgbValue,
SQLINTEGER cbValueMax,
SQLINTEGER *pcbValue)
{
RETCODE ret;
StatementClass *stmt = (StatementClass *) hstmt;
MYLOG(0, "Entering\n");
ENTER_STMT_CS((StatementClass *) hstmt);
SC_clear_error((StatementClass *) hstmt);
StartRollbackState(stmt);
ret = PGAPI_GetStmtAttr(hstmt, fAttribute, rgbValue,
cbValueMax, pcbValue);
ret = DiscardStatementSvp(stmt, ret, FALSE);
LEAVE_STMT_CS((StatementClass *) hstmt);
return ret;
}
RETCODE SQL_API
SQLSetStmtAttrW(SQLHSTMT hstmt,
SQLINTEGER fAttribute,
PTR rgbValue,
SQLINTEGER cbValueMax)
{
RETCODE ret;
StatementClass *stmt = (StatementClass *) hstmt;
MYLOG(0, "Entering\n");
ENTER_STMT_CS(stmt);
SC_clear_error(stmt);
StartRollbackState(stmt);
ret = PGAPI_SetStmtAttr(hstmt, fAttribute, rgbValue,
cbValueMax);
ret = DiscardStatementSvp(stmt, ret, FALSE);
LEAVE_STMT_CS(stmt);
return ret;
}
RETCODE SQL_API
SQLGetConnectAttrW(HDBC hdbc,
SQLINTEGER fAttribute,
PTR rgbValue,
SQLINTEGER cbValueMax,
SQLINTEGER *pcbValue)
{
RETCODE ret;
MYLOG(0, "Entering\n");
CC_examine_global_transaction((ConnectionClass *) hdbc);
ENTER_CONN_CS((ConnectionClass *) hdbc);
CC_clear_error((ConnectionClass *) hdbc);
ret = PGAPI_GetConnectAttr(hdbc, fAttribute, rgbValue,
cbValueMax, pcbValue);
LEAVE_CONN_CS((ConnectionClass *) hdbc);
return ret;
}
RETCODE SQL_API
SQLSetConnectAttrW(HDBC hdbc,
SQLINTEGER fAttribute,
PTR rgbValue,
SQLINTEGER cbValue)
{
RETCODE ret;
ConnectionClass *conn = (ConnectionClass *) hdbc;
MYLOG(0, "Entering\n");
CC_examine_global_transaction(conn);
ENTER_CONN_CS(conn);
CC_clear_error(conn);
CC_set_in_unicode_driver(conn);
ret = PGAPI_SetConnectAttr(hdbc, fAttribute, rgbValue,
cbValue);
LEAVE_CONN_CS(conn);
return ret;
}
/* new function */
RETCODE SQL_API
SQLSetDescFieldW(SQLHDESC DescriptorHandle, SQLSMALLINT RecNumber,
SQLSMALLINT FieldIdentifier, PTR Value,
SQLINTEGER BufferLength)
{
RETCODE ret;
SQLLEN vallen;
char *uval = NULL;
BOOL val_alloced = FALSE;
MYLOG(0, "Entering\n");
if (BufferLength > 0 || SQL_NTS == BufferLength)
{
switch (FieldIdentifier)
{
case SQL_DESC_BASE_COLUMN_NAME:
case SQL_DESC_BASE_TABLE_NAME:
case SQL_DESC_CATALOG_NAME:
case SQL_DESC_LABEL:
case SQL_DESC_LITERAL_PREFIX:
case SQL_DESC_LITERAL_SUFFIX:
case SQL_DESC_LOCAL_TYPE_NAME:
case SQL_DESC_NAME:
case SQL_DESC_SCHEMA_NAME:
case SQL_DESC_TABLE_NAME:
case SQL_DESC_TYPE_NAME:
uval = ucs2_to_utf8(Value, BufferLength > 0 ? BufferLength / WCLEN : BufferLength, &vallen, FALSE);
val_alloced = TRUE;
break;
}
}
if (!val_alloced)
{
uval = Value;
vallen = BufferLength;
}
ret = PGAPI_SetDescField(DescriptorHandle, RecNumber, FieldIdentifier,
uval, (SQLINTEGER) vallen);
if (val_alloced)
free(uval);
return ret;
}
RETCODE SQL_API
SQLGetDescFieldW(SQLHDESC hdesc, SQLSMALLINT iRecord, SQLSMALLINT iField,
PTR rgbValue, SQLINTEGER cbValueMax,
SQLINTEGER *pcbValue)
{
RETCODE ret;
SQLINTEGER blen = 0, bMax, *pcbV;
char *rgbV = NULL, *rgbVt;
MYLOG(0, "Entering\n");
switch (iField)
{
case SQL_DESC_BASE_COLUMN_NAME:
case SQL_DESC_BASE_TABLE_NAME:
case SQL_DESC_CATALOG_NAME:
case SQL_DESC_LABEL:
case SQL_DESC_LITERAL_PREFIX:
case SQL_DESC_LITERAL_SUFFIX:
case SQL_DESC_LOCAL_TYPE_NAME:
case SQL_DESC_NAME:
case SQL_DESC_SCHEMA_NAME:
case SQL_DESC_TABLE_NAME:
case SQL_DESC_TYPE_NAME:
bMax = cbValueMax * 3 / WCLEN;
rgbV = malloc(bMax + 1);
pcbV = &blen;
for (rgbVt = rgbV;; bMax = blen + 1, rgbVt = realloc(rgbV, bMax))
{
if (!rgbVt)
{
ret = SQL_ERROR;
break;
}
rgbV = rgbVt;
ret = PGAPI_GetDescField(hdesc, iRecord, iField, rgbV, bMax, pcbV);
if (SQL_SUCCESS_WITH_INFO != ret || blen < bMax)
break;
}
if (SQL_SUCCEEDED(ret))
{
blen = (SQLINTEGER) utf8_to_ucs2(rgbV, blen, (SQLWCHAR *) rgbValue, cbValueMax / WCLEN);
if (SQL_SUCCESS == ret && blen * WCLEN >= cbValueMax)
{
ret = SQL_SUCCESS_WITH_INFO;
DC_set_error(hdesc, STMT_TRUNCATED, "The buffer was too small for the rgbDesc.");
}
if (pcbValue)
*pcbValue = blen * WCLEN;
}
if (rgbV)
free(rgbV);
break;
default:
rgbV = rgbValue;
bMax = cbValueMax;
pcbV = pcbValue;
ret = PGAPI_GetDescField(hdesc, iRecord, iField, rgbV, bMax, pcbV);
break;
}
return ret;
}
RETCODE SQL_API
SQLGetDiagRecW(SQLSMALLINT fHandleType,
SQLHANDLE handle,
SQLSMALLINT iRecord,
SQLWCHAR *szSqlState,
SQLINTEGER *pfNativeError,
SQLWCHAR *szErrorMsg,
SQLSMALLINT cbErrorMsgMax,
SQLSMALLINT *pcbErrorMsg)
{
RETCODE ret;
SQLSMALLINT buflen, tlen;
char qstr_ansi[8], *mtxt = NULL;
MYLOG(0, "Entering\n");
buflen = 0;
if (szErrorMsg && cbErrorMsgMax > 0)
{
buflen = cbErrorMsgMax;
mtxt = malloc(buflen);
}
ret = PGAPI_GetDiagRec(fHandleType, handle, iRecord, (SQLCHAR *) qstr_ansi,
pfNativeError, (SQLCHAR *) mtxt, buflen, &tlen);
if (SQL_SUCCEEDED(ret))
{
if (szSqlState)
utf8_to_ucs2(qstr_ansi, -1, szSqlState, 6);
if (mtxt && tlen <= cbErrorMsgMax)
{
SQLULEN ulen = utf8_to_ucs2_lf(mtxt, tlen, FALSE, szErrorMsg, cbErrorMsgMax, TRUE);
if (ulen == (SQLULEN) -1)
tlen = (SQLSMALLINT) locale_to_sqlwchar((SQLWCHAR *) szErrorMsg, mtxt, cbErrorMsgMax, FALSE);
else
tlen = (SQLSMALLINT) ulen;
if (tlen >= cbErrorMsgMax)
ret = SQL_SUCCESS_WITH_INFO;
else if (tlen < 0)
{
char errc[32];
SPRINTF_FIXED(errc, "Error: SqlState=%s", qstr_ansi);
tlen = utf8_to_ucs2(errc, -1, szErrorMsg, cbErrorMsgMax);
}
}
if (pcbErrorMsg)
*pcbErrorMsg = tlen;
}
if (mtxt)
free(mtxt);
return ret;
}
SQLRETURN SQL_API
SQLColAttributeW(SQLHSTMT hstmt,
SQLUSMALLINT iCol,
SQLUSMALLINT iField,
SQLPOINTER pCharAttr,
SQLSMALLINT cbCharAttrMax,
SQLSMALLINT *pcbCharAttr,
#if defined(_WIN64) || defined(SQLCOLATTRIBUTE_SQLLEN)
SQLLEN *pNumAttr
#else
SQLPOINTER pNumAttr
#endif
)
{
CSTR func = "SQLColAttributeW";
RETCODE ret;
StatementClass *stmt = (StatementClass *) hstmt;
SQLSMALLINT *rgbL, blen = 0, bMax;
char *rgbD = NULL, *rgbDt;
MYLOG(0, "Entering\n");
if (SC_connection_lost_check(stmt, __FUNCTION__))
return SQL_ERROR;
ENTER_STMT_CS(stmt);
SC_clear_error(stmt);
StartRollbackState(stmt);
switch (iField)
{
case SQL_DESC_BASE_COLUMN_NAME:
case SQL_DESC_BASE_TABLE_NAME:
case SQL_DESC_CATALOG_NAME:
case SQL_DESC_LABEL:
case SQL_DESC_LITERAL_PREFIX:
case SQL_DESC_LITERAL_SUFFIX:
case SQL_DESC_LOCAL_TYPE_NAME:
case SQL_DESC_NAME:
case SQL_DESC_SCHEMA_NAME:
case SQL_DESC_TABLE_NAME:
case SQL_DESC_TYPE_NAME:
case SQL_COLUMN_NAME:
bMax = cbCharAttrMax * 3 / WCLEN;
rgbD = malloc(bMax);
rgbL = &blen;
for (rgbDt = rgbD;; bMax = blen + 1, rgbDt = realloc(rgbD, bMax))
{
if (!rgbDt)
{
ret = SQL_ERROR;
break;
}
rgbD = rgbDt;
ret = PGAPI_ColAttributes(hstmt, iCol, iField, rgbD,
bMax, rgbL, pNumAttr);
if (SQL_SUCCESS_WITH_INFO != ret || blen < bMax)
break;
}
if (SQL_SUCCEEDED(ret))
{
blen = (SQLSMALLINT) utf8_to_ucs2(rgbD, blen, (SQLWCHAR *) pCharAttr, cbCharAttrMax / WCLEN);
if (SQL_SUCCESS == ret && blen * WCLEN >= cbCharAttrMax)
{
ret = SQL_SUCCESS_WITH_INFO;
SC_set_error(stmt, STMT_TRUNCATED, "The buffer was too small for the pCharAttr.", func);
}
if (pcbCharAttr)
*pcbCharAttr = blen * WCLEN;
}
if (rgbD)
free(rgbD);
break;
default:
rgbD = pCharAttr;
bMax = cbCharAttrMax;
rgbL = pcbCharAttr;
ret = PGAPI_ColAttributes(hstmt, iCol, iField, rgbD,
bMax, rgbL, pNumAttr);
break;
}
ret = DiscardStatementSvp(stmt, ret, FALSE);
LEAVE_STMT_CS(stmt);
return ret;
}
RETCODE SQL_API
SQLGetDiagFieldW(SQLSMALLINT fHandleType,
SQLHANDLE handle,
SQLSMALLINT iRecord,
SQLSMALLINT fDiagField,
SQLPOINTER rgbDiagInfo,
SQLSMALLINT cbDiagInfoMax,
SQLSMALLINT *pcbDiagInfo)
{
RETCODE ret;
SQLSMALLINT *rgbL, blen = 0, bMax;
char *rgbD = NULL, *rgbDt;
MYLOG(0, "Entering Handle=(%u,%p) Rec=%d Id=%d info=(%p,%d)\n", fHandleType,
handle, iRecord, fDiagField, rgbDiagInfo, cbDiagInfoMax);
switch (fDiagField)
{
case SQL_DIAG_DYNAMIC_FUNCTION:
case SQL_DIAG_CLASS_ORIGIN:
case SQL_DIAG_CONNECTION_NAME:
case SQL_DIAG_MESSAGE_TEXT:
case SQL_DIAG_SERVER_NAME:
case SQL_DIAG_SQLSTATE:
case SQL_DIAG_SUBCLASS_ORIGIN:
bMax = cbDiagInfoMax * 3 / WCLEN + 1;
if (rgbD = malloc(bMax), !rgbD)
return SQL_ERROR;
rgbL = &blen;
for (rgbDt = rgbD;; bMax = blen + 1, rgbDt = realloc(rgbD, bMax))
{
if (!rgbDt)
{
free(rgbD);
return SQL_ERROR;
}
rgbD = rgbDt;
ret = PGAPI_GetDiagField(fHandleType, handle, iRecord, fDiagField, rgbD,
bMax, rgbL);
if (SQL_SUCCESS_WITH_INFO != ret || blen < bMax)
break;
}
if (SQL_SUCCEEDED(ret))
{
SQLULEN ulen = (SQLSMALLINT) utf8_to_ucs2_lf(rgbD, blen, FALSE, (SQLWCHAR *) rgbDiagInfo, cbDiagInfoMax / WCLEN, TRUE);
if (ulen == (SQLULEN) -1)
blen = (SQLSMALLINT) locale_to_sqlwchar((SQLWCHAR *) rgbDiagInfo, rgbD, cbDiagInfoMax / WCLEN, FALSE);
else
blen = (SQLSMALLINT) ulen;
if (SQL_SUCCESS == ret && blen * WCLEN >= cbDiagInfoMax)
ret = SQL_SUCCESS_WITH_INFO;
if (pcbDiagInfo)
{
*pcbDiagInfo = blen * WCLEN;
}
}
if (rgbD)
free(rgbD);
break;
default:
rgbD = rgbDiagInfo;
bMax = cbDiagInfoMax;
rgbL = pcbDiagInfo;
ret = PGAPI_GetDiagField(fHandleType, handle, iRecord, fDiagField, rgbD,
bMax, rgbL);
break;
}
return ret;
}
/* new function */
RETCODE SQL_API
SQLGetDescRecW(SQLHDESC DescriptorHandle,
SQLSMALLINT RecNumber, SQLWCHAR *Name,
SQLSMALLINT BufferLength, SQLSMALLINT *StringLength,
SQLSMALLINT *Type, SQLSMALLINT *SubType,
SQLLEN *Length, SQLSMALLINT *Precision,
SQLSMALLINT *Scale, SQLSMALLINT *Nullable)
{
RETCODE ret;
char *NameA = NULL;
SQLSMALLINT nlen;
MYLOG(0, "Entering h=%p rec=%d name=%p blen=%d\n", DescriptorHandle, RecNumber, Name, BufferLength);
MYLOG(0, "str=%p type=%p sub=%p len=%p prec=%p scale=%p null=%p\n", StringLength, Type, SubType, Length, Precision, Scale, Nullable);
if (BufferLength > 0)
NameA = malloc(BufferLength);
ret = PGAPI_GetDescRec(DescriptorHandle, RecNumber, (SQLCHAR *) NameA, BufferLength,
&nlen, Type, SubType, Length, Precision,
Scale, Nullable);
if (SQL_SUCCEEDED(ret))
{
if (NameA && nlen <= BufferLength)
{
SQLULEN ulen = utf8_to_ucs2_lf(NameA, nlen, FALSE, Name, BufferLength, TRUE);
if (ulen == (SQLULEN) -1)
nlen = (SQLSMALLINT) locale_to_sqlwchar((SQLWCHAR *) Name, NameA, BufferLength, FALSE);
else
nlen = (SQLSMALLINT) ulen;
if (nlen >= BufferLength)
ret = SQL_SUCCESS_WITH_INFO;
}
if (StringLength)
*StringLength = nlen;
}
if (NameA)
free(NameA);
return ret;
}
/* new function */
RETCODE SQL_API
SQLSetDescRecW(SQLHDESC DescriptorHandle,
SQLSMALLINT RecNumber, SQLSMALLINT Type,
SQLSMALLINT SubType, SQLLEN Length,
SQLSMALLINT Precision, SQLSMALLINT Scale,
PTR Data, SQLLEN *StringLength,
SQLLEN *Indicator)
{
RETCODE ret;
SQLLEN vallen;
char *uval = NULL;
BOOL val_alloced = FALSE;
MYLOG(0, "Entering h=%p rec=%d type=%d sub=%d len=" FORMAT_LEN " prec=%d scale=%d data=%p\n", DescriptorHandle, RecNumber, Type, SubType, Length, Precision, Scale, Data);
MYLOG(0, "str=%p ind=%p\n", StringLength, Indicator);
if (Length > 0 || SQL_NTS == Length)
{
uval = ucs2_to_utf8(Data, Length > 0 ? Length / WCLEN : Length, &vallen, FALSE);
val_alloced = TRUE;
}
if (!val_alloced)
{
uval = Data;
vallen = Length;
}
ret = PGAPI_SetDescRec(DescriptorHandle, RecNumber, Type,
SubType, Length, Precision, Scale, uval,
&vallen, Indicator);
if (val_alloced)
free(uval);
return ret;
}