|
From: Paul G. <gr...@gm...> - 2010-11-14 22:37:40
|
Hello HaskellDB users,
First of all, can anybody point some more or less real world example of
HaskellDB usage,
containing everything from defining tables and queries to actual operations
like selecting
and inserting? This would be very useful because now the only source of
knowledge for me
are API docs (which are hard to use without basic understanding) and old
short pdf file...
And more specifically my question is: a am trying to write some
helloworldish application
using haskelldb. I've made a sqlite3 database that contains blog entries. It
looks like follows:
database = "test.db"
withDB = sqliteConnect database
data Title = Title
instance FieldTag Title where fieldName _ = "title"
title = mkAttr Title
data Article = Article
instance FieldTag Article where fieldName _ = "article"
article = mkAttr Article
data Timestamp = Timestamp
instance FieldTag Timestamp where fieldName _ = "timestamp"
timestamp = mkAttr Timestamp
data UId = UId
instance FieldTag UId where fieldName _ = "id"
uid = mkAttr UId
type BlogTable = RecCons UId (Expr Int)
(RecCons Title (Expr String)
(RecCons Article (Expr String)
(RecCons Timestamp (Expr Int)
RecNil)))
blogTable :: Table BlogTable
blogTable = baseTable "blog" $
hdbMakeEntry UId #
hdbMakeEntry Title #
hdbMakeEntry Article #
hdbMakeEntry Timestamp
Next I want to query all ids (uids in my code to avoid confusion with
Prelude.id)
Now it looks like that:
type BlogUIdRec = RecCons UId Int RecNil
getAll :: IO [Record BlogUIdRec]
getAll = withDB $ \db -> query db $ do
t <- table blogTable
project $ uid << t!uid
main = do
rs <- getAll
print $ map (\ rec -> rec ! uid :: Int) rs
That works but I wonder should I always explicitly specify type when I
select fields
(rec ! uid :: Int) and is it possible to avoid creating dummy type for any
query
like BlogUIdRec? If I omit any of them my program fails to compile.
Thanks!
|