
' 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 FreeBASIC (0.24) Console compiler.


#Include Once "..\..\include\Cheetah4_FreeBasic.bi"

declare sub DisplayRecords( byval tagHandle1 as uinteger ) 

   
   Dim idxHandle As Long
   Dim tagHandle1 As Long
   
   Dim errcode   As Long
   Dim sData     As String
   Dim sKey      As String
   Dim nRecNum   As Long
   Dim 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
   DisplayRecords tagHandle1


   ' 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

   DisplayRecords tagHandle1
   

ExitOut:

   
' CLOSE THE INDEX //                 
   xdbCloseIndex idxHandle

' SHUTDOWN THE SYSTEM //
   xdbShutdown

   Print
   Print "Completed"
   
   Sleep

   End
   
      
sub DisplayRecords( byval tagHandle1 as uinteger ) 

   dim errcode as long
   dim nRecNum as long
   dim sKey    as string 
 
   ' 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 exit do
      
      print "Key: "; sKey; "   RecNum: "; str$(nRecNum)
      
      errcode = xdbMove( 0, tagHandle1, XDB_MOVENEXT_ATOMIC, nRecNum )
      'errcode = xdbMove( 0, tagHandle1, XDB_MOVENEXT, nRecNum )
   loop
   
end sub
        

