diff options
Diffstat (limited to 'src/core/xbdbf.cpp')
-rwxr-xr-x | src/core/xbdbf.cpp | 770 |
1 files changed, 381 insertions, 389 deletions
diff --git a/src/core/xbdbf.cpp b/src/core/xbdbf.cpp index 8e020fb..3d13369 100755 --- a/src/core/xbdbf.cpp +++ b/src/core/xbdbf.cpp @@ -61,7 +61,7 @@ void xbDbf::InitVars() ulAppendLocked = 0; SetAutoLock( -1 ); lloRecLocks.SetDupKeys( xbFalse ); - #endif + #endif // XB_LOCKING_SUPPORT #ifdef XB_INDEX_SUPPORT ixList = NULL; @@ -69,10 +69,15 @@ void xbDbf::InitVars() vpCurIxTag = NULL; sCurIxType = ""; ClearTagList(); - #endif - #ifdef XB_NDXINF_SUPPORT - llNdxInfData.Clear(); - #endif + #endif // XB_INDEX_SUPPORT + + #ifdef XB_MEMO_SUPPORT + Memo = NULL; + #endif // XB_MEMO_SUPPORT + + #ifdef XB_INF_SUPPORT + llInfData.Clear(); + #endif // XB_INF_SUPPORT } /************************************************************************/ @@ -116,7 +121,7 @@ xbInt16 xbDbf::Abort(){ #ifdef XB_MEMO_SUPPORT if( MemoFieldsExist()){ if(( iRc = Memo->Abort()) != XB_NO_ERROR ){ - iErrorStop = 10; + iErrorStop = 100; throw iRc; } } @@ -214,7 +219,7 @@ xbInt16 xbDbf::AppendRecord(){ // std::cout << "xbDbf::CreateKeys(x)\n"; if(( iRc = ixList->ix->CreateKeys( 1 )) != XB_NO_ERROR ){ - iErrorStop = 10; + iErrorStop = 100; throw iRc; } @@ -224,14 +229,12 @@ xbInt16 xbDbf::AppendRecord(){ // lock everything up for an update #ifdef XB_LOCKING_SUPPORT - // xbInt16 iAutoLock = GetAutoLock(); - if( iAutoLock ){ - + if( iAutoLock && !bTableLocked ){ if(( iRc = LockHeader( XB_LOCK )) != XB_NO_ERROR ){ if( iRc == XB_LOCK_FAILED ) { return iRc; } else { - iErrorStop = 20; + iErrorStop = 110; throw iRc; } } @@ -240,14 +243,14 @@ xbInt16 xbDbf::AppendRecord(){ LockHeader( XB_UNLOCK ); return iRc; } else { - iErrorStop = 30; + iErrorStop = 120; throw iRc; } } #ifdef XB_INDEX_SUPPORT if(( iRc = LockIndices( XB_LOCK )) != XB_NO_ERROR ){ - iErrorStop = 40; + iErrorStop = 130; throw iRc; } #endif // XB_INDEX_SUPPORT @@ -255,7 +258,7 @@ xbInt16 xbDbf::AppendRecord(){ } #endif // XB_LOCKING_SUPPORT if(( iRc = ReadHeader( 1, 1 )) != XB_NO_ERROR ){ - iErrorStop = 50; + iErrorStop = 140; throw iRc; } #ifdef XB_INDEX_SUPPORT @@ -264,7 +267,7 @@ xbInt16 xbDbf::AppendRecord(){ while( ixList ){ if(( iRc = ixList->ix->CheckForDupKeys()) != 0 ){ if( iRc < 0 ){ - iErrorStop = 60; + iErrorStop = 150; throw iRc; } return XB_KEY_NOT_UNIQUE; @@ -289,7 +292,7 @@ xbInt16 xbDbf::AppendRecord(){ ixList = GetIxList(); while( ixList ){ if(( iRc = ixList->ix->AddKeys( ulCurRec )) != XB_NO_ERROR ){ - iErrorStop = 50; + iErrorStop = 160; throw iRc; } ixList = ixList->next; @@ -298,7 +301,7 @@ xbInt16 xbDbf::AppendRecord(){ #ifdef XB_LOCKING_SUPPORT if( iAutoLock ){ if(( iRc = LockIndices( XB_UNLOCK )) != XB_NO_ERROR ){ - iErrorStop = 60; + iErrorStop = 170; throw iRc; } } @@ -307,14 +310,14 @@ xbInt16 xbDbf::AppendRecord(){ // rewrite the header record if(( iRc = WriteHeader( 1, 1 )) != XB_NO_ERROR ){ - iErrorStop = 60; + iErrorStop = 180; throw iRc; } #ifdef XB_LOCKING_SUPPORT if( iAutoLock ){ if(( iRc = LockHeader( XB_UNLOCK )) != XB_NO_ERROR ){ - iErrorStop = 70; + iErrorStop = 190; throw iRc; } } @@ -322,25 +325,25 @@ xbInt16 xbDbf::AppendRecord(){ // write the last record if(( iRc = xbFseek( (uiHeaderLen+((xbInt64)(ulNoOfRecs-1)*uiRecordLen)), 0 )) != XB_NO_ERROR ){ - iErrorStop = 80; + iErrorStop = 200; throw iRc; } if(( iRc = xbFwrite( RecBuf, uiRecordLen, 1 )) != XB_NO_ERROR ){ - iErrorStop = 90; + iErrorStop = 210; throw iRc; } // write the end of file marker if(( iRc = xbFputc( XB_CHAREOF )) != XB_NO_ERROR ){ - iErrorStop = 100; + iErrorStop = 220; throw iRc; } #ifdef XB_LOCKING_SUPPORT if( iAutoLock ){ if(( iRc = LockAppend( XB_UNLOCK )) != XB_NO_ERROR ){ - iErrorStop = 110; + iErrorStop = 230; throw( iRc ); } } @@ -376,7 +379,7 @@ xbInt16 xbDbf::AppendRecord(){ } /************************************************************************/ -#ifdef XB_NDXINF_SUPPORT +#ifdef XB_INF_SUPPORT //! @brief Asscoiate a non production index to a DBF file. /*! @@ -385,7 +388,7 @@ xbInt16 xbDbf::AppendRecord(){ indices are not. This method is specific to the Xbas64 library and providex a means to link non production NDX index files to the DBF file so they will be opened automatically when the DBF file is opened.<br> - This routine requires NDXINF support be enabled when building the library.<br> + This routine requires INF support be enabled when building the library.<br> This routine creates a file with the same name as the DBF file, but with an extension of INF.<br> @@ -412,13 +415,13 @@ xbInt16 xbDbf::AssociateIndex( const xbString &sIxType, const xbString &sIxName, if( sIxTypeIn != "NDX" || sIxName == "" ) return XB_INVALID_INDEX; - if(( iRc = LoadNdxInfData()) != XB_NO_ERROR ){ - iErrorStop = 10; + if(( iRc = LoadInfData()) != XB_NO_ERROR ){ + iErrorStop = 100; throw iRc; } // check if entry exists - xbLinkListNode<xbString> * llN = llNdxInfData.GetHeadNode(); + xbLinkListNode<xbString> * llN = llInfData.GetHeadNode(); xbBool bFound = xbFalse; xbString s; @@ -434,17 +437,17 @@ xbInt16 xbDbf::AssociateIndex( const xbString &sIxType, const xbString &sIxName, xbBool bUpdated = xbFalse; if( iOpt == 0 && !bFound ){ s.Sprintf( "%s%c%c", sIxName.Str(), 0x0d, 0x0a ); - llNdxInfData.InsertAtEnd( s ); + llInfData.InsertAtEnd( s ); bUpdated = xbTrue; } else if( iOpt == 1 && bFound ){ - llNdxInfData.RemoveByVal( s ); + llInfData.RemoveByVal( s ); bUpdated = xbTrue; } if( bUpdated ){ - if(( iRc = SaveNdxInfData()) != XB_NO_ERROR ){ - iErrorStop = 20; + if(( iRc = SaveInfData()) != XB_NO_ERROR ){ + iErrorStop = 110; throw iRc; } } @@ -457,7 +460,7 @@ xbInt16 xbDbf::AssociateIndex( const xbString &sIxType, const xbString &sIxName, } return iRc; } -#endif // XB_NDXINF_SUPPORT +#endif // XB_INF_SUPPORT /************************************************************************/ //! @brief Blank the record buffer. @@ -477,7 +480,7 @@ xbInt16 xbDbf::BlankRecord() try{ if( iDbfStatus == XB_CLOSED ){ - iErrorStop = 10; + iErrorStop = 100; iRc = XB_NOT_OPEN; throw iRc; } @@ -485,12 +488,12 @@ xbInt16 xbDbf::BlankRecord() if( iDbfStatus == XB_UPDATED ){ if( GetAutoCommit() == 1 ){ if(( iRc = Commit()) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 110; throw iRc; } } else { if(( iRc = Abort()) != XB_NO_ERROR ){ - iErrorStop = 30; + iErrorStop = 120; throw iRc; } } @@ -511,9 +514,6 @@ xbInt16 xbDbf::BlankRecord() } /************************************************************************/ #ifdef XB_INDEX_SUPPORT - -#ifdef XB_DEBUG_SUPPORT - /*! This method is used to check an index tag's intgerity. @@ -533,9 +533,6 @@ xbInt16 xbDbf::CheckTagIntegrity( xbInt16 iTagOpt, xbInt16 iOutputOpt ){ xbInt16 iErrorStop = 0; try{ - -// std::cout << "CheckTagIntegrity()\n"; - if( iTagOpt == 0 ){ if( pCurIx ) return pCurIx->CheckTagIntegrity( vpCurIxTag, iOutputOpt ); @@ -550,7 +547,7 @@ xbInt16 xbDbf::CheckTagIntegrity( xbInt16 iTagOpt, xbInt16 iOutputOpt ){ while( llN ){ pTag = llN->GetKey(); if(( iRc = pTag->GetIx()->CheckTagIntegrity( pTag->GetVpTag(), iOutputOpt )) != XB_NO_ERROR ){ - iErrorStop = 100; + iErrorStop = 110; throw iRc; } llN = llN->GetNextNode(); @@ -563,12 +560,9 @@ xbInt16 xbDbf::CheckTagIntegrity( xbInt16 iTagOpt, xbInt16 iOutputOpt ){ xbase->WriteLogMessage( sMsg.Str() ); xbase->WriteLogMessage( GetErrorMessage( iRc )); } - return iRc; } -#endif // XB_DEBUG_SUPPORT - /************************************************************************/ /*! This method is used to reindex / rebuild index tag. @@ -588,24 +582,14 @@ xbInt16 xbDbf::Reindex( xbInt16 iTagOpt ){ if( iTagOpt == 0 ){ if( pCurIx ){ - // - // xbString = save current tag name - // - iRc = pCurIx->Reindex( &vpCurIxTag ); if( iRc != XB_NO_ERROR ){ iErrorStop = 100; throw iRc; } - // - // restore current tag name - // return iRc; - } else { - return XB_INVALID_TAG; - } } else { @@ -617,13 +601,11 @@ xbInt16 xbDbf::Reindex( xbInt16 iTagOpt ){ pTag = llN->GetKey(); vp = pTag->GetVpTag(); if(( iRc = pTag->GetIx()->Reindex( &vp )) != XB_NO_ERROR ){ - iErrorStop = 100; + iErrorStop = 110; throw iRc; } llN = llN->GetNextNode(); } - - } } catch (xbInt16 iRc ){ @@ -632,7 +614,6 @@ xbInt16 xbDbf::Reindex( xbInt16 iTagOpt ){ xbase->WriteLogMessage( sMsg.Str() ); xbase->WriteLogMessage( GetErrorMessage( iRc )); } - return iRc; } @@ -680,12 +661,12 @@ xbInt16 xbDbf::Close(){ if( GetAutoCommit() == 1 ){ if(( iRc = Commit()) != XB_NO_ERROR ){ - iErrorStop = 10; + iErrorStop = 100; throw iRc; } } else { if(( iRc = Abort()) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 110; throw iRc; } } @@ -704,7 +685,6 @@ xbInt16 xbDbf::Close(){ RecBuf2 = NULL; } - #ifdef XB_MEMO_SUPPORT if( iMemoFieldCnt > 0 ){ Memo->CloseMemoFile(); @@ -721,24 +701,15 @@ xbInt16 xbDbf::Close(){ } #endif - #ifdef XB_LOCKING_ON - if( xblfh ){ - fclose( xblfh ); - xblfh = NULL; - } - #endif - if(( iRc = xbase->RemoveTblFromTblList( this )) != XB_NO_ERROR ){ xbString sMsg; sMsg.Sprintf( "Alias = [%s]", sAlias.Str()); xbase->WriteLogMessage( sMsg.Str() ); - iErrorStop = 30; + iErrorStop = 120; throw iRc; } - xbFclose(); InitVars(); - } catch (xbInt16 iRc ){ xbString sMsg; @@ -749,7 +720,6 @@ xbInt16 xbDbf::Close(){ return iRc; } - /************************************************************************/ #ifdef XB_INDEX_SUPPORT //! @brief Close an open index file @@ -778,35 +748,30 @@ xbInt16 xbDbf::CloseIndexFile( xbIx *pIx ){ p = p->next; } if( !bFound ){ - iErrorStop = 10; + iErrorStop = 100; iRc = XB_NOT_OPEN; throw iRc; } - // close it if(( iRc = pIx->Close()) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 110; throw iRc; } - // remove it from the list if(( iRc = RemoveIndex( pIx )) != XB_NO_ERROR ){ - iErrorStop = 30; + iErrorStop = 120; throw iRc; } - // refresh the tag list if(( iRc = UpdateTagList()) != XB_NO_ERROR ){ - iErrorStop = 40; + iErrorStop = 130; throw iRc; } - if( pIx == pCurIx ){ pCurIx = NULL; vpCurIxTag = NULL; sCurIxType = ""; } - } catch (xbInt16 iRc ){ xbString sMsg; @@ -836,12 +801,12 @@ xbInt16 xbDbf::Commit(){ if( iDbfStatus == XB_UPDATED ){ if( ulCurRec == 0 ){ if(( iRc = AppendRecord()) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 100; throw iRc; } } else { if(( iRc = PutRecord( ulCurRec )) != XB_NO_ERROR ){ - iErrorStop = 30; + iErrorStop = 110; throw iRc; } } @@ -858,7 +823,6 @@ xbInt16 xbDbf::Commit(){ return iRc; } - /************************************************************************/ //! @brief Copy table (dbf) file structure. /*! @@ -891,13 +855,13 @@ xbInt16 xbDbf::CopyDbfStructure( xbDbf * dNewTable, const xbString &sNewTableNam try{ if( iDbfStatus == XB_CLOSED ){ - iErrorStop = 10; + iErrorStop = 100; iRc = XB_DBF_FILE_NOT_OPEN; throw iRc; } if( !dNewTable ){ - iErrorStop = 20; + iErrorStop = 110; iRc = XB_INVALID_OBJECT; throw iRc; } @@ -907,7 +871,7 @@ xbInt16 xbDbf::CopyDbfStructure( xbDbf * dNewTable, const xbString &sNewTableNam // Allocate a Schema = No Of Fields + 1 if((newTableSchema=(xbSchema *)malloc( (size_t) lSchemaRecCnt * sizeof(xbSchema)))==NULL){ - iErrorStop = 30; + iErrorStop = 120; iRc = XB_NO_MEMORY; throw iRc; } @@ -937,7 +901,7 @@ xbInt16 xbDbf::CopyDbfStructure( xbDbf * dNewTable, const xbString &sNewTableNam // Call the create a table function if(( iRc = dNewTable->CreateTable( sNewTableName, sNewTableAlias, newTableSchema, iOverlay, iShareMode )) != XB_NO_ERROR ){ - iErrorStop = 40; + iErrorStop = 130; throw iRc; } } @@ -955,7 +919,6 @@ xbInt16 xbDbf::CopyDbfStructure( xbDbf * dNewTable, const xbString &sNewTableNam return iRc; } - /************************************************************************/ #ifdef XB_INDEX_SUPPORT //! @brief Create a new tag (index) for this dbf file (table). @@ -984,16 +947,19 @@ xbInt16 xbDbf::CreateTag( const xbString &sIxType, const xbString &sName, const xbInt16 iErrorStop = 0; xbInt16 iRc = XB_NO_ERROR; + + #ifdef XB_LOCKING_SUPPORT xbBool bLocked = xbFalse; + #endif try{ xbString sType = sIxType; sType.ToUpperCase(); #ifdef XB_LOCKING_SUPPORT - if( !bTableLocked ){ + if( iAutoLock && !bTableLocked ){ if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){ - iErrorStop = 10; + iErrorStop = 100; throw( iRc ); } bLocked = xbTrue; @@ -1001,7 +967,7 @@ xbInt16 xbDbf::CreateTag( const xbString &sIxType, const xbString &sName, const #endif // XB_LOCKING_SUPPORT if( sIxType == "" ){ - iErrorStop = 100; + iErrorStop = 110; iRc = XB_INVALID_OPTION; throw iRc; @@ -1010,12 +976,12 @@ xbInt16 xbDbf::CreateTag( const xbString &sIxType, const xbString &sName, const xbIxNdx *ixNdx = new xbIxNdx( this ); if(( iRc = ixNdx->CreateTag( sName, sKey, sFilter, iDescending, iUnique, iOverLay, vpTagOut )) != XB_NO_ERROR ){ - iErrorStop = 110; + iErrorStop = 120; throw iRc; } if(( iRc = AddIndex( ixNdx, sIxType )) != XB_NO_ERROR ){ - iErrorStop = 120; + iErrorStop = 130; throw iRc; } *pIxOut = ixNdx; @@ -1033,7 +999,7 @@ xbInt16 xbDbf::CreateTag( const xbString &sIxType, const xbString &sName, const } else if( sIxType == "MDX" ){ if( GetVersion() == 3 ){ // MDX indexes were version 4 and higher - iErrorStop = 200; + iErrorStop = 140; iRc = XB_INVALID_INDEX; throw iRc; } @@ -1055,19 +1021,19 @@ xbInt16 xbDbf::CreateTag( const xbString &sIxType, const xbString &sName, const ixMdx = new xbIxMdx( this ); if(( iRc = ixMdx->CreateTag( sName, sKey, sFilter, iDescending, iUnique, iOverLay, vpTagOut )) != XB_NO_ERROR ){ - iErrorStop = 210; + iErrorStop = 150; throw iRc; } if( !bMdxFound ){ if(( iRc = AddIndex( ixMdx, "MDX" )) != XB_NO_ERROR ){ - iErrorStop = 220; + iErrorStop = 160; throw iRc; } cIndexFlag = 0x01; if(( iRc = WriteHeader( 1, 0 )) != XB_NO_ERROR ){ - iErrorStop = 230; + iErrorStop = 170; throw iRc; } } @@ -1106,7 +1072,6 @@ xbInt16 xbDbf::CreateTag( const xbString &sIxType, const xbString &sName, const LockTable( XB_UNLOCK ); #endif // XB_LOCKING_SUPPORT - return iRc; } #endif // XB_INDEX_SUPPORT @@ -1131,27 +1096,35 @@ xbInt16 xbDbf::DeleteAll( xbInt16 iOption ) try{ if(( iRc = GetRecordCnt( ulRecCnt )) != XB_NO_ERROR ){ - iErrorStop = 10; + iErrorStop = 100; throw iRc; } if( ulRecCnt == 0 ) return XB_NO_ERROR; for( xbUInt32 ul = 0; ul < ulRecCnt; ul++ ){ if(( iRc = GetRecord( ul+1 )) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 110; throw iRc; } if( iOption == 0 ){ /* delete all option */ if( !RecordDeleted()){ if(( iRc = DeleteRecord()) != XB_NO_ERROR ){ - iErrorStop = 30; + iErrorStop = 120; + throw iRc; + } + if(( iRc = Commit()) != XB_NO_ERROR ){ + iErrorStop = 130; throw iRc; } } } else { /* undelete all option */ if( RecordDeleted()){ if(( iRc = UndeleteRecord()) != XB_NO_ERROR ){ - iErrorStop = 40; + iErrorStop = 140; + throw iRc; + } + if(( iRc = Commit()) != XB_NO_ERROR ){ + iErrorStop = 150; throw iRc; } } @@ -1181,17 +1154,20 @@ xbInt16 xbDbf::DeleteAllIndexFiles(){ xbInt16 iRc = 0; xbInt16 iErrorStop = 0; + + #ifdef XB_LOCKING_SUPPORT xbBool bLocked = xbFalse; + #endif // XB_LOCKING_SUPPORT - #ifdef XB_NDXINF_SUPPORT + #ifdef XB_INF_SUPPORT xbString sIxName; - #endif // XB_NDXINF_SUPPORT + #endif // XB_INF_SUPPORT try{ #ifdef XB_LOCKING_SUPPORT - if( !bTableLocked ){ + if( iAutoLock && !bTableLocked ){ if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){ - iErrorStop = 10; + iErrorStop = 100; throw( iRc ); } bLocked = xbTrue; @@ -1202,15 +1178,15 @@ xbInt16 xbDbf::DeleteAllIndexFiles(){ while( ixList ){ ixList->ix->Close(); ixList->ix->xbRemove(); - #ifdef XB_NDXINF_SUPPORT - // if XB_NDXINF_SUPPORT is enabled, all open non prod indices should be in here + #ifdef XB_INF_SUPPORT + // if XB_INF_SUPPORT is enabled, all open non prod indices should be in here if( *ixList->sFmt != "MDX" ){ // production indices not stored in .INF dataset if(( iRc = ixList->ix->GetFileNamePart( sIxName )) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 110; throw iRc; } if(( iRc = AssociateIndex( *ixList->sFmt, sIxName, 1 )) != XB_NO_ERROR ){ - iErrorStop = 30; + iErrorStop = 120; throw iRc; } } @@ -1244,7 +1220,7 @@ xbInt16 xbDbf::DeleteAllRecords(){ } /************************************************************************/ -#ifdef XB_NDXINF_SUPPORT +#ifdef XB_INF_SUPPORT //! @brief Delete .INF File /*! The original Dbase (TM) software supported non production indices (NDX) and production indices (MDX). @@ -1252,45 +1228,44 @@ xbInt16 xbDbf::DeleteAllRecords(){ indices are not. This method is specific to the Xbas64 library and providex a means to link non production NDX index files to the DBF file so they will be opened automatically when the DBF file is opened.<br> - This routine requires NDXINF support be enabled when building the library.<br> + This routine requires INF support be enabled when building the library.<br> This routine deletes the .INF file.<br> \returns <a href="xbretcod_8h.html">Return Codes</a> */ -xbInt16 xbDbf::DeleteNdxInfData(){ +xbInt16 xbDbf::DeleteInfData(){ xbInt16 iRc = 0; xbInt16 iErrorStop = 0; try{ - xbString sNdxInfFileName; - if(( iRc = GetNdxInfFileName( sNdxInfFileName )) != XB_NO_ERROR ){ - iErrorStop = 10; + xbString sInfFileName; + if(( iRc = GetInfFileName( sInfFileName )) != XB_NO_ERROR ){ + iErrorStop = 100; throw iRc; } xbFile f( xbase ); - f.SetFileName( sNdxInfFileName ); + f.SetFileName( sInfFileName ); if( f.FileExists()){ if(( iRc = f.xbRemove()) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 110; throw iRc; } } } catch( xbInt16 iRc ){ xbString sMsg; - sMsg.Sprintf( "xbdbf::DeleteNdxInfData() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc ); + sMsg.Sprintf( "xbdbf::DeleteInfData() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc ); xbase->WriteLogMessage( sMsg.Str() ); xbase->WriteLogMessage( GetErrorMessage( iRc )); } return iRc; } -#endif // XB_NDXINF_SUPPORT +#endif // XB_INF_SUPPORT /************************************************************************/ //! @brief Delete the current record. /*! This routine flags the current record for deletion if it's not already flagged. - - + \returns XB_NO_ERROR<br> XB_INVALID_RECORD */ @@ -1322,27 +1297,35 @@ xbInt16 xbDbf::DeleteTable(){ xbInt16 iRc = 0; xbInt16 iErrorStop = 0; + #ifdef XB_LOCKING_SUPPORT + xbBool bTableLocked = xbFalse; + #endif + try{ #ifdef XB_LOCKING_SUPPORT - if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){ - iErrorStop = 10; - throw iRc; + if( iAutoLock && !bTableLocked ){ + if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){ + iErrorStop = 100; + throw iRc; + } else { + bTableLocked = xbTrue; + } } #endif // XB_LOCKING_SUPPORT #ifdef XB_INDEX_SUPPORT if(( iRc = DeleteAllIndexFiles()) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 110; throw iRc; } - #ifdef XB_NDXINF_SUPPORT - if(( iRc = DeleteNdxInfData()) != XB_NO_ERROR ){ - iErrorStop = 30; + #ifdef XB_INF_SUPPORT + if(( iRc = DeleteInfData()) != XB_NO_ERROR ){ + iErrorStop = 120; throw iRc; } - #endif // XB_NDXINF_SUPPORT + #endif // XB_INF_SUPPORT #endif // XB_INDEX_SUPPORT #ifdef XB_MEMO_SUPPORT @@ -1354,12 +1337,12 @@ xbInt16 xbDbf::DeleteTable(){ #endif // XB_MEMO_SUPPORT if(( iRc = Close()) != XB_NO_ERROR ){ - iErrorStop = 40; + iErrorStop = 130; throw iRc; } if(( iRc = xbRemove()) != XB_NO_ERROR ){ - iErrorStop = 50; + iErrorStop = 140; throw iRc; } @@ -1367,7 +1350,7 @@ xbInt16 xbDbf::DeleteTable(){ if( iMemoFieldCnt > 0 ){ xbFile f( xbase ); if(( iRc = f.xbRemove( sMemoFileName )) != XB_NO_ERROR ){ - iErrorStop = 60; + iErrorStop = 150; throw iRc; } } @@ -1380,12 +1363,14 @@ xbInt16 xbDbf::DeleteTable(){ xbase->WriteLogMessage( GetErrorMessage( iRc )); } #ifdef XB_LOCKING_SUPPORT - LockTable( XB_UNLOCK ); + if( bTableLocked ) + LockTable( XB_UNLOCK ); #endif // XB_LOCKING_SUPPORT return iRc; } /************************************************************************/ +#ifdef XB_INDEX_SUPPORT //! @brief Delete an index tag. /*! @@ -1401,17 +1386,22 @@ xbInt16 xbDbf::DeleteTag( const xbString &sIxType, const xbString &sName ){ xbInt16 iRc = XB_NO_ERROR; xbIx *pIx = NULL; + #ifdef XB_LOCKING_SUPPORT + xbBool bTableLocked = xbFalse; + #endif + try{ #ifdef XB_LOCKING_SUPPORT - // xbInt16 iAutoLock = GetAutoLock(); if( iAutoLock && !GetTableLocked() ){ if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){ iErrorStop = 100; throw iRc; + } else { + bTableLocked = xbTrue; } } - #endif + #endif // XB_LOCKING_SUPPORT if( sIxType == "" ){ iErrorStop = 110; @@ -1429,12 +1419,12 @@ xbInt16 xbDbf::DeleteTag( const xbString &sIxType, const xbString &sName ){ bDone = xbTrue; // remove from .INF if it's there - #ifdef XB_NDXINF_SUPPORT + #ifdef XB_INF_SUPPORT if(( iRc = AssociateIndex( "NDX", sName, 1 )) != XB_NO_ERROR ){ iErrorStop = 120; throw iRc; } - #endif // XB_NDXINF_SUPPORT + #endif // XB_INF_SUPPORT if(( iRc = ixl->ix->DeleteTag( ixl->ix->GetTag( 0 ))) != XB_NO_ERROR ){ iErrorStop = 130; @@ -1518,15 +1508,6 @@ xbInt16 xbDbf::DeleteTag( const xbString &sIxType, const xbString &sName ){ throw iRc; } - // unlock as necessary - #ifdef XB_LOCKING_SUPPORT - if( iAutoLock && GetTableLocked() ){ - if(( iRc = LockTable( XB_UNLOCK )) != XB_NO_ERROR ){ - iErrorStop = 200; - throw iRc; - } - } - #endif } catch (xbInt16 iRc ){ @@ -1540,14 +1521,20 @@ xbInt16 xbDbf::DeleteTag( const xbString &sIxType, const xbString &sName ){ if( iAutoLock && GetTableLocked() ){ LockTable( XB_UNLOCK ); } - #endif + #endif // XB_LOCKING_SUPPORT + } + #ifdef XB_LOCKING_SUPPORT + if( bTableLocked ){ + LockTable( XB_UNLOCK ); } + #endif // XB_LOCKING_SUPPORT + + return iRc; } - +#endif // XB_INDEX_SUPPORT /************************************************************************/ -#ifdef XB_DEBUG_SUPPORT //! @brief Dump dbf file header. /*! This routine dumps dbf header information to the console. @@ -1625,8 +1612,9 @@ xbInt16 xbDbf::DumpHeader( xbInt16 iOption ) const { std::cout << "Lang Driver = " << (int) cIndexFlag << " - "; xbase->BitDump( cIndexFlag ); std::cout << std::endl; - + #ifdef XB_INDEX_SUPPORT std::cout << "Open Index Files = " << GetPhysicalIxCnt() << std::endl; + #endif // XB_INDEX_SUPPORT } if( iOption != 1 ){ @@ -1657,7 +1645,6 @@ xbInt16 xbDbf::DumpHeader( xbInt16 iOption ) const { return XB_NO_ERROR; } - /************************************************************************/ //! Dump record /*! @@ -1690,7 +1677,7 @@ xbInt16 xbDbf::DumpRecord( xbUInt32 ulRecNo, xbInt16 iOutputDest, xbInt16 iOutpu if( iOutputFmt >= 1 ){ if( iOutputFmt == 2 ){ - sTemp = "RecNo"; + sTemp = "RecNo,DEL"; for( i = 0; i < iNoOfFields; i++ ){ s2 = SchemaPtr[i].cFieldName; s2.Trim(); @@ -1701,19 +1688,16 @@ xbInt16 xbDbf::DumpRecord( xbUInt32 ulRecNo, xbInt16 iOutputDest, xbInt16 iOutpu } if( RecordDeleted() ) - s2.Sprintf( "%ld Deleted", ulRecNo ); + s2.Sprintf( "%ld,DEL", ulRecNo ); + else + s2.Sprintf( "%ld,", ulRecNo ); - else{ - s2.Sprintf( "%ld ", ulRecNo ); - //xbString s3; - for( i = 0; i < iNoOfFields; i++ ){ - GetField( i, sTemp ); - sTemp.Trim(); - s2.Sprintf( "%s,'%s'", s2.Str(), sTemp.Str()); - } - xbase->WriteLogMessage( s2.Str(),iOutputDest ); + for( i = 0; i < iNoOfFields; i++ ){ + GetField( i, sTemp ); + sTemp.Trim(); + s2.Sprintf( "%s,'%s'", s2.Str(), sTemp.Str()); } - + xbase->WriteLogMessage( s2.Str(),iOutputDest ); return XB_NO_ERROR; } @@ -1768,6 +1752,9 @@ xbInt16 xbDbf::DumpRecord( xbUInt32 ulRecNo, xbInt16 iOutputDest, xbInt16 iOutpu /************************************************************************/ +#ifdef XB_DEBUG_SUPPORT + + #ifdef XB_LOCKING_SUPPORT //! @brief Dump the table lock status /*! @@ -1884,11 +1871,11 @@ xbInt16 xbDbf::GetAutoCommit( xbInt16 iOption ) const { /************************************************************************/ +#ifdef XB_LOCKING_SUPPORT //! @brief Get Auto Lock setting. /*! \returns Auto lock setting. */ -#ifdef XB_LOCKING_SUPPORT xbInt16 xbDbf::GetAutoLock() const{ return iAutoLock; } @@ -1903,7 +1890,7 @@ xbInt16 xbDbf::GetAutoLock() const{ xbUInt32 xbDbf::GetCreateMemoBlockSize() const { return ulCreateMemoBlockSize; } -#endif +#endif // XB_MEMO_SUPPORT /************************************************************************/ //! @brief Get a pointer to the current index object. @@ -2099,7 +2086,6 @@ xbInt32 xbDbf::GetFieldCnt() const { //! @brief Get the first first record lock. /*! Get the first record lock from a linked list of record locks. - \returns First record lock. */ xbLinkListNode<xbUInt32> * xbDbf::GetFirstRecLock() const { @@ -2152,7 +2138,7 @@ xbInt16 xbDbf::GetFirstRecord( xbInt16 iOption ) xbBool xbDbf::GetHeaderLocked() const { return this->bHeaderLocked; } -#endif +#endif // XB_LOCKING_SUPPORT #ifdef XB_INDEX_SUPPORT //! @brief Return pointer to list of index files for the table. @@ -2204,6 +2190,7 @@ xbInt16 xbDbf::GetLastRecord( xbInt16 iOption ) /************************************************************************/ +#ifdef XB_LOCKING_SUPPORT //! @brief Get lock flavor. /*! This routine is currently in place to provide structure for future locking @@ -2217,10 +2204,12 @@ xbInt16 xbDbf::GetLockFlavor() const{ else return iLockFlavor; } +#endif // XB_LOCKING_SUPPORT /************************************************************************/ #ifdef XB_MEMO_SUPPORT /************************************************************************/ +#ifdef XB_LOCKING_SUPPORT //! @brief Get the lock status of the memo file. /*! \returns Lock status of memo file. @@ -2231,6 +2220,8 @@ xbBool xbDbf::GetMemoLocked() const { else return xbFalse; } +#endif // XB_LOCKING_SUPPORT + /************************************************************************/ //! @brief Get pointer to Memo object. /*! @@ -2240,30 +2231,31 @@ xbBool xbDbf::GetMemoLocked() const { xbMemo * xbDbf::GetMemoPtr(){ return Memo; } -#endif + +#endif // XB_MEMO_SUPPORT /************************************************************************/ -#ifdef XB_NDXINF_SUPPORT +#ifdef XB_INF_SUPPORT //! @brief Return the .INF file name /*! If NDXIDX support is enabled in the library, and a non production (ndx) has been associated with the dbf file, the .INF file name can be retrieved with this routine. - \param sNdxInfFileName Output string containing .INF file name. + \param sInfFileName Output string containing .INF file name. \returns <a href="xbretcod_8h.html">Return Codes</a> */ -xbInt16 xbDbf::GetNdxInfFileName( xbString &sNdxInfFileName ){ +xbInt16 xbDbf::GetInfFileName( xbString &sInfFileName ){ - sNdxInfFileName = GetFqFileName(); - xbUInt32 lLen = sNdxInfFileName.Len(); + sInfFileName = GetFqFileName(); + xbUInt32 lLen = sInfFileName.Len(); if( lLen < 5 ) return XB_FILE_NOT_FOUND; - sNdxInfFileName.PutAt(lLen-2, 'I'); - sNdxInfFileName.PutAt(lLen-1, 'N'); - sNdxInfFileName.PutAt(lLen, 'F'); + sInfFileName.PutAt(lLen-2, 'I'); + sInfFileName.PutAt(lLen-1, 'N'); + sInfFileName.PutAt(lLen, 'F'); return XB_NO_ERROR; } @@ -2273,10 +2265,10 @@ xbInt16 xbDbf::GetNdxInfFileName( xbString &sNdxInfFileName ){ \returns List of .INF entries. */ -xbLinkListNode<xbString> * xbDbf::GetNdxInfList() const{ - return llNdxInfData.GetHeadNode(); +xbLinkListNode<xbString> * xbDbf::GetInfList() const{ + return llInfData.GetHeadNode(); } -#endif // XB_NDXINF_SUPPORT +#endif // XB_INF_SUPPORT /************************************************************************/ @@ -2362,7 +2354,7 @@ xbInt32 xbDbf::GetPhysicalIxCnt() const { #endif return lCnt; } -#endif +#endif // XB_INDEX_SUPPORT /************************************************************************/ @@ -2421,38 +2413,38 @@ xbInt16 xbDbf::GetRecord( xbUInt32 ulRecNo ){ try{ /* verify the file is open */ if( iDbfStatus == XB_CLOSED ){ - iErrorStop = 10; + iErrorStop = 100; iRc = XB_NOT_OPEN; throw iRc; } if( iDbfStatus == XB_UPDATED ){ if( GetAutoCommit() == 1 ){ if(( iRc = Commit()) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 110; throw iRc; } } else { if(( iRc = Abort()) != XB_NO_ERROR ){ - iErrorStop = 30; + iErrorStop = 120; throw iRc; } } } if( ulRecNo > ulNoOfRecs || ulRecNo == 0L ){ - iErrorStop = 40; + iErrorStop = 130; iRc = XB_INVALID_RECORD; throw iRc; } if(( xbFseek( (uiHeaderLen+(( (xbInt64) ulRecNo-1L ) * uiRecordLen )), SEEK_SET )) != XB_NO_ERROR ){ - iErrorStop = 50; + iErrorStop = 140; iRc = XB_SEEK_ERROR; throw iRc; } if( xbFread( RecBuf, uiRecordLen, 1 ) != XB_NO_ERROR ){ - iErrorStop = 60; + iErrorStop = 150; iRc = XB_READ_ERROR; throw iRc; } @@ -2483,6 +2475,24 @@ char * xbDbf::GetRecordBuf( xbInt16 iOpt ) const { return RecBuf; } + + + +/************************************************************************/ +//! @brief Get the current number of records in the dbf data file. +/*! + \returns Record count or <a href="xbretcod_8h.html">Return Codes</a> +*/ +xbUInt32 xbDbf::GetRecordCount(){ + + xbUInt32 ulCnt; + xbInt16 iRc = GetRecordCnt( ulCnt ); + if( iRc < 0 ) + return (xbUInt32) iRc; + else + return ulCnt; +} + /************************************************************************/ //! @brief Get the current number of records in the dbf data file. /*! @@ -2493,30 +2503,26 @@ xbInt16 xbDbf::GetRecordCnt( xbUInt32 & ulRecCnt ) { xbInt16 iRc = XB_NO_ERROR; xbInt16 iErrorStop = 0; - xbBool bIlocked = xbFalse; + + #ifdef XB_LOCKING_SUPPORT + xbBool bLocked = xbFalse; + #endif // XB_LOCKING_SUPPORT + try{ #ifdef XB_LOCKING_SUPPORT if( iAutoLock && !bTableLocked ){ if(( iRc = LockHeader( XB_LOCK )) != XB_NO_ERROR ){ - iErrorStop = 10; + iErrorStop = 100; throw iRc; } else - bIlocked = xbTrue; + bLocked = xbTrue; } + #endif // XB_LOCKING_SUPPORT if((iRc = ReadHeader(1,1)) != XB_NO_ERROR){ - LockHeader( XB_UNLOCK ); - iErrorStop = 20; + iErrorStop = 110; throw iRc; } - - if( iAutoLock && bIlocked ){ - if(( iRc = LockHeader( XB_UNLOCK )) != XB_NO_ERROR ){ - iErrorStop = 30; - throw iRc; - } - } - #endif } catch( xbInt16 iRc ){ if( iRc != XB_LOCK_FAILED ){ @@ -2526,6 +2532,13 @@ xbInt16 xbDbf::GetRecordCnt( xbUInt32 & ulRecCnt ) xbase->WriteLogMessage( GetErrorMessage( iRc )); } } + + #ifdef XB_LOCKING_SUPPORT + if( bLocked ){ + LockHeader( XB_UNLOCK ); + } + #endif + ulRecCnt = ulNoOfRecs; return iRc; } @@ -2538,15 +2551,17 @@ xbUInt16 xbDbf::GetRecordLen() const { return uiRecordLen; } /************************************************************************/ +#ifdef XB_LOCKING_SUPPORT //! @brief Get table locked status /*! \returns Table lock status. */ - xbBool xbDbf::GetTableLocked() const { return this->bTableLocked; } +#endif // XB_LOCKING_SUPPORT /************************************************************************/ +#ifdef XB_INDEX_SUPPORT //! @brief Get tag list for dbf file. /*! This routine returns a list of tags for the file.<br> @@ -2559,6 +2574,7 @@ xbBool xbDbf::GetTableLocked() const { xbLinkListNode<xbTag *> *xbDbf::GetTagList() const { return llTags.GetHeadNode(); } +#endif // XB_INDEX_SUPPORT /************************************************************************/ //! @brief Get the table alias. /*! @@ -2578,37 +2594,37 @@ xbXBase * xbDbf::GetXbasePtr() const { return xbase; } /************************************************************************/ -#ifdef XB_NDXINF_SUPPORT +#ifdef XB_INF_SUPPORT //! @brief Load .INF data file, /*! Protected method. This routine loads the ndx inf file. \returns <a href="xbretcod_8h.html">Return Codes</a> */ -xbInt16 xbDbf::LoadNdxInfData(){ +xbInt16 xbDbf::LoadInfData(){ xbInt16 iRc = 0; xbInt16 iErrorStop = 0; try{ // create file name - xbString sNdxInfFileName; - if(( iRc = GetNdxInfFileName( sNdxInfFileName )) != XB_NO_ERROR ){ - iErrorStop = 10; + xbString sInfFileName; + if(( iRc = GetInfFileName( sInfFileName )) != XB_NO_ERROR ){ + iErrorStop = 100; throw iRc; } // if file does not exist, return no error xbFile fMd( xbase ); - if( !fMd.FileExists( sNdxInfFileName )) + if( !fMd.FileExists( sInfFileName )) return XB_NO_ERROR; // open file file in read only mode - if(( iRc = fMd.xbFopen( "r", sNdxInfFileName, GetShareMode())) != XB_NO_ERROR ){ - iErrorStop = 20; + if(( iRc = fMd.xbFopen( "r", sInfFileName, GetShareMode())) != XB_NO_ERROR ){ + iErrorStop = 110; throw iRc; } // clear the linked list - llNdxInfData.Clear(); + llInfData.Clear(); // for each entry in the file, add a linked list item xbString sRec; @@ -2625,25 +2641,25 @@ xbInt16 xbDbf::LoadNdxInfData(){ sFn.ExtractElement(sRec.Str(), '=', 2 ); sFn.ZapChar( 0x0d ); sFn.ZapChar( 0x0a ); - llNdxInfData.InsertAtEnd( sFn ); + llInfData.InsertAtEnd( sFn ); } } } // close the file if(( iRc = fMd.xbFclose()) != XB_NO_ERROR ){ - iErrorStop = 40; + iErrorStop = 120; throw iRc; } } catch( xbInt16 iRc ){ xbString sMsg; - sMsg.Sprintf( "xbdbf::LoadNdxInfData() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc ); + sMsg.Sprintf( "xbdbf::LoadInfData() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc ); xbase->WriteLogMessage( sMsg.Str() ); xbase->WriteLogMessage( GetErrorMessage( iRc )); } return iRc; } -#endif // XB_NDXINF_SUPPORT +#endif // XB_INF_SUPPORT /************************************************************************/ #ifdef XB_LOCKING_SUPPORT @@ -2673,7 +2689,7 @@ xbInt16 xbDbf::LockAppend( xbInt16 iLockFunction ) if( iRc == XB_LOCK_FAILED ) return iRc; else{ - iErrorStop = 120; + iErrorStop = 110; throw iRc; } } @@ -2685,7 +2701,7 @@ xbInt16 xbDbf::LockAppend( xbInt16 iLockFunction ) if( iRc == XB_LOCK_FAILED ){ return iRc; } else { - iErrorStop = 130; + iErrorStop = 120; throw iRc; } } @@ -2695,13 +2711,12 @@ xbInt16 xbDbf::LockAppend( xbInt16 iLockFunction ) // } else { - other lock-table flavor options go here Clipper, Fox, etc - } } else { - iErrorStop = 190; + iErrorStop = 130; iRc = XB_INVALID_OPTION; throw iRc; } } else if( iLockFunction == XB_UNLOCK ){ - iErrorStop = 200; if( ulAppendLocked == 0 ) /* verify we have an active append lock */ @@ -2747,7 +2762,6 @@ xbInt16 xbDbf::LockAppend( xbInt16 iLockFunction ) return iRc; } - /************************************************************************/ //! @brief Lock Header /*! @@ -2781,7 +2795,7 @@ xbInt16 xbDbf::LockHeader( xbInt16 iLockFunction ) } } else { - iErrorStop = 190; + iErrorStop = 130; iRc = XB_INVALID_OPTION; throw iRc; } @@ -2802,7 +2816,7 @@ xbInt16 xbDbf::LockHeader( xbInt16 iLockFunction ) throw iRc; } } else { - iErrorStop = 290; + iErrorStop = 230; iRc = XB_INVALID_OPTION; throw iRc; } @@ -2825,7 +2839,7 @@ xbInt16 xbDbf::LockHeader( xbInt16 iLockFunction ) } /************************************************************************/ #ifdef XB_INDEX_SUPPORT -//! @brief Lock Index files. +//! @brief Lock Index files. /*! This routine locks all the index files. \param iLockFunction XB_LOCK<br>XB_UNLOCK @@ -2858,13 +2872,25 @@ xbInt16 xbDbf::LockIndices( xbInt16 iLockFunction ) } #endif + #ifdef XB_MDX_SUPPORT + if( *ixLI->sFmt == "MDX" ){ + if( !ixLI->ix->GetLocked()){ + if(( iRc = ixLI->ix->xbLock( XB_LOCK, LK4026531838, 1 )) != XB_NO_ERROR ){ + ixLI->ix->xbLock( XB_UNLOCK, LK4026531838, 1 ); + iErrorStop = 100; + throw iRc; + } + ixLI->ix->SetLocked( xbTrue ); + } + } + #endif + } else if( iLockFunction == XB_UNLOCK ){ #ifdef XB_NDX_SUPPORT if( *ixLI->sFmt == "NDX" ){ if( ixLI->ix->GetLocked()){ if(( iRc = ixLI->ix->xbLock( XB_UNLOCK, LK4026531838, 1 )) != XB_NO_ERROR ){ - ixLI->ix->xbLock( XB_UNLOCK, LK4026531838, 1 ); iErrorStop = 100; throw iRc; } @@ -2872,6 +2898,19 @@ xbInt16 xbDbf::LockIndices( xbInt16 iLockFunction ) } } #endif + + #ifdef XB_MDX_SUPPORT + if( *ixLI->sFmt == "MDX" ){ + if( ixLI->ix->GetLocked()){ + if(( iRc = ixLI->ix->xbLock( XB_UNLOCK, LK4026531838, 1 )) != XB_NO_ERROR ){ + iErrorStop = 100; + throw iRc; + } + ixLI->ix->SetLocked( xbFalse ); + } + } + #endif + } ixLI = ixLI->next; } @@ -2937,14 +2976,14 @@ xbInt16 xbDbf::LockRecord( xbInt16 iLockFunction, xbUInt32 ulRecNo ) if( iRc == XB_LOCK_FAILED ){ return iRc; } else { - iErrorStop = 130; + iErrorStop = 110; throw iRc; } } // other lock-table flavor options go here Clipper, Fox, etc } else { - iErrorStop = 190; + iErrorStop = 120; iRc = XB_INVALID_OPTION; throw iRc; } @@ -2953,7 +2992,7 @@ xbInt16 xbDbf::LockRecord( xbInt16 iLockFunction, xbUInt32 ulRecNo ) iRc = lloRecLocks.InsertKey( ulRecNo ); if( iRc != XB_NO_ERROR ){ xbLock( XB_UNLOCK, LK4026531838 - ulRecNo, 1 ); - iErrorStop = 140; + iErrorStop = 130; throw iRc; } @@ -2967,11 +3006,11 @@ xbInt16 xbDbf::LockRecord( xbInt16 iLockFunction, xbUInt32 ulRecNo ) if( GetLockFlavor() == LK_DBASE ){ iRc = xbLock( XB_UNLOCK, LK4026531838 - ulRecNo, 1 ); if( iRc != XB_NO_ERROR ){ - iErrorStop = 220; + iErrorStop = 210; throw iRc; } } else { - iErrorStop = 290; + iErrorStop = 220; iRc = XB_INVALID_OPTION; throw iRc; } @@ -2998,11 +3037,9 @@ xbInt16 xbDbf::LockRecord( xbInt16 iLockFunction, xbUInt32 ulRecNo ) xbase->WriteLogMessage( GetErrorMessage( iRc )); } } - return iRc; } - /************************************************************************/ //! @brief Lock table. /*! @@ -3113,6 +3150,7 @@ xbInt16 xbDbf::LockTable( xbInt16 iLockFunction ) return iRc; } +#endif // XB_LOCKING_SUPPORT /************************************************************************/ @@ -3121,6 +3159,8 @@ xbInt16 xbDbf::LockTable( xbInt16 iLockFunction ) \returns xbTrue - Memo fields exist.<br>xbFalse - Memo fields don't exist. */ xbBool xbDbf::MemoFieldsExist() const { + + #ifdef XB_MEMO_SUPPORT if( iMemoFieldCnt > 0 ) return xbTrue; @@ -3153,34 +3193,34 @@ xbInt16 xbDbf::Open( const xbString & sTableName, const xbString & sAlias ){ xbInt16 iRc = 0; xbInt16 iErrorStop = 0; - try{ - if(( iRc = Open( sTableName, sAlias, XB_READ_WRITE, XB_MULTI_USER )) != XB_NO_ERROR ){ - iErrorStop = 10; + iErrorStop = 100; throw iRc; } // do any .INF data things on the file, like open indices - #ifdef XB_NDXINF_SUPPORT - if(( iRc = LoadNdxInfData()) != XB_NO_ERROR ){ - iErrorStop = 20; + #ifdef XB_INF_SUPPORT + if(( iRc = LoadInfData()) != XB_NO_ERROR ){ + iErrorStop = 110; throw iRc; } - xbUInt32 llNodeCnt = llNdxInfData.GetNodeCnt(); + xbUInt32 llNodeCnt = llInfData.GetNodeCnt(); if( llNodeCnt > 0 ){ xbString s2; - xbLinkListNode<xbString> * llN = llNdxInfData.GetHeadNode(); + xbLinkListNode<xbString> * llN = llInfData.GetHeadNode(); for( xbUInt32 i = 0; i < llNodeCnt; i++ ){ s2 = llN->GetKey(); + #ifdef XB_NDX_SUPPORT if(( iRc = OpenIndex( "NDX", s2 )) != XB_NO_ERROR ){ - iErrorStop = 100; + iErrorStop = 120; throw iRc ; } + #endif // XB_NDX_SUPPORT llN = llN->GetNextNode(); } } - #endif // XB_NDXINF_SUPPORT + #endif // XB_INF_SUPPORT } catch (xbInt16 iRc ){ xbString sMsg; @@ -3190,7 +3230,6 @@ xbInt16 xbDbf::Open( const xbString & sTableName, const xbString & sAlias ){ } return iRc; } -#endif // XB_LOCKING_SUPPORT /************************************************************************/ #ifdef XB_INDEX_SUPPORT @@ -3216,7 +3255,7 @@ xbInt16 xbDbf::OpenIndex( const xbString &sIxType, const xbString &sFileName ){ sType.ToUpperCase(); if( sType == "" ){ - iErrorStop = 10; + iErrorStop = 100; iRc = XB_INVALID_OPTION; throw iRc; @@ -3224,7 +3263,7 @@ xbInt16 xbDbf::OpenIndex( const xbString &sIxType, const xbString &sFileName ){ } else if( sType == "NDX" ){ pIx = new xbIxNdx( this ); if(( iRc = pIx->Open( sFileName )) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 110; throw iRc; } #endif @@ -3234,24 +3273,24 @@ xbInt16 xbDbf::OpenIndex( const xbString &sIxType, const xbString &sFileName ){ pIx = new xbIxMdx( this ); if(( iRc = pIx->Open( sFileName )) != XB_NO_ERROR ){ - iErrorStop = 30; + iErrorStop = 120; throw iRc; } #endif } else { - iErrorStop = 40; + iErrorStop = 130; iRc = XB_INVALID_OPTION; throw iRc; } if(( iRc = AddIndex( pIx, sIxType )) != XB_NO_ERROR ){ - iErrorStop = 50; + iErrorStop = 140; throw iRc; } if(( iRc = UpdateTagList()) != XB_NO_ERROR ){ - iErrorStop = 60; + iErrorStop = 150; throw iRc; } pCurIx = pIx; @@ -3304,38 +3343,41 @@ xbInt16 xbDbf::Pack( xbUInt32 &ulDeletedRecCnt ) xbUInt32 ulRecCnt = 0; ulDeletedRecCnt = 0; + #ifdef XB_LOCKING_SUPPORT + xbBool bLocked = xbFalse; + #endif // XB_LOCKING_SUPPORT + try{ if( !FileIsOpen() ){ - iErrorStop = 10; + iErrorStop = 100; iRc = XB_DBF_FILE_NOT_OPEN; throw iRc; } #ifdef XB_LOCKING_SUPPORT - if( iAutoLock ){ + if( iAutoLock && !bTableLocked ){ if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 110; throw iRc; + } else { + bLocked = xbTrue; } } #endif if(( iRc = GetRecordCnt( ulRecCnt )) != XB_NO_ERROR ){ - iErrorStop = 30; + iErrorStop = 120; throw iRc; } xbBool bDone = xbFalse; for( xbUInt32 ulI = 1; ulI <= ulRecCnt && !bDone; ulI++ ){ - if(( iRc = GetRecord( ulI )) != XB_NO_ERROR ){ - iErrorStop = 40; + iErrorStop = 130; throw iRc; } - if( RecordDeleted()){ - ulDeletedRecCnt++; if( ulI > ulLastMovedRec ) ulStartPos = ulI; @@ -3352,24 +3394,24 @@ xbInt16 xbDbf::Pack( xbUInt32 &ulDeletedRecCnt ) bDone = xbTrue; } else{ - iErrorStop = 50; + iErrorStop = 140; throw iRc; } if( ulMoveRec > 0 ){ if(( iRc = DeleteRecord()) != XB_NO_ERROR ){ - iErrorStop = 60; + iErrorStop = 150; throw iRc; } if(( iRc = PutRecord( ulMoveRec )) != XB_NO_ERROR ){ - iErrorStop = 70; + iErrorStop = 160; throw iRc; } if(( iRc = UndeleteRecord()) != XB_NO_ERROR ){ - iErrorStop = 80; + iErrorStop = 170; throw iRc; } if(( iRc = PutRecord( ulI )) != XB_NO_ERROR ){ - iErrorStop = 90; + iErrorStop = 180; throw iRc; } ulLastPackedRec = ulI; @@ -3391,20 +3433,20 @@ xbInt16 xbDbf::Pack( xbUInt32 &ulDeletedRecCnt ) // rewrite the header record if(( iRc = WriteHeader( 1, 1 )) != XB_NO_ERROR ){ - iErrorStop = 200; + iErrorStop = 190; throw iRc; } // truncate the file to the new size if(( iRc = xbTruncate( uiHeaderLen + uiRecordLen * ulLastPackedRec )) != XB_NO_ERROR ){ - iErrorStop = 210; + iErrorStop = 200; throw iRc; } } if( ulNoOfRecs > 0 ){ if(( iRc = GetRecord( 1 )) != XB_NO_ERROR ){ - iErrorStop = 220; + iErrorStop = 210; throw iRc; } } else { @@ -3415,21 +3457,12 @@ xbInt16 xbDbf::Pack( xbUInt32 &ulDeletedRecCnt ) #ifdef XB_MEMO_SUPPORT if( iMemoFieldCnt > 0 ){ if(( iRc = Memo->PackMemo( 0 )) != XB_NO_ERROR ){ - iErrorStop = 230; + iErrorStop = 220; throw iRc; } } #endif // XB_MEMO_SUPPORT - #ifdef XB_LOCKING_SUPPORT - if( iAutoLock ){ - if(( iRc = LockTable( XB_UNLOCK )) != XB_NO_ERROR ){ - iErrorStop = 240; - throw iRc; - } - } - - #endif } catch (xbInt16 iRc ){ if( iRc != XB_LOCK_FAILED ){ @@ -3439,6 +3472,11 @@ xbInt16 xbDbf::Pack( xbUInt32 &ulDeletedRecCnt ) xbase->WriteLogMessage( GetErrorMessage( iRc )); } } + #ifdef XB_LOCKING_SUPPORT + if( bLocked ){ + LockTable( XB_UNLOCK ); + } + #endif return iRc; } @@ -3492,24 +3530,17 @@ xbInt16 xbDbf::PutRecord(xbUInt32 ulRecNo) } /* lock the database */ #ifdef XB_LOCKING_SUPPORT - // xbInt16 iAutoLock = GetAutoLock(); if( iAutoLock && !bTableLocked ){ if(( iRc = LockHeader( XB_LOCK )) != XB_NO_ERROR ){ - if( iRc == XB_LOCK_FAILED ) - return iRc; - else{ - iErrorStop = 140; throw iRc; - } } + } + #endif // XB_LOCKING_SUPPORT - if(( iRc = ReadHeader( 1, 1 )) != XB_NO_ERROR ){ - iErrorStop = 150; - LockHeader( XB_UNLOCK ); - throw iRc; - } + if(( iRc = ReadHeader( 1, 1 )) != XB_NO_ERROR ){ + iErrorStop = 150; + throw iRc; } - #endif // verify valid record number request if( ulRecNo > ulNoOfRecs || ulRecNo == 0L ){ @@ -3519,30 +3550,20 @@ xbInt16 xbDbf::PutRecord(xbUInt32 ulRecNo) } #ifdef XB_LOCKING_SUPPORT - if( iAutoLock ){ - if( !bTableLocked ){ - if(( iRc = LockRecord( XB_LOCK, ulRecNo )) != XB_NO_ERROR ){ - if( iRc == XB_LOCK_FAILED ) - return iRc; - else{ - iErrorStop = 170; - throw iRc; - } + if( iAutoLock && !bTableLocked ){ + if(( iRc = LockRecord( XB_LOCK, ulRecNo )) != XB_NO_ERROR ){ + iErrorStop = 170; + throw iRc; } - } #ifdef XB_INDEX_SUPPORT if(( iRc = LockIndices( XB_LOCK )) != XB_NO_ERROR ){ - if( iRc == XB_LOCK_FAILED ) - return iRc; - else{ - iErrorStop = 180; - throw iRc; - } + iErrorStop = 180; + throw iRc; } - #endif + #endif // XB_INDEX_SUPPORT } - #endif + #endif // XB_LOCKING_SUPPORT // build keys, check for duplicate keys, add keys #ifdef XB_INDEX_SUPPORT @@ -3559,7 +3580,7 @@ xbInt16 xbDbf::PutRecord(xbUInt32 ulRecNo) iErrorStop = 200; throw iRc; } - return XB_KEY_NOT_UNIQUE; + throw XB_KEY_NOT_UNIQUE; } ixList = ixList->next; } @@ -3576,9 +3597,6 @@ xbInt16 xbDbf::PutRecord(xbUInt32 ulRecNo) ixList = GetIxList(); while( ixList ){ - -// if(( iRc = ixList->ix->DeleteKeys( ulCurRec )) != XB_NO_ERROR ){ - if(( iRc = ixList->ix->DeleteKeys()) != XB_NO_ERROR ){ iErrorStop = 220; throw iRc; @@ -3600,19 +3618,6 @@ xbInt16 xbDbf::PutRecord(xbUInt32 ulRecNo) } } - #ifdef XB_LOCKING_SUPPORT - if( iAutoLock && !bTableLocked ){ - if(( iRc = LockHeader( XB_UNLOCK )) != XB_NO_ERROR ){ - if( iRc == XB_LOCK_FAILED ) - return iRc; - else{ - iErrorStop = 230; - throw iRc; - } - } - } - #endif - // update record iRc = xbFseek( (uiHeaderLen+(( (xbInt64) ulRecNo-1L ) * uiRecordLen )),0 ); if( iRc != XB_NO_ERROR ){ @@ -3635,47 +3640,30 @@ xbInt16 xbDbf::PutRecord(xbUInt32 ulRecNo) } #endif - // unlock record - #ifdef XB_LOCKING_SUPPORT - if( iAutoLock ){ - #ifdef XB_INDEX_SUPPORT - LockIndices( XB_UNLOCK ); - #endif // XB_INDEX_SUPPORT - if( !bTableLocked ){ - if(( iRc = LockRecord( XB_UNLOCK, ulRecNo )) != XB_NO_ERROR ){ - if( iRc == XB_LOCK_FAILED ) - return iRc; - else{ - iErrorStop = 270; - throw iRc; - } - } - } - } - - #endif ulCurRec = ulRecNo; iDbfStatus = XB_OPEN; } catch (xbInt16 iRc ){ - - #ifdef XB_LOCKING_SUPPORT - if( iAutoLock ){ - LockHeader( XB_UNLOCK ); - LockAppend( XB_UNLOCK ); - LockRecord( XB_UNLOCK, ulRecNo ); - LockIndices( XB_UNLOCK ); - } - #endif // XB_LOCKING_SUPPORT - - if( iRc != XB_LOCK_FAILED ){ + if( iRc != XB_LOCK_FAILED && iRc != XB_KEY_NOT_UNIQUE ){ xbString sMsg; sMsg.Sprintf( "xbdbf::PutRecord() Exception Caught. Error Stop = [%d] iRc = [%d] record = [%d]", iErrorStop, iRc, ulRecNo ); xbase->WriteLogMessage( sMsg.Str() ); xbase->WriteLogMessage( GetErrorMessage( iRc )); } } + + #ifdef XB_LOCKING_SUPPORT + if( iAutoLock ){ + LockHeader( XB_UNLOCK ); + LockAppend( XB_UNLOCK ); + LockRecord( XB_UNLOCK, ulRecNo ); + #ifdef XB_INDEX_SUPPORT + LockIndices( XB_UNLOCK ); + #endif // XB_INDEX_SUPPORT + } + #endif // XB_LOCKING_SUPPORT + return iRc; } @@ -3788,14 +3776,14 @@ void xbDbf::ResetNoOfRecords() { } /************************************************************************/ -#ifdef XB_NDXINF_SUPPORT +#ifdef XB_INF_SUPPORT // @brief Update .INF data file. /* Protected method. \returns <a href="xbretcod_8h.html">Return Codes</a> */ -xbInt16 xbDbf::SaveNdxInfData(){ +xbInt16 xbDbf::SaveInfData(){ xbInt16 iRc = 0; xbInt16 iErrorStop = 0; @@ -3803,17 +3791,17 @@ xbInt16 xbDbf::SaveNdxInfData(){ try{ - xbUInt32 llNodeCnt = llNdxInfData.GetNodeCnt(); + xbUInt32 llNodeCnt = llInfData.GetNodeCnt(); - xbString sNdxInfFileName; - if(( iRc = GetNdxInfFileName( sNdxInfFileName )) != XB_NO_ERROR ){ - iErrorStop = 10; + xbString sInfFileName; + if(( iRc = GetInfFileName( sInfFileName )) != XB_NO_ERROR ){ + iErrorStop = 100; throw iRc; } // open the file - if(( iRc = fMd.xbFopen( "w", sNdxInfFileName, GetShareMode())) != XB_NO_ERROR ){ - iErrorStop = 20; + if(( iRc = fMd.xbFopen( "w", sInfFileName, GetShareMode())) != XB_NO_ERROR ){ + iErrorStop = 110; throw iRc; } @@ -3821,13 +3809,12 @@ xbInt16 xbDbf::SaveNdxInfData(){ xbString s2; s2.Sprintf( "[dbase]%c%c", 0x0d, 0x0a ); if(( iRc = fMd.xbFputs( s2 )) != XB_NO_ERROR ){ - iErrorStop = 30; + iErrorStop = 120; throw iRc; } - // for each entry in the linked list, write a line - xbLinkListNode<xbString> * llN = llNdxInfData.GetHeadNode(); + xbLinkListNode<xbString> * llN = llInfData.GetHeadNode(); for( xbUInt32 i = 0; i < llNodeCnt; i++ ){ s2 = llN->GetKey(); if( i > 0 ) @@ -3836,7 +3823,7 @@ xbInt16 xbDbf::SaveNdxInfData(){ s1.Sprintf( "NDX=%s%c%c", s2.Str(), 0x0d, 0x0a ); if(( iRc = fMd.xbFputs( s1 )) != XB_NO_ERROR ){ - iErrorStop = 40; + iErrorStop = 130; throw iRc; } llN = llN->GetNextNode(); @@ -3844,7 +3831,7 @@ xbInt16 xbDbf::SaveNdxInfData(){ // close the file if(( iRc = fMd.xbFclose()) != XB_NO_ERROR ){ - iErrorStop = 50; + iErrorStop = 140; throw iRc; } @@ -3852,13 +3839,13 @@ xbInt16 xbDbf::SaveNdxInfData(){ if( fMd.FileIsOpen()) fMd.xbFclose(); xbString sMsg; - sMsg.Sprintf( "xbdbf::SaveNdxInfData() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc ); + sMsg.Sprintf( "xbdbf::SaveInfData() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc ); xbase->WriteLogMessage( sMsg.Str() ); xbase->WriteLogMessage( GetErrorMessage( iRc )); } return iRc; } -#endif // XB_NDXINF_SUPPORT +#endif // XB_INF_SUPPORT /************************************************************************/ //! @brief Set auto commit. /*! @@ -3884,14 +3871,14 @@ xbInt16 xbDbf::SetAutoCommit( xbBool iAutoCommit ) { -1 - (minus one) Use system default.<br> \returns <a href="xbretcod_8h.html">Return Codes</a> */ - +#ifdef XB_LOCKING_SUPPORT void xbDbf::SetAutoLock( xbInt16 iAutoLock ){ if( iAutoLock == -1 ) this->iAutoLock = xbase->GetDefaultAutoLock(); else this->iAutoLock = iAutoLock; } - +#endif /************************************************************************/ @@ -3946,9 +3933,11 @@ void xbDbf::SetCurTag( const xbString &sIxType, xbIx *pIx, void *vpTag ){ \param bHeaderLocked xbTrue - Locked<br>xbFalse - Not locked. \returns void */ +#ifdef XB_LOCKING_SUPPORT void xbDbf::SetHeaderLocked( xbBool bHeaderLocked ){ this->bHeaderLocked = bHeaderLocked; } +#endif /************************************************************************/ //! @brief Set lock flavor. @@ -3957,10 +3946,11 @@ void xbDbf::SetHeaderLocked( xbBool bHeaderLocked ){ \param iLockFlavor 1 - Use Dbase (tm) style locking. \returns void */ - +#ifdef XB_LOCKING_SUPPORT void xbDbf::SetLockFlavor( xbInt16 iLockFlavor ){ this->iLockFlavor = iLockFlavor; } +#endif /************************************************************************/ //! @brief Set table locked status. @@ -3968,10 +3958,11 @@ void xbDbf::SetLockFlavor( xbInt16 iLockFlavor ){ \param bTableLocked - xbTrue Table locked.<br>xbFalse Table unlocked. \returns void */ - +#ifdef XB_LOCKING_SUPPORT void xbDbf::SetTableLocked( xbBool bTableLocked ){ this->bTableLocked = bTableLocked; } +#endif /************************************************************************/ //! @brief Undelete all records. /*! @@ -4044,8 +4035,8 @@ void xbDbf::UpdateSchemaIxFlag( xbInt16 iFldNo, unsigned char cVal ){ SchemaPtr[iFldNo].cIxFlag = cVal; } -#endif // XB_INDEX_SUPPORT /************************************************************************/ + //! @brief Update tag list. /*! This routine updates the internal tag list of open index tags. @@ -4086,6 +4077,7 @@ xbInt16 xbDbf::UpdateTagList(){ } return iRc; } +#endif // XB_INDEX_SUPPORT /************************************************************************/ // @brief Write Header @@ -4094,8 +4086,8 @@ xbInt16 xbDbf::UpdateTagList(){ \param iPositionOption 0 - Don't fseek to beginning of file before read.<br> 1 - Go to beginning of file before read. - \param iWriteOption 0 - Write entire 32 byte header.<br> - 1 - Write first eight bytes needed for table updates - last update date and number of records. + \param iWriteOption 0 - Write entire 32 byte header.<br> + 1 - Write first eight bytes needed for table updates - last update date and number of records. \returns <a href="xbretcod_8h.html">Return Codes</a> */ xbInt16 xbDbf::WriteHeader( xbInt16 iPositionOption, xbInt16 iWriteOption ) @@ -4125,7 +4117,6 @@ xbInt16 xbDbf::WriteHeader( xbInt16 iPositionOption, xbInt16 iWriteOption ) return XB_NO_ERROR; } - /************************************************************************/ //! @brief Zap (remove) everything from the file, /*! @@ -4137,16 +4128,20 @@ xbInt16 xbDbf::Zap(){ xbInt16 iRc = 0; xbInt16 iErrorStop = 0; + #ifdef XB_LOCKING_SUPPORT + xbBool bLocked = xbFalse; + #endif // XB_LOCKING_SUPPORT + try{ if( iDbfStatus != XB_OPEN ){ - iErrorStop = 10; + iErrorStop = 100; iRc = XB_DBF_FILE_NOT_OPEN; throw iRc; } #ifdef XB_LOCKING_SUPPORT if( iAutoLock ){ if(( iRc = LockTable( XB_LOCK )) != XB_NO_ERROR ){ - iErrorStop = 20; + iErrorStop = 110; throw iRc; } } @@ -4160,13 +4155,13 @@ xbInt16 xbDbf::Zap(){ // rewrite the header record if(( iRc = WriteHeader( 1, 1 )) != XB_NO_ERROR ){ - iErrorStop = 30; + iErrorStop = 120; throw iRc; } // truncate the file to the new size if(( iRc = xbTruncate( uiHeaderLen )) != XB_NO_ERROR ){ - iErrorStop = 40; + iErrorStop = 130; throw iRc; } BlankRecord(); @@ -4175,7 +4170,7 @@ xbInt16 xbDbf::Zap(){ #ifdef XB_MEMO_SUPPORT if( iMemoFieldCnt ){ if(( iRc = Memo->Zap()) != XB_NO_ERROR ){ - iErrorStop = 50; + iErrorStop = 140; throw iRc; } } @@ -4197,14 +4192,6 @@ xbInt16 xbDbf::Zap(){ } #endif // XB_INDEX_SUPPORT - #ifdef XB_LOCKING_SUPPORT - if( iAutoLock ){ - if(( iRc = LockTable( XB_UNLOCK )) != XB_NO_ERROR ){ - iErrorStop = 70; - throw iRc; - } - } - #endif // XB_LOCKING_SUPPORT } catch (xbInt16 iRc ){ if( iRc != XB_LOCK_FAILED ){ @@ -4214,6 +4201,11 @@ xbInt16 xbDbf::Zap(){ xbase->WriteLogMessage( GetErrorMessage( iRc )); } } + #ifdef XB_LOCKING_SUPPORT + if( bLocked ){ + LockTable( XB_UNLOCK ); + } + #endif // XB_LOCKING_SUPPORT return iRc; } /************************************************************************/ |