Show/Hide Toolbars

CacheRDD

Navigation: Example Code

Example Explained

Scroll Prev Top Next More

Example Code Analyzed

 

Establishing a connection to the database

/* 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

cRDD := 'CACHERDD'

 

Creating a Table

/*
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 */)

 

Opening a Table:

/*
Attempt 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 attempt 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

 

Creating Indexes

/*
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"

 

Populate Table

/* 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

 

Navigation and Data Retrieval

/* 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 criteria */
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 Work-Area:

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