Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Force 4.0 Reference - set relation to inserts or remove an item in the relation list http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 set relation to     Inserts or remove an item in the relation list
------------------------------------------------------------------------------
 Syntax
   [!sAliasParent] set relation to ;
                   [xExpr|record nRecord into sAliasChild] ;
                   [additive]

 Arguments
   sAliasParent is the database to set as the parent alias.

   xExpr is a seek expression controlling the relation.

   nRecord is an expression that evaluates to a ulong numeric and
   serves as the basis to reposition the record pointer in the child
   database.

   sAliasChild is the database to set as the child alias.

 Description
   The set relation to command inserts or deletes a relation in the
   relation list for the current or selected alias. set relation to
   establishes a relationship between two or more aliases. A parent
   alias is the current or specified alias. A child alias is any other
   active database alias.

   Once a relationship is established, the child's record pointer is
   automatically repositioned through its relation expression when the record
   pointer is repositioned in the parent alias. If the child alias
   record is not found, then eof() will be .t..

   Movement of the record pointer in the child alias is performed by either
   a seek (if xExpr is given), or by a go to (if nRecord is specified).
   If the xExpr relation expression is defined, then the parent and
   child alias must be indexed on the same index key. For a relation
   given by the record clause, the child alias must not have an active index.

   More than one relation can be established by using the additive clause.
   If the additive clause is specified, then the relation is added to the
   relation list for the parent alias. Otherwise, the relation list is
   truncated to include only the specified relation.

   If set relation to is specified without an alias, then all relationships
   with the parent are released.

 Example
   #define EXAMPLE_DATABASE
   #include example.hdr

   dbfdef sCity static
      char( 25 ) lastname
      char( 20 ) city
   enddef
   
   dbfdef sCountry static
      char( 20 ) city
      char( 20 ) country
   enddef
   
   dbfdef sCont static
      char( 20 ) city
      char( 20 ) continent
   enddef
   
   indexdef static
      char(20) sIdxCity    sCity->city
      char(20) sIdxCountry sCountry->city
      char(20) sIdxCont    sCont->city
   enddef
   
   vardef static
      char( 20 ) aCity[ 10 ] := "Los Angeles", "London", "Johannesburg", ;
                                "Sidney", "Winnipeg", "Auckland", "Nairobi", ;
                                "Manila", "Caracas", "New Delhi"
   
      char( 20 ) aCountry[ 10 ] := "United States", "United Kingdom", ;
                                   "South Africa", "Australia", "Canada", ;
                                   "New Zealand", "Kenya", "Philippines", ;
                                   "Venezuela", "India"
   
      char( 20 ) aCont[ 10 ] := "North America", "Europe", ;
                                "Africa", "Australia", "North America", ;
                                "Oceania", "Africa", "Asia", ;
                                "South America", "Asia"
   enddef
   
   proc MakeDbf static
   vardef
      uint n
   enddef
   build "scity.dbf"    from alias sCity             // build databases
   build "scountry.dbf" from alias sCountry
   build "scont.dbf"    from alias sCont
   open "scity.dbf"    alias sCity                   // open databases
   open "scountry.dbf" alias sCountry
   open "scont.dbf"    alias sCont
   for n := 0 to 9                                   // add records
      !sCity    append blank
      !sCountry append blank
      !sCont    append blank
      sCity->lastname   := aLastName[ n ]
      sCity->city       := aCity[ n ]
      sCountry->city    := aCity[ n ]
      sCountry->country := aCountry[ n ]
      sCont->city       := aCity[ n ]
      sCont->continent  := aCont[ n ]
   next
   !sCity    index "scity.fdx"    alias sIdxCity     // build indexes
   !sCountry index "scountry.fdx" alias sIdxCountry
   !sCont    index "scont.fdx"    alias sIdxCont
   !sCity    set index to sIdxCity                   // open indexes
   !sCountry set index to sIdxCountry
   !sCont    set index to sIdxCont
   endproc
   
   func ulong FindContinent static
   !sCont go top
   ! sCont locate for sCont->city == sCity->city
   return( arecno( sCont ) )
   endfunc
   
   proc ListDbf static
   ? "Name":15, "City":15, "Country":15, "Continent":15
   ? replicate( "-", 65 )
   !sCity go top
   do while .not. aeof( sCity )
      ? sCity->lastname:15, sCity->city:15, sCountry->country:15, ;
         sCont->continent:15
      !sCity skip
   enddo
   ?
   wait
   ?
   endproc
   
   proc Test_setrelationto
   MakeDbf()                                // create databases and indexes
   !sCity set relation to sCity->city into sCountry // set up a relation
   ListDbf()                                        // list data
   !sCity set relation to sCity->city into sCont additive // add second relation
   ListDbf()                                        // list data
   !sCity set relation to                           // delete both relations
   ListDbf()                                        // list data
   !sCont close index                               // close continent index
   !sCity set relation to sCity->city into sCountry // establish relation
   // add a relation with "manual" record positioning
   !sCity set relation to record FindContinent() into sCont additive
   ListDbf()                                        // list data
   erase "scity.dbf"                                // clean up
   erase "scountry.dbf"
   erase "scont.dbf"
   erase "scity.fdx"
   erase "scountry.fdx"
   erase "scont.fdx"
   endproc

   proc main
   Test_setrelationto()
   endproc

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson