[REBOL] Re: The all function.
From: louisaturk::eudoramail::com at: 14-Apr-2002 21:00
Anton and Joel,
How thankful I am for you and the others on this list. You all have saved
me so much time. And, frankly, I probably wouldn't be able to get past
problems like this without you.
From studying what both of you have said, I think that the root problem
may be that I have a basic design flaw in my database. This is (I think)
what has confused me, and also is perhaps what has made it difficult for
you to understand my problem. I think I only have one key but need two
keys. (Or maybe I don't need any keys at all! Is only one key per record
allowed?)
Here are the first few records from the database:
>> probe database
make hash! ["bbi"
make object! [
code: "bbi"
chknum: "298"
date: 26-Jan-2001
amount: $40.00
special: $0.00
] "bbi"
make object! [
code: "bbi"
chknum: "315"
date: 9-Feb-2001
amount: $40.00
special: $0.00
] "boo"
make object! [
code: "boo"
chknum: "9898"
date: 19-Jan-2001
amount: $100.00
special: $0.00
] "boo"
make object! [
code: "boo"
chknum: ""
date: 20-Apr-2001
amount: $100.00
special: $0.00
] "bho"
make object! [
code: "bho"
chknum: "5259"
date: 27-Jul-2001
amount: $100.00
special: $0.00
What I am wanting to do is to make sure that the record is not already in
the database before I insert it. In other words, no duplicate _records_
are allowed. Note, however, that although the records are all unique, the
code data is not necessarily unique, nor is the chknum data. There may be
many records with the same code, and also many records with the same
chknum. Only the code / chknum combination is unique.
So, do I perhaps need the records to look like this?:
] "bho" "5259" ; <===<<< TWO KEYS.
make object! [
code: "bho"
chknum: "5259"
date: 27-Jul-2001
amount: $100.00
special: $0.00
Would not this line then return none if the unique record does not exist,
and return the chknum otherswise?:
all [find database code find database chknum]
Am I correct about all this? If so, won't the following functions have to
be rewritten?:
load-data: has [data] [
if exists? db-file [
data: reduce load/all db-file
clear database
if data [
foreach item data [
item: make record item
repend database [item/code item] ;item/key_field item
]
]
database: make hash! database
]
]
update-key-data: func [code-old code-new /local record] [
record: find database code-old
if none? record [alert reform ["Missing record:" code-old] exit]
insert clear first record code-new
]
save-data: has [data] [
data: copy []
foreach [key obj] database [
append/only data obj 5 ; number of fields in each record
]
save db-file data
]
find-data: func [code] [select database code]
remove-data: func [code] [remove/part find database code 2]
insert-data: func [code' chknum' date' amount' special'] [
repend database [
code'
make record [
code: code'
chknum: chknum'
date: date'
amount: amount'
special: special'
]
]
]
These are the functions that Carl wrote for me several years ago for a
database that only had one key. Thanks again Carl! They have served me
well in several databases. This is my first need for two keys.
Thanks again for your help.
Louis