[Haskell-cafe] Database relations mapping

2008-02-23 Thread Radosław Grzanka
Hi,
  I'm developing toy application to learn HDBC. I have problem with
doing relations mapping. Actually I don't know is it a problem or
feature. ;)

Anyway, I have two tables with relation between them:

(this example is simplified to the whole structure of database, you can imagine)

CREATE TABLE TD_set (
 setId INTEGER PRIMARY KEY,
 setName TEXT NOT NULL
 )

CREATE TABLE TO_card (
 cardId INTEGER PRIMARY KEY AUTOINCREMENT,
 setId INTEGER NOT NULL,   -- Relation to TD_set
 setSeq INTEGER NOT NULL
 )

(no foreign key as sqlite3 does not care anyway - at least AFAIK)

And in Haskell:

 type CardSet = String

 data Card = Card {
set :: CardSet,
seqNo:: Integer,
 }

There is no problem with filling the structure Card with seqNo but I
can't fill set. I would have to put there IO (CardSet) but I don't
want to do that as whole structure returned from my mapping function
is already inside IO monad. Can I do it?

Here is my (perfect) function - http://hpaste.org/5839 . See
getCards and some helper functions. This does not compile as (I
believe) getElement wants to return IO (CardSet)

Thank you in advance for all your input.
Radek.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Database relations mapping

2008-02-23 Thread Jonathan Cast

On 23 Feb 2008, at 3:30 AM, Radosław Grzanka wrote:


Hi,
  I'm developing toy application to learn HDBC. I have problem with
doing relations mapping. Actually I don't know is it a problem or
feature. ;)

Anyway, I have two tables with relation between them:

(this example is simplified to the whole structure of database, you  
can imagine)


CREATE TABLE TD_set (
 setId INTEGER PRIMARY KEY,
 setName TEXT NOT NULL
 )

CREATE TABLE TO_card (
 cardId INTEGER PRIMARY KEY AUTOINCREMENT,
 setId INTEGER NOT NULL,   -- Relation  
to TD_set

 setSeq INTEGER NOT NULL
 )

(no foreign key as sqlite3 does not care anyway - at least AFAIK)

And in Haskell:


type CardSet = String

data Card = Card {
   set :: CardSet,
   seqNo:: Integer,
}


There is no problem with filling the structure Card with seqNo but I
can't fill set. I would have to put there IO (CardSet) but I don't
want to do that as whole structure returned from my mapping function
is already inside IO monad. Can I do it?



Here is my (perfect) function - http://hpaste.org/5839 . See
getCards and some helper functions. This does not compile as (I
believe) getElement wants to return IO (CardSet)

Thank you in advance for all your input.


You want to put toItem into the IO monad:

toItem [cardId, setId, setSeq] = do
  set_ - getElement
  return $ Card {
set  = set_
seqNo= fromSql setSeq,
}
toItem x = fail (Unexpected result in getCards:  ++ (show x))

Then use

mapM toItem cards

instead of

return $ map toItem cards

jcc

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe