CString CSQLDirect::GetCol( int nCol ) { CString svValue; // Due to the nature of the SQL API, repeated calls to the same column will result in a NULL value passed // back into svData. We need to keep track of the columns that have been used already and if so, pass back // the data stored in our array. int nIndex=FindColumn( nCol ); if( nIndex==-1 ) { // Get the column from the SQL cursor. UCHAR svData[8192]; SDWORD cbDataLen; SQLGetData( m_hStmt,nCol,GetColumnType( nCol ),&svData,8192,&cbDataLen ); svValue=svData; svValue.TrimRight(); AddColumn( nCol,svValue ); } else { // Get the value from the column array. svValue=GetColumn( nCol )->m_svValue; } return svValue; }
void ReadValue( sqlite3_stmt* stmt, Value& vl, int col, EValueType type ) { if( type == kUnknown ) type = GetColumnType( stmt, col ); switch( type ) { case kDouble : vl = sqlite3_column_double( stmt, col ); break; case kInt : vl = sqlite3_column_int( stmt, col ); break; case kText : { vl.mType = kText; vl.mStr.clear(); const char* s = (const char*) sqlite3_column_text( stmt, col ); FROM_UTF8( s, vl.mStr ); } break; case kBLOB : /* TODO */ default: vl.mType = kUnknown; break; } }
void ReadTableStructure( Type& type, sqlite3* db, const wstring& query ) { type.mNotNULL = false; sqlite3_stmt* stmt = SqliteSelect( db, query ); if( stmt ) { int count = sqlite3_column_count( stmt ); for( int i = 0 ; i < count ; ++i ) { const char* name = sqlite3_column_name( stmt, i ); Member fld; FROM_UTF8( name, fld.mName ); ToLower( fld.mName ); fld.mNotNULL = false; fld.mType = GetColumnType( stmt, i ); type.mMembers.push_back( fld ); } FINALIZE_STATEMENT( stmt ); } }
/* Traverse and sort the columns, the sort order is in the internal array use bSetSort as true for a call from outside the class */ void CMultiColumnSortListCtrl::SortCombinedColumns(bool bSetSort /*= false*/) { if( bSetSort ) { m_bSorting = true; } int iNumCombinedSortedCols = GetNumCombinedSortedColumns(); for( int nIndex = iNumCombinedSortedCols - 1; nIndex >= 0 ; nIndex-- ) { SORT_STATE ssEachItem = GetItemSortState( m_aCombinedSortedColumns[nIndex] ); SORT_TYPE sortType = GetColumnType(m_aCombinedSortedColumns[nIndex]); CSortClass csc( this, m_aCombinedSortedColumns[nIndex], sortType ); csc.Sort( DESCENDING == ssEachItem ); //Ariel Benary <*****@*****.**> //refresh the sort image m_ctlHeaderCtrl.SetSortImage( m_aCombinedSortedColumns[nIndex], ssEachItem ); } if( bSetSort ) { m_bSorting = false; } }
EValueType GetColumnType( sqlite3* db, const wstring& table, const wstring& field ) { EValueType res = kUnknown; wstring query = L"SELECT [" + field + L"] FROM [" + table + L"] LIMIT 1"; sqlite3_stmt* stmt = SqliteSelect( db, query ); if( stmt ) { res = GetColumnType( stmt, 0 ); FINALIZE_STATEMENT( stmt ); } return res; }
/*--------------------------------------------------------------------------------*/ bool PostgresDatabase::CreateTable(const AString& name, const AString& columns) { AString sql; SQLQuery *query = NULL; uint_t i, n = columns.CountColumns(); sql.printf("create table %s (", name.str()); for (i = 0; i < n; i++) { AString column = columns.Column(i); if (i > 0) sql.printf(", "); sql.printf("%s %s", column.Word(0).str(), GetColumnType(column).str()); } sql.printf(")"); if ((query = RunQuery(sql)) != NULL) { bool success = query->GetResult(); delete query; return success; } return false; }
TdsColumnData::TdsColumnData(TDSCOLUMN* pColumn) { m_nColumnType = GetColumnType(pColumn); m_nColumnSize = GetColumnSize(pColumn); m_strColumnName = GetColumnName(pColumn); }
bool DBRowDescriptor::VerifyValue( uint32 index, PyRep* value ) { return DBTYPE_IsCompatible( GetColumnType( index ), value ); }
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; }
bool StatementObject::Invoke(NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result) { //sprintf(tmp, "stmt Invoke [%s]\n", NPN_UTF8FromIdentifier(name)); log(tmp); NPError rc; VOID_TO_NPVARIANT(*result); int index = 0; if (name == mc_Init_id) { if (argCount < 2) { NPN_SetException(this, "Too few parameters count"); return true; } #if 0 if (!(NPVARIANT_IS_INT32(args[0]) && NPVARIANT_IS_OBJECT(args[1]))) { NPN_SetException(this, "Wrong argument type"); return true; } SQLHANDLE hdbc = (SQLHANDLE)(long)NPVARIANT_TO_INT32(args[0]); #else if (!(NPVARIANT_IS_OBJECT(args[0]) && NPVARIANT_IS_OBJECT(args[1]))) { NPN_SetException(this, "Wrong argument type"); return true; } SQLHANDLE hdbc = (SQLHANDLE)NPVARIANT_TO_OBJECT(args[0]); #endif Init(hdbc, NPVARIANT_TO_OBJECT(args[1])); return true; } else if (name == mc_AddParameter_id) { if (argCount < 1) { NPN_SetException(this, "Too few parameters count"); return true; } AddParam(args); return true; } else if (name == mc_Execute_id) { if (argCount < 1) { NPN_SetException(this, "Too few parameters count"); return true; } if (!NPVARIANT_IS_STRING(args[0])) { NPN_SetException(this, "Wrong argument type"); return true; } Execute((NPString*)&NPVARIANT_TO_STRING(args[0])); return true; } else if (name == mc_Close_id) { Close(); return true; } else if (name == mc_Fetch_id) { bool ret; rc = Fetch(&ret); if (rc != NPERR_NO_ERROR) return true; BOOLEAN_TO_NPVARIANT(ret, *result); return true; } else if (name == mc_MoreResults_id) { bool ret; rc = MoreResults(&ret); if (rc != NPERR_NO_ERROR) return true; BOOLEAN_TO_NPVARIANT(ret, *result); return true; } else if (name == mc_GetColumnName_id) { const char *ret; if (argCount < 1) { NPN_SetException(this, "Too few parameters count"); return true; } if (NPVARIANT_IS_INT32(args[0])) index = NPVARIANT_TO_INT32(args[0]); else if (NPVARIANT_IS_DOUBLE(args[0])) index = (int)NPVARIANT_TO_DOUBLE(args[0]); else { NPN_SetException(this, "Wrong argument type"); return true; } rc = GetColumnName(index, &ret); if (rc != NPERR_NO_ERROR) return true; STRING_TO_NPVARIANT(ret, *result); return true; } else if (name == mc_GetVariant_id) { if (argCount < 1) { NPN_SetException(this, "Too few parameters count"); return true; } if (NPVARIANT_IS_INT32(args[0])) index = NPVARIANT_TO_INT32(args[0]); else if (NPVARIANT_IS_DOUBLE(args[0])) index = (int)NPVARIANT_TO_DOUBLE(args[0]); else { NPN_SetException(this, "Wrong argument type"); return true; } GetVariant(index, result); return true; } else if (name == mc_GetColumnType_id) { int ret; if (argCount < 1) { NPN_SetException(this, "Too few parameters count"); return true; } if (NPVARIANT_IS_INT32(args[0])) index = NPVARIANT_TO_INT32(args[0]); else if (NPVARIANT_IS_DOUBLE(args[0])) index = (int)NPVARIANT_TO_DOUBLE(args[0]); else { NPN_SetException(this, "Wrong argument type"); return true; } rc = GetColumnType(index, &ret); if (rc != NPERR_NO_ERROR) return true; INT32_TO_NPVARIANT(ret, *result); return true; } else if (name == mc_IsColumnNullable_id) { bool ret; if (argCount < 1) { NPN_SetException(this, "Too few parameters count"); return true; } if (NPVARIANT_IS_INT32(args[0])) index = NPVARIANT_TO_INT32(args[0]); else if (NPVARIANT_IS_DOUBLE(args[0])) index = (int)NPVARIANT_TO_DOUBLE(args[0]); else { NPN_SetException(this, "Wrong argument type"); return true; } rc = IsColumnNullable(index, &ret); if (rc != NPERR_NO_ERROR) return true; BOOLEAN_TO_NPVARIANT(ret, *result); return true; } else if (name == mc_GetTables_id) { if (argCount < 4) { NPN_SetException(this, "Too few parameters count"); return true; } if (!(NPVARIANT_IS_NULL(args[0]) || NPVARIANT_IS_STRING(args[0]))) { NPN_SetException(this, "Wrong 1 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[1]) || NPVARIANT_IS_STRING(args[1]))) { NPN_SetException(this, "Wrong 2 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[2]) || NPVARIANT_IS_STRING(args[2]))) { NPN_SetException(this, "Wrong 3 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[3]) || NPVARIANT_IS_STRING(args[3]))) { NPN_SetException(this, "Wrong 4 argument type"); return true; } GetTables(&args[0], &args[1], &args[2], &args[3]); return true; } else if (name == mc_GetColumns_id) { if (argCount < 4) { NPN_SetException(this, "Too few parameters count"); return true; } if (!(NPVARIANT_IS_NULL(args[0]) || NPVARIANT_IS_STRING(args[0]))) { NPN_SetException(this, "Wrong 1 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[1]) || NPVARIANT_IS_STRING(args[1]))) { NPN_SetException(this, "Wrong 2 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[2]) || NPVARIANT_IS_STRING(args[2]))) { NPN_SetException(this, "Wrong 3 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[3]) || NPVARIANT_IS_STRING(args[3]))) { NPN_SetException(this, "Wrong 4 argument type"); return true; } GetColumns(&args[0], &args[1], &args[2], &args[3]); return true; } else if (name == mc_GetTypeInfo_id) { if (argCount < 1) { NPN_SetException(this, "Too few parameters count"); return true; } if (NPVARIANT_IS_INT32(args[0])) index = NPVARIANT_TO_INT32(args[0]); else if (NPVARIANT_IS_DOUBLE(args[0])) index = (int)NPVARIANT_TO_DOUBLE(args[0]); else { NPN_SetException(this, "Wrong argument type"); return true; } GetTypeInfo(index); return true; } else if (name == mc_GetPrimaryKeys_id) { if (argCount < 3) { NPN_SetException(this, "Too few parameters count"); return true; } if (!(NPVARIANT_IS_NULL(args[0]) || NPVARIANT_IS_STRING(args[0]))) { NPN_SetException(this, "Wrong 1 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[1]) || NPVARIANT_IS_STRING(args[1]))) { NPN_SetException(this, "Wrong 2 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[2]) || NPVARIANT_IS_STRING(args[2]))) { NPN_SetException(this, "Wrong 3 argument type"); return true; } GetPrimaryKeys(&args[0], &args[1], &args[2]); return true; } else if (name == mc_GetForeignKeys_id) { if (argCount < 6) { NPN_SetException(this, "Too few parameters count"); return true; } if (!(NPVARIANT_IS_NULL(args[0]) || NPVARIANT_IS_STRING(args[0]))) { NPN_SetException(this, "Wrong 1 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[1]) || NPVARIANT_IS_STRING(args[1]))) { NPN_SetException(this, "Wrong 2 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[2]) || NPVARIANT_IS_STRING(args[2]))) { NPN_SetException(this, "Wrong 3 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[3]) || NPVARIANT_IS_STRING(args[3]))) { NPN_SetException(this, "Wrong 4 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[4]) || NPVARIANT_IS_STRING(args[4]))) { NPN_SetException(this, "Wrong 5 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[5]) || NPVARIANT_IS_STRING(args[5]))) { NPN_SetException(this, "Wrong 6 argument type"); return true; } GetForeignKeys(&args[0], &args[1], &args[2], &args[3], &args[4], &args[5]); return true; } else if (name == mc_GetProcedures_id) { if (argCount < 3) { NPN_SetException(this, "Too few parameters count"); return true; } if (!(NPVARIANT_IS_NULL(args[0]) || NPVARIANT_IS_STRING(args[0]))) { NPN_SetException(this, "Wrong 1 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[1]) || NPVARIANT_IS_STRING(args[1]))) { NPN_SetException(this, "Wrong 2 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[2]) || NPVARIANT_IS_STRING(args[2]))) { NPN_SetException(this, "Wrong 3 argument type"); return true; } GetProcedures(&args[0], &args[1], &args[2]); return true; } else if (name == mc_GetProcedureColumns_id) { if (argCount < 4) { NPN_SetException(this, "Too few parameters count"); return true; } if (!(NPVARIANT_IS_NULL(args[0]) || NPVARIANT_IS_STRING(args[0]))) { NPN_SetException(this, "Wrong 1 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[1]) || NPVARIANT_IS_STRING(args[1]))) { NPN_SetException(this, "Wrong 2 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[2]) || NPVARIANT_IS_STRING(args[2]))) { NPN_SetException(this, "Wrong 3 argument type"); return true; } if (!(NPVARIANT_IS_NULL(args[3]) || NPVARIANT_IS_STRING(args[3]))) { NPN_SetException(this, "Wrong 4 argument type"); return true; } GetProcedureColumns(&args[0], &args[1], &args[2], &args[3]); return true; } return false; }