Example Code

  Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic! Mail us feedback on this topic!  

//----------------------------------------------------------------------------------------------//

Click here to get an explanation part-by-part to code below ...

//----------------------------------------------------------------------------------------------//

#define __CACHERDD__

FUNCTION Main()

#ifdef __CACHERDD__
/* Server connection parameters, can be supplied as command line vars */
Local cServerIP  := '127.0.0.1'
Local cPort      := '1972'
Local cUser      := '_system'
Local cPassword  := 'SYS'
Local cSecs      := '30'
Local cNameSpace := 'AR'
Local nConxn
#endif

/* Location and name of the data file */
Local cDbfFile := 'c:\MyDataFolder\MyTable.dbf'

/* Location and name of the index file */
Local cIdxFile := 'c:\MyDataFolder\MyTable.cdx'

/* Name of the database driver */
Local cRDD

/* Structure definition for <cDbfFile> */
Local aStr := {}

/*
Based on the following #defines the corrensponding
RDD Methods will be overloaded and all database requests
will be routed to the appropriate method.
*/

/* If native DBFCDX RDD is required */
#ifdef __DBFCDX__
  REQUEST DbfCdx
  cRDD := 'DBFCDX'

#endif

/* If Advantage Database Server RDD is required */
#ifdef __ADVANTAGE__
  REQUEST DbfCdx
  AdsSetFileType( 2 )
  SET SERVER REMOTE
  AdsLocking( .T. )
  cRDD := 'ADS'

#endif

/* If CACHERDD is required */
#ifdef __CACHERDD__
  /* Force CachéRDD methods to be linked with the applications */
  REQUEST CachéRDD

  /* Put Caché connection parameters on the RDD stack */
  CachéSetServerParams( cServerIP, val( cPort ), cUser, cPassword, val( cSecs ) )

  /* Request a connection to the desired NameSpace */
  nConxn := CachéAddConnection( cNameSpace )

  /* If FAILURE then quit the application gracefully! */
  if ( nConxn == 0 )
     Alert( 'Connection to the Server not established!' )
     Return nil
  endif

  /* Force CachéRDD to not generate a shared lock */
  CachéSetUseExclusive( 1 )

  cRDD := 'CACHERDD'

#endif

/*
The code above quarded by #ifdef construct is the one which
will be different for different RDDs. Rest everything
remains the same in the application. Every RDD provides some
additional functions to exploit its additional features, but
those will be discussed later. For example CachéRDD provides
CachéSetServerParams() and CachéAddConnection() as above.
*/

/*
Inform the RDD engine about which RDD driver is in effect for
next next commands to request to. This is done transparently
by the RDD engine. Subsequent all database requests are sent to
the corresponding methods of in-effect RDD.
*/
RddSetDefault( cRDD )

/* Clears the screen */
CLS

/*
Definition of data table structure.
FieldName FieldType FieldLength DecimalPlaces
*/
Aadd( aStr, { 'Code'  , 'C',    8, 0 } )
Aadd( aStr, { 'Name'  , 'C',   25, 0 } )
Aadd( aStr, { 'Salary', 'N',   10, 2 } )
Aadd( aStr, { 'Dob'   , 'D',    8, 0 } )
Aadd( aStr, { 'Mrd'   , 'L',    1, 0 } )
Aadd( aStr, { 'Text'  , 'C', 1000, 0 } )

/* Request RDD to create the above table */
DbCreate( cDbfFile, aStr /*, cRDD */)

/*
Attemp to open the table in SHARED mode
assigning it an ALIAS and in a NEW work area
*/
USE ( cDbfFile ) NEW SHARED ALIAS 'MYTABLE'
/*
And check if the table could been opened successfully.
If for any reason the attemp to open a table fails,
NetErr() function returns TRUE. In this case alert the user
to this effect and return gracefully.
*/
if NetErr()
Alert( cDbfFile + ' : could not been opened!' )
Return nil
endif

/*
Create indexes. Subsequently we will refer them with numbers in the order these are created.

1st Index <CODE> is a normal char index on field Code.
2nd Index <NAME> is a normal char index on field Name.
3rd Index <SALARY> is a numeric index on field Salary.
4th Index <DOB> is a date index on field Dob with a FOR condition. fields
*/
INDEX ON Code   TAG 'CODE'   TO ( cIdxFile )
INDEX ON Name   TAG 'NAME'   TO ( cIdxFile )
INDEX ON Salary TAG 'SALARY' TO ( cIdxFile )
INDEX ON Dob    TAG 'DOB'    TO ( cIdxFile ) FOR Left( Name,1 ) == "P"

/* Insert a record in the table */
APPEND BLANK

/* In case insertion of a new record succeeds */
If !( NetErr() )
  /* Populate fields with some values */

  REPLACE MyTable->Code   WITH "JOHNY"
  REPLACE MyTable->Name   WITH "Johny Walker"
  REPLACE MyTable->Salary WITH 7500.00
  REPLACE MyTable->Dob    WITH CtoD( '11/11/1956' )
  REPLACE MyTable->Mrd    WITH .T.
  REPLACE MyTable->Text   WITH 'This is a large text.'

  /* Tell RDD to force a physically commit the record buffer */
  DbCommit()

  /* New appended record is always locked, so release it */
  DbUnlock()
Endif

/* Set table to naviagte rows in 1st Index <CODE> */
DbSetOrder( 1 )

/* Search for a record matching 'LINEN' with current index */
If DbSeek( 'LINEN' )

  /* Record is found, attempt to lock it */
  If RLock()
     /* Record locked, update fields */
     REPLACE MyTable->Mrd WITH .F.

     /* Unlock, otherwise no other process will be able to update it */
     DbUnlock()

  Endif
Endif

/* Set the index order to 2 which is <DOB> */
DbSetOrder( 2 )

/* Execute a command instead of function to search a record */
SEEK CtoD( '11/11/1956' )

/* Check if the record is there matching the search creteria */
If Found()
  alert( MyTable->Name + ' : is recorded in the database!' )
Endif

/*
Move to the top of the file.
This top position is based on the current controlling index.
*/
DbGoTop()

/* Check if there are any records are there in the table */
If !( Bof() )
  /* Display contents of few fields on the screen */
  ? MyTable->Code, MyTable->Name, MyTable->Salary

Endif

/* Skip to the next record. Again under current index order */
DbSkip()

/* Check if we have moved to the end of table */
If .NOT. Eof()
  ? MyTable->Code, MyTable->Name, MyTable->Salary

Else
  /* If already at the end of table, move to the last record */
  DbGoBottom()

Endif

/* Set table to obey natural order */
SET ORDER TO 0

/* Goto the top of the table ID = 1 */
DbGoTop()

/* Navigate the whole table and display rows */
Do While !( EoF() )
  ? MyTable->Code, MyTable->Name, MyTable->Salary
  DbSkip( 1 )

Enddo

/* Close the current work area */
DbCloseArea()

RETURN NIL

//----------------------------------------------------------------------------------------------//

Click here to get an explanation to above code part-by-part...

//----------------------------------------------------------------------------------------------//

Page url: http://www.vouch.info/cacherdd/index.html?cache_example_code.htm