From: Immanuel N. <i.n...@iu...> - 2007-10-24 21:13:22
|
Am Dienstag, den 23.10.2007, 13:54 +0200 schrieb Björn Bringert: > > Hi Immanuel, > > your withDB type looks strange. You say that it is: > > withDB :: GetRec er vr => Query (Rel er) -> IO [Record vr] > > This looks like it takes a query, runs it, and returns the results. That > is, I expect that your withDB implementation is something like this: > > withDB :: GetRec er vr => Query (Rel er) -> IO [Record vr] > withDB q = connect (... db connection arguments ...) (\db -> query db q) Actually I copied this withDB from a HaskellDB Wiki http://www.haskell.org/hawiki/HaskellDbTutorial Which appearently does not exist anymore. > > But you are then trying to use the same withDB function to do an insert, > even though the withDB in itself does a query. What I would do instead > is to have withDB be something like (not tested): > > withDB :: (Database -> IO a) -> IO a > withDB f = connect (... db connection arguments ...) f > > Then your query example would be: > > select = do withDB $ \db -> query db $ do t <- table t1 > project (c1 << t!c1) > > And the insert would be as you wrote it: > > testInsert = withDB $ \db -> insert db t1 ((c1 << constant 3) # > &#-1;&#-1; (c2 << constant "foo")) Thank you! I will try this. > > > Note that every use of withDB opens and closes a database connection,so > in real programs, you want to make sure not to do this over and over for > performance reasons. In fact I haven't considered this. Since I am not that familliar with HaskellDB. Could you sketch how to insert efficiently without redundant connection open/close? say given the list of type [(Int,String)] that should be inserted into table with two columns of appropriate types. Thanks, Immanuel |