Snippets

SeanB FindByID - Always finds records by their first single-field primary index.

Created by SeanB
Struct tIndexFinder
    Integer iFile 
    Integer iField 
    Integer iIndex
    String sColumn 
End_Struct

Object oIndexID is a cObject 
    Property tIndexFinder[] pIndexFinderList
    Property tIndexFinder pLastItem
    
    Function DF_Database_ID_Index_detail Integer iFile Returns tIndexFinder
        tIndexFinder idDetail
        Integer iLastIndex
        Integer iIndex
        Integer iNumSegments
        Integer iColumn 
        String sColumn 
        
        Move iFile to IdDetail.iFile
        Move 0 to IdDetail.iIndex
        Move 0 to IdDetail.iField

        Get_Attribute DF_FILE_LAST_INDEX_NUMBER of iFile to iLastIndex
        For iIndex from 1 to iLastIndex
            Get_Attribute DF_INDEX_NUMBER_SEGMENTS of iFile iIndex to iNumSegments
            If (iNumSegments = 1) Begin
                Get_Attribute DF_INDEX_SEGMENT_FIELD of iFile iIndex 1 to iColumn
                Get_Attribute DF_FIELD_NAME of iFile iIndex to sColumn
                
                Move iIndex to IdDetail.iIndex
                Move iColumn to IdDetail.iField
                Move sColumn to IdDetail.sColumn

                Function_Return idDetail
            End
        Loop
        Function_Return idDetail
    End_Function
    
    Function ItemForFile Integer iFile Returns tIndexFinder
        tIndexFinder IdDetail
        tIndexFinder[] IndexFinderList
        Integer iPos 
        Integer iMax 
        
        Get pLastItem to IdDetail
        If (IdDetail.iFile = iFile) Function_Return IdDetail
        
        Get pIndexFinderList to IndexFinderList
        Move (SizeOfArray(IndexFinderList)) to iMax 
        For iPos from 0 to (iMax-1) 
            If ( IndexFinderList[iPos].iFile = iFile ) Begin 
                Set pLastItem to IndexFinderList[iPos]
                Function_Return IndexFinderList[iPos]
            End
        Loop  
        
        //we have not found it, so we need to go to the FileList 
        Get DF_Database_ID_Index_detail iFile to IdDetail 
        
        Move (SizeOfArray(IndexFinderList)) to iMax 
        Move IDDetail to IndexFinderList[iMax]
        //todo:sort this for faster finding....
        
        Set pIndexFinderList to IndexFinderList
        Set pLastItem to IDDetail
        Function_Return IDDetail 
    End_Function
    
    Function IndexForID Integer iFile Returns Integer 
        tIndexFinder IdDetail
        Move (ItemForFile(Self,iFile)) to IdDetail
        Function_Return IdDetail.iIndex
    End_Function
    
    Function FieldForID Integer iFile Returns Integer 
        tIndexFinder IdDetail
        Move (ItemForFile(Self,iFile)) to IdDetail
        Function_Return IdDetail.iField
    End_Function
End_Object
Global_Variable Integer ghoIndexID
Get oIndexID to ghoIndexID

Procedure find_by_id for DataDictionary String sValue 
    Integer iFile 
    Integer iIndex 
    Integer iField 
    
    Get Main_File to iFile 
    Get IndexForID of ghoIndexID iFile to iIndex
    Get FieldForID of ghoIndexID iFile to iField
    
    Send Request_Clear
    Clear iFile 
    Set_Field_Value iFile iField to sValue 
    Send Request_Find EQ iFile iIndex 
End_Procedure

//Use oFindByID.pkg
//
//Examples use the Example Order Entry Data Dictionaries
//--
//Usage
//Send find_by_id to {dataDictionary} {sValue}
//The procedure assumes the first index with one segment is the ID Data-dictionary
//
//Send find_by_id to oVendor_DD 8
//Showln Vendor.Name
//
//Send find_by_id to oInvt_DD "UNIXGUIDE"
//Showln Invt.Description
//Showln Vendor.Name
//Showln "---"
//
//Send find_by_id to oInvt_DD "UPDATE"
//Showln Invt.Description
//Showln Vendor.Name
//Showln "---"

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.