DON'T USE THIS REPO: IT'S OBSOLETE.
[versaplex.git] / vxodbc / odbcapi30w.cc
blob1ad8eec8b5299ed4726489164dc8c83b201ae7b2
1 /*
2 * Description: This module contains UNICODE routines
3 */
5 #include "psqlodbc.h"
7 #include <stdio.h>
8 #include <string.h>
10 #include "pgapifunc.h"
11 #include "connection.h"
12 #include "statement.h"
15 RETCODE SQL_API SQLGetStmtAttrW(SQLHSTMT hstmt,
16 SQLINTEGER fAttribute,
17 PTR rgbValue,
18 SQLINTEGER cbValueMax,
19 SQLINTEGER * pcbValue)
21 RETCODE ret;
22 StatementClass *stmt = (StatementClass *) hstmt;
23 mylog("Start\n");
25 ENTER_STMT_CS((StatementClass *) hstmt);
26 SC_clear_error((StatementClass *) hstmt);
27 StartRollbackState(stmt);
28 ret = PGAPI_GetStmtAttr(hstmt, fAttribute, rgbValue,
29 cbValueMax, pcbValue);
30 ret = DiscardStatementSvp(stmt, ret, FALSE);
31 LEAVE_STMT_CS((StatementClass *) hstmt);
32 return ret;
35 RETCODE SQL_API SQLSetStmtAttrW(SQLHSTMT hstmt,
36 SQLINTEGER fAttribute,
37 PTR rgbValue, SQLINTEGER cbValueMax)
39 RETCODE ret;
40 StatementClass *stmt = (StatementClass *) hstmt;
41 mylog("Start\n");
43 ENTER_STMT_CS(stmt);
44 SC_clear_error(stmt);
45 StartRollbackState(stmt);
46 ret = PGAPI_SetStmtAttr(hstmt, fAttribute, rgbValue, cbValueMax);
47 ret = DiscardStatementSvp(stmt, ret, FALSE);
48 LEAVE_STMT_CS(stmt);
49 return ret;
52 RETCODE SQL_API SQLGetConnectAttrW(HDBC hdbc,
53 SQLINTEGER fAttribute,
54 PTR rgbValue,
55 SQLINTEGER cbValueMax,
56 SQLINTEGER * pcbValue)
58 RETCODE ret;
59 mylog("Start\n");
61 ENTER_CONN_CS((ConnectionClass *) hdbc);
62 CC_clear_error((ConnectionClass *) hdbc);
63 ret = PGAPI_GetConnectAttr(hdbc, fAttribute, rgbValue,
64 cbValueMax, pcbValue);
65 LEAVE_CONN_CS((ConnectionClass *) hdbc);
66 return ret;
69 RETCODE SQL_API SQLSetConnectAttrW(HDBC hdbc,
70 SQLINTEGER fAttribute,
71 PTR rgbValue, SQLINTEGER cbValue)
73 RETCODE ret;
74 ConnectionClass *conn = (ConnectionClass *) hdbc;
75 mylog("Start\n");
77 ENTER_CONN_CS(conn);
78 CC_clear_error(conn);
79 CC_set_in_unicode_driver(conn);
80 ret = PGAPI_SetConnectAttr(hdbc, fAttribute, rgbValue, cbValue);
81 LEAVE_CONN_CS(conn);
82 return ret;
85 /* new function */
86 RETCODE SQL_API
87 SQLSetDescFieldW(SQLHDESC DescriptorHandle, SQLSMALLINT RecNumber,
88 SQLSMALLINT FieldIdentifier, PTR Value,
89 SQLINTEGER BufferLength)
91 RETCODE ret;
92 SQLLEN vallen;
93 char *uval = NULL;
94 BOOL val_alloced = FALSE;
95 mylog("Start\n");
97 if (BufferLength > 0 || SQL_NTS == BufferLength)
99 switch (FieldIdentifier)
101 case SQL_DESC_BASE_COLUMN_NAME:
102 case SQL_DESC_BASE_TABLE_NAME:
103 case SQL_DESC_CATALOG_NAME:
104 case SQL_DESC_LABEL:
105 case SQL_DESC_LITERAL_PREFIX:
106 case SQL_DESC_LITERAL_SUFFIX:
107 case SQL_DESC_LOCAL_TYPE_NAME:
108 case SQL_DESC_NAME:
109 case SQL_DESC_SCHEMA_NAME:
110 case SQL_DESC_TABLE_NAME:
111 case SQL_DESC_TYPE_NAME:
112 uval =
113 ucs2_to_utf8((const SQLWCHAR *)Value,
114 BufferLength >
115 0 ? BufferLength / WCLEN : BufferLength,
116 &vallen, FALSE);
117 val_alloced = TRUE;
118 break;
121 if (!val_alloced)
123 uval = (char *)Value;
124 vallen = BufferLength;
126 ret =
127 PGAPI_SetDescField(DescriptorHandle, RecNumber, FieldIdentifier,
128 uval, (SQLINTEGER) vallen);
129 if (val_alloced)
130 free(uval);
131 return ret;
134 RETCODE SQL_API
135 SQLGetDescFieldW(SQLHDESC hdesc, SQLSMALLINT iRecord,
136 SQLSMALLINT iField, PTR rgbValue,
137 SQLINTEGER cbValueMax, SQLINTEGER * pcbValue)
139 RETCODE ret;
140 SQLINTEGER blen = 0, bMax, *pcbV;
141 char *rgbV = NULL;
142 mylog("Start\n");
144 switch (iField)
146 case SQL_DESC_BASE_COLUMN_NAME:
147 case SQL_DESC_BASE_TABLE_NAME:
148 case SQL_DESC_CATALOG_NAME:
149 case SQL_DESC_LABEL:
150 case SQL_DESC_LITERAL_PREFIX:
151 case SQL_DESC_LITERAL_SUFFIX:
152 case SQL_DESC_LOCAL_TYPE_NAME:
153 case SQL_DESC_NAME:
154 case SQL_DESC_SCHEMA_NAME:
155 case SQL_DESC_TABLE_NAME:
156 case SQL_DESC_TYPE_NAME:
157 bMax = cbValueMax * 3 / WCLEN;
158 rgbV = (char *)malloc(bMax + 1);
159 pcbV = &blen;
160 for (;; bMax = blen + 1, rgbV = (char *)realloc(rgbV, bMax))
162 ret =
163 PGAPI_GetDescField(hdesc, iRecord, iField, rgbV, bMax,
164 pcbV);
165 if (SQL_SUCCESS_WITH_INFO != ret || blen < bMax)
166 break;
168 if (SQL_SUCCEEDED(ret))
170 blen =
171 (SQLINTEGER) utf8_to_ucs2(rgbV, blen,
172 (SQLWCHAR *) rgbValue,
173 cbValueMax / WCLEN);
174 if (SQL_SUCCESS == ret && blen * WCLEN >= cbValueMax)
176 ret = SQL_SUCCESS_WITH_INFO;
177 DC_set_error((DescriptorClass *)hdesc, STMT_TRUNCATED,
178 "The buffer was too small for the rgbDesc.");
180 if (pcbValue)
181 *pcbValue = blen * WCLEN;
183 if (rgbV)
184 free(rgbV);
185 break;
186 default:
187 rgbV = (char *)rgbValue;
188 bMax = cbValueMax;
189 pcbV = pcbValue;
190 ret =
191 PGAPI_GetDescField(hdesc, iRecord, iField, rgbV, bMax,
192 pcbV);
193 break;
196 return ret;
199 RETCODE SQL_API SQLGetDiagRecW(SQLSMALLINT fHandleType,
200 SQLHANDLE handle,
201 SQLSMALLINT iRecord,
202 SQLWCHAR * szSqlState,
203 SQLINTEGER * pfNativeError,
204 SQLWCHAR * szErrorMsg,
205 SQLSMALLINT cbErrorMsgMax,
206 SQLSMALLINT * pcbErrorMsg)
208 RETCODE ret;
209 SQLSMALLINT buflen, tlen;
210 char *qstr = NULL, *mtxt = NULL;
211 mylog("Start\n");
213 if (szSqlState)
214 qstr = (char *)malloc(8);
215 buflen = 0;
216 if (szErrorMsg && cbErrorMsgMax > 0)
218 buflen = cbErrorMsgMax;
219 mtxt = (char *)malloc(buflen);
221 ret = PGAPI_GetDiagRec(fHandleType, handle, iRecord,
222 (SQLCHAR *)qstr, pfNativeError,
223 (SQLCHAR *)mtxt, buflen, &tlen);
224 if (SQL_SUCCEEDED(ret))
226 if (qstr)
227 utf8_to_ucs2(qstr, strlen(qstr), szSqlState, 6);
228 if (mtxt && tlen <= cbErrorMsgMax)
230 tlen =
231 (SQLSMALLINT) utf8_to_ucs2(mtxt, tlen, szErrorMsg,
232 cbErrorMsgMax);
233 if (tlen >= cbErrorMsgMax)
234 ret = SQL_SUCCESS_WITH_INFO;
236 if (pcbErrorMsg)
237 *pcbErrorMsg = tlen;
239 if (qstr)
240 free(qstr);
241 if (mtxt)
242 free(mtxt);
243 return ret;
246 SQLRETURN SQL_API SQLColAttributeW(SQLHSTMT hstmt,
247 SQLUSMALLINT iCol,
248 SQLUSMALLINT iField,
249 SQLPOINTER pCharAttr,
250 SQLSMALLINT cbCharAttrMax,
251 SQLSMALLINT * pcbCharAttr,
252 #if defined(WITH_UNIXODBC) || (defined(WIN32) && ! defined(_WIN64))
253 SQLPOINTER pNumAttr
254 #else
255 SQLLEN * pNumAttr
256 #endif
259 CSTR func = "SQLColAttributeW";
260 RETCODE ret;
261 StatementClass *stmt = (StatementClass *) hstmt;
262 SQLSMALLINT *rgbL, blen = 0, bMax;
263 char *rgbD = NULL;
264 mylog("Start\n");
266 ENTER_STMT_CS(stmt);
267 SC_clear_error(stmt);
268 StartRollbackState(stmt);
269 switch (iField)
271 case SQL_DESC_BASE_COLUMN_NAME:
272 case SQL_DESC_BASE_TABLE_NAME:
273 case SQL_DESC_CATALOG_NAME:
274 case SQL_DESC_LABEL:
275 case SQL_DESC_LITERAL_PREFIX:
276 case SQL_DESC_LITERAL_SUFFIX:
277 case SQL_DESC_LOCAL_TYPE_NAME:
278 case SQL_DESC_NAME:
279 case SQL_DESC_SCHEMA_NAME:
280 case SQL_DESC_TABLE_NAME:
281 case SQL_DESC_TYPE_NAME:
282 case SQL_COLUMN_NAME:
283 bMax = cbCharAttrMax * 3 / WCLEN;
284 rgbD = (char *)malloc(bMax);
285 rgbL = &blen;
286 for (;; bMax = blen + 1, rgbD = (char *)realloc(rgbD, bMax))
288 ret = PGAPI_ColAttributes(hstmt, iCol, iField, rgbD,
289 bMax, rgbL, (SQLINTEGER *)pNumAttr);
290 if (SQL_SUCCESS_WITH_INFO != ret || blen < bMax)
291 break;
293 if (SQL_SUCCEEDED(ret))
295 blen =
296 (SQLSMALLINT) utf8_to_ucs2(rgbD, blen,
297 (SQLWCHAR *) pCharAttr,
298 cbCharAttrMax / WCLEN);
299 if (SQL_SUCCESS == ret && blen * WCLEN >= cbCharAttrMax)
301 ret = SQL_SUCCESS_WITH_INFO;
302 SC_set_error(stmt, STMT_TRUNCATED,
303 "The buffer was too small for the pCharAttr.",
304 func);
306 if (pcbCharAttr)
307 *pcbCharAttr = blen * WCLEN;
309 if (rgbD)
310 free(rgbD);
311 break;
312 default:
313 rgbD = (char *)pCharAttr;
314 bMax = cbCharAttrMax;
315 rgbL = pcbCharAttr;
316 ret = PGAPI_ColAttributes(hstmt, iCol, iField, rgbD,
317 bMax, rgbL, (SQLINTEGER *)pNumAttr);
318 break;
320 ret = DiscardStatementSvp(stmt, ret, FALSE);
321 LEAVE_STMT_CS(stmt);
323 return ret;
326 RETCODE SQL_API SQLGetDiagFieldW(SQLSMALLINT fHandleType,
327 SQLHANDLE handle,
328 SQLSMALLINT iRecord,
329 SQLSMALLINT fDiagField,
330 SQLPOINTER rgbDiagInfo,
331 SQLSMALLINT cbDiagInfoMax,
332 SQLSMALLINT * pcbDiagInfo)
334 RETCODE ret;
335 SQLSMALLINT *rgbL, blen = 0, bMax;
336 char *rgbD = NULL;
337 mylog("Start\n");
339 mylog("Handle=(%u,%p) Rec=%d Id=%d info=(%p,%d)\n",
340 fHandleType, handle, iRecord, fDiagField, rgbDiagInfo,
341 cbDiagInfoMax);
342 switch (fDiagField)
344 case SQL_DIAG_DYNAMIC_FUNCTION:
345 case SQL_DIAG_CLASS_ORIGIN:
346 case SQL_DIAG_CONNECTION_NAME:
347 case SQL_DIAG_MESSAGE_TEXT:
348 case SQL_DIAG_SERVER_NAME:
349 case SQL_DIAG_SQLSTATE:
350 case SQL_DIAG_SUBCLASS_ORIGIN:
351 bMax = cbDiagInfoMax * 3 / WCLEN + 1;
352 if (rgbD = (char *)malloc(bMax), !rgbD)
353 return SQL_ERROR;
354 rgbL = &blen;
355 for (;; bMax = blen + 1, rgbD = (char *)realloc(rgbD, bMax))
357 ret =
358 PGAPI_GetDiagField(fHandleType, handle, iRecord,
359 fDiagField, rgbD, bMax, rgbL);
360 if (SQL_SUCCESS_WITH_INFO != ret || blen < bMax)
361 break;
363 if (SQL_SUCCEEDED(ret))
365 blen =
366 (SQLSMALLINT) utf8_to_ucs2(rgbD, blen,
367 (SQLWCHAR *) rgbDiagInfo,
368 cbDiagInfoMax / WCLEN);
369 if (SQL_SUCCESS == ret && blen * WCLEN >= cbDiagInfoMax)
370 ret = SQL_SUCCESS_WITH_INFO;
371 if (pcbDiagInfo)
373 #ifdef WIN32
374 extern int platformId;
376 if (VER_PLATFORM_WIN32_WINDOWS == platformId
377 && NULL == rgbDiagInfo && 0 == cbDiagInfoMax)
378 blen++;
379 #endif /* WIN32 */
380 *pcbDiagInfo = blen * WCLEN;
383 if (rgbD)
384 free(rgbD);
385 break;
386 default:
387 rgbD = (char *)rgbDiagInfo;
388 bMax = cbDiagInfoMax;
389 rgbL = pcbDiagInfo;
390 ret =
391 PGAPI_GetDiagField(fHandleType, handle, iRecord, fDiagField,
392 rgbD, bMax, rgbL);
393 break;
396 return ret;