home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 106 / EnigmaAmiga106CD.iso / www / afc / afc-dir / database_examples.lha / Examples / DataBase_Example1.e < prev    next >
Text File  |  1998-04-20  |  9KB  |  259 lines

  1. /*
  2.   DataBase Example - by Andrea Galimberti - (C) Brighting Brain Brothers
  3.  
  4.   This example shows how to use the DataBase class. It also exploits the
  5.   dbfdate custom field, so you have to set the search path for the
  6.   dbfdate.m module. The source for the dbfdate custom field is also
  7.   provided as an example.
  8. */
  9.  
  10.  
  11. OPT OSVERSION=37
  12. OPT LARGE
  13.  
  14. MODULE 'afc/database',
  15.        'afc/explain_exception',
  16.        'tools/easygui',
  17.        'dos/dos',
  18.        '*dbfdate' -> here you put your path for the dbfdate
  19.                   -> module
  20.  
  21. /* This is only the structure we pass to the EG_INFO tag of easygui: it
  22.    contains pointers to manipulate the gadgets and the pointer to our
  23.    database object */
  24. OBJECT data
  25.   main:LONG   ->gui
  26.   fn:LONG     ->gadgdet: first name
  27.   ln:LONG     ->gadgdet: last name
  28.   ad:LONG     ->gadgdet: address
  29.   ph:LONG     ->gadgdet: phone
  30.   rc:LONG     ->gadgdet: record count
  31.   da:LONG     ->gadgdet: date
  32.  
  33.   table:PTR TO database   ->database object
  34. ENDOBJECT
  35.  
  36.  
  37. PROC empty() IS EMPTY
  38.  
  39. /* Writes the contents of the string gadgets in a new record */
  40. PROC add(info:PTR TO data)
  41.   DEF s[5]:STRING
  42.  
  43.   info.table.add() -> put table in add mode
  44.   info.table.fields.first() -> move to the first field in table
  45.   /* set its value gettig it from the 'first name' string gadget */
  46.   info.table.fields.setv(getstr(info.main,info.fn))
  47.   info.table.fields.succ() -> move to the next field in table
  48.   info.table.fields.setv(getstr(info.main,info.ln)) -> and so on...
  49.   info.table.fields.succ()
  50.   info.table.fields.setv(getstr(info.main,info.ad))
  51.   info.table.fields.succ()
  52.   info.table.fields.setv(getstr(info.main,info.ph))
  53.   info.table.fields.succ()
  54.   info.table.fields.setv(getstr(info.main,info.da))
  55.   /* when all the fields are set, we write the new record to file */
  56.   info.table.update()
  57.   /* and we update the record count gadget, reading the number of records
  58.      directly from the database object */
  59.   settext(info.main,info.rc,StringF(s,'\d',info.table.recordCount()))
  60.   /* update all gadgets */
  61.   updategad(info)
  62. ENDPROC
  63.  
  64. /* Modifies the current record using the contents of the string gadgets */
  65. PROC modify(info:PTR TO data)
  66.  
  67.   info.table.edit() -> put table in edit mode
  68.   info.table.fields.first() -> move to the first field in table
  69.   /* set its contents according to the string in the 'first name' string
  70.      gadget */
  71.   info.table.fields.setv(getstr(info.main,info.fn))
  72.   info.table.fields.succ() -> move to the next field in table
  73.   info.table.fields.setv(getstr(info.main,info.ln)) -> and so on...
  74.   info.table.fields.succ()
  75.   info.table.fields.setv(getstr(info.main,info.ad))
  76.   info.table.fields.succ()
  77.   info.table.fields.setv(getstr(info.main,info.ph))
  78.   info.table.fields.succ()
  79.   info.table.fields.setv(getstr(info.main,info.da))
  80.   /* when we are finished setting the fields, we write the new contents to
  81.      the current record */
  82.   info.table.update()
  83.   /* update all gadgets */
  84.   updategad(info)
  85. ENDPROC
  86.  
  87. /* Updates the string gadget contents to reflect the contents of the fields
  88.    of the current record */
  89. PROC updategad(info:PTR TO data)
  90.  
  91.   info.table.fields.first() -> move to the first field in table
  92.   /* get the contents of the field and put it in the first string gadget */
  93.   setstr(info.main,info.fn,info.table.fields.getv())
  94.   info.table.fields.succ() -> move to the next field in table
  95.   setstr(info.main,info.ln,info.table.fields.getv()) -> and so on...
  96.   info.table.fields.succ()
  97.   setstr(info.main,info.ad,info.table.fields.getv())
  98.   info.table.fields.succ()
  99.   setstr(info.main,info.ph,info.table.fields.getv())
  100.   info.table.fields.succ()
  101.   setstr(info.main,info.da,info.table.fields.getv())
  102. ENDPROC
  103.  
  104. /* Deletes the current record */
  105. PROC delete(info:PTR TO data)
  106.   DEF s[5]:STRING
  107.  
  108.   info.table.delete() -> delete current record
  109.   updategad(info) -> update gadget contents
  110.   /* update the record count gadget */
  111.   settext(info.main,info.rc,StringF(s,'\d',info.table.recordCount()))
  112. ENDPROC
  113.  
  114. /* Moves to the previous record */
  115. PROC prevrec(info:PTR TO data)
  116.  
  117.   /* if the prev() method returns FALSE then we are on the first record
  118.      of the table (or the table is empty) */
  119.   IF info.table.prev() THEN updategad(info)
  120. ENDPROC
  121.  
  122. /* Moves to the next record */
  123. PROC nextrec(info:PTR TO data)
  124.  
  125.   /* if the succ() method returns FALSE then we are on the last record of
  126.      the table (or the table is empty) */
  127.   IF info.table.succ() THEN updategad(info)
  128. ENDPROC
  129.  
  130. /* Packs the table (this means that deleted records are physically removed
  131.    from the table data) */
  132. PROC packtable(info:PTR TO data)
  133.   DEF s[5]:STRING
  134.  
  135.   info.table.close() -> you cannot pack an open table
  136.   info.table.pack()  -> pack the table
  137.   /* a simple message telling we are done */
  138.   easyguiA('Message:',[ROWS,
  139.                         [TEXT,'Pack done!',NIL,TRUE,12],
  140.                         [BUTTON,NIL,'ALL right']
  141.                       ])
  142.   info.table.open() -> reopen the packed table
  143.   /* update the record count gadget */
  144.   settext(info.main,info.rc,StringF(s,'\d',info.table.recordCount()))
  145.   /* if table isn't empty then refresh gadgets */
  146.   IF info.table.first() THEN updategad(info)
  147. ENDPROC
  148.  
  149.  
  150. PROC main() HANDLE
  151.   DEF gui, gh:PTR TO guihandle, res=-1
  152.   DEF table=NIL:PTR TO database, dd=NIL:PTR TO data
  153.   DEF gad1,gad2,gad3,gad4,gad5,gad6, buf1[50]:STRING, buf2[50]:STRING
  154.   DEF buf3[50]:STRING, buf4[30]:STRING, s[5]:STRING, buf5[10]:STRING
  155.   DEF lk=NIL
  156.  
  157.   /* Well, this is our GUI definition */
  158.   gui:=[EQROWS,
  159.         gad1:=[STR,{empty},'First Name',buf1,50,30],
  160.         gad2:=[STR,{empty},'Last Name',buf2,50,30],
  161.         gad3:=[STR,{empty},'Address',buf3,50,30],
  162.         gad4:=[STR,{empty},'Phone',buf4,30,30],
  163.         gad6:=[STR,{empty},'Date',buf5,10,30],
  164.         [EQCOLS,
  165.           [BUTTON,{delete},'Delete'],
  166.           [SPACEH],
  167.           [BUTTON,{add},'Add'],
  168.           [BUTTON,{modify},'Update'],
  169.           [BUTTON,{prevrec},'<<'],
  170.           [BUTTON,{nextrec},'>>']
  171.         ],
  172.         [COLS,
  173.           gad5:=[TEXT,'0','Records: ',FALSE,3],
  174.           [SPACEH],
  175.           [BUTTON,{packtable},'Pack table']
  176.         ]
  177.        ]
  178.  
  179.   /* Create the database object and set the table name to 'ram:address'.
  180.      The table name can also be set afterwards with the tablename() method. */
  181.   NEW table.database('ram:address')
  182.  
  183.   /* Make the DataBase engine recognize an external field type: the 'date'
  184.      field type is not a built-in one. You have to specify the identification
  185.      constant and the pointer to the PROC that builds the field object
  186.      (this informations must come with the module containing the custom
  187.      field) */
  188.   table.dbftypeadd(DBFIELD_DATE,{builder})
  189.  
  190.   /* the lock() system call is used only as a shortcut to ask if the file
  191.      'ram:address' exists or not */
  192.   IF (lk:=Lock('ram:address',ACCESS_WRITE))
  193.     UnLock(lk)
  194.     table.open() -> if the table exists then we open it
  195.   ELSE -> otherwise we create it from scratch
  196.     -> add all the needed fields
  197.     table.fields.add([DBFTAG_NAME,'First Name',
  198.                       DBFTAG_TYPE,DBFIELD_STRING,
  199.                       DBFTAG_LENGTH,50,
  200.                       NIL,NIL])
  201.     table.fields.add([DBFTAG_NAME,'Last Name',
  202.                       DBFTAG_TYPE,DBFIELD_STRING,
  203.                       DBFTAG_LENGTH,50,
  204.                       NIL,NIL])
  205.     table.fields.add([DBFTAG_NAME,'Address',
  206.                       DBFTAG_TYPE,DBFIELD_STRING,
  207.                       DBFTAG_LENGTH,50,
  208.                       NIL,NIL])
  209.     table.fields.add([DBFTAG_NAME,'Phone',
  210.                       DBFTAG_TYPE,DBFIELD_STRING,
  211.                       DBFTAG_LENGTH,30,
  212.                       NIL,NIL])
  213.     /* note that we use the external 'date' field type just as if it were
  214.        a built-in one */
  215.     table.fields.add([DBFTAG_NAME,'Date',
  216.                       DBFTAG_TYPE,DBFIELD_DATE,
  217.                       NIL,NIL])
  218.     -> create the table file
  219.     table.create()
  220.   ENDIF
  221.  
  222.   NEW dd -> this is an instance of the info structure we pass to easygui
  223.   IF dd=NIL THEN Raise("MEM")
  224.  
  225.   -> create the gui
  226.   gh:=guiinitA('Simple address book',gui,[EG_INFO,dd,NIL,NIL])
  227.   /* fill in the info structure with the gadgets pointers, and the pointer
  228.      our database object */
  229.   dd.main:=gh
  230.   dd.fn:=gad1
  231.   dd.ln:=gad2
  232.   dd.ad:=gad3
  233.   dd.ph:=gad4
  234.   dd.rc:=gad5
  235.   dd.da:=gad6
  236.   dd.table:=table
  237.  
  238.   /* update the record count gadget */
  239.   settext(gh,gad5,StringF(s,'\d',table.recordCount()))
  240.   /* if table isn't empty then we update the string gadgets with the
  241.      contents of the first record */
  242.   IF table.first() THEN updategad(dd)
  243.  
  244.   /* wait for a gui message */
  245.   WHILE res<0
  246.     Wait(gh.sig)
  247.     res:=guimessage(gh)
  248.   ENDWHILE
  249.  
  250.   table.close() -> when we are finished we close the table
  251.  
  252. EXCEPT DO
  253.   IF gh THEN cleangui(gh) -> close and dispose the gui
  254.   IF table THEN END table -> dispose the DataBase object
  255.   IF dd THEN END dd       -> dispose the info structure
  256.   explain_exception()     -> tell if something went wrong
  257. ENDPROC
  258.  
  259.