home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / database / informix / 2377 < prev    next >
Encoding:
Internet Message Format  |  1992-11-10  |  3.1 KB

  1. Path: sparky!uunet!destroyer!sol.ctr.columbia.edu!emory!emory!not-for-mail
  2. From: johnl@obelix.informix.com (Jonathan Leffler)
  3. Newsgroups: comp.databases.informix
  4. Subject: Re:  Comparing/passing records in 4GL/C
  5. Date: 11 Nov 1992 02:23:12 -0500
  6. Organization: Mailing List Gateway
  7. Lines: 76
  8. Sender: walt@mathcs.emory.edu
  9. Distribution: world
  10. Message-ID: <1dqcd0INNkva@emory.mathcs.emory.edu>
  11. Reply-To: johnl@obelix.informix.com (Jonathan Leffler)
  12. NNTP-Posting-Host: emory.mathcs.emory.edu
  13. X-Informix-List-ID: <list.1584>
  14.  
  15.  
  16. >From: uunet!magnus.acs.ohio-state.edu!kbrummel (Karl P Brummel)
  17. >Subject: Comparing/passing records in 4GL/C
  18. >Date: 5 Nov 92 15:24:59 GMT
  19. >X-Informix-List-Id: <news.2067>
  20.  
  21. >I'm interested in comparing two entire records at runtime, so I can tell if
  22. >one (which started out a copy of the first) is still the same as the first:
  23. >i.e., I want to detect if the record has been updated.  I can't do something
  24. >like
  25. >if ( rec1 = rec2 ) then...
  26. >or
  27. >if (rec1.* = rec2.*) then...
  28. >in 4GL, and I don't seem to be able to find a way to pass a pointer to a
  29. >record to a C function. 
  30.  
  31. That's not very surprising -- there are no pointers anywhere in I4GL.
  32.  
  33. >I can pass all the fields with
  34. >call myfunc(rec1.*,rec2.*),
  35. >but then I have to pop all the fields off the stack, which means I have to
  36. >know all the fields in the record.  What I want to do is get a pointer to
  37. >the first, a pointer to the second, find the size somehow (at run time)
  38. >and do a
  39. >memcmp(rec1,rec2,sizeof(rec));
  40. >or something similar.
  41.  
  42. No chance.
  43.  
  44. >The library function field_touched() almost does what
  45. >I want, so there must be a way to know this information at runtime.  Any
  46. >ideas how I write field_changed()?
  47.  
  48. I doubt if it does.  What it does is tell you if the user typed anything
  49. in any of the fields in the input record.  It may be that the user typed
  50. an 'A' over an existing 'A' -- that makes field_touched think something
  51. happened.  And it only tells you if the record was modified during input.
  52.  
  53. >Poking through the headers for 4GL didn't give me any great ideas.
  54.  
  55. Bad luck -- it wouldn't have helped me either.
  56.  
  57. Roughly speaking, what you have to do is, for each field in the record, is:
  58.  
  59.     LET is_changed = FALSE
  60.     IF is_changed = FALSE THEN
  61.         CASE
  62.         WHEN rec1.columnA IS NULL AND rec2.columnA IS NULL
  63.             LET is_changed = FALSE
  64.         WHEN rec1.columnA IS NULL OR rec2.columnA IS NULL
  65.             LET is_changed = TRUE
  66.         WHEN rec1.columnA != rec2.columnA
  67.             LET is_changed = TRUE
  68.         --OTHERWISE
  69.             --LET is_changed = FALSE
  70.         END CASE
  71.     END IF
  72.     IF is_changed = FALSE THEN
  73.         CASE
  74.         WHEN rec1.columnB IS NULL AND rec2.columnB IS NULL
  75.             ...
  76.         END CASE
  77.     END IF
  78.  
  79. Obviously, for each field which cannot contain nulls, you only need the last
  80. test -- a darned sight easier.  This algorithm can end up testing is_changed
  81. a few times, but it avoids poblems with the tests being indented off the RHS
  82. of the screen.
  83.  
  84. This is non-trivial, especially on tables with 50-odd columns -- I normally
  85. use a shell script to generate the code, and I leave the shell script in the
  86. program as a set of comments, so if (when?) the table is changed, I can
  87. recreate the test automatically.
  88.  
  89. Yours,
  90. Jonathan Leffler (johnl@obelix.informix.com) #include <disclaimer.h>
  91.