home *** CD-ROM | disk | FTP | other *** search
- /*
- Listing 9.10. Load an array with records from a related database.
- Author: Craig Yellick
- Excerpted from "Clipper 5: A Developer's Guide"
- Copyright (c) 1991 M&T Books
- 501 Galveston Drive
- Redwood City, CA 94063-4728
- (415) 366-3600
- */
-
- function dbf2Array(parent, pFields_, pRecs, ;
- child, cFields_, cRecs, ;
- key, relation)
- /*
- Example:
-
- The data_ array will be filled with records in PARENT
- where the state is MN, and the CHILD components of the
- array will be filled only by CHILD records where the
- value of the Age field is less than 18.
-
- data_ := dbf2Array("PARENT", ,, { || State = "MN" } ;
- "CHILD", ,, { || Age < 18 } ;
- { || PARENT->ID }, ;
- { || CHILD->Parent == PARENT->ID })
-
- */
-
-
- local i, r_, rr_, rf_, fP_, fC_
- local a_ := {}
-
- /*
- If no list of fields was specified, create an array of
- field value retrieval blocks for all fields
- in database. Otherwise, create array just for
- specified fields.
- */
- select (select(parent))
- if pFields_ = nil
- fP_ := array(fcount())
- aeval(fP_, { |n,i| fP_[i] := fieldblock(field(i))})
- else
- fP_ := array(len(pFields_))
- aeval(fP_, { |n,i| fP_[i] := fieldblock(pFields_[i])})
- endif
- select (select(child))
- if cFields_ = nil
- fC_ := array(fcount())
- aeval(fC_, { |n,i| fC_[i] := fieldblock(field(i))})
- else
- fC_ := array(len(cFields_))
- aeval(fC_, { |n,i| fC_[i] := fieldblock(cFields_[i])})
- endif
-
-
- // If no code block specified, include all records.
- if pRecs = nil
- pRecs := {|| .t.}
- endif
- if cRecs = nil
- cRecs := {|| .t.}
- endif
-
- /*
- For each record in the database, check to see if it
- satisfies the code block.
- */
- select (select(parent))
- goto top
- do while .not. eof()
- if eval(pRecs)
- /*
- Loop through the list of parent fields, building
- an array containing the values of each field for
- the current record.
- */
- r_ := {}
- aeval(fP_, { |b| aadd(r_, eval(b)) } )
-
- /*
- Seek the key in the child database, build
- an array containing the values for each
- related record.
- */
- rr_ := {}
- select (select(child))
- seek eval(key)
- do while eval(relation) .and. !eof()
-
- if eval(cRecs)
- /*
- Build array of fields in related record.
- */
- rf_ := {}
- aeval(fC_, { |b| aadd(rf_, eval(b)) } )
-
- /*
- Add array of fields to related record list.
- */
- aadd(rr_, rf_)
- endif
- select (select(child))
- skip
- enddo
-
- /*
- Add set of related records to parent record.
- */
- aadd(r_, rr_)
-
- /*
- Add the parent record value to the return array.
- */
- aadd(a_, r_)
-
- endif
- select (select(parent))
- skip
- enddo
-
- return a_
-
- // end of file CHP0910.PRG
-