database - How to effectively check if an entry is present using HDBC in haskell? -
like mentionned in question, have unique constraint on table in want insert data. have check if data present , insert if not.
that can't seem able do. here code:
import system.environment (getargs) import database.hdbc import database.hdbc.sqlite3 import text.regex.posix import qualified data.bytestring.char8 b getfrom bstr = bstr =~ "from:.+@.+\\.(fr|com).?" :: b.bytestring getto bstr = bstr =~ "to:.+@.+\\.(fr|com).?" :: b.bytestring getsubject bstr = bstr =~ "subject:.+" :: b.bytestring main = --[dbpath, rawmail] <- getargs bstr <- b.getcontents conn <- connectsqlite3 "bmsg.db" let sqnomdest = tosql $ getto bstr sqnomexped = tosql $ getfrom bstr -- begin of problematic part -- make query know if entry if present qdbefore <- quickquery' conn "select d_id dest maildest ?" [sqnomdest] qebefore <- quickquery' conn "select e_id exped mailexped ?" [sqnomexped] -- if not insert , else return arbitrary int case qdbefore of --my check wrong since snippet go return 0 [[sqlnull]] -> run conn "insert dest(maildest) values(?)" [sqnomdest] --unique on column constraint if ran alone on present raise exeption _ -> return 0 case qebefore of [[sqlnull]] -> run conn "insert exped(mailexped) values(?)" [sqnomexped] --same here constraint _ -> return 0 commit conn --end of problematic part [[qd]] <- quickquery' conn "select d_id dest maildest ?" [sqnomdest] [[qe]] <- quickquery' conn "select e_id exped mailexped ?" [sqnomexped] run conn "insert mails(d_id, e_id, sujet, mail) values (?, ?, ?, ?)" [qd, qe, tosql $ getsubject bstr, tosql bstr] commit conn disconnect conn -- create table dest -- ( -- d_id integer primary key, -- maildest varchar(64) not null unique -- ); -- create table exped -- ( -- e_id integer primary key, -- mailexped varchar(64) not null unique -- ); -- create table mails -- ( -- m_id integer primary key, -- d_id integer not null, -- e_id integer not null, -- sujet varchar(128), -- mail text not null, -- constraint fk_dest foreign key (d_id) references dest(d_id), -- constraint fk_exped foreign key (e_id) references exped(e_id) -- );
there no error, compiles, patern match fail when give email not present.
if query has no results, empty list, not list 1 row , 1 null -- that's why case fails when insert new thing. failing case, match on empty list.
Comments
Post a Comment