ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

How to compare 2 data structures field by field?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • JonBoy
    replied
    No - it doesn't have such a compare. But use SQL and system tables not the QUSLFLD API. Much easier.

    I think I'm going to play with the technique a bit in a few minutes.

    As I said before though - try to get away from the renaming of fields. Use qualified DS.

    Leave a comment:


  • sri8707
    replied
    Originally posted by JonBoy View Post
    Been think ing some more about this.

    As long as there is an external description to work with, another approach would be to use the system tables to determine the field names and their individual offsets. Then do the compare in a loop using the offsets to determine the substring of the DS to compare.
    Thanks JonBoy. I was thinking about using arrays but realised that wouldnt work either. (Because RPGLE is not a language designed for dynamically obtaining values for the fields from the arrays. I could define array as FIELD1 FIELD1_OLD, but then I cannot retrieve the value pertaining to FIELD_1 dynamically. Hence, that approach is ruled out).

    Yes, i have external description to work with. Somehow I need to offset it for my comparison. I will try with QUSLFLD API to see if it helps.

    I presume RPGLE do not have a method of comparing 2 DS field by field? If so, kindly let me know. Again, below is my DS image -

    DS1:
    Field1
    Field2
    Field3
    Field4
    Field5

    DS2:
    Field1_Old
    Field2_Old
    Field3_Old
    Field4_Old
    Field5_Old

    Leave a comment:


  • JonBoy
    replied
    Been think ing some more about this.

    As long as there is an external description to work with, another approach would be to use the system tables to determine the field names and their individual offsets. Then do the compare in a loop using the offsets to determine the substring of the DS to compare.

    Leave a comment:


  • JimKerr
    replied
    The technique we use captures the image of the record before and after update (i.e. two records written) to an audit file which is similar to the actual file being maintained, additional fields contain date, time stamp, user etc. Checking so many fields for a change seems like a lot of work so using array would seem a good choice.

    Leave a comment:


  • JonBoy
    commented on 's reply
    I should add that an OA based approach, while affording maximum flexibility, will obviously not perform anywhere near as well as a series of IF statements.

  • JonBoy
    replied
    I don't see how your CVT array <shudder - how I hate those things> would help. RPG just doesn't support the use of a variable to supply the name of a field.

    Some years ago I described a "solution" to this kind of problem http://archive.ibmsystemsmag.com/ibm...calculations-/ and http://archive.ibmsystemsmag.com/ibm...-calculations/

    I don't think it really helps in your case but ...

    OK - the Open Access solution. Have you written an OA handler or studied them? How quickly do you need this? I need a topic for a magazine article that is dues this weekend and I'm tempted to use this as an example.

    Leave a comment:


  • sri8707
    replied
    Hi JonBoy, thanks for your reply. With regards to journaling, I do not want to add journals/triggers to the existing file to capture the audit log.

    Can you provide more details on your second point? I didn't get that.

    Another way I could think of is to have a CT array as below -

    Field1_New Field1_Old FieldName1
    Field2_New Field2_Old FieldName2
    ................
    ................
    Field20_New Field20_Old FieldName20

    Then program as,
    Read the array from 1 to 20
    If value1(i) <> value2(i) // If field1_new not equal to field1_old
    Eval change = value3(i) // Set changed field as fieldname1

    But, again, this doesnt look a good practice to me to manually write all the file-fieldnames into the array. I want to somehow avoid using all 20 field names in a file. Because at later instance, few additional fields might be added to the file, and i do not want to re-edit the program then.

    Leave a comment:


  • JonBoy
    replied
    One point before talking solutions.

    If I was trying to do this generically I would not use prefixing. I would use Qualified DS and a template. Like so:

    Code:
    **free
    
    dcl-ds myDS_T extname('FILE1') Template end-ds;
    
    dcl-ds Ds1  LikeDS(myDS_T);
    
    dcl-ds Ds2  LikeDS(myDS_T);
    That said, there are a couple of options I can think of.

    1) Write a simple code generator to produce the IF statements you need. Generate the code into a copy file and incorporate it into the program where needed.

    2) A generics solution that could work really well if all of your cases involve externally described DS. Basically you could write a name/value Open Access handler and then "write" each DS to a file and have the handler store and compare the values. This is probably much easier to write than to describe! Shout if you want the think about this as an option.

    P.S. I'm fascinated as to why you don't just use journaling with before/after images to achieve your goals. Much simpler and requires no modifications to the user programs.

    Leave a comment:


  • sri8707
    started a topic How to compare 2 data structures field by field?

    How to compare 2 data structures field by field?

    I would like to compare 2 DS field by field and capture the changed fields into an audit file. Below is the scenario -

    User is provided with a display screen where he can change any of the 20 fields pertaining to a file. Say if the user changed 10 fields, I want to write 10 records into the audit log file with below details -

    Field changed
    Before value
    After value
    Date/Time, etc

    At the moment, I have written logic as below -


    <code>
    D File E DS EXTNAME(File1) // Contains latest image of the 20 fields
    *
    D P_File E DS EXTNAME(File1) Prefix(P_) // Contains previous image of the 20 fields

    C If Field1 <> P_Field1
    C Eval BeforeImage = P_Field1
    C Eval AfterImage = Field1
    C Eval Comment = 'Field1 changed'
    C Write Auditfile
    C EndIf

    C If Field2 <> P_Field2
    C Eval BeforeImage = P_Field2
    C Eval AfterImage = Field2
    C Eval Comment = 'Field2 changed'
    C Write Auditfile
    C EndIf
    </code>

    As you can see, I am writing 20 IF statements for manually checking changes one by one. But, is there a better approach to do this? (Something like looping through data structure to check all fields)
Working...
X