diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 356034f76f..c7037c3e9f 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -17,6 +17,25 @@ past entries belonging to author(s): Viktor Szakats. */ +2010-03-23 12:55 UTC+0200 Mindaugas Kavaliauskas (dbtopas/at/dbtopas.lt) + * harbour/contrib/rddsql/hbrddsql.h + * harbour/contrib/rddsql/sqlbase.c + * harbour/contrib/rddsql/sddfb/sddfb.c + * harbour/contrib/rddsql/sddmy/sddmy.c + * harbour/contrib/rddsql/sddoci/sddoci.c + * harbour/contrib/rddsql/sddodbc/sddodbc.c + * harbour/contrib/rddsql/sddpg/sddpg.c + * harbour/contrib/rddsql/sddsqlt3/sddsqlt3.c + * moved SDD backed specific data from SQLCONNECTION and SQLAREA + to a separate SDDCONN and SDDDATA structures. This helps to + avoid fake type casting and warinings like "break + strict-aliasing rules". + * 1 -> HB_TRUE + * formatting, style + ! connection array is now array of connection pointers. This + fixes GPF if many connections are created and connection + table has to be reallocated + 2010-03-23 08:37 UTC+0100 Viktor Szakats (harbour.01 syenar.hu) * utils/hbmk2/hbmk2.prg * Minor (non-functional) corrections to latest additions. diff --git a/harbour/contrib/rddsql/hbrddsql.h b/harbour/contrib/rddsql/hbrddsql.h index d24ed13763..0e1a834737 100644 --- a/harbour/contrib/rddsql/hbrddsql.h +++ b/harbour/contrib/rddsql/hbrddsql.h @@ -98,29 +98,24 @@ typedef struct _SQLBASEAREA void ** pRow; /* array of native pointers or cached PHB_ITEM */ HB_BYTE * pRowFlags; - void * pRecord; + void * pRecord; /* current record */ HB_BYTE bRecordFlags; - void * pResult; /* SQL result */ - void * pStmt; /* SQL statement */ - void * pTrans; /* SQL transaction */ - - void * pNatRecord; - void * pNatLength; - HB_BOOL fFetched; HB_BOOL fPositioned; HB_BOOL fAppend; HB_BOOL fRecordChanged; + + void * pSDDData; /* SDD specific data */ } SQLBASEAREA, * SQLBASEAREAP; typedef struct _SQLDDCONNECTION { struct _SDDNODE * pSDD; - void * hConnection; - void * hCargo; unsigned int uiAreaCount; + + void * pSDDConn; /* SDD specific data */ } SQLDDCONNECTION; diff --git a/harbour/contrib/rddsql/sddfb/sddfb.c b/harbour/contrib/rddsql/sddfb/sddfb.c index 748a51fbba..112e68e696 100644 --- a/harbour/contrib/rddsql/sddfb/sddfb.c +++ b/harbour/contrib/rddsql/sddfb/sddfb.c @@ -64,6 +64,18 @@ #include "ibase.h" +typedef struct +{ + isc_db_handle hDb; +} SDDCONN; + +typedef struct +{ + isc_tr_handle hTrans; + isc_stmt_handle hStmt; + XSQLDA ISC_FAR * pSqlda; +} SDDDATA; + static HB_ERRCODE fbConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ); static HB_ERRCODE fbDisconnect( SQLDDCONNECTION * pConnection ); static HB_ERRCODE fbExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ); @@ -164,7 +176,9 @@ static HB_ERRCODE fbConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) /* TODO: error code in status[1]; */ return HB_FAILURE; } - pConnection->hConnection = ( void * ) ( HB_PTRDIFF ) db; + pConnection->pSDDConn = hb_xgrab( sizeof( SDDCONN ) ); + ( ( SDDCONN * ) pConnection->pSDDConn )->hDb = db; +/* HB_TRACE( HB_TR_ALWAYS, ("db=%d", db) ); */ return HB_SUCCESS; } @@ -173,7 +187,8 @@ static HB_ERRCODE fbDisconnect( SQLDDCONNECTION * pConnection ) { ISC_STATUS_ARRAY status; - isc_detach_database( status, ( isc_db_handle * ) ( void * ) &pConnection->hConnection ); + isc_detach_database( status, ( ( SDDCONN * ) pConnection->pSDDConn )->hDb ); + hb_xfree( pConnection->pSDDConn ); return HB_SUCCESS; } @@ -188,9 +203,11 @@ static HB_ERRCODE fbExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) static HB_ERRCODE fbOpen( SQLBASEAREAP pArea ) { + isc_db_handle hDb = ( ( SDDCONN * ) pArea->pConnection->pSDDConn )->hDb; + SDDDATA * pSDDData; ISC_STATUS_ARRAY status; - isc_tr_handle pTrans = ( isc_tr_handle ) 0; - isc_stmt_handle pStmt = ( isc_stmt_handle ) 0; + isc_tr_handle hTrans = ( isc_tr_handle ) 0; + isc_stmt_handle hStmt = ( isc_stmt_handle ) 0; XSQLDA ISC_FAR * pSqlda; XSQLVAR * pVar; PHB_ITEM pItemEof, pItem; @@ -200,16 +217,23 @@ static HB_ERRCODE fbOpen( SQLBASEAREAP pArea ) HB_USHORT uiFields, uiCount; int iType; - if ( isc_start_transaction( status, &pTrans, 1, ( isc_db_handle * ) ( void * ) &pArea->pConnection->hConnection, 0, NULL ) ) + pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) ); + pSDDData = ( SDDDATA * ) pArea->pSDDData; + + memset( &status, 0, sizeof( status ) ); + +/* HB_TRACE( HB_TR_ALWAYS, ("db=%d", hDb) ); */ + if ( isc_start_transaction( status, &hTrans, 1, &hDb, 0, NULL ) ) { +/* HB_TRACE( HB_TR_ALWAYS, ("hTrans=%d status=%ld %ld %ld %ld", ( int ) hTrans, ( long ) status[0], ( long ) status[1], ( long ) status[2], ( long ) status[3] ) ); */ hb_errRT_FireBirdDD( EG_OPEN, ESQLDD_START, "Start transaction failed", NULL, ( HB_ERRCODE ) isc_sqlcode( status ) ); return HB_FAILURE; } - if ( isc_dsql_allocate_statement( status, ( isc_db_handle * ) ( void * ) &pArea->pConnection->hConnection, &pStmt ) ) + if ( isc_dsql_allocate_statement( status, &hDb, &hStmt ) ) { hb_errRT_FireBirdDD( EG_OPEN, ESQLDD_STMTALLOC, "Allocate statement failed", NULL, ( HB_ERRCODE ) isc_sqlcode( status ) ); - isc_rollback_transaction( status, &pTrans ); + isc_rollback_transaction( status, &hTrans ); return HB_FAILURE; } @@ -217,15 +241,14 @@ static HB_ERRCODE fbOpen( SQLBASEAREAP pArea ) pSqlda->sqln = 1; pSqlda->version = 1; - if ( isc_dsql_prepare( status, &pTrans, &pStmt, 0, pArea->szQuery, SQL_DIALECT_V5, pSqlda ) ) + if ( isc_dsql_prepare( status, &hTrans, &hStmt, 0, pArea->szQuery, SQL_DIALECT_V5, pSqlda ) ) { hb_errRT_FireBirdDD( EG_OPEN, ESQLDD_INVALIDQUERY, "Prepare statement failed", pArea->szQuery, ( HB_ERRCODE ) isc_sqlcode( status ) ); - isc_dsql_free_statement( status, &pStmt, DSQL_drop ); - isc_rollback_transaction( status, &pTrans ); + isc_dsql_free_statement( status, &hStmt, DSQL_drop ); + isc_rollback_transaction( status, &hTrans ); hb_xfree( pSqlda ); return HB_FAILURE; } - if ( pSqlda->sqld > pSqlda->sqln ) { uiFields = pSqlda->sqld; @@ -234,16 +257,20 @@ static HB_ERRCODE fbOpen( SQLBASEAREAP pArea ) pSqlda->sqln = uiFields; pSqlda->version = 1; - if ( isc_dsql_describe( status, & pStmt, SQL_DIALECT_V5, pSqlda ) ) + if ( isc_dsql_describe( status, & hStmt, SQL_DIALECT_V5, pSqlda ) ) { hb_errRT_FireBirdDD( EG_OPEN, ESQLDD_STMTDESCR, "Describe statement failed", NULL, ( HB_ERRCODE ) isc_sqlcode( status ) ); - isc_dsql_free_statement( status, &pStmt, DSQL_drop ); - isc_rollback_transaction( status, &pTrans ); + isc_dsql_free_statement( status, &hStmt, DSQL_drop ); + isc_rollback_transaction( status, &hTrans ); hb_xfree( pSqlda ); return HB_FAILURE; } } + pSDDData->hTrans = hTrans; + pSDDData->hStmt = hStmt; + pSDDData->pSqlda = pSqlda; + uiFields = pSqlda->sqld; SELF_SETFIELDEXTENT( ( AREAP ) pArea, uiFields ); @@ -378,10 +405,6 @@ static HB_ERRCODE fbOpen( SQLBASEAREAP pArea ) hb_xfree( pBuffer ); - pArea->pResult = pSqlda; - pArea->pStmt = ( void * ) ( HB_PTRDIFF ) pStmt; - pArea->pTrans = ( void * ) ( HB_PTRDIFF ) pTrans; - if ( bError ) { hb_itemClear( pItemEof ); @@ -406,36 +429,29 @@ static HB_ERRCODE fbOpen( SQLBASEAREAP pArea ) static HB_ERRCODE fbClose( SQLBASEAREAP pArea ) { + SDDDATA * pSDDData = ( SDDDATA * ) pArea->pSDDData; ISC_STATUS_ARRAY status; - if ( pArea->pResult ) + if ( pSDDData->pSqlda ) { - hb_xfree( pArea->pResult ); - pArea->pResult = NULL; + hb_xfree( pSDDData->pSqlda ); } - if ( pArea->pStmt ) + if ( pSDDData->hStmt ) { - isc_stmt_handle stmt = ( isc_stmt_handle ) ( HB_PTRDIFF ) pArea->pStmt; - - /* We can not pass here ( isc_stmt_handle* ) &pArea->pStmt. - It will not work on 64bit big-endian system, since on 64bit - handle is unsigned int */ - isc_dsql_free_statement( status, &stmt, DSQL_drop ); - pArea->pStmt = NULL; + isc_dsql_free_statement( status, & pSDDData->hStmt, DSQL_drop ); } - if ( pArea->pTrans ) + if ( pSDDData->hTrans ) { - isc_tr_handle tr = ( isc_tr_handle ) ( HB_PTRDIFF ) pArea->pTrans; - - isc_rollback_transaction( status, &tr ); - pArea->pTrans = NULL; + isc_rollback_transaction( status, & pSDDData->hTrans ); } + hb_xfree( pSDDData ); return HB_SUCCESS; } static HB_ERRCODE fbGoTo( SQLBASEAREAP pArea, HB_ULONG ulRecNo ) { + SDDDATA * pSDDData = ( SDDDATA * ) pArea->pSDDData; ISC_STATUS_ARRAY status; XSQLVAR * pVar; PHB_ITEM pItem, pArray; @@ -445,17 +461,17 @@ static HB_ERRCODE fbGoTo( SQLBASEAREAP pArea, HB_ULONG ulRecNo ) while ( ulRecNo > pArea->ulRecCount && ! pArea->fFetched ) { - isc_stmt_handle stmt = ( isc_stmt_handle ) ( HB_PTRDIFF ) pArea->pStmt; - isc_tr_handle tr = ( isc_tr_handle ) ( HB_PTRDIFF ) pArea->pTrans; + isc_stmt_handle stmt = pSDDData->hStmt; + isc_tr_handle tr = pSDDData->hTrans; - lErr = isc_dsql_fetch( status, &stmt, SQL_DIALECT_V5, ( XSQLDA * ) pArea->pResult ); + lErr = isc_dsql_fetch( status, &stmt, SQL_DIALECT_V5, pSDDData->pSqlda ); if ( lErr == 0 ) { pArray = hb_itemArrayNew( pArea->area.uiFieldCount ); for ( ui = 1; ui <= pArea->area.uiFieldCount; ui++ ) { - pVar = ( XSQLVAR * ) pArea->pResult; + pVar = ( XSQLVAR * ) pSDDData->pSqlda; pVar += ( ui - 1 ); if ( ( pVar->sqltype & 1 ) && ( * pVar->sqlind < 0 ) ) @@ -478,7 +494,7 @@ static HB_ERRCODE fbGoTo( SQLBASEAREAP pArea, HB_ULONG ulRecNo ) case SQL_LONG: pItem = hb_itemPutNL( NULL, * ( long * ) pVar->sqldata ); - break; + break; case SQL_FLOAT: pItem = hb_itemPutND( NULL, * ( float * ) pVar->sqldata ); @@ -515,17 +531,17 @@ static HB_ERRCODE fbGoTo( SQLBASEAREAP pArea, HB_ULONG ulRecNo ) hb_errRT_FireBirdDD( EG_OPEN, ESQLDD_STMTFREE, "Statement free error", NULL, ( HB_ERRCODE ) isc_sqlcode( status ) ); return HB_FAILURE; } - pArea->pStmt = NULL; + pSDDData->hStmt = NULL; if ( isc_commit_transaction( status, &tr ) ) { hb_errRT_FireBirdDD( EG_OPEN, ESQLDD_COMMIT, "Transaction commit error", NULL, ( HB_ERRCODE ) isc_sqlcode( status ) ); return HB_FAILURE; } - pArea->pTrans = NULL; + pSDDData->hTrans = NULL; - hb_xfree( pArea->pResult ); /* TODO: free is more complex */ - pArea->pResult = NULL; + hb_xfree( pSDDData->pSqlda ); /* TODO: free is more complex */ + pSDDData->pSqlda = NULL; } else diff --git a/harbour/contrib/rddsql/sddmy/sddmy.c b/harbour/contrib/rddsql/sddmy/sddmy.c index eb29237d17..726fa92d1d 100644 --- a/harbour/contrib/rddsql/sddmy/sddmy.c +++ b/harbour/contrib/rddsql/sddmy/sddmy.c @@ -67,6 +67,19 @@ typedef int my_socket; #endif +typedef struct +{ + MYSQL * pMySql; +} SDDCONN; + +typedef struct +{ + MYSQL_RES * pResult; + MYSQL_ROW pNatRecord; + unsigned long * pNatLength; +} SDDDATA; + + static HB_ERRCODE mysqlConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ); static HB_ERRCODE mysqlDisconnect( SQLDDCONNECTION * pConnection ); static HB_ERRCODE mysqlExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ); @@ -98,8 +111,7 @@ static void hb_mysqldd_init( void * cargo ) HB_SYMBOL_UNUSED( cargo ); if ( ! hb_sddRegister( & mysqldd ) || - ( sizeof( MYSQL_ROW_OFFSET ) != sizeof( void * ) ) || - ( sizeof( MYSQL_ROW ) != sizeof( void * ) ) ) + ( sizeof( MYSQL_ROW_OFFSET ) != sizeof( void * ) ) ) { hb_errInternal( HB_EI_RDDINVALID, NULL, NULL, NULL ); HB_FUNC_EXEC( SQLBASE ); /* force SQLBASE linking */ @@ -142,7 +154,7 @@ static HB_USHORT hb_errRT_MySQLDD( HB_ERRCODE errGenCode, HB_ERRCODE errSubCode, static HB_ERRCODE mysqlConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) { - MYSQL* pMySql; + MYSQL * pMySql; pMySql = mysql_init( NULL ); if ( ! mysql_real_connect( pMySql, hb_arrayGetCPtr( pItem, 2 ), hb_arrayGetCPtr( pItem, 3 ), hb_arrayGetCPtr( pItem, 4 ), @@ -152,31 +164,34 @@ static HB_ERRCODE mysqlConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) mysql_close( pMySql ); return HB_FAILURE; } - pConnection->hConnection = ( void * ) pMySql; + pConnection->pSDDConn = hb_xgrab( sizeof( SDDCONN ) ); + ( ( SDDCONN * ) pConnection->pSDDConn )->pMySql = pMySql; return HB_SUCCESS; } static HB_ERRCODE mysqlDisconnect( SQLDDCONNECTION * pConnection ) { - mysql_close( ( MYSQL * ) pConnection->hConnection ); + mysql_close( ( ( SDDCONN * ) pConnection->pSDDConn )->pMySql ); + hb_xfree( pConnection->pSDDConn ); return HB_SUCCESS; } static HB_ERRCODE mysqlExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) { + MYSQL * pMySql = ( ( SDDCONN * ) pConnection->pSDDConn )->pMySql; MYSQL_RES * pResult; HB_ULONG ulAffectedRows; PHB_ITEM pNewID = NULL; - if ( mysql_real_query( ( MYSQL * ) pConnection->hConnection, hb_itemGetCPtr( pItem ), hb_itemGetCLen( pItem ) ) ) + if ( mysql_real_query( pMySql, hb_itemGetCPtr( pItem ), hb_itemGetCLen( pItem ) ) ) { - hb_rddsqlSetError( mysql_errno( ( MYSQL * ) pConnection->hConnection ), mysql_error( ( MYSQL * ) pConnection->hConnection ), hb_itemGetCPtr( pItem ), NULL, 0 ); + hb_rddsqlSetError( mysql_errno( pMySql ), mysql_error( pMySql ), hb_itemGetCPtr( pItem ), NULL, 0 ); return HB_FAILURE; } - pResult = mysql_store_result( ( MYSQL * ) pConnection->hConnection ); + pResult = mysql_store_result( pMySql ); if ( pResult ) { ulAffectedRows = ( HB_ULONG ) mysql_num_rows( pResult ); @@ -185,12 +200,12 @@ static HB_ERRCODE mysqlExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) } else { - if ( mysql_field_count( ( MYSQL * ) pConnection->hConnection ) == 0 ) + if ( mysql_field_count( pMySql ) == 0 ) { - ulAffectedRows = ( HB_ULONG ) mysql_affected_rows( ( MYSQL * ) pConnection->hConnection ); - if( mysql_insert_id( ( MYSQL * ) pConnection->hConnection ) != 0 ) + ulAffectedRows = ( HB_ULONG ) mysql_affected_rows( pMySql ); + if( mysql_insert_id( pMySql ) != 0 ) { - pNewID = hb_itemPutNInt( NULL, mysql_insert_id( ( MYSQL * ) pConnection->hConnection ) ); + pNewID = hb_itemPutNInt( NULL, mysql_insert_id( pMySql ) ); } hb_rddsqlSetError( 0, NULL, hb_itemGetCPtr( pItem ), pNewID, ulAffectedRows ); if( pNewID ) @@ -198,7 +213,7 @@ static HB_ERRCODE mysqlExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) } else /* error */ { - hb_rddsqlSetError( mysql_errno( ( MYSQL * ) pConnection->hConnection ), mysql_error( ( MYSQL * ) pConnection->hConnection ), hb_itemGetCPtr( pItem ), NULL, 0 ); + hb_rddsqlSetError( mysql_errno( pMySql ), mysql_error( pMySql ), hb_itemGetCPtr( pItem ), NULL, 0 ); return HB_FAILURE; } } @@ -208,6 +223,8 @@ static HB_ERRCODE mysqlExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) static HB_ERRCODE mysqlOpen( SQLBASEAREAP pArea ) { + MYSQL * pMySql = ( ( SDDCONN * ) pArea->pConnection->pSDDConn )->pMySql; + SDDDATA * pSDDData; PHB_ITEM pItemEof, pItem; HB_ULONG ulIndex; HB_USHORT uiFields, uiCount; @@ -218,21 +235,24 @@ static HB_ERRCODE mysqlOpen( SQLBASEAREAP pArea ) MYSQL_FIELD * pMyField; void ** pRow; - if ( mysql_real_query( ( MYSQL * ) pArea->pConnection->hConnection, pArea->szQuery, strlen( pArea->szQuery ) ) ) + pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) ); + pSDDData = ( SDDDATA * ) pArea->pSDDData; + + if ( mysql_real_query( pMySql, pArea->szQuery, strlen( pArea->szQuery ) ) ) { - hb_errRT_MySQLDD( EG_OPEN, ESQLDD_INVALIDQUERY, ( char * ) mysql_error( ( MYSQL * ) pArea->pConnection->hConnection ), pArea->szQuery, - mysql_errno( ( MYSQL * ) pArea->pConnection->hConnection ) ); + hb_errRT_MySQLDD( EG_OPEN, ESQLDD_INVALIDQUERY, ( char * ) mysql_error( pMySql ), pArea->szQuery, + mysql_errno( pMySql ) ); return HB_FAILURE; } - if ( ( pArea->pResult = mysql_store_result( ( MYSQL * ) pArea->pConnection->hConnection ) ) == NULL ) + if ( ( pSDDData->pResult = mysql_store_result( pMySql ) ) == NULL ) { - hb_errRT_MySQLDD( EG_MEM, ESQLDD_INVALIDQUERY, ( char * ) mysql_error( ( MYSQL * ) pArea->pConnection->hConnection ), pArea->szQuery, - mysql_errno( ( MYSQL * ) pArea->pConnection->hConnection ) ); + hb_errRT_MySQLDD( EG_MEM, ESQLDD_INVALIDQUERY, ( char * ) mysql_error( pMySql ), pArea->szQuery, + mysql_errno( pMySql ) ); return HB_FAILURE; } - uiFields = ( HB_USHORT ) mysql_num_fields( ( MYSQL_RES * ) pArea->pResult ); + uiFields = ( HB_USHORT ) mysql_num_fields( pSDDData->pResult ); SELF_SETFIELDEXTENT( ( AREAP ) pArea, uiFields ); pItemEof = hb_itemArrayNew( uiFields ); @@ -242,7 +262,7 @@ static HB_ERRCODE mysqlOpen( SQLBASEAREAP pArea ) bError = HB_FALSE; for ( uiCount = 0; uiCount < uiFields; uiCount++ ) { - pMyField = mysql_fetch_field_direct( ( MYSQL_RES * ) pArea->pResult, uiCount ); + pMyField = mysql_fetch_field_direct( pSDDData->pResult, uiCount ); hb_strncpy( pBuffer, pMyField->name, 256 - 1 ); pBuffer[ MAX_FIELD_NAME ] = '\0'; @@ -388,7 +408,7 @@ static HB_ERRCODE mysqlOpen( SQLBASEAREAP pArea ) return HB_FAILURE; } - pArea->ulRecCount = ( HB_ULONG ) mysql_num_rows( ( MYSQL_RES * ) pArea->pResult ); + pArea->ulRecCount = ( HB_ULONG ) mysql_num_rows( pSDDData->pResult ); pArea->pRow = ( void ** ) hb_xgrab( ( pArea->ulRecCount + 1 ) * sizeof( void * ) ); pArea->pRowFlags = ( HB_BYTE * ) hb_xgrab( ( pArea->ulRecCount + 1 ) * sizeof( HB_BYTE ) ); @@ -403,10 +423,10 @@ static HB_ERRCODE mysqlOpen( SQLBASEAREAP pArea ) pRow++; for ( ulIndex = 1; ulIndex <= pArea->ulRecCount; ulIndex++ ) { - *pRow++ = ( void * ) mysql_row_tell( ( MYSQL_RES * ) pArea->pResult ); - mysql_fetch_row( ( MYSQL_RES * ) pArea->pResult ); + *pRow++ = ( void * ) mysql_row_tell( pSDDData->pResult ); + mysql_fetch_row( pSDDData->pResult ); } - pArea->fFetched = 1; + pArea->fFetched = HB_TRUE; return HB_SUCCESS; } @@ -414,17 +434,21 @@ static HB_ERRCODE mysqlOpen( SQLBASEAREAP pArea ) static HB_ERRCODE mysqlClose( SQLBASEAREAP pArea ) { - if ( pArea->pResult ) + SDDDATA * pSDDData = ( SDDDATA * ) pArea->pSDDData; + + if( pSDDData->pResult ) { - mysql_free_result( ( MYSQL_RES * ) pArea->pResult ); - pArea->pResult = NULL; + mysql_free_result( pSDDData->pResult ); } + hb_xfree( pSDDData ); return HB_SUCCESS; } static HB_ERRCODE mysqlGoTo( SQLBASEAREAP pArea, HB_ULONG ulRecNo ) { + SDDDATA * pSDDData = ( SDDDATA * ) pArea->pSDDData; + if ( ulRecNo == 0 || ulRecNo > pArea->ulRecCount ) { pArea->pRecord = pArea->pRow[ 0 ]; @@ -439,9 +463,9 @@ static HB_ERRCODE mysqlGoTo( SQLBASEAREAP pArea, HB_ULONG ulRecNo ) if ( ! ( pArea->bRecordFlags & SQLDD_FLAG_CACHED ) ) { - mysql_row_seek( ( MYSQL_RES * ) pArea->pResult, ( MYSQL_ROW_OFFSET ) pArea->pRecord ); - pArea->pNatRecord = ( void * ) mysql_fetch_row( ( MYSQL_RES * ) pArea->pResult ); - pArea->pNatLength = ( void * ) mysql_fetch_lengths( ( MYSQL_RES * ) pArea->pResult ); + mysql_row_seek( pSDDData->pResult, ( MYSQL_ROW_OFFSET ) pArea->pRecord ); + pSDDData->pNatRecord = mysql_fetch_row( pSDDData->pResult ); + pSDDData->pNatLength = mysql_fetch_lengths( pSDDData->pResult ); } pArea->fPositioned = HB_TRUE; @@ -452,6 +476,7 @@ static HB_ERRCODE mysqlGoTo( SQLBASEAREAP pArea, HB_ULONG ulRecNo ) static HB_ERRCODE mysqlGetValue( SQLBASEAREAP pArea, HB_USHORT uiIndex, PHB_ITEM pItem ) { + SDDDATA * pSDDData = ( SDDDATA * ) pArea->pSDDData; LPFIELD pField; char* pValue; char szBuffer[ 64 ]; @@ -463,8 +488,8 @@ static HB_ERRCODE mysqlGetValue( SQLBASEAREAP pArea, HB_USHORT uiIndex, PHB_ITEM uiIndex--; pField = pArea->area.lpFields + uiIndex; - pValue = ( ( MYSQL_ROW ) ( pArea->pNatRecord ) ) [ uiIndex ]; - ulLen = ( ( unsigned long * ) ( pArea->pNatLength ) ) [ uiIndex ]; + pValue = pSDDData->pNatRecord[ uiIndex ]; + ulLen = pSDDData->pNatLength[ uiIndex ]; /* NULL => NIL (?) */ if ( ! pValue ) diff --git a/harbour/contrib/rddsql/sddoci/sddoci.c b/harbour/contrib/rddsql/sddoci/sddoci.c index 06b74644b3..40f5760b2e 100644 --- a/harbour/contrib/rddsql/sddoci/sddoci.c +++ b/harbour/contrib/rddsql/sddoci/sddoci.c @@ -94,6 +94,18 @@ #define D_HB_CHAR char #endif + +typedef struct +{ + OCI_Connection * pConn; +} SDDCONN; + +typedef struct +{ + OCI_Statement * pStmt; +} SDDDATA; + + static HB_ERRCODE ocilibConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ); static HB_ERRCODE ocilibDisconnect( SQLDDCONNECTION * pConnection ); static HB_ERRCODE ocilibExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ); @@ -221,24 +233,27 @@ static HB_ERRCODE ocilibConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) if( cn ) { - pConnection->hConnection = ( void * ) cn; + pConnection->pSDDConn = hb_xgrab( sizeof( SDDCONN ) ); + ( ( SDDCONN * ) pConnection->pSDDConn )->pConn = cn; return HB_SUCCESS; } - - pConnection->hConnection = NULL; return HB_FAILURE; } static HB_ERRCODE ocilibDisconnect( SQLDDCONNECTION * pConnection ) { - return OCI_ConnectionFree( ( OCI_Connection * ) pConnection->hConnection ) ? HB_SUCCESS : HB_FAILURE; + HB_ERRCODE errCode; + + errCode = OCI_ConnectionFree( ( ( SDDCONN * ) pConnection->pSDDConn )->pConn ) ? HB_SUCCESS : HB_FAILURE; + hb_xfree( pConnection->pSDDConn ); + return errCode; } static HB_ERRCODE ocilibExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) { - OCI_Statement * st = OCI_StatementCreate( ( OCI_Connection * ) pConnection->hConnection ); + OCI_Statement * st = OCI_StatementCreate( ( ( SDDCONN * ) pConnection->pSDDConn )->pConn ); void * hStatement; char * szError; HB_ERRCODE errCode; @@ -264,7 +279,7 @@ static HB_ERRCODE ocilibExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) hb_strfree( hStatement ); szError = ocilibGetError( &errCode ); - hb_rddsqlSetError( errCode, szError, hb_itemGetCPtr( pItem ), NULL, errCode ); + hb_rddsqlSetError( errCode, szError, hb_itemGetCPtr( pItem ), NULL, 0 ); hb_xfree( szError ); OCI_StatementFree( st ); return HB_FAILURE; @@ -273,8 +288,9 @@ static HB_ERRCODE ocilibExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) static HB_ERRCODE ocilibOpen( SQLBASEAREAP pArea ) { - OCI_Statement * st = OCI_StatementCreate( ( OCI_Connection * ) pArea->pConnection->hConnection ); + 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; @@ -282,6 +298,9 @@ static HB_ERRCODE ocilibOpen( SQLBASEAREAP pArea ) char * szError; HB_BOOL bError; + pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) ); + pSDDData = ( SDDDATA * ) pArea->pSDDData; + if( ! st ) { szError = ocilibGetError( &errCode ); @@ -479,25 +498,27 @@ static HB_ERRCODE ocilibOpen( SQLBASEAREAP pArea ) pArea->pRow[ 0 ] = pItemEof; pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED; - pArea->pStmt = ( void * ) st; + pSDDData->pStmt = st; return HB_SUCCESS; } static HB_ERRCODE ocilibClose( SQLBASEAREAP pArea ) { - if( pArea->pStmt ) + SDDDATA * pSDDData = ( SDDDATA * ) pArea->pSDDData; + + if( pSDDData->pStmt ) { - OCI_StatementFree( ( OCI_Statement * ) pArea->pStmt ); - pArea->pStmt = NULL; + OCI_StatementFree( pSDDData->pStmt ); } + hb_xfree( pSDDData ); return HB_SUCCESS; } static HB_ERRCODE ocilibGoTo( SQLBASEAREAP pArea, HB_ULONG ulRecNo ) { - OCI_Statement * st = ( OCI_Statement * ) pArea->pStmt; + OCI_Statement * st = ( ( SDDDATA * ) pArea->pSDDData )->pStmt; OCI_Resultset * rs = OCI_GetResultset( st ); while( ulRecNo > pArea->ulRecCount && ! pArea->fFetched ) diff --git a/harbour/contrib/rddsql/sddodbc/sddodbc.c b/harbour/contrib/rddsql/sddodbc/sddodbc.c index d769b38e74..d61c697aee 100644 --- a/harbour/contrib/rddsql/sddodbc/sddodbc.c +++ b/harbour/contrib/rddsql/sddodbc/sddodbc.c @@ -98,6 +98,18 @@ #define O_HB_CHAR char #endif + +typedef struct +{ + SQLHENV hEnv; + SQLHDBC hConn; +} SDDCONN; + +typedef struct +{ + SQLHSTMT hStmt; +} SDDDATA; + static HB_ERRCODE odbcConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ); static HB_ERRCODE odbcDisconnect( SQLDDCONNECTION * pConnection ); static HB_ERRCODE odbcExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ); @@ -224,8 +236,9 @@ static HB_ERRCODE odbcConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) SQL_DRIVER_NOPROMPT ) ) ) { hb_strfree( hConnStr ); - pConnection->hCargo = ( void * ) hEnv; - pConnection->hConnection = ( void * ) hConnect; + pConnection->pSDDConn = hb_xgrab( sizeof( SDDCONN ) ); + ( ( SDDCONN * ) pConnection->pSDDConn )->hConn = hConnect; + ( ( SDDCONN * ) pConnection->pSDDConn )->hEnv = hEnv; return HB_SUCCESS; } else @@ -257,24 +270,28 @@ static HB_ERRCODE odbcConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) static HB_ERRCODE odbcDisconnect( SQLDDCONNECTION * pConnection ) { - SQLDisconnect( ( SQLHDBC ) pConnection->hConnection ); - SQLFreeHandle( SQL_HANDLE_DBC, ( SQLHDBC ) pConnection->hConnection ); - SQLFreeHandle( SQL_HANDLE_ENV, ( SQLHENV ) pConnection->hCargo ); + SDDCONN * pSDDConn = ( SDDCONN * ) pConnection->pSDDConn; + + SQLDisconnect( pSDDConn->hConn ); + SQLFreeHandle( SQL_HANDLE_DBC, pSDDConn->hConn ); + SQLFreeHandle( SQL_HANDLE_ENV, pSDDConn->hEnv ); + hb_xfree( pSDDConn ); return HB_SUCCESS; } static HB_ERRCODE odbcExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) { + SDDCONN * pSDDConn = ( SDDCONN * ) pConnection->pSDDConn; void * hStatement; SQLHSTMT hStmt; SQLLEN iCount; char * szError; HB_ERRCODE errCode; - if ( ! SQL_SUCCEEDED( SQLAllocStmt( ( SQLHDBC ) pConnection->hConnection, &hStmt ) ) ) + if ( ! SQL_SUCCEEDED( SQLAllocStmt( pSDDConn->hConn, &hStmt ) ) ) { - szError = odbcGetError( ( SQLHENV ) pConnection->hCargo, ( SQLHDBC ) pConnection->hConnection, SQL_NULL_HSTMT, &errCode ); + szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, SQL_NULL_HSTMT, &errCode ); hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTALLOC, szError, hb_itemGetCPtr( pItem ), errCode ); hb_xfree( szError ); return HB_FAILURE; @@ -295,7 +312,7 @@ static HB_ERRCODE odbcExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) else hb_strfree( hStatement ); - szError = odbcGetError( ( SQLHENV ) pConnection->hCargo, ( SQLHDBC ) pConnection->hConnection, hStmt, &errCode ); + szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode ); hb_rddsqlSetError( errCode, szError, hb_itemGetCPtr( pItem ), NULL, errCode ); hb_xfree( szError ); SQLFreeStmt( hStmt, SQL_DROP ); @@ -305,6 +322,8 @@ static HB_ERRCODE odbcExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) static HB_ERRCODE odbcOpen( SQLBASEAREAP pArea ) { + SDDCONN * pSDDConn = ( SDDCONN * ) pArea->pConnection->pSDDConn; + SDDDATA * pSDDData; void * hQuery; SQLHSTMT hStmt; SQLSMALLINT iNameLen; @@ -314,9 +333,12 @@ static HB_ERRCODE odbcOpen( SQLBASEAREAP pArea ) HB_ERRCODE errCode; char * szError; - if ( ! SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_STMT, ( SQLHDBC ) pArea->pConnection->hConnection, &hStmt ) ) ) + pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) ); + pSDDData = ( SDDDATA * ) pArea->pSDDData; + + if ( ! SQL_SUCCEEDED( SQLAllocHandle( SQL_HANDLE_STMT, pSDDConn->hConn, &hStmt ) ) ) { - szError = odbcGetError( ( SQLHENV ) pArea->pConnection->hCargo, ( SQLHDBC ) pArea->pConnection->hConnection, SQL_NULL_HSTMT, &errCode ); + 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; @@ -328,7 +350,7 @@ static HB_ERRCODE odbcOpen( SQLBASEAREAP pArea ) { hb_strfree( hQuery ); hb_itemRelease( pItem ); - szError = odbcGetError( ( SQLHENV ) pArea->pConnection->hCargo, ( SQLHDBC ) pArea->pConnection->hConnection, hStmt, &errCode ); + szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode ); SQLFreeStmt( hStmt, SQL_DROP ); hb_errRT_ODBCDD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode ); hb_xfree( szError ); @@ -342,7 +364,7 @@ static HB_ERRCODE odbcOpen( SQLBASEAREAP pArea ) if ( ! SQL_SUCCEEDED( SQLNumResultCols( hStmt, &iNameLen ) ) ) { - szError = odbcGetError( ( SQLHENV ) pArea->pConnection->hCargo, ( SQLHDBC ) pArea->pConnection->hConnection, hStmt, &errCode ); + szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, &errCode ); SQLFreeStmt( hStmt, SQL_DROP ); hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTDESCR + 1000, szError, pArea->szQuery, errCode ); hb_xfree( szError ); @@ -372,7 +394,7 @@ static HB_ERRCODE odbcOpen( SQLBASEAREAP pArea ) if ( ! SQL_SUCCEEDED( SQLDescribeCol( hStmt, ( SQLSMALLINT ) uiIndex + 1, ( SQLTCHAR * ) cName, HB_SIZEOFARRAY( cName ), &iNameLen, &iDataType, &uiSize, &iDec, &iNull ) ) ) { hb_itemRelease( pItemEof ); - szError = odbcGetError( ( SQLHENV ) pArea->pConnection->hCargo, ( SQLHDBC ) pArea->pConnection->hConnection, hStmt, NULL ); + szError = odbcGetError( pSDDConn->hEnv, pSDDConn->hConn, hStmt, NULL ); SQLFreeStmt( hStmt, SQL_DROP ); hb_errRT_ODBCDD( EG_OPEN, ESQLDD_STMTDESCR + 1001, szError, pArea->szQuery, 0 ); hb_xfree( szError ); @@ -535,25 +557,27 @@ static HB_ERRCODE odbcOpen( SQLBASEAREAP pArea ) pArea->pRow[ 0 ] = pItemEof; pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED; - pArea->pStmt = ( void * ) hStmt; + pSDDData->hStmt = hStmt; return HB_SUCCESS; } static HB_ERRCODE odbcClose( SQLBASEAREAP pArea ) { - if ( pArea->pStmt ) + SDDDATA * pSDDData = ( SDDDATA * ) pArea->pSDDData; + + if ( pSDDData->hStmt ) { - SQLFreeStmt( ( SQLHSTMT ) pArea->pStmt, SQL_DROP ); - pArea->pStmt = NULL; + SQLFreeStmt( pSDDData->hStmt, SQL_DROP ); } + hb_xfree( pSDDData ); return HB_SUCCESS; } static HB_ERRCODE odbcGoTo( SQLBASEAREAP pArea, HB_ULONG ulRecNo ) { - SQLHSTMT hStmt = ( SQLHSTMT ) pArea->pStmt; + SQLHSTMT hStmt = ( ( SDDDATA * ) pArea->pSDDData )->hStmt; SQLRETURN res; SQLLEN iLen; PHB_ITEM pArray, pItem; diff --git a/harbour/contrib/rddsql/sddpg/sddpg.c b/harbour/contrib/rddsql/sddpg/sddpg.c index f73427befd..e30d3319c3 100644 --- a/harbour/contrib/rddsql/sddpg/sddpg.c +++ b/harbour/contrib/rddsql/sddpg/sddpg.c @@ -90,6 +90,16 @@ #define VARBITOID 1562 #define NUMERICOID 1700 +typedef struct +{ + PGconn * pConn; +} SDDCONN; + +typedef struct +{ + PGresult * pResult; +} SDDDATA; + static HB_ERRCODE pgsqlConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ); static HB_ERRCODE pgsqlDisconnect( SQLDDCONNECTION * pConnection ); @@ -163,7 +173,7 @@ static HB_USHORT hb_errRT_PostgreSQLDD( HB_ERRCODE errGenCode, HB_ERRCODE errSub static HB_ERRCODE pgsqlConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) { - PGconn* pConn; + PGconn * pConn; ConnStatusType status; pConn = PQsetdbLogin( hb_arrayGetCPtr( pItem, 2 ), NULL, NULL, NULL, hb_arrayGetCPtr( pItem, 5 ), hb_arrayGetCPtr( pItem, 3 ), hb_arrayGetCPtr( pItem, 4 ) ); @@ -180,31 +190,32 @@ static HB_ERRCODE pgsqlConnect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) PQfinish( pConn ); return HB_FAILURE; } - pConnection->hConnection = ( void * ) pConn; + pConnection->pSDDConn = hb_xgrab( sizeof( SDDCONN ) ); + ( ( SDDCONN * ) pConnection->pSDDConn )->pConn = pConn; return HB_SUCCESS; } -static HB_ERRCODE pgsqlDisconnect( SQLDDCONNECTION* pConnection ) +static HB_ERRCODE pgsqlDisconnect( SQLDDCONNECTION * pConnection ) { - if ( ! pConnection->hConnection ) - return HB_FAILURE; - PQfinish( ( PGconn * ) pConnection->hConnection ); + PQfinish( ( ( SDDCONN * ) pConnection->pSDDConn )->pConn ); + hb_xfree( pConnection->pSDDConn ); return HB_SUCCESS; } -static HB_ERRCODE pgsqlExecute( SQLDDCONNECTION* pConnection, PHB_ITEM pItem ) +static HB_ERRCODE pgsqlExecute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) { + PGconn * pConn = ( ( SDDCONN * ) pConnection->pSDDConn )->pConn; int iTuples; - PGresult* pResult; - ExecStatusType status; + PGresult * pResult; + ExecStatusType status; unsigned long ulAffectedRows; - pResult = PQexec( ( PGconn * ) pConnection->hConnection, hb_itemGetCPtr( pItem ) ); + pResult = PQexec( pConn, hb_itemGetCPtr( pItem ) ); if ( ! pResult ) { - hb_rddsqlSetError( 1, PQerrorMessage( ( PGconn * ) pConnection->hConnection ), hb_itemGetCPtr( pItem ), NULL, 0 ); + hb_rddsqlSetError( 1, PQerrorMessage( pConn ), hb_itemGetCPtr( pItem ), NULL, 0 ); return HB_FAILURE; } @@ -229,7 +240,9 @@ static HB_ERRCODE pgsqlExecute( SQLDDCONNECTION* pConnection, PHB_ITEM pItem ) static HB_ERRCODE pgsqlOpen( SQLBASEAREAP pArea ) { - PGresult* pResult; + PGconn * pConn = ( ( SDDCONN * ) pArea->pConnection->pSDDConn )->pConn; + SDDDATA * pSDDData; + PGresult * pResult; ExecStatusType status; PHB_ITEM pItemEof, pItem; HB_USHORT uiFields, uiCount; @@ -237,8 +250,10 @@ static HB_ERRCODE pgsqlOpen( SQLBASEAREAP pArea ) char* pBuffer; DBFIELDINFO pFieldInfo; + pArea->pSDDData = memset( hb_xgrab( sizeof( SDDDATA ) ), 0, sizeof( SDDDATA ) ); + pSDDData = ( SDDDATA * ) pArea->pSDDData; - pResult = PQexec( ( PGconn * ) pArea->pConnection->hConnection, pArea->szQuery ); + pResult = PQexec( pConn, pArea->szQuery ); if ( ! pResult ) { hb_errRT_PostgreSQLDD( EG_OPEN, ESQLDD_LOWMEMORY, "Query failed", NULL, 0 ); /* Low memory, etc */ @@ -249,10 +264,11 @@ static HB_ERRCODE pgsqlOpen( SQLBASEAREAP pArea ) if ( status != PGRES_TUPLES_OK && status != PGRES_COMMAND_OK ) { hb_errRT_PostgreSQLDD( EG_OPEN, ESQLDD_INVALIDQUERY, PQresultErrorMessage( pResult ), pArea->szQuery, ( HB_ERRCODE ) status ); + PQclear( pResult ); return HB_FAILURE; } - pArea->pResult = pResult; + pSDDData->pResult = pResult; uiFields = ( HB_USHORT ) PQnfields( pResult ); SELF_SETFIELDEXTENT( ( AREAP ) pArea, uiFields ); @@ -449,7 +465,7 @@ static HB_ERRCODE pgsqlOpen( SQLBASEAREAP pArea ) * pArea->pRow = pItemEof; pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED; - pArea->fFetched = 1; + pArea->fFetched = HB_TRUE; return HB_SUCCESS; } @@ -457,17 +473,20 @@ static HB_ERRCODE pgsqlOpen( SQLBASEAREAP pArea ) static HB_ERRCODE pgsqlClose( SQLBASEAREAP pArea ) { - if ( pArea->pResult ) + SDDDATA * pSDDData = ( SDDDATA * ) pArea->pSDDData; + + if ( pSDDData->pResult ) { - PQclear( ( PGresult * ) pArea->pResult ); - pArea->pResult = NULL; + PQclear( pSDDData->pResult ); } + hb_xfree( pSDDData ); return HB_SUCCESS; } static HB_ERRCODE pgsqlGetValue( SQLBASEAREAP pArea, HB_USHORT uiIndex, PHB_ITEM pItem ) { + SDDDATA * pSDDData = ( SDDDATA * ) pArea->pSDDData; LPFIELD pField; char* pValue; HB_BOOL bError; @@ -478,11 +497,11 @@ static HB_ERRCODE pgsqlGetValue( SQLBASEAREAP pArea, HB_USHORT uiIndex, PHB_ITEM uiIndex--; pField = pArea->area.lpFields + uiIndex; - if ( PQgetisnull( ( PGresult * ) pArea->pResult, pArea->ulRecNo - 1, uiIndex ) ) + if ( PQgetisnull( pSDDData->pResult, pArea->ulRecNo - 1, uiIndex ) ) return HB_SUCCESS; - pValue = PQgetvalue( ( PGresult * ) pArea->pResult, pArea->ulRecNo - 1, uiIndex ); - ulLen = ( HB_SIZE ) PQgetlength( ( PGresult * ) pArea->pResult, pArea->ulRecNo - 1, uiIndex ); + 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 ); */ diff --git a/harbour/contrib/rddsql/sddsqlt3/sddsqlt3.c b/harbour/contrib/rddsql/sddsqlt3/sddsqlt3.c index 28e0ea8dfc..eb3df063b7 100644 --- a/harbour/contrib/rddsql/sddsqlt3/sddsqlt3.c +++ b/harbour/contrib/rddsql/sddsqlt3/sddsqlt3.c @@ -65,6 +65,16 @@ #define S_HB_ITEMPUTSTR( itm, str ) hb_itemPutStrUTF8( itm, str ) #define S_HB_ITEMPUTSTRLEN( itm, str, len ) hb_itemPutStrLenUTF8( itm, str, len ) +typedef struct +{ + sqlite3 * pDb; +} SDDCONN; + +typedef struct +{ + sqlite3_stmt * pStmt; +} SDDDATA; + static HB_ERRCODE sqlite3Connect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ); static HB_ERRCODE sqlite3Disconnect( SQLDDCONNECTION * pConnection ); static HB_ERRCODE sqlite3Execute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ); @@ -148,19 +158,19 @@ static HB_USHORT hb_errRT_SQLT3DD( HB_ERRCODE errGenCode, HB_ERRCODE errSubCode, } -static char * sqlite3GetError( SQLDDCONNECTION * pConnection, HB_ERRCODE * pErrCode ) +static char * sqlite3GetError( sqlite3 * pDb, HB_ERRCODE * pErrCode ) { char * szRet; int iNativeErr = 9999; - if( pConnection && pConnection->hConnection ) + if( pDb ) { - PHB_ITEM pRet = S_HB_ITEMPUTSTR( NULL, sqlite3_errmsg( ( sqlite3 * ) pConnection->hConnection ) ); + PHB_ITEM pRet = S_HB_ITEMPUTSTR( NULL, sqlite3_errmsg( pDb ) ); szRet = hb_strdup( hb_itemGetCPtr( pRet ) ); hb_itemRelease( pRet ); - iNativeErr = sqlite3_errcode( ( sqlite3 * ) pConnection->hConnection ); + iNativeErr = sqlite3_errcode( pDb ); } else szRet = hb_strdup( "Unable to get error message" ); @@ -180,36 +190,42 @@ static HB_ERRCODE sqlite3Connect( SQLDDCONNECTION * pConnection, PHB_ITEM pItem void * hConn; if( sqlite3_open( S_HB_ARRAYGETSTR( pItem, 2, &hConn, NULL ), &db ) == SQLITE_OK ) - pConnection->hConnection = db; + { + pConnection->pSDDConn = hb_xgrab( sizeof( SDDCONN ) ); + ( ( SDDCONN * ) pConnection->pSDDConn )->pDb = db; + } else { - pConnection->hConnection = NULL; sqlite3_close( db ); } hb_strfree( hConn ); - - return pConnection->hConnection ? HB_SUCCESS : HB_FAILURE; + return db ? HB_SUCCESS : HB_FAILURE; } static HB_ERRCODE sqlite3Disconnect( SQLDDCONNECTION * pConnection ) { - return sqlite3_close( ( sqlite3 * ) pConnection->hConnection ) ? HB_SUCCESS : HB_FAILURE; + HB_ERRCODE errCode; + + errCode = sqlite3_close( ( ( SDDCONN * ) pConnection->pSDDConn )->pDb ) ? HB_SUCCESS : HB_FAILURE; + hb_xfree( pConnection->pSDDConn ); + return errCode; } static HB_ERRCODE sqlite3Execute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem ) { + sqlite3 * pDb = ( ( SDDCONN * ) pConnection->pSDDConn )->pDb; HB_ERRCODE errCode; int iRow, iCol; void * hStatement; char ** pResult = NULL; char * pszErrMsg = NULL; - if( sqlite3_get_table( ( sqlite3 * ) pConnection->hConnection, S_HB_ITEMGETSTR( pItem, &hStatement, NULL ), &pResult, &iRow, &iCol, &pszErrMsg ) != SQLITE_OK ) + if( sqlite3_get_table( pDb, S_HB_ITEMGETSTR( pItem, &hStatement, NULL ), &pResult, &iRow, &iCol, &pszErrMsg ) != SQLITE_OK ) { hb_strfree( hStatement ); - sqlite3GetError( pConnection, &errCode ); + sqlite3GetError( pDb, &errCode ); hb_errRT_SQLT3DD( EG_OPEN, ESQLDD_STMTALLOC, pszErrMsg, hb_itemGetCPtr( pItem ), errCode ); hb_xfree( pszErrMsg ); return HB_FAILURE; @@ -226,7 +242,9 @@ static HB_ERRCODE sqlite3Execute( SQLDDCONNECTION * pConnection, PHB_ITEM pItem 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; @@ -236,14 +254,17 @@ static HB_ERRCODE sqlite3Open( SQLBASEAREAP pArea ) 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( ( sqlite3 * ) pArea->pConnection->hConnection, pszQuery, ( int ) nQueryLen, &st, NULL ) != SQLITE_OK ) + if( sqlite3_prepare_v2( pDb, pszQuery, ( int ) nQueryLen, &st, NULL ) != SQLITE_OK ) { hb_strfree( hQuery ); hb_itemRelease( pItem ); - szError = sqlite3GetError( pArea->pConnection, &errCode ); + szError = sqlite3GetError( pDb, &errCode ); hb_errRT_SQLT3DD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode ); sqlite3_finalize( st ); hb_xfree( szError ); @@ -257,7 +278,7 @@ static HB_ERRCODE sqlite3Open( SQLBASEAREAP pArea ) if( sqlite3_step( st ) != SQLITE_ROW ) { - szError = sqlite3GetError( pArea->pConnection, &errCode ); + szError = sqlite3GetError( pDb, &errCode ); hb_errRT_SQLT3DD( EG_OPEN, ESQLDD_INVALIDQUERY, szError, pArea->szQuery, errCode ); sqlite3_finalize( st ); hb_xfree( szError ); @@ -384,25 +405,27 @@ static HB_ERRCODE sqlite3Open( SQLBASEAREAP pArea ) pArea->pRow[ 0 ] = pItemEof; pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED; - pArea->pStmt = ( void * ) st; + pSDDData->pStmt = st; return HB_SUCCESS; } static HB_ERRCODE sqlite3Close( SQLBASEAREAP pArea ) { - if( pArea->pStmt ) + SDDDATA * pSDDData = ( SDDDATA * ) pArea->pSDDData; + + if( pSDDData->pStmt ) { - sqlite3_finalize( ( sqlite3_stmt * ) pArea->pStmt ); - pArea->pStmt = NULL; + sqlite3_finalize( pSDDData->pStmt ); } + hb_xfree( pSDDData ); return HB_SUCCESS; } static HB_ERRCODE sqlite3GoTo( SQLBASEAREAP pArea, HB_ULONG ulRecNo ) { - sqlite3_stmt * st = ( sqlite3_stmt * ) pArea->pStmt; + sqlite3_stmt * st = ( ( SDDDATA * ) pArea->pSDDData )->pStmt; while( ulRecNo > pArea->ulRecCount && ! pArea->fFetched ) { diff --git a/harbour/contrib/rddsql/sqlbase.c b/harbour/contrib/rddsql/sqlbase.c index 05b050e021..e573ba6035 100644 --- a/harbour/contrib/rddsql/sqlbase.c +++ b/harbour/contrib/rddsql/sqlbase.c @@ -46,7 +46,7 @@ * If you write modifications of your own for Harbour, it is your choice * whether to permit this exception to apply to your modifications. * If you do not wish that, delete this exception notice. - * + * */ #include "hbapi.h" @@ -63,7 +63,7 @@ #define CONNECTION_LIST_EXPAND 4 static HB_USHORT s_rddidSQLBASE = 0; -static SQLDDCONNECTION * s_pConnection = NULL; +static SQLDDCONNECTION ** s_pConnection = NULL; static HB_ULONG s_ulConnectionCount = 0; static HB_ULONG s_ulConnectionCurrent = 0; static char * s_szError = NULL; @@ -650,7 +650,7 @@ static HB_ERRCODE sqlbaseCreate( SQLBASEAREAP pArea, LPDBOPENINFO pOpenInfo ) pArea->ulConnection = pOpenInfo->ulConnection ? pOpenInfo->ulConnection : s_ulConnectionCurrent; if ( pArea->ulConnection > s_ulConnectionCount || - ( pArea->ulConnection && ! s_pConnection[ pArea->ulConnection - 1 ].hConnection ) ) + ( pArea->ulConnection && ! s_pConnection[ pArea->ulConnection - 1 ] ) ) { hb_errRT_SQLBASE( EG_OPEN, ESQLDD_NOTCONNECTED, "Not connected", NULL ); return HB_FAILURE; @@ -658,7 +658,7 @@ static HB_ERRCODE sqlbaseCreate( SQLBASEAREAP pArea, LPDBOPENINFO pOpenInfo ) if ( pArea->ulConnection ) { - pArea->pConnection = & s_pConnection[ pArea->ulConnection - 1 ]; + pArea->pConnection = s_pConnection[ pArea->ulConnection - 1 ]; pArea->pConnection->uiAreaCount++; pArea->pSDD = pArea->pConnection->pSDD; } @@ -753,7 +753,7 @@ static HB_ERRCODE sqlbaseCreate( SQLBASEAREAP pArea, LPDBOPENINFO pOpenInfo ) * (pArea->pRow) = pItemEof; pArea->pRowFlags[ 0 ] = SQLDD_FLAG_CACHED; - pArea->fFetched = 1; + pArea->fFetched = HB_TRUE; if ( SUPER_CREATE( ( AREAP ) pArea, pOpenInfo ) != HB_SUCCESS ) { @@ -788,7 +788,7 @@ static HB_ERRCODE sqlbaseOpen( SQLBASEAREAP pArea, LPDBOPENINFO pOpenInfo ) pArea->ulConnection = pOpenInfo->ulConnection ? pOpenInfo->ulConnection : s_ulConnectionCurrent; if ( pArea->ulConnection == 0 || pArea->ulConnection > s_ulConnectionCount || - ! s_pConnection[ pArea->ulConnection - 1 ].hConnection ) + ! s_pConnection[ pArea->ulConnection - 1 ] ) { hb_errRT_SQLBASE( EG_OPEN, ESQLDD_NOTCONNECTED, "Not connected", NULL ); return HB_FAILURE; @@ -798,7 +798,7 @@ static HB_ERRCODE sqlbaseOpen( SQLBASEAREAP pArea, LPDBOPENINFO pOpenInfo ) /* This should not happen (in __dbTrans()), because RDD is registered with RDT_FULL */ return HB_FAILURE; - pArea->pConnection = & s_pConnection[ pArea->ulConnection - 1 ]; + pArea->pConnection = s_pConnection[ pArea->ulConnection - 1 ]; pArea->pConnection->uiAreaCount++; pArea->pSDD = pArea->pConnection->pSDD; @@ -912,9 +912,10 @@ static HB_ERRCODE sqlbaseExit( LPRDDNODE pRDD ) /* Disconnect all connections */ for ( ul = 0; ul < s_ulConnectionCount; ul++ ) { - if ( s_pConnection[ ul ].hConnection ) + if ( s_pConnection[ ul ] ) { - s_pConnection[ ul ].pSDD->Disconnect( & s_pConnection[ ul ] ); + s_pConnection[ ul ]->pSDD->Disconnect( s_pConnection[ ul ] ); + hb_xfree( s_pConnection[ ul ] ); } } hb_xfree( s_pConnection ); @@ -947,8 +948,8 @@ static HB_ERRCODE sqlbaseRddInfo( LPRDDNODE pRDD, HB_USHORT uiIndex, HB_ULONG ul HB_SYMBOL_UNUSED( pRDD ); ulConn = ulConnect ? ulConnect : s_ulConnectionCurrent; - if ( ulConn > 0 && ulConn < s_ulConnectionCount && s_pConnection[ --ulConn ].hConnection ) - pConn = & s_pConnection[ ulConn ]; + if ( ulConn > 0 && ulConn <= s_ulConnectionCount ) + pConn = s_pConnection[ ulConn - 1 ]; else pConn = NULL; @@ -990,26 +991,6 @@ static HB_ERRCODE sqlbaseRddInfo( LPRDDNODE pRDD, HB_USHORT uiIndex, HB_ULONG ul HB_ULONG ul; const char * pStr; - /* Find free connection handle */ - for ( ul = 0; ul < s_ulConnectionCount; ul++ ) - { - if ( ! s_pConnection[ ul ].hConnection ) - break; - } - - if ( ul >= s_ulConnectionCount ) - { - /* Realloc connection table */ - if ( s_pConnection ) - s_pConnection = ( SQLDDCONNECTION * ) hb_xrealloc( s_pConnection, sizeof( SQLDDCONNECTION ) * ( s_ulConnectionCount + CONNECTION_LIST_EXPAND ) ); - else - s_pConnection = ( SQLDDCONNECTION * ) hb_xgrab( sizeof( SQLDDCONNECTION ) * CONNECTION_LIST_EXPAND ); - - memset( &s_pConnection[ s_ulConnectionCount ], 0, sizeof( SQLDDCONNECTION ) * CONNECTION_LIST_EXPAND ); - ul = s_ulConnectionCount; - s_ulConnectionCount += CONNECTION_LIST_EXPAND; - } - pStr = hb_arrayGetCPtr( pItem, 1 ); if ( pStr ) { @@ -1022,18 +1003,40 @@ static HB_ERRCODE sqlbaseRddInfo( LPRDDNODE pRDD, HB_USHORT uiIndex, HB_ULONG ul } } - pConn = & s_pConnection[ ul ]; - ul++; hb_rddsqlSetError( 0, NULL, NULL, NULL, 0 ); + pConn = ( SQLDDCONNECTION * ) hb_xgrab( sizeof( SQLDDCONNECTION ) ); + memset( pConn, 0, sizeof( SQLDDCONNECTION ) ); if ( pNode && pNode->Connect( pConn, pItem ) == HB_SUCCESS ) { pConn->pSDD = pNode; + + /* Find free connection handle */ + for ( ul = 0; ul < s_ulConnectionCount; ul++ ) + { + if ( ! s_pConnection[ ul ] ) + break; + } + if ( ul >= s_ulConnectionCount ) + { + /* Realloc connection table */ + if ( s_pConnection ) + s_pConnection = ( SQLDDCONNECTION ** ) hb_xrealloc( s_pConnection, sizeof( SQLDDCONNECTION * ) * ( s_ulConnectionCount + CONNECTION_LIST_EXPAND ) ); + else + s_pConnection = ( SQLDDCONNECTION ** ) hb_xgrab( sizeof( SQLDDCONNECTION * ) * CONNECTION_LIST_EXPAND ); + + memset( s_pConnection + s_ulConnectionCount, 0, sizeof( SQLDDCONNECTION * ) * CONNECTION_LIST_EXPAND ); + ul = s_ulConnectionCount; + s_ulConnectionCount += CONNECTION_LIST_EXPAND; + } + s_pConnection[ ul ] = pConn; + ul++; + s_ulConnectionCurrent = ul; } else + { + hb_xfree( pConn ); ul = 0; - - if ( ul ) - s_ulConnectionCurrent = ul; + } hb_itemPutNI( pItem, ul ); break; @@ -1044,7 +1047,12 @@ static HB_ERRCODE sqlbaseRddInfo( LPRDDNODE pRDD, HB_USHORT uiIndex, HB_ULONG ul hb_rddsqlSetError( 0, NULL, NULL, NULL, 0 ); if ( pConn && ! pConn->uiAreaCount && pConn->pSDD->Disconnect( pConn ) == HB_SUCCESS ) { - pConn->hConnection = 0; + hb_xfree( pConn ); + s_pConnection[ ulConn ] = NULL; + if( s_ulConnectionCurrent == ulConn ) + { + s_ulConnectionCurrent = 0; + } hb_itemPutL( pItem, HB_TRUE ); return HB_SUCCESS; } @@ -1056,9 +1064,7 @@ static HB_ERRCODE sqlbaseRddInfo( LPRDDNODE pRDD, HB_USHORT uiIndex, HB_ULONG ul { hb_rddsqlSetError( 0, NULL, NULL, NULL, 0 ); if ( pConn ) - { hb_itemPutL( pItem, pConn->pSDD->Execute( pConn, pItem ) == HB_SUCCESS ); - } else hb_itemPutL( pItem, HB_FALSE );