home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / xbase / compiler / doobject / relation.do < prev    next >
Text File  |  1991-06-10  |  1KB  |  62 lines

  1. /*
  2.     demonstrate the dObject relation facility between two related
  3.     .DBF files,  first, create a customer file with customer names
  4. */
  5. custDb = createDbf("customer",[
  6.     ["cId",'N',8,0],
  7.     ["cName",'C',32,0],
  8.     ["cAddress",'C',48,0],
  9.     ["cCity",'C',16,0],
  10.     ["cState",'C',2,0],
  11.     ["cZip",'C',5,0]]);
  12.  
  13. /*
  14.     fill in some test customer names
  15. */
  16. #define MAXCUST 5
  17. for(i=0;i<MAXCUST;i=i+1) {
  18.     cId = i;
  19.     cName = "name"+asString(i);
  20.     cAddress = "";
  21.     cCity = "Chicago";
  22.     cState = "IL";
  23.     cZip = "60640";
  24.     ? "writing record ",i;
  25.     write(custDb,0L);
  26. }
  27. close(custDb);
  28.     
  29. /*
  30.     create a database of customer purchases
  31. */
  32. purchaseDb = createDbf("purchase",[
  33.     ["cId",'N',8,0],
  34.     ["pItem",'C',32,0],
  35.     ["pDate",'D',0,0],
  36.     ["pAmount",'N',8,2]]);
  37.  
  38. /*
  39.     create some test data
  40. */
  41. for(i=0;i<MAXCUST;i=i+1)
  42.     for(j=0;j<2;j=j+1) {
  43.         cId = i;
  44.         pItem = "item"+asString(i);
  45.         pDate = date();
  46.         pAmount = 5.25;
  47.         write(purchaseDb,0L);
  48.     }
  49. close(purchaseDb);
  50.  
  51. /*
  52.     find the purchases for customer #3
  53. */
  54. custDb = new(Dbffile,"customer");
  55. purchaseDb = new(Dbffile,"purchase");
  56. ndx = createIndex(purchaseDb,"CID","cId",0,0);
  57. setRelation(custDb,"cId",select(purchaseDb),select(ndx),0);
  58. top(custDb);
  59. locate(custDb,"cId",3);
  60. ? "record number for customer 3 is ",recno(custDb);
  61. ? "customer id field in purchase database is ",recno(purchaseDb);
  62.