
' StandaloneIndex
' Shows how to use an index without an associated database. This
' allows you to use the index to store keys and "record numbers". 
' Basically, the "record number" can be any 32-bit value that is
' of meaning to the user.

' This code can be used with Cheetah DLL's that have either full
' database support compiled into it, or with DLL's that only have
' the index routines compiled in them.

' This demo uses the PowerBASIC Console compiler.

#Compile Exe

#Include "..\..\include\Cheetah4_PowerBasic.inc"

'JPRO_COMPILER=PB/CC


Function PBMain() As Long
   
   Local idxHandle As Long
   Local tagHandle1 As Long
   
   Local errcode   As Long
   Local sData     As String
   Local sKey      As String
   Local nRecNum   As Long
   Local NumRead   As Long
                            
   Cls
   
   sData = "disk = standalone.btx;"  
   errcode = xdbCreateIndex( sData ) 
   If errcode Then GoTo ExitOut

   ' In order to use the index without an associated database, you
   ' must specify the "NoDatabase" property and set it to "TRUE".
   sData = "disk=standalone.btx; nodatabase=true;" 
   errcode = xdbOpenIndex( sData, 0, idxHandle ) 
   If errcode Then GoTo exitout

   Print "Create/open index. Handle:"; Str$(idxHandle)
   

   '// CREATE AN INDEX TAG
   ' When you create a Tag, it is your responsibility to specify
   ' the size of the keys.
   sData = "TagName=scores; Size=20; Uppercase=TRUE"
   errcode = xdbCreateTag( sData, idxHandle ) 
   If errcode Then GoTo exitout

   errcode = xdbGetTag( "scores", idxHandle, tagHandle1 ) 
   If errcode Then GoTo exitout

   Print "GetTag. 'Scores' Handle:"; Str$(tagHandle1)
   
   
   '// ADD INDEX KEYS //         
   If xdbAddKey( tagHandle1, "Paul",   100 ) Then GoTo exitout
   If xdbAddKey( tagHandle1, "Jeff",    85 ) Then GoTo exitout
   If xdbAddKey( tagHandle1, "Blayne",  91 ) Then GoTo exitout
   If xdbAddKey( tagHandle1, "Todd",    79 ) Then GoTo exitout
   If xdbAddKey( tagHandle1, "Donna",   82 ) Then GoTo exitout
   If xdbAddKey( tagHandle1, "Bob",     57 ) Then GoTo exitout
   If xdbAddKey( tagHandle1, "Tammy",   98 ) Then GoTo exitout

   Print "Index keys and values added."
   Print

   '// MOVE THROUGH THE INDEX TAG
   GoSub DisplayRecords


   '// DELETE SOME KEYS
   ' The key and recnum must match exactly in order for it to be deleted
   Print
   If xdbDeleteKey( tagHandle1, "Donna", 82 ) = %ERR_SEEKEXACTMATCH Then 
      Print "Could not delete Key: Donna, 82"
   Else
      Print "Deleted. Key: Donna, 82"
   End If   

   If xdbDeleteKey( tagHandle1, "Blayne", 91 ) = %ERR_SEEKEXACTMATCH Then 
      Print "Could not delete Key/Recnum: Blayne, 91"
   Else
      Print "Deleted. Key: Blayne, 91"
   End If   

   If xdbDeleteKey( tagHandle1, "Paul", 100 ) = %ERR_SEEKEXACTMATCH Then 
      Print "Could not delete Key/Recnum: Paul, 100"
   Else
      Print "Deleted. Key: Paul, 100"
   End If   
   Print

   GoSub DisplayRecords
   

ExitOut:

   
'// CLOSE THE INDEX //                 
   xdbCloseIndex idxHandle

'// SHUTDOWN THE SYSTEM //
   xdbShutdown

   Print
   Print "Completed"
   
   While InKey$ = "": Wend

Exit Function
      

      
DisplayRecords:

   ' Because there is no database, specify 0 for the database handle.
   errcode = xdbMove( 0, tagHandle1, %XDB_MOVEFIRST, nRecNum )

   Do Until errcode <> 0   ' loop until end of file reached
    
      errcode = xdbGetKey( tagHandle1, sKey, nRecNum )
      If errcode Then GoTo ExitOut
      
      Print "Key: "; sKey; "   RecNum: "; Str$(nRecNum)
      
      errcode = xdbMove( 0, tagHandle1, %XDB_MOVENEXT_ATOMIC, nRecNum )
      'errcode = xdbMove( 0, tagHandle1, %XDB_MOVENEXT, nRecNum )
   Loop
   
Return
        
End Function

