/* * Obtain physical row ID at current WorkArea cursor position. */ static HB_ERRCODE hb_delimRecId( DELIMAREAP pArea, PHB_ITEM pRecNo ) { HB_ERRCODE errCode; ULONG ulRecNo; HB_TRACE(HB_TR_DEBUG, ("hb_delimRecId(%p,%p)", pArea, pRecNo)); errCode = SELF_RECNO( ( AREAP ) pArea, &ulRecNo ); #ifdef HB_C52_STRICT /* this is for strict Clipper compatibility but IMHO Clipper should not do that and always set fixed size independent to the record number */ if( ulRecNo < 10000000 ) { hb_itemPutNLLen( pRecNo, ulRecNo, 7 ); } else { hb_itemPutNLLen( pRecNo, ulRecNo, 10 ); } #else hb_itemPutNInt( pRecNo, ulRecNo ); #endif return errCode; }
static HB_ERRCODE pgsqlGetValue( SQLBASEAREAP pArea, HB_USHORT uiIndex, PHB_ITEM pItem ) { SDDDATA * pSDDData = ( SDDDATA * ) pArea->pSDDData; LPFIELD pField; char * pValue; HB_BOOL bError; PHB_ITEM pError; HB_SIZE ulLen; bError = HB_FALSE; uiIndex--; pField = pArea->area.lpFields + uiIndex; if( PQgetisnull( pSDDData->pResult, pArea->ulRecNo - 1, uiIndex ) ) return HB_SUCCESS; pValue = PQgetvalue( pSDDData->pResult, pArea->ulRecNo - 1, uiIndex ); ulLen = ( HB_SIZE ) PQgetlength( pSDDData->pResult, pArea->ulRecNo - 1, uiIndex ); /* printf( "fieldget recno:%d index:%d value:%s len:%d\n", pArea->ulRecNo, uiIndex, pValue, ulLen ); */ switch( pField->uiType ) { case HB_FT_STRING: hb_itemPutCL( pItem, pValue, ulLen ); break; case HB_FT_MEMO: hb_itemPutCL( pItem, pValue, ulLen ); hb_itemSetCMemo( pItem ); break; case HB_FT_INTEGER: case HB_FT_LONG: case HB_FT_DOUBLE: if( pField->uiDec ) hb_itemPutNDLen( pItem, atof( pValue ), ( int ) pField->uiLen - ( ( int ) pField->uiDec + 1 ), ( int ) pField->uiDec ); else if( pField->uiLen > 9 ) hb_itemPutNDLen( pItem, atof( pValue ), ( int ) pField->uiLen, ( int ) pField->uiDec ); else hb_itemPutNLLen( pItem, atol( pValue ), ( int ) pField->uiLen ); break; case HB_FT_LOGICAL: hb_itemPutL( pItem, pValue[ 0 ] == 'T' || pValue[ 0 ] == 'Y' ); break; case HB_FT_DATE: { char szDate[ 9 ]; szDate[ 0 ] = pValue[ 0 ]; szDate[ 1 ] = pValue[ 1 ]; szDate[ 2 ] = pValue[ 2 ]; szDate[ 3 ] = pValue[ 3 ]; szDate[ 4 ] = pValue[ 5 ]; szDate[ 5 ] = pValue[ 6 ]; szDate[ 6 ] = pValue[ 8 ]; szDate[ 7 ] = pValue[ 9 ]; szDate[ 8 ] = '\0'; hb_itemPutDS( pItem, szDate ); break; } default: bError = HB_TRUE; break; } if( bError ) { pError = hb_errNew(); hb_errPutGenCode( pError, EG_DATATYPE ); hb_errPutDescription( pError, hb_langDGetErrorDesc( EG_DATATYPE ) ); hb_errPutSubCode( pError, EDBF_DATATYPE ); SELF_ERROR( ( AREAP ) pArea, pError ); hb_itemRelease( pError ); return HB_FAILURE; } return HB_SUCCESS; }
static HB_ERRCODE sqlite3Open( SQLBASEAREAP pArea ) { sqlite3 * pDb = ( ( SDDCONN * ) pArea->pConnection->pSDDConn )->pDb; sqlite3_stmt * st = NULL; SDDDATA * pSDDData; const char * pszQuery; HB_SIZE nQueryLen; void * hQuery; HB_USHORT uiFields, uiIndex; PHB_ITEM pItemEof, pItem; HB_ERRCODE errCode; char * szError; HB_BOOL bError; pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) ); pSDDData = ( SDDDATA * ) pArea->pSDDData; pItem = hb_itemPutC( NULL, pArea->szQuery ); pszQuery = S_HB_ITEMGETSTR( pItem, &hQuery, &nQueryLen ); if( sqlite3_prepare_v2( pDb, pszQuery, ( int ) nQueryLen, &st, NULL ) != SQLITE_OK ) { hb_strfree( hQuery ); hb_itemRelease( pItem ); szError = sqlite3GetError( pDb, &errCode ); hb_errRT_SQLT3DD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode ); sqlite3_finalize( st ); hb_xfree( szError ); return HB_FAILURE; } else { hb_strfree( hQuery ); hb_itemRelease( pItem ); } if( sqlite3_step( st ) != SQLITE_ROW ) { szError = sqlite3GetError( pDb, &errCode ); hb_errRT_SQLT3DD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode ); sqlite3_finalize( st ); hb_xfree( szError ); return HB_FAILURE; } uiFields = ( HB_USHORT ) sqlite3_column_count( st ); SELF_SETFIELDEXTENT( ( AREAP ) pArea, uiFields ); pItemEof = hb_itemArrayNew( uiFields ); /* HB_TRACE( HB_TR_ALWAYS, ("fieldcount=%d", iNameLen) ); */ errCode = 0; bError = HB_FALSE; for( uiIndex = 0; uiIndex < uiFields; ++uiIndex ) { DBFIELDINFO pFieldInfo; PHB_ITEM pName; int iDataType; int iSize; int iDec; pName = S_HB_ITEMPUTSTR( NULL, sqlite3_column_name( st, uiIndex ) ); pFieldInfo.atomName = hb_itemGetCPtr( pName ); iDataType = sqlite3_column_type( st, uiIndex ); iSize = sqlite3_column_bytes( st, uiIndex ); iDec = 0; pFieldInfo.uiLen = ( HB_USHORT ) iSize; pFieldInfo.uiDec = ( HB_USHORT ) iDec; /* HB_TRACE( HB_TR_ALWAYS, ("field: name=%s type=%d len=%d dec=%d nullable=%d", pFieldInfo.atomName, iDataType, iSize, iDec ) ); */ switch( iDataType ) { case SQLITE_TEXT: pFieldInfo.uiType = HB_FT_STRING; break; case SQLITE_FLOAT: case SQLITE_INTEGER: pFieldInfo.uiType = HB_FT_LONG; break; case SQLITE_BLOB: pFieldInfo.uiType = HB_FT_BLOB; break; default: /* HB_TRACE( HB_TR_ALWAYS, ("new sql type=%d", iDataType) ); */ bError = HB_TRUE; errCode = ( HB_ERRCODE ) iDataType; pFieldInfo.uiType = 0; pFieldInfo.uiType = HB_FT_STRING; break; } if( ! bError ) { switch( pFieldInfo.uiType ) { case HB_FT_STRING: { char * pStr = ( char * ) hb_xgrab( ( HB_SIZE ) pFieldInfo.uiLen + 1 ); memset( pStr, ' ', pFieldInfo.uiLen ); pStr[ pFieldInfo.uiLen ] = '\0'; pItem = hb_itemPutCLPtr( NULL, pStr, pFieldInfo.uiLen ); break; } case HB_FT_BLOB: pItem = hb_itemPutC( NULL, NULL ); break; case HB_FT_LONG: if( pFieldInfo.uiDec == 0 ) pItem = hb_itemPutNLLen( NULL, 0, pFieldInfo.uiLen ); else pItem = hb_itemPutNDLen( NULL, 0.0, pFieldInfo.uiLen, pFieldInfo.uiDec ); break; default: pItem = hb_itemNew( NULL ); bError = HB_TRUE; } hb_arraySetForward( pItemEof, uiIndex + 1, pItem ); hb_itemRelease( pItem ); if( ! bError ) bError = ( SELF_ADDFIELD( ( AREAP ) pArea, &pFieldInfo ) == HB_FAILURE ); } hb_itemRelease( pName ); if( bError ) break; } if( bError ) { hb_itemRelease( pItemEof ); sqlite3_finalize( st ); hb_errRT_SQLT3DD( EG_CORRUPTION, ESQLDD_INVALIDFIELD, "Invalid field type", pArea->szQuery, errCode ); return HB_FAILURE; } pArea->ulRecCount = 0; pArea->ulRecMax = SQLDD_ROWSET_INIT; pArea->pRow = ( void ** ) hb_xgrab( SQLDD_ROWSET_INIT * sizeof( void * ) ); memset( pArea->pRow, 0, SQLDD_ROWSET_INIT * sizeof( void * ) ); pArea->pRowFlags = ( HB_BYTE * ) hb_xgrab( SQLDD_ROWSET_INIT * sizeof( HB_BYTE ) ); memset( pArea->pRowFlags, 0, SQLDD_ROWSET_INIT * sizeof( HB_BYTE ) ); pArea->pRow[ 0 ] = pItemEof; pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED; pSDDData->pStmt = st; return HB_SUCCESS; }
static HB_ERRCODE sqlite3Open( SQLBASEAREAP pArea ) { sqlite3 * pDb = ( ( SDDCONN * ) pArea->pConnection->pSDDConn )->pDb; sqlite3_stmt * st = NULL; SDDDATA * pSDDData; const char * pszQuery; HB_SIZE nQueryLen; void * hQuery; HB_USHORT uiFields, uiIndex; PHB_ITEM pItemEof, pItem; HB_ERRCODE errCode; char * szError; HB_BOOL bError; pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) ); pSDDData = ( SDDDATA * ) pArea->pSDDData; pItem = hb_itemPutC( NULL, pArea->szQuery ); pszQuery = S_HB_ITEMGETSTR( pItem, &hQuery, &nQueryLen ); if( sqlite3_prepare_v2( pDb, pszQuery, ( int ) nQueryLen, &st, NULL ) != SQLITE_OK ) { hb_strfree( hQuery ); hb_itemRelease( pItem ); szError = sqlite3GetError( pDb, &errCode ); hb_errRT_SQLT3DD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode ); sqlite3_finalize( st ); hb_xfree( szError ); return HB_FAILURE; } else { hb_strfree( hQuery ); hb_itemRelease( pItem ); } if( sqlite3_step( st ) != SQLITE_ROW ) { szError = sqlite3GetError( pDb, &errCode ); hb_errRT_SQLT3DD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode ); sqlite3_finalize( st ); hb_xfree( szError ); return HB_FAILURE; } uiFields = ( HB_USHORT ) sqlite3_column_count( st ); SELF_SETFIELDEXTENT( ( AREAP ) pArea, uiFields ); pItemEof = hb_itemArrayNew( uiFields ); #if 0 HB_TRACE( HB_TR_ALWAYS, ( "fieldcount=%d", uiFields ) ); #endif errCode = 0; bError = HB_FALSE; for( uiIndex = 0; uiIndex < uiFields; ++uiIndex ) { DBFIELDINFO pFieldInfo; int iDataType = sqlite3_column_type( st, uiIndex ); PHB_ITEM pName = S_HB_ITEMPUTSTR( NULL, sqlite3_column_name( st, uiIndex ) ); HB_USHORT uiNameLen = ( HB_USHORT ) hb_itemGetCLen( pName ); if( ( ( AREAP ) pArea )->uiMaxFieldNameLength < uiNameLen ) ( ( AREAP ) pArea )->uiMaxFieldNameLength = uiNameLen; pFieldInfo.atomName = hb_itemGetCPtr( pName ); #if 0 HB_TRACE( HB_TR_ALWAYS, ( "field: name=%s type=%d len=%d", pFieldInfo.atomName, iDataType, sqlite3_column_bytes( st, uiIndex ) ) ); #endif /* There are no field length limits stored in the SQLite3 database, so we're resorting to setting some arbitrary default values to make apps relying on these (f.e. Browse()/GET) to behave somewhat better. For better results, update apps to untie UI metrics from any database field/value widths. [vszakats] */ pFieldInfo.uiLen = 10; pFieldInfo.uiDec = 0; switch( iDataType ) { case SQLITE3_TEXT: pFieldInfo.uiType = HB_FT_STRING; break; case SQLITE_FLOAT: pFieldInfo.uiType = HB_FT_LONG; pFieldInfo.uiDec = ( HB_USHORT ) hb_setGetDecimals(); pFieldInfo.uiLen += pFieldInfo.uiDec + 1; break; case SQLITE_INTEGER: pFieldInfo.uiType = HB_FT_LONG; break; case SQLITE_BLOB: case SQLITE_NULL: pFieldInfo.uiType = HB_FT_BLOB; break; default: #if 0 HB_TRACE( HB_TR_ALWAYS, ( "new sql type=%d", iDataType ) ); #endif bError = HB_TRUE; errCode = ( HB_ERRCODE ) iDataType; pFieldInfo.uiType = 0; break; } if( ! bError ) { switch( pFieldInfo.uiType ) { case HB_FT_STRING: { char * pStr = ( char * ) hb_xgrab( ( HB_SIZE ) pFieldInfo.uiLen + 1 ); memset( pStr, ' ', pFieldInfo.uiLen ); pStr[ pFieldInfo.uiLen ] = '\0'; pItem = hb_itemPutCLPtr( NULL, pStr, pFieldInfo.uiLen ); break; } case HB_FT_BLOB: pItem = hb_itemPutC( NULL, NULL ); break; case HB_FT_LONG: if( pFieldInfo.uiDec == 0 ) pItem = hb_itemPutNLLen( NULL, 0, pFieldInfo.uiLen ); else pItem = hb_itemPutNDLen( NULL, 0.0, pFieldInfo.uiLen, pFieldInfo.uiDec ); break; default: pItem = hb_itemNew( NULL ); bError = HB_TRUE; } hb_arraySetForward( pItemEof, uiIndex + 1, pItem ); hb_itemRelease( pItem ); if( ! bError ) bError = ( SELF_ADDFIELD( ( AREAP ) pArea, &pFieldInfo ) == HB_FAILURE ); } hb_itemRelease( pName ); if( bError ) break; } if( bError ) { hb_itemRelease( pItemEof ); sqlite3_finalize( st ); hb_errRT_SQLT3DD( EG_CORRUPTION, ESQLDD_INVALIDFIELD, "Invalid field type", pArea->szQuery, errCode ); return HB_FAILURE; } pArea->ulRecCount = 0; pArea->ulRecMax = SQLDD_ROWSET_INIT; pArea->pRow = ( void ** ) hb_xgrabz( SQLDD_ROWSET_INIT * sizeof( void * ) ); pArea->pRowFlags = ( HB_BYTE * ) hb_xgrabz( SQLDD_ROWSET_INIT * sizeof( HB_BYTE ) ); pArea->pRow[ 0 ] = pItemEof; pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED; pSDDData->pStmt = st; return HB_SUCCESS; }
static HB_ERRCODE odbcGoTo( SQLBASEAREAP pArea, HB_ULONG ulRecNo ) { SQLHSTMT hStmt; SQLRETURN res; SQLLEN iLen; PHB_ITEM pArray, pItem; LPFIELD pField; HB_USHORT ui; /* No pArea->pSDDData for DBCreate() area... * though pArea->fFetched == HB_TRUE for them */ hStmt = pArea->pSDDData ? ( ( SDDDATA * ) pArea->pSDDData )->hStmt : NULL; while( ulRecNo > pArea->ulRecCount && ! pArea->fFetched ) { if( ! SQL_SUCCEEDED( SQLFetch( hStmt ) ) ) { pArea->fFetched = HB_TRUE; break; } pArray = hb_itemArrayNew( pArea->area.uiFieldCount ); pItem = NULL; for( ui = 1; ui <= pArea->area.uiFieldCount; ui++ ) { iLen = SQL_NULL_DATA; res = 0; pField = pArea->area.lpFields + ui - 1; switch( pField->uiType ) { case HB_FT_STRING: if( pField->uiType & HB_FF_BINARY ) { char buffer[ 1 ]; iLen = 0; if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, SQL_C_BINARY, buffer, 0, &iLen ) ) ) { if( iLen >= 0 ) { char * val = ( char * ) hb_xgrab( iLen + 1 ); if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, SQL_C_BINARY, val, iLen + 1, &iLen ) ) ) pItem = hb_itemPutCLPtr( pItem, val, ( HB_SIZE ) iLen ); else hb_xfree( val ); } } } else { O_HB_CHAR buffer[ 1 ]; #if defined( UNICODE ) SQLSMALLINT iTargetType = SQL_C_WCHAR; #else SQLSMALLINT iTargetType = SQL_C_CHAR; #endif iLen = 0; if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, iTargetType, buffer, 0, &iLen ) ) ) { if( iLen >= 0 ) { O_HB_CHAR * val = ( O_HB_CHAR * ) hb_xgrab( iLen + sizeof( O_HB_CHAR ) ); if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, iTargetType, val, iLen + sizeof( O_HB_CHAR ), &iLen ) ) ) { #if defined( UNICODE ) iLen >>= 1; #endif pItem = O_HB_ITEMPUTSTRLEN( pItem, val, ( HB_SIZE ) iLen ); } hb_xfree( val ); } } } break; case HB_FT_INTEGER: #if ODBCVER >= 0x0300 if( pField->uiTypeExtended == ( HB_USHORT ) SQL_BIGINT ) { HB_I64 val = 0; /* NOTE: SQL_C_SBIGINT not available before ODBC 3.0 */ if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, SQL_C_SBIGINT, &val, sizeof( val ), &iLen ) ) ) pItem = hb_itemPutNIntLen( pItem, val, pField->uiLen ); } else #endif { SQLINTEGER val = 0; if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, SQL_C_LONG, &val, sizeof( val ), &iLen ) ) ) pItem = hb_itemPutNLLen( pItem, val, pField->uiLen ); } break; case HB_FT_LONG: if( pField->uiDec == 0 && pField->uiLen < 10 ) { SQLINTEGER val = 0; if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, SQL_C_LONG, &val, sizeof( val ), &iLen ) ) ) pItem = hb_itemPutNLLen( pItem, val, pField->uiLen ); } else { double val = 0.0; if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, SQL_C_DOUBLE, &val, sizeof( val ), &iLen ) ) ) pItem = hb_itemPutNDLen( pItem, val, pField->uiLen, pField->uiDec ); } break; case HB_FT_DOUBLE: { double val = 0.0; if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, SQL_C_DOUBLE, &val, sizeof( val ), &iLen ) ) ) pItem = hb_itemPutNDLen( pItem, val, pField->uiLen, pField->uiDec ); break; } case HB_FT_LOGICAL: { unsigned char val = 0; if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, SQL_C_BIT, &val, sizeof( val ), &iLen ) ) ) pItem = hb_itemPutL( pItem, val != 0 ); break; } case HB_FT_DATE: { DATE_STRUCT val = { 0, 0, 0 }; if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, SQL_C_DATE, &val, sizeof( val ), &iLen ) ) ) pItem = hb_itemPutD( pItem, val.year, val.month, val.day ); break; } case HB_FT_TIME: { TIME_STRUCT val = { 0, 0, 0 }; if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, SQL_C_TIME, &val, sizeof( val ), &iLen ) ) ) pItem = hb_itemPutTDT( pItem, 0, hb_timeEncode( val.hour, val.minute, val.second, 0 ) ); break; } case HB_FT_TIMESTAMP: { TIMESTAMP_STRUCT val = { 0, 0, 0, 0, 0, 0, 0 }; if( SQL_SUCCEEDED( res = SQLGetData( hStmt, ui, SQL_C_TIMESTAMP, &val, sizeof( val ), &iLen ) ) ) pItem = hb_itemPutTDT( pItem, hb_dateEncode( val.year, val.month, val.day ), hb_timeEncode( val.hour, val.minute, val.second, val.fraction / 1000000 ) ); break; } } /* TODO: check SQL_SUCCEEDED( res ) */ /* TODO: check for SQL_NO_TOTAL. What does this mean? */ HB_SYMBOL_UNUSED( res ); if( pItem ) { /* NULL -> NIL */ if( iLen == SQL_NULL_DATA ) hb_itemClear( pItem ); else hb_arraySetForward( pArray, ui, pItem ); } } if( pItem ) hb_itemRelease( pItem ); if( pArea->ulRecCount + 1 >= pArea->ulRecMax ) { pArea->pRow = ( void ** ) hb_xrealloc( pArea->pRow, ( pArea->ulRecMax + SQLDD_ROWSET_RESIZE ) * sizeof( void * ) ); pArea->pRowFlags = ( HB_BYTE * ) hb_xrealloc( pArea->pRowFlags, ( pArea->ulRecMax + SQLDD_ROWSET_RESIZE ) * sizeof( HB_BYTE ) ); pArea->ulRecMax += SQLDD_ROWSET_RESIZE; } pArea->ulRecCount++; pArea->pRow[ pArea->ulRecCount ] = pArray; pArea->pRowFlags[ pArea->ulRecCount ] = SQLDD_FLAG_CACHED; }
static HB_ERRCODE odbcOpen( SQLBASEAREAP pArea ) { SDDCONN * pSDDConn = ( SDDCONN * ) pArea->pConnection->pSDDConn; SDDDATA * pSDDData; O_HB_CHAR * pchQuery; SQLHSTMT hStmt; SQLSMALLINT iNameLen; PHB_ITEM pItemEof, pItem; HB_BOOL bError; HB_USHORT uiFields, uiIndex; HB_ERRCODE errCode; char * szError; SQLRETURN result; pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) ); pSDDData = ( SDDDATA * ) pArea->pSDDData; #if ODBCVER >= 0x0300 if( ! SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_STMT, pSDDConn->hConn, &hStmt ) ) ) #else if( ! SQL_SUCCEEDED( SQLAllocStmt( pSDDConn->hConn, &hStmt ) ) ) #endif { szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, SQL_NULL_HSTMT, &errCode ); hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTALLOC, szError, pArea->szQuery, errCode ); hb_xfree( szError ); return HB_FAILURE; } pchQuery = O_HB_CHARDUP( pArea->szQuery ); result = SQLExecDirect( hStmt, ( SQLTCHAR * ) HB_UNCONST( pchQuery ), ( SQLINTEGER ) O_HB_STRLEN( pchQuery ) ); hb_xfree( pchQuery ); if( ! SQL_SUCCEEDED( result ) ) { szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode ); #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_STMT, hStmt ); #else SQLFreeStmt( hStmt, SQL_DROP ); #endif hb_errRT_ODBCDD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode ); hb_xfree( szError ); return HB_FAILURE; } if( ! SQL_SUCCEEDED( SQLNumResultCols( hStmt, &iNameLen ) ) ) { szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode ); #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_STMT, hStmt ); #else SQLFreeStmt( hStmt, SQL_DROP ); #endif hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTDESCR + 1000, szError, pArea->szQuery, errCode ); hb_xfree( szError ); return HB_FAILURE; } uiFields = ( HB_USHORT ) iNameLen; SELF_SETFIELDEXTENT( &pArea->area, uiFields ); pItemEof = hb_itemArrayNew( uiFields ); pItem = hb_itemNew( NULL ); #if 0 HB_TRACE( HB_TR_ALWAYS, ( "fieldcount=%d", iNameLen ) ); #endif errCode = 0; bError = HB_FALSE; for( uiIndex = 0; uiIndex < uiFields; uiIndex++ ) { DBFIELDINFO dbFieldInfo; char * pszName; SQLTCHAR cName[ 256 ]; SQLULEN uiSize; SQLSMALLINT iDataType, iDec, iNull; if( ! SQL_SUCCEEDED( SQLDescribeCol( hStmt, ( SQLSMALLINT ) uiIndex + 1, cName, HB_SIZEOFARRAY( cName ), &iNameLen, &iDataType, &uiSize, &iDec, &iNull ) ) ) { hb_itemRelease( pItemEof ); hb_itemRelease( pItem ); szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, NULL ); #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_STMT, hStmt ); #else SQLFreeStmt( hStmt, SQL_DROP ); #endif hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTDESCR + 1001, szError, pArea->szQuery, 0 ); hb_xfree( szError ); return HB_FAILURE; } memset( &dbFieldInfo, 0, sizeof( dbFieldInfo ) ); dbFieldInfo.atomName = pszName = O_HB_OSSTRDUP( ( O_HB_CHAR * ) cName ); /* We do mapping of many SQL types to one Harbour field type here, so, we need store real SQL type in uiTypeExtended. SQL types are signed, so, HB_USHORT type casting is a little hackish. We need to remember use this casting also in expressions like this: if( pField->uiTypeExtended == ( HB_USHORT ) SQL_BIGINT ) or introduce our own unsigned SQL types. [Mindaugas] */ dbFieldInfo.uiTypeExtended = ( HB_USHORT ) iDataType; dbFieldInfo.uiLen = ( HB_USHORT ) uiSize; dbFieldInfo.uiDec = iDec; if( iNull == SQL_NULLABLE ) dbFieldInfo.uiFlags |= HB_FF_NULLABLE; #if 0 HB_TRACE( HB_TR_ALWAYS, ( "field: name=%s type=%d len=%d dec=%d null=%d", dbFieldInfo.atomName, iDataType, uiSize, iDec, iNull ) ); #endif switch( iDataType ) { case SQL_CHAR: case SQL_VARCHAR: case SQL_LONGVARCHAR: dbFieldInfo.uiType = HB_FT_STRING; break; case SQL_WCHAR: case SQL_WVARCHAR: case SQL_WLONGVARCHAR: dbFieldInfo.uiType = HB_FT_STRING; dbFieldInfo.uiFlags |= HB_FF_UNICODE; break; case SQL_BINARY: case SQL_VARBINARY: case SQL_LONGVARBINARY: dbFieldInfo.uiType = HB_FT_STRING; dbFieldInfo.uiFlags |= HB_FF_BINARY; break; case SQL_TINYINT: case SQL_SMALLINT: case SQL_INTEGER: case SQL_BIGINT: dbFieldInfo.uiType = HB_FT_INTEGER; break; case SQL_DECIMAL: case SQL_NUMERIC: dbFieldInfo.uiType = HB_FT_LONG; break; case SQL_REAL: case SQL_FLOAT: case SQL_DOUBLE: dbFieldInfo.uiType = HB_FT_DOUBLE; break; case SQL_BIT: dbFieldInfo.uiType = HB_FT_LOGICAL; break; case SQL_DATE: #if ODBCVER >= 0x0300 case SQL_TYPE_DATE: #endif dbFieldInfo.uiType = HB_FT_DATE; break; case SQL_TIME: #if ODBCVER >= 0x0300 case SQL_TYPE_TIME: #endif dbFieldInfo.uiType = HB_FT_TIME; break; /* SQL_DATETIME = SQL_DATE = 9 */ case SQL_TIMESTAMP: #if ODBCVER >= 0x0300 case SQL_TYPE_TIMESTAMP: #endif dbFieldInfo.uiType = HB_FT_TIMESTAMP; break; default: #if 0 HB_TRACE( HB_TR_ALWAYS, ( "new sql type=%d", iDataType ) ); #endif bError = HB_TRUE; errCode = ( HB_ERRCODE ) iDataType; break; } if( ! bError ) { switch( dbFieldInfo.uiType ) { case HB_FT_STRING: { char * pStr; pStr = ( char * ) hb_xgrab( ( HB_SIZE ) dbFieldInfo.uiLen + 1 ); memset( pStr, ' ', dbFieldInfo.uiLen ); pStr[ dbFieldInfo.uiLen ] = '\0'; hb_itemPutCL( pItem, pStr, dbFieldInfo.uiLen ); hb_xfree( pStr ); break; } case HB_FT_MEMO: hb_itemPutC( pItem, NULL ); break; case HB_FT_INTEGER: hb_itemPutNI( pItem, 0 ); break; case HB_FT_LONG: if( dbFieldInfo.uiDec == 0 ) hb_itemPutNLLen( pItem, 0, dbFieldInfo.uiLen ); else hb_itemPutNDLen( pItem, 0.0, dbFieldInfo.uiLen, dbFieldInfo.uiDec ); break; case HB_FT_DOUBLE: hb_itemPutNDLen( pItem, 0.0, dbFieldInfo.uiLen, dbFieldInfo.uiDec ); break; case HB_FT_LOGICAL: hb_itemPutL( pItem, HB_FALSE ); break; case HB_FT_DATE: hb_itemPutDL( pItem, 0 ); break; case HB_FT_TIME: hb_itemPutTDT( pItem, 0, 0 ); break; case HB_FT_TIMESTAMP: hb_itemPutTDT( pItem, 0, 0 ); break; default: hb_itemClear( pItem ); bError = HB_TRUE; break; } hb_arraySetForward( pItemEof, uiIndex + 1, pItem ); if( ! bError ) bError = ( SELF_ADDFIELD( &pArea->area, &dbFieldInfo ) == HB_FAILURE ); hb_xfree( pszName ); } if( bError ) break; } hb_itemRelease( pItem ); if( bError ) { hb_itemRelease( pItemEof ); #if ODBCVER >= 0x0300 SQLFreeHandle( SQL_HANDLE_STMT, hStmt ); #else SQLFreeStmt( hStmt, SQL_DROP ); #endif hb_errRT_ODBCDD( EG_CORRUPTION, ESQLDD_INVALIDFIELD, "Invalid field type", pArea->szQuery, errCode ); return HB_FAILURE; } pArea->ulRecCount = 0; pArea->ulRecMax = SQLDD_ROWSET_INIT; pArea->pRow = ( void ** ) hb_xgrab( SQLDD_ROWSET_INIT * sizeof( void * ) ); pArea->pRowFlags = ( HB_BYTE * ) hb_xgrab( SQLDD_ROWSET_INIT * sizeof( HB_BYTE ) ); pArea->pRow[ 0 ] = pItemEof; pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED; pSDDData->hStmt = hStmt; return HB_SUCCESS; }
static HB_ERRCODE mysqlGetValue( SQLBASEAREAP pArea, HB_USHORT uiIndex, PHB_ITEM pItem ) { SDDDATA * pSDDData = ( SDDDATA * ) pArea->pSDDData; LPFIELD pField; char * pValue; char szBuffer[ 64 ]; HB_BOOL bError; PHB_ITEM pError; HB_SIZE ulLen; bError = HB_FALSE; uiIndex--; pField = pArea->area.lpFields + uiIndex; pValue = pSDDData->pNatRecord[ uiIndex ]; ulLen = pSDDData->pNatLength[ uiIndex ]; /* NULL => NIL (?) */ if( ! pValue ) { hb_itemClear( pItem ); return HB_SUCCESS; } switch( pField->uiType ) { case HB_FT_STRING: { #if 0 char * pStr; /* Expand strings to field length */ pStr = ( char * ) hb_xgrab( pField->uiLen + 1 ); if( pValue ) memcpy( pStr, pValue, ulLen ); if( ( HB_SIZE ) pField->uiLen > ulLen ) memset( pStr + ulLen, ' ', pField->uiLen - ulLen ); pStr[ pField->uiLen ] = '\0'; hb_itemPutCRaw( pItem, pStr, pField->uiLen ); #else /* Do not expand strings */ if( pValue ) hb_itemPutCL( pItem, pValue, ulLen ); else hb_itemPutC( pItem, NULL ); #endif break; } case HB_FT_MEMO: if( pValue ) hb_itemPutCL( pItem, pValue, ulLen ); else hb_itemPutC( pItem, NULL ); hb_itemSetCMemo( pItem ); break; case HB_FT_INTEGER: case HB_FT_LONG: case HB_FT_DOUBLE: if( pValue ) { hb_strncpy( szBuffer, pValue, sizeof( szBuffer ) - 1 ); if( pField->uiDec ) { hb_itemPutNDLen( pItem, atof( szBuffer ), ( int ) pField->uiLen - ( ( int ) pField->uiDec + 1 ), ( int ) pField->uiDec ); } else hb_itemPutNLLen( pItem, atol( szBuffer ), ( int ) pField->uiLen ); } else { if( pField->uiDec ) hb_itemPutNDLen( pItem, 0.0, ( int ) pField->uiLen - ( ( int ) pField->uiDec + 1 ), ( int ) pField->uiDec ); else hb_itemPutNLLen( pItem, 0, ( int ) pField->uiLen ); } break; case HB_FT_DATE: { char szDate[ 9 ]; szDate[ 0 ] = pValue[ 0 ]; szDate[ 1 ] = pValue[ 1 ]; szDate[ 2 ] = pValue[ 2 ]; szDate[ 3 ] = pValue[ 3 ]; szDate[ 4 ] = pValue[ 5 ]; szDate[ 5 ] = pValue[ 6 ]; szDate[ 6 ] = pValue[ 8 ]; szDate[ 7 ] = pValue[ 9 ]; szDate[ 8 ] = '\0'; hb_itemPutDS( pItem, szDate ); break; } case HB_FT_TIMESTAMP: { char szTimeStamp[ 15 ]; szTimeStamp[ 0 ] = pValue[ 0 ]; szTimeStamp[ 1 ] = pValue[ 1 ]; szTimeStamp[ 2 ] = pValue[ 2 ]; szTimeStamp[ 3 ] = pValue[ 3 ]; szTimeStamp[ 4 ] = pValue[ 5 ]; szTimeStamp[ 5 ] = pValue[ 6 ]; szTimeStamp[ 6 ] = pValue[ 8 ]; szTimeStamp[ 7 ] = pValue[ 9 ]; szTimeStamp[ 8 ] = pValue[ 11 ]; szTimeStamp[ 9 ] = pValue[ 12 ]; szTimeStamp[ 10 ] = pValue[ 14 ]; szTimeStamp[ 11 ] = pValue[ 15 ]; szTimeStamp[ 12 ] = pValue[ 17 ]; szTimeStamp[ 13 ] = pValue[ 18 ]; szTimeStamp[ 14 ] = '\0'; hb_itemPutTS( pItem, szTimeStamp ); break; } case HB_FT_TIME: { char szTimeStamp[ 15 ]; szTimeStamp[ 0 ] = '0'; szTimeStamp[ 1 ] = '0'; szTimeStamp[ 2 ] = '0'; szTimeStamp[ 3 ] = '0'; szTimeStamp[ 4 ] = '0'; szTimeStamp[ 5 ] = '0'; szTimeStamp[ 6 ] = '0'; szTimeStamp[ 7 ] = '0'; szTimeStamp[ 8 ] = pValue[ 0 ]; szTimeStamp[ 9 ] = pValue[ 1 ]; szTimeStamp[ 10 ] = pValue[ 3 ]; szTimeStamp[ 11 ] = pValue[ 4 ]; szTimeStamp[ 12 ] = pValue[ 6 ]; szTimeStamp[ 13 ] = pValue[ 7 ]; szTimeStamp[ 14 ] = '\0'; hb_itemPutTS( pItem, szTimeStamp ); break; } default: bError = HB_TRUE; break; } if( bError ) { pError = hb_errNew(); hb_errPutGenCode( pError, EG_DATATYPE ); hb_errPutDescription( pError, hb_langDGetErrorDesc( EG_DATATYPE ) ); hb_errPutSubCode( pError, EDBF_DATATYPE ); SELF_ERROR( ( AREAP ) pArea, pError ); hb_itemRelease( pError ); return HB_FAILURE; } return HB_SUCCESS; }
static HB_ERRCODE ocilibOpen( SQLBASEAREAP pArea ) { OCI_Statement * st = OCI_StatementCreate( ( ( SDDCONN * ) pArea->pConnection->pSDDConn )->pConn ); OCI_Resultset * rs; SDDDATA * pSDDData; void * hQuery; HB_USHORT uiFields, uiIndex; PHB_ITEM pItemEof, pItem; HB_ERRCODE errCode; char * szError; HB_BOOL bError; pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) ); pSDDData = ( SDDDATA * ) pArea->pSDDData; if( ! st ) { szError = ocilibGetError( &errCode ); hb_errRT_OCIDD( EG_OPEN, ESQLDD_STMTALLOC, szError, pArea->szQuery, errCode ); hb_xfree( szError ); return HB_FAILURE; } pItem = hb_itemPutC( NULL, pArea->szQuery ); if( ! OCI_ExecuteStmt( st, M_HB_ITEMGETSTR( pItem, &hQuery, NULL ) ) ) { hb_strfree( hQuery ); hb_itemRelease( pItem ); szError = ocilibGetError( &errCode ); OCI_StatementFree( st ); hb_errRT_OCIDD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode ); hb_xfree( szError ); return HB_FAILURE; } else { hb_strfree( hQuery ); hb_itemRelease( pItem ); } rs = OCI_GetResultset( st ); uiFields = ( HB_USHORT ) OCI_GetColumnCount( rs ); SELF_SETFIELDEXTENT( ( AREAP ) pArea, uiFields ); pItemEof = hb_itemArrayNew( uiFields ); pItem = hb_itemNew( NULL ); #if 0 HB_TRACE( HB_TR_ALWAYS, ( "fieldcount=%d", iNameLen ) ); #endif errCode = 0; bError = HB_FALSE; for( uiIndex = 0; uiIndex < uiFields; ++uiIndex ) { DBFIELDINFO pFieldInfo; PHB_ITEM pName; OCI_Column * col = OCI_GetColumn( rs, uiIndex + 1 ); unsigned int uiDataType; unsigned int uiSize; int iDec; HB_BOOL bNullable; if( ! col ) { hb_itemRelease( pItemEof ); hb_itemRelease( pItem ); szError = ocilibGetError( NULL ); OCI_StatementFree( st ); hb_errRT_OCIDD( EG_OPEN, ESQLDD_STMTDESCR + 1001, szError, pArea->szQuery, 0 ); hb_xfree( szError ); return HB_FAILURE; } pName = D_HB_ITEMPUTSTR( NULL, OCI_ColumnGetName( col ) ); pFieldInfo.atomName = hb_itemGetCPtr( pName ); uiDataType = OCI_ColumnGetType( col ); uiSize = OCI_ColumnGetSize( col ); iDec = OCI_ColumnGetPrecision( col ); bNullable = ( HB_BOOL ) OCI_ColumnGetNullable( col ); if( bNullable ) pFieldInfo.uiFlags |= HB_FF_NULLABLE; pFieldInfo.uiLen = ( HB_USHORT ) uiSize; pFieldInfo.uiDec = ( HB_USHORT ) iDec; #if 0 HB_TRACE( HB_TR_ALWAYS, ( "field: name=%s type=%d len=%d dec=%d nullable=%d %d %d %d %d", pFieldInfo.atomName, uiDataType, uiSize, iDec, bNullable, OCI_ColumnGetScale( col ), OCI_ColumnGetPrecision( col ), OCI_ColumnGetFractionalPrecision( col ), OCI_ColumnGetLeadingPrecision( col ) ) ); #endif switch( uiDataType ) { case OCI_CDT_TEXT: pFieldInfo.uiType = HB_FT_STRING; break; case OCI_CDT_NUMERIC: pFieldInfo.uiType = HB_FT_LONG; /* For plain 'NUMERIC', precision is zero and scale is -127 */ if( OCI_ColumnGetPrecision( col ) > 0 ) pFieldInfo.uiLen = ( HB_USHORT ) OCI_ColumnGetPrecision( col ); if( OCI_ColumnGetScale( col ) >= 0 ) pFieldInfo.uiDec = ( HB_USHORT ) OCI_ColumnGetScale( col ); else pFieldInfo.uiDec = ( HB_USHORT ) hb_setGetDecimals(); break; case OCI_CDT_LONG: pFieldInfo.uiType = HB_FT_VARLENGTH; break; case OCI_CDT_RAW: pFieldInfo.uiType = HB_FT_BLOB; break; case OCI_CDT_DATETIME: case OCI_CDT_TIMESTAMP: case OCI_CDT_INTERVAL: pFieldInfo.uiType = HB_FT_TIME; break; default: #if 0 HB_TRACE( HB_TR_ALWAYS, ( "new sql type=%d", uiDataType ) ); #endif bError = HB_TRUE; errCode = ( HB_ERRCODE ) uiDataType; pFieldInfo.uiType = 0; pFieldInfo.uiType = HB_FT_STRING; break; } if( ! bError ) { switch( pFieldInfo.uiType ) { case HB_FT_STRING: { char * pStr = ( char * ) hb_xgrab( ( HB_SIZE ) pFieldInfo.uiLen + 1 ); memset( pStr, ' ', pFieldInfo.uiLen ); pStr[ pFieldInfo.uiLen ] = '\0'; hb_itemPutCLPtr( pItem, pStr, pFieldInfo.uiLen ); break; } case HB_FT_MEMO: case HB_FT_VARLENGTH: case HB_FT_BLOB: hb_itemPutC( pItem, NULL ); break; case HB_FT_INTEGER: hb_itemPutNI( pItem, 0 ); break; case HB_FT_LONG: if( pFieldInfo.uiDec == 0 ) hb_itemPutNLLen( pItem, 0, pFieldInfo.uiLen ); else hb_itemPutNDLen( pItem, 0.0, pFieldInfo.uiLen, pFieldInfo.uiDec ); break; case HB_FT_DOUBLE: hb_itemPutNDLen( pItem, 0.0, pFieldInfo.uiLen, pFieldInfo.uiDec ); break; case HB_FT_LOGICAL: hb_itemPutL( pItem, HB_FALSE ); break; case HB_FT_DATE: hb_itemPutDL( pItem, 0 ); break; case HB_FT_TIME: case HB_FT_TIMESTAMP: hb_itemPutTDT( pItem, 0, 0 ); break; default: hb_itemClear( pItem ); bError = HB_TRUE; } hb_arraySetForward( pItemEof, uiIndex + 1, pItem ); if( ! bError ) bError = ( SELF_ADDFIELD( ( AREAP ) pArea, &pFieldInfo ) == HB_FAILURE ); } hb_itemRelease( pName ); if( bError ) break; } hb_itemRelease( pItem ); if( bError ) { hb_itemRelease( pItemEof ); OCI_StatementFree( st ); hb_errRT_OCIDD( EG_CORRUPTION, ESQLDD_INVALIDFIELD, "Invalid field type", pArea->szQuery, errCode ); return HB_FAILURE; } pArea->ulRecCount = 0; pArea->ulRecMax = SQLDD_ROWSET_INIT; pArea->pRow = ( void ** ) hb_xgrab( SQLDD_ROWSET_INIT * sizeof( void * ) ); memset( pArea->pRow, 0, SQLDD_ROWSET_INIT * sizeof( void * ) ); pArea->pRowFlags = ( HB_BYTE * ) hb_xgrab( SQLDD_ROWSET_INIT * sizeof( HB_BYTE ) ); memset( pArea->pRowFlags, 0, SQLDD_ROWSET_INIT * sizeof( HB_BYTE ) ); pArea->pRow[ 0 ] = pItemEof; pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED; pSDDData->pStmt = st; return HB_SUCCESS; }