home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a012 / 1.ddi / CHAP09.EXE / CHP0910.PRG < prev    next >
Encoding:
Text File  |  1991-06-01  |  3.1 KB  |  125 lines

  1. /*
  2.    Listing 9.10. Load an array with records from a related database.
  3.    Author: Craig Yellick
  4.    Excerpted from "Clipper 5: A Developer's Guide"
  5.    Copyright (c) 1991 M&T Books
  6.                       501 Galveston Drive
  7.                       Redwood City, CA 94063-4728
  8.                       (415) 366-3600
  9. */
  10.  
  11. function dbf2Array(parent, pFields_, pRecs, ;
  12.                    child,  cFields_, cRecs, ;
  13.                    key, relation)
  14. /*
  15.    Example:
  16.  
  17.    The data_ array will be filled with records in PARENT
  18.    where the state is MN, and the CHILD components of the
  19.    array will be filled only by CHILD records where the
  20.    value of the Age field is less than 18.
  21.  
  22.    data_ := dbf2Array("PARENT", ,, { || State = "MN" } ;
  23.                       "CHILD",  ,, { || Age < 18 } ;
  24.                       { || PARENT->ID }, ;
  25.                       { || CHILD->Parent == PARENT->ID })
  26.  
  27. */
  28.  
  29.  
  30. local i, r_, rr_, rf_, fP_, fC_
  31. local a_ := {}
  32.  
  33.   /*
  34.      If no list of fields was specified, create an array of
  35.      field value retrieval blocks for all fields
  36.      in database. Otherwise, create array just for
  37.      specified fields.
  38.   */
  39.   select (select(parent))
  40.   if pFields_ = nil
  41.     fP_ := array(fcount())
  42.     aeval(fP_, { |n,i| fP_[i] := fieldblock(field(i))})
  43.   else
  44.     fP_ := array(len(pFields_))
  45.     aeval(fP_, { |n,i| fP_[i] := fieldblock(pFields_[i])})
  46.   endif
  47.   select (select(child))
  48.   if cFields_ = nil
  49.     fC_ := array(fcount())
  50.     aeval(fC_, { |n,i| fC_[i] := fieldblock(field(i))})
  51.   else
  52.     fC_ := array(len(cFields_))
  53.     aeval(fC_, { |n,i| fC_[i] := fieldblock(cFields_[i])})
  54.   endif
  55.  
  56.  
  57.   //  If no code block specified, include all records.
  58.   if pRecs = nil
  59.     pRecs := {|| .t.}
  60.   endif
  61.   if cRecs = nil
  62.     cRecs := {|| .t.}
  63.   endif
  64.  
  65.   /*
  66.      For each record in the database, check to see if it
  67.      satisfies the code block.
  68.   */
  69.   select (select(parent))
  70.   goto top
  71.   do while .not. eof()
  72.     if eval(pRecs)
  73.       /*
  74.          Loop through the list of parent fields, building
  75.          an array containing the values of each field for
  76.          the current record.
  77.       */
  78.       r_ := {}
  79.       aeval(fP_, { |b| aadd(r_, eval(b)) } )
  80.  
  81.       /*
  82.          Seek the key in the child database, build
  83.          an array containing the values for each
  84.          related record.
  85.       */
  86.       rr_ := {}
  87.       select (select(child))
  88.       seek eval(key)
  89.       do while eval(relation) .and. !eof()
  90.  
  91.         if eval(cRecs)
  92.           /*
  93.              Build array of fields in related record.
  94.           */
  95.           rf_ := {}
  96.           aeval(fC_, { |b| aadd(rf_, eval(b)) } )
  97.  
  98.           /*
  99.              Add array of fields to related record list.
  100.           */
  101.           aadd(rr_, rf_)
  102.         endif
  103.         select (select(child))
  104.         skip
  105.       enddo
  106.  
  107.       /*
  108.          Add set of related records to parent record.
  109.       */
  110.       aadd(r_, rr_)
  111.  
  112.       /*
  113.          Add the parent record value to the return array.
  114.       */
  115.       aadd(a_, r_)
  116.  
  117.     endif
  118.     select (select(parent))
  119.     skip
  120.   enddo
  121.  
  122. return a_
  123.  
  124. // end of file CHP0910.PRG
  125.