90 SQLRETURN rc = SQL_SUCCESS;
92 rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &__henv);
93 if (rc != SQL_SUCCESS) {
95 ByteString::format(
"Unable to allocate an environment handle [%d]",
100 rc = SQLSetEnvAttr(__henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
101 if (rc != SQL_SUCCESS) {
108 rc = SQLAllocHandle(SQL_HANDLE_DBC, __henv, &__hdbc);
109 if (rc != SQL_SUCCESS) {
115 SQLSMALLINT length2 = 0;
116 rc = SQLDriverConnectA(__hdbc,
NULL,
117 (SQLCHAR*)_cons, (SQLSMALLINT)_conslen,
118 NULL, 0, &length2, SQL_DRIVER_NOPROMPT
120 if (!SQL_SUCCEEDED(rc)) {
126#if defined(__WINNT__) && !defined(strncasecmp)
127#define strncasecmp(s1,s2,n) _strnicmp(s1,s2,n)
130 SQLSMALLINT len =
sizeof(buf);
131 rc = SQLGetInfoA(__hdbc, SQL_DBMS_NAME, buf, len, &len);
132 if (rc != SQL_SUCCESS) {
136 __IS_MS_SS = strncasecmp(buf,
"Microsoft SQL Server",
__MIN(9, len)) == 0;
139 rc = SQLSetConnectAttrA(__hdbc, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, 0);
140 if (rc != SQL_SUCCESS) {
146 SQLUINTEGER GetDataSupport = 0;
149 rc = SQLGetInfoA(__hdbc, SQL_GETDATA_EXTENSIONS,
150 &GetDataSupport,
sizeof(GetDataSupport), &length);
153 GetDataSupport & SQL_GD_ANY_COLUMN,
154 GetDataSupport & SQL_GD_ANY_ORDER
157 GetDataSupport & SQL_GD_BLOCK,
158 GetDataSupport & SQL_GD_BOUND,
159 GetDataSupport & SQL_GD_OUTPUT_PARAMS
166 SQLRETURN rc_ = SQLFreeHandle(SQL_HANDLE_DBC, __hdbc);
167 if (rc_ != SQL_SUCCESS) {
198 SQLHSTMT hstmt =
NULL;
199 SQLRETURN rc = SQLAllocHandle(SQL_HANDLE_STMT, __hdbc, &hstmt);
200 if (rc != SQL_SUCCESS) {
205 rc = SQLExecDirectA(hstmt, (SQLCHAR*) _sql, (SQLINTEGER)_sqllen);
206 if (!SQL_SUCCEEDED(rc)) {
211 SQLRETURN rc_ = SQLFreeStmt(hstmt, SQL_CLOSE);
212 if (rc_ != SQL_SUCCESS) {
216 rc_ = SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
217 if (rc_ != SQL_SUCCESS) {
222 return rc == SQL_SUCCESS;
282#if 0 && defined(__WINNT__)
287 SQLRETURN rc = SQLGetInfoW(__hdbc, SQL_DBMS_NAME, buf,
__countof(buf, WCHAR) , &length);
288 if (rc != SQL_SUCCESS) {
292 sb.append(buf, length /
sizeof(WCHAR));
294 rc = SQLGetInfoW(__hdbc, SQL_DBMS_VER, buf,
__countof(buf, WCHAR) , &length);
295 if (rc != SQL_SUCCESS) {
300 sb.append(buf, length /
sizeof(WCHAR));
302 ByteString s = UTF8Encoder::encode(sb.toString());
303 if (s.length() < *_buflen) {
304 *_buflen = s.length();
306 strncpy(_buf, s.data(), *_buflen);
309 SQLSMALLINT size = (SQLSMALLINT)
__MIN(*_buflen, SHRT_MAX);
310 SQLSMALLINT offset = 0;
312 SQLRETURN rc = SQL_SUCCESS;
314 rc = SQLGetInfoA(__hdbc, SQL_DBMS_NAME,
315 _buf + offset, size - offset, &length);
316 if (rc != SQL_SUCCESS) {
321 if ((offset + length) >= size) {
327 *(_buf + offset) =
' ';
330 rc = SQLGetInfoA(__hdbc, SQL_DBMS_VER,
331 _buf + offset, size - offset, &length);
332 if (rc != SQL_SUCCESS) {
337 if ((offset + length) >= size) {
342 *_buflen = offset + length;
357 if (_rc == SQL_SUCCESS_WITH_INFO || _rc ==
SQL_ERROR) {
359 _htype == SQL_HANDLE_ENV
360 || _htype == SQL_HANDLE_DBC
361 || _htype == SQL_HANDLE_STMT
362 || _htype == SQL_HANDLE_DESC
367#if 0 && defined(__WINNT__)
374 SQLSMALLINT RecNumber = 0;
375 SQLINTEGER NativeError;
376 WCHAR SQLState[SQL_SQLSTATE_SIZE + 1];
377 WCHAR MessageText[1024];
378 SQLSMALLINT MessageTextLength;
380 while ((rc = SQLGetDiagRecW(
381 _htype, _handle, ++RecNumber, SQLState, &NativeError,
382 MessageText,
__countof(MessageText, WCHAR), &MessageTextLength
384 if (sb.length() > 0) {
387 sb.format(L
"[%d][%ls]", RecNumber, SQLState);
388 sb.append(MessageText, (
size_t) MessageTextLength);
391 String s = sb.toString();
393 if (_rc == SQL_SUCCESS_WITH_INFO) {
395 s.replace(L
"\n", L
"\n ").data());
399 __lastErrorMessage = UTF8Encoder::encode(s);
402 __DCL_TRACE1(L
"Fatal!! [%ls]\n", _e->toString().data());
406 ByteStringBuilder sb;
407 SQLSMALLINT RecNumber = 0;
408 SQLINTEGER NativeError;
409 SQLCHAR SQLState[SQL_SQLSTATE_SIZE + 1];
410 SQLCHAR ErrorMsg[1024];
411 SQLSMALLINT ErrorMsgLen;
413 while ((rc = SQLGetDiagRecA(
414 _htype, _handle, ++RecNumber, SQLState, &NativeError,
415 ErrorMsg,
__countof(ErrorMsg, SQLCHAR), &ErrorMsgLen
417 if (sb.length() > 0) {
420 sb.format(
"[%d][%hs]", RecNumber, SQLState);
421 sb.append((
const char*)ErrorMsg, (
size_t)ErrorMsgLen);
431 String ws = LocaleDecoder::decode(sb.toByteString());
432 s = UTF8Encoder::encode(ws);
434 if (_rc == SQL_SUCCESS_WITH_INFO) {
436 ws.replace(L
"\n", L
"\n ").data());
441 __DCL_TRACE1(L
"Fatal!! [%ls]\n", _e->toString().data());
445 s = sb.toByteString();
447 if (_rc == SQL_SUCCESS_WITH_INFO) {
449 s.replace(
"\n",
"\n ").data());
453 __lastErrorMessage = s;
457 #define CASE_STR(rc) case rc: __lastErrorMessage = #rc; break;
466 __lastErrorMessage = ByteString::format(
"SQLRETURN[%d] mabe bug!!", _rc);