home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Enigma Amiga Life 106
/
EnigmaAmiga106CD.iso
/
www
/
afc
/
afc-dir
/
afc_examples.lha
/
DataBase_Examples.lha
/
Examples
/
DataBase_Example1.e
< prev
next >
Wrap
Text File
|
1998-04-20
|
9KB
|
259 lines
/*
DataBase Example - by Andrea Galimberti - (C) Brighting Brain Brothers
This example shows how to use the DataBase class. It also exploits the
dbfdate custom field, so you have to set the search path for the
dbfdate.m module. The source for the dbfdate custom field is also
provided as an example.
*/
OPT OSVERSION=37
OPT LARGE
MODULE 'afc/database',
'afc/explain_exception',
'tools/easygui',
'dos/dos',
'*dbfdate' -> here you put your path for the dbfdate
-> module
/* This is only the structure we pass to the EG_INFO tag of easygui: it
contains pointers to manipulate the gadgets and the pointer to our
database object */
OBJECT data
main:LONG ->gui
fn:LONG ->gadgdet: first name
ln:LONG ->gadgdet: last name
ad:LONG ->gadgdet: address
ph:LONG ->gadgdet: phone
rc:LONG ->gadgdet: record count
da:LONG ->gadgdet: date
table:PTR TO database ->database object
ENDOBJECT
PROC empty() IS EMPTY
/* Writes the contents of the string gadgets in a new record */
PROC add(info:PTR TO data)
DEF s[5]:STRING
info.table.add() -> put table in add mode
info.table.fields.first() -> move to the first field in table
/* set its value gettig it from the 'first name' string gadget */
info.table.fields.setv(getstr(info.main,info.fn))
info.table.fields.succ() -> move to the next field in table
info.table.fields.setv(getstr(info.main,info.ln)) -> and so on...
info.table.fields.succ()
info.table.fields.setv(getstr(info.main,info.ad))
info.table.fields.succ()
info.table.fields.setv(getstr(info.main,info.ph))
info.table.fields.succ()
info.table.fields.setv(getstr(info.main,info.da))
/* when all the fields are set, we write the new record to file */
info.table.update()
/* and we update the record count gadget, reading the number of records
directly from the database object */
settext(info.main,info.rc,StringF(s,'\d',info.table.recordCount()))
/* update all gadgets */
updategad(info)
ENDPROC
/* Modifies the current record using the contents of the string gadgets */
PROC modify(info:PTR TO data)
info.table.edit() -> put table in edit mode
info.table.fields.first() -> move to the first field in table
/* set its contents according to the string in the 'first name' string
gadget */
info.table.fields.setv(getstr(info.main,info.fn))
info.table.fields.succ() -> move to the next field in table
info.table.fields.setv(getstr(info.main,info.ln)) -> and so on...
info.table.fields.succ()
info.table.fields.setv(getstr(info.main,info.ad))
info.table.fields.succ()
info.table.fields.setv(getstr(info.main,info.ph))
info.table.fields.succ()
info.table.fields.setv(getstr(info.main,info.da))
/* when we are finished setting the fields, we write the new contents to
the current record */
info.table.update()
/* update all gadgets */
updategad(info)
ENDPROC
/* Updates the string gadget contents to reflect the contents of the fields
of the current record */
PROC updategad(info:PTR TO data)
info.table.fields.first() -> move to the first field in table
/* get the contents of the field and put it in the first string gadget */
setstr(info.main,info.fn,info.table.fields.getv())
info.table.fields.succ() -> move to the next field in table
setstr(info.main,info.ln,info.table.fields.getv()) -> and so on...
info.table.fields.succ()
setstr(info.main,info.ad,info.table.fields.getv())
info.table.fields.succ()
setstr(info.main,info.ph,info.table.fields.getv())
info.table.fields.succ()
setstr(info.main,info.da,info.table.fields.getv())
ENDPROC
/* Deletes the current record */
PROC delete(info:PTR TO data)
DEF s[5]:STRING
info.table.delete() -> delete current record
updategad(info) -> update gadget contents
/* update the record count gadget */
settext(info.main,info.rc,StringF(s,'\d',info.table.recordCount()))
ENDPROC
/* Moves to the previous record */
PROC prevrec(info:PTR TO data)
/* if the prev() method returns FALSE then we are on the first record
of the table (or the table is empty) */
IF info.table.prev() THEN updategad(info)
ENDPROC
/* Moves to the next record */
PROC nextrec(info:PTR TO data)
/* if the succ() method returns FALSE then we are on the last record of
the table (or the table is empty) */
IF info.table.succ() THEN updategad(info)
ENDPROC
/* Packs the table (this means that deleted records are physically removed
from the table data) */
PROC packtable(info:PTR TO data)
DEF s[5]:STRING
info.table.close() -> you cannot pack an open table
info.table.pack() -> pack the table
/* a simple message telling we are done */
easyguiA('Message:',[ROWS,
[TEXT,'Pack done!',NIL,TRUE,12],
[BUTTON,NIL,'ALL right']
])
info.table.open() -> reopen the packed table
/* update the record count gadget */
settext(info.main,info.rc,StringF(s,'\d',info.table.recordCount()))
/* if table isn't empty then refresh gadgets */
IF info.table.first() THEN updategad(info)
ENDPROC
PROC main() HANDLE
DEF gui, gh:PTR TO guihandle, res=-1
DEF table=NIL:PTR TO database, dd=NIL:PTR TO data
DEF gad1,gad2,gad3,gad4,gad5,gad6, buf1[50]:STRING, buf2[50]:STRING
DEF buf3[50]:STRING, buf4[30]:STRING, s[5]:STRING, buf5[10]:STRING
DEF lk=NIL
/* Well, this is our GUI definition */
gui:=[EQROWS,
gad1:=[STR,{empty},'First Name',buf1,50,30],
gad2:=[STR,{empty},'Last Name',buf2,50,30],
gad3:=[STR,{empty},'Address',buf3,50,30],
gad4:=[STR,{empty},'Phone',buf4,30,30],
gad6:=[STR,{empty},'Date',buf5,10,30],
[EQCOLS,
[BUTTON,{delete},'Delete'],
[SPACEH],
[BUTTON,{add},'Add'],
[BUTTON,{modify},'Update'],
[BUTTON,{prevrec},'<<'],
[BUTTON,{nextrec},'>>']
],
[COLS,
gad5:=[TEXT,'0','Records: ',FALSE,3],
[SPACEH],
[BUTTON,{packtable},'Pack table']
]
]
/* Create the database object and set the table name to 'ram:address'.
The table name can also be set afterwards with the tablename() method. */
NEW table.database('ram:address')
/* Make the DataBase engine recognize an external field type: the 'date'
field type is not a built-in one. You have to specify the identification
constant and the pointer to the PROC that builds the field object
(this informations must come with the module containing the custom
field) */
table.dbftypeadd(DBFIELD_DATE,{builder})
/* the lock() system call is used only as a shortcut to ask if the file
'ram:address' exists or not */
IF (lk:=Lock('ram:address',ACCESS_WRITE))
UnLock(lk)
table.open() -> if the table exists then we open it
ELSE -> otherwise we create it from scratch
-> add all the needed fields
table.fields.add([DBFTAG_NAME,'First Name',
DBFTAG_TYPE,DBFIELD_STRING,
DBFTAG_LENGTH,50,
NIL,NIL])
table.fields.add([DBFTAG_NAME,'Last Name',
DBFTAG_TYPE,DBFIELD_STRING,
DBFTAG_LENGTH,50,
NIL,NIL])
table.fields.add([DBFTAG_NAME,'Address',
DBFTAG_TYPE,DBFIELD_STRING,
DBFTAG_LENGTH,50,
NIL,NIL])
table.fields.add([DBFTAG_NAME,'Phone',
DBFTAG_TYPE,DBFIELD_STRING,
DBFTAG_LENGTH,30,
NIL,NIL])
/* note that we use the external 'date' field type just as if it were
a built-in one */
table.fields.add([DBFTAG_NAME,'Date',
DBFTAG_TYPE,DBFIELD_DATE,
NIL,NIL])
-> create the table file
table.create()
ENDIF
NEW dd -> this is an instance of the info structure we pass to easygui
IF dd=NIL THEN Raise("MEM")
-> create the gui
gh:=guiinitA('Simple address book',gui,[EG_INFO,dd,NIL,NIL])
/* fill in the info structure with the gadgets pointers, and the pointer
our database object */
dd.main:=gh
dd.fn:=gad1
dd.ln:=gad2
dd.ad:=gad3
dd.ph:=gad4
dd.rc:=gad5
dd.da:=gad6
dd.table:=table
/* update the record count gadget */
settext(gh,gad5,StringF(s,'\d',table.recordCount()))
/* if table isn't empty then we update the string gadgets with the
contents of the first record */
IF table.first() THEN updategad(dd)
/* wait for a gui message */
WHILE res<0
Wait(gh.sig)
res:=guimessage(gh)
ENDWHILE
table.close() -> when we are finished we close the table
EXCEPT DO
IF gh THEN cleangui(gh) -> close and dispose the gui
IF table THEN END table -> dispose the DataBase object
IF dd THEN END dd -> dispose the info structure
explain_exception() -> tell if something went wrong
ENDPROC