BBjRecordSet Example

rem ' BBjRecordSet.addMapping sample

rem ' This sample introduces a RecordSetValue Source mapping
rem ' Mappings can be done with any type of recordset, they are done here
rem ' with MemoryRecordSets so the example stands on its own.
rem ' Here we map Location, a number to be one of several regions.
rem '0= NORTH, 1= SOUTH, 2 = WEST, 3 = EAST
rem ' we specify an ErrorMappingAction because anything other than these 4 values is exceptional
rem ' see also the FormatMappingAction.
declare BBjRecordSet UserRS!
declare BBjRecordSet MapRS!
GOSUB SetupRecordSets
declare BBjMappingSource source!
declare BBjMappingAction action!

rem 'configure our source and fallback action
source! = UserRS!.createRecordSetMappingSource(MapRS!,"CODE","DESC")
action! = UserRS!.createErrorMappingAction()

rem 'Add the recordset
UserRS!.addMapping("LOCATION","MAPPED_LOC",source!,action!)
UserRS!.first()
while(1)
    gosub printRecord
    UserRS!.next(err=*end)
wend
end

printRecord:
    name$ = UserRS!.getCurrentRecordData().getFieldValue("NAME")
    location$ = UserRS!.getCurrentRecordData().getFieldValue("MAPPED_LOC")
    PRINT name$, " lives in the ", location$
return

SetupRecordSets:
    UserRS! = BBJAPI().createMemoryRecordSet("NAME:C(100*=10),LOCATION:C(1)")
    Data! = UserRS!.getEmptyRecordData()
    Data!.setFieldValue("NAME", "John Doe")
    Data!.setFieldValue("LOCATION", "1")
    UserRS!.insert(Data!)
    Data! = UserRS!.getEmptyRecordData()
    Data!.setFieldValue("NAME", "Jane Smith")
    Data!.setFieldValue("LOCATION", "3")
    UserRS!.insert(Data!)
    Data! = UserRS!.getEmptyRecordData()
    Data!.setFieldValue("NAME", "Jane Doe")
    Data!.setFieldValue("LOCATION", "2")
    UserRS!.insert(Data!)
    Data! = UserRS!.getEmptyRecordData()
    Data!.setFieldValue("NAME", "Dr Droopy")
    Data!.setFieldValue("LOCATION", "0")
    UserRS!.insert(Data!)

    rem ' Create the recordset to use for mapping LOCATION to something more screenfriendly.
    MapRS! = BBjAPI().createMemoryRecordSet("CODE:C(1),DESC:C(20*=10)")
    Data! = MapRS!.getEmptyRecordData()
    Data!.setFieldValue("CODE","0")
    Data!.setFieldValue("DESC","NORTH")
    MapRS!.insert(Data!)
    Data! = MapRS!.getEmptyRecordData()
    Data!.setFieldValue("CODE","1")
    Data!.setFieldValue("DESC","SOUTH")
    MapRS!.insert(Data!)
    Data! = MapRS!.getEmptyRecordData()
    Data!.setFieldValue("CODE","2")
    Data!.setFieldValue("DESC","EAST")
    MapRS!.insert(Data!)
    Data! = MapRS!.getEmptyRecordData()
    Data!.setFieldValue("CODE","3")
    Data!.setFieldValue("DESC","WEST")
    MapRS!.insert(Data!)
return