SQLCHAR * dm_SQL_WtoU8 (SQLWCHAR * inStr, ssize_t size) { SQLCHAR *outStr = NULL; size_t len; if (inStr == NULL) return NULL; if (size == SQL_NTS) { outStr = strdup_WtoU8 (inStr); } else { len = calc_len_for_utf8 (inStr, size); if ((outStr = (SQLCHAR *) malloc (len + 1)) != NULL) { len = wcsntoutf8 (inStr, outStr, size, len, NULL); outStr[len] = '\0'; } } return outStr; }
NPError StatementObject::GetColumnName(int index, const char **retval) { NPError rc; SQLWCHAR colName[512]; if (!m_hstmt) { NPN_SetException(this, "Statement isn't initialized"); return NPERR_GENERIC_ERROR; } if (index < 0 || index >= m_columnCount) { NPN_SetException(this, "Column index is out of range"); return NPERR_GENERIC_ERROR; } index++; rc = CheckStmtRC(SQLDescribeColW (m_hstmt, index, colName, sizeof(colName)/sizeof(wchar_t), NULL, NULL, NULL, NULL, NULL)); if (rc == NPERR_NO_ERROR) *retval = strdup_WtoU8((const wchar_t *)colName); return rc; }
NPError StatementObject::GetVariant(int index, NPVariant *value) { if (!m_hstmt) { NPN_SetException(this, "Statement isn't initialized"); return NPERR_GENERIC_ERROR; } if (index < 0 || index >= m_columnCount) { NPN_SetException(this, "Column index is out of range"); return NPERR_GENERIC_ERROR; } SQLLEN cbInd; int type; NPError rc; GetColumnType(index, &type); index++; NULL_TO_NPVARIANT(*value); switch (type) { case VALUE_TYPE_BIT: { bool v_bool; rc = CheckStmtRC(SQLGetData(m_hstmt, index, SQL_C_BIT, &v_bool, 0, &cbInd)); if (rc != NPERR_NO_ERROR) return rc; if (cbInd != SQL_NULL_DATA) BOOLEAN_TO_NPVARIANT(v_bool, *value); break; } case VALUE_TYPE_TINYINT: case VALUE_TYPE_SMALLINT: case VALUE_TYPE_BIGINT: case VALUE_TYPE_INTEGER: { SQLINTEGER v_int32; rc = CheckStmtRC(SQLGetData(m_hstmt, index, SQL_C_LONG, &v_int32, 0, &cbInd)); if (rc != NPERR_NO_ERROR) return rc; if (cbInd != SQL_NULL_DATA) INT32_TO_NPVARIANT(v_int32, *value); break; } case VALUE_TYPE_FLOAT: case VALUE_TYPE_REAL: case VALUE_TYPE_DOUBLE: { double v_double; rc = CheckStmtRC(SQLGetData(m_hstmt, index, SQL_C_DOUBLE, &v_double, 0, &cbInd)); if (rc != NPERR_NO_ERROR) return rc; if (cbInd != SQL_NULL_DATA) DOUBLE_TO_NPVARIANT(v_double, *value); break; } /** case VALUE_TYPE_BINARY: case VALUE_TYPE_VARBINARY: case VALUE_TYPE_LONGVARBINARY: { const char hexString[] = "0123456789ABCDEF"; SQLLEN len; SQLWCHAR buf[32]; unsigned char *pin; char *pout; rc = CheckStmtRC(SQLGetData(m_hstmt, index, SQL_C_BINARY, buf, 0, &len)); if (rc != NPERR_NO_ERROR) return rc; if (len == SQL_NO_TOTAL) { const SQLLEN dwUnit = 8192; unsigned char *pBuff; unsigned char *pData = NULL; SQLLEN dwAvialable; SQLLEN bufSize = dwUnit; char *data = NULL; pData = (unsigned char *)NPN_MemAlloc(bufSize); if (!pData) { NPN_SetException(this, "Memory allocation error"); return NPERR_GENERIC_ERROR; } len = 0; pBuff = pData; while(1) { SQLRETURN ret; pBuff = pData + len; ret = SQLGetData(m_hstmt, index, SQL_C_BINARY, pBuff, dwUnit, &dwAvialable); if (ret == SQL_NO_DATA_FOUND) break; if ((rc = CheckStmtRC(ret)) != NPERR_NO_ERROR) return rc; if (dwAvialable < dwUnit) { len += dwAvialable; break; } len += dwUnit; bufSize += dwUnit; unsigned char* tmp = (unsigned char*)NPN_MemAlloc(bufSize); if (!pBuff) { NPN_SetException(this, "Memory allocation error"); return NPERR_GENERIC_ERROR; } memcpy(pBuff, pData, len); NPN_MemFree(pData); pData = pBuff; pBuff = pData + len; } data = (char*)NPN_MemAlloc(len*2+1); if (!data) { NPN_MemFree(pData); NPN_SetException(this, "Memory allocation error"); return NPERR_GENERIC_ERROR; } pout = data; pin = pData; while(len--) { unsigned char binVal = *pin++; *pout++ = hexString[(binVal >> 4) & 0xf]; *pout++ = hexString[binVal & 0xf]; } *pout = 0; NPN_MemFree(pData); STRING_TO_NPVARIANT(data, *value); } else if (len != SQL_NULL_DATA) { unsigned char *pData; SQLLEN bufSize = len + 1; char *data = NULL; pData = (unsigned char *)NPN_MemAlloc(bufSize); if (!pData) { NPN_SetException(this, "Memory allocation error"); return NPERR_GENERIC_ERROR; } rc = CheckStmtRC(SQLGetData(m_hstmt, index, SQL_C_BINARY, pData, bufSize, &len)); if (rc != NPERR_NO_ERROR) return rc; data = (char*)NPN_MemAlloc(len*2+1); if (!data) { NPN_MemFree(pData); NPN_SetException(this, "Memory allocation error"); return NPERR_GENERIC_ERROR; } pout = data; pin = pData; while(len--) { unsigned char binVal = *pin++; *pout++ = hexString[(binVal >> 4) & 0xf]; *pout++ = hexString[binVal & 0xf]; } *pout = 0; NPN_MemFree(pData); STRING_TO_NPVARIANT(data, *value); } break; } **/ case VALUE_TYPE_BINARY: case VALUE_TYPE_VARBINARY: case VALUE_TYPE_LONGVARBINARY: case VALUE_TYPE_GUID: case VALUE_TYPE_CHAR: case VALUE_TYPE_VARCHAR: case VALUE_TYPE_LONGVARCHAR: case VALUE_TYPE_WCHAR: case VALUE_TYPE_WVARCHAR: case VALUE_TYPE_WLONGVARCHAR: case VALUE_TYPE_UNKNOWN: case VALUE_TYPE_NUMERIC: case VALUE_TYPE_DECIMAL: case VALUE_TYPE_DATE: case VALUE_TYPE_TIME: case VALUE_TYPE_TIMESTAMP: default: { SQLLEN len; SQLLEN dtype; SQLWCHAR buf[32]; rc = CheckStmtRC(SQLColAttributesW (m_hstmt, index, SQL_COLUMN_TYPE, NULL, 0, NULL, &dtype)); if (rc != NPERR_NO_ERROR) return rc; if (dtype == SQL_LONGVARBINARY || dtype == SQL_VARBINARY || dtype == SQL_BINARY || dtype == SQL_LONGVARCHAR || dtype == SQL_VARCHAR || dtype == SQL_CHAR || dtype == SQL_WLONGVARCHAR || dtype == SQL_WVARCHAR || dtype == SQL_WCHAR) { rc = CheckStmtRC(SQLGetData(m_hstmt, index, SQL_C_WCHAR, buf, 0, &len)); if (rc != NPERR_NO_ERROR) return rc; if (len == SQL_NO_TOTAL) { const SQLLEN dwUnit = 4096*sizeof(SQLWCHAR)+sizeof(SQLWCHAR); unsigned char *pBuff = NULL; unsigned char *pData = NULL; SQLLEN dwAvialable; SQLLEN bufSize = dwUnit; char *data = NULL; SQLRETURN ret; pData = (unsigned char *)NPN_MemAlloc(bufSize); if (!pData) { NPN_SetException(this, "Memory allocation error"); return NPERR_GENERIC_ERROR; } len = 0; while(1) { pBuff = pData + len; ret = SQLGetData(m_hstmt, index, SQL_C_WCHAR, pBuff, dwUnit, &dwAvialable); if (ret == SQL_NO_DATA_FOUND) break; if ((rc = CheckStmtRC(ret)) != NPERR_NO_ERROR) return rc; if (dwAvialable < dwUnit) { len += dwAvialable; break; } len += dwUnit - sizeof(SQLWCHAR); bufSize += dwUnit - sizeof(SQLWCHAR); unsigned char* tmp = (unsigned char*)NPN_MemAlloc(bufSize); if (!tmp) { NPN_SetException(this, "Memory allocation error"); return NPERR_GENERIC_ERROR; } memcpy(tmp, pData, len); NPN_MemFree(pData); pData = tmp; } data = strdup_WtoU8((SQLWCHAR *)pData); if (!data) { NPN_MemFree(pData); NPN_SetException(this, "Memory allocation error"); return NPERR_GENERIC_ERROR; } NPN_MemFree(pData); STRING_TO_NPVARIANT(data, *value); } else if (len != SQL_NULL_DATA) { unsigned char *pData; SQLLEN bufSize = len + sizeof(SQLWCHAR); char *data = NULL; pData = (unsigned char *)NPN_MemAlloc(bufSize); if (!pData) { NPN_SetException(this, "Memory allocation error"); return NPERR_GENERIC_ERROR; } rc = CheckStmtRC(SQLGetData(m_hstmt, index, SQL_C_WCHAR, pData, bufSize, &len)); if (rc != NPERR_NO_ERROR) return rc; data = strdup_WtoU8((SQLWCHAR *)pData); if (!data) { NPN_MemFree(pData); NPN_SetException(this, "Memory allocation error"); return NPERR_GENERIC_ERROR; } NPN_MemFree(pData); STRING_TO_NPVARIANT(data, *value); } } else { rc = CheckStmtRC(SQLGetData(m_hstmt, index, SQL_C_WCHAR, buf, sizeof(buf), &len)); if (rc != NPERR_NO_ERROR) return rc; if (len != SQL_NULL_DATA) { const char *data = strdup_WtoU8(buf); if (!data) { NPN_SetException(this, "Memory allocation error"); return NPERR_GENERIC_ERROR; } STRING_TO_NPVARIANT(data, *value); } } break; } } return NPERR_NO_ERROR; }
const char * StatementObject::GetErrorState() { return strdup_WtoU8(m_errState); }
const char * StatementObject::GetErrorString() { return strdup_WtoU8(m_errMessage); }