tags:

views:

80

answers:

1

I just started a new project and wanted to use HaskellDB in the beginning. I created a database with 2 columns:

create table sensor (
    service text,
    name text
);

..found out how to do the basic HaskellDB machinery (ohhh..the documentation) and wanted to do an insert. However, I wanted to do a partial insert (there are supposed to be more columns), something like:

insert into sensor (service) values ('myservice');

Translated into HaskellDB:

transaction db $ insert db SE.sensor (SE.service <<- (Just $ senService sensor))

But...that simply doesn't work. What also does not work is if I specify the column names in different order, which is not exactly conenient as well. Is there a way to do a partial insert in haskelldb?

The error codes I get are - when I just inserted a different column (the 'name') as the first one:

Couldn't match expected type `SEI.Service'
       against inferred type `SEI.Name'
  Expected type: SEI.Intsensor
  Inferred type: Database.HaskellDB.HDBRec.RecCons
                   SEI.Name (Expr String) er
When using functional dependencies to combine
  Database.HaskellDB.Query.InsertRec
    (Database.HaskellDB.HDBRec.RecCons f (e a) r)
    (Database.HaskellDB.HDBRec.RecCons f (Expr a) er),
etc..

And when I do the 'service' as the first - and only - field, I get:

Couldn't match expected type `Database.HaskellDB.HDBRec.RecCons
                                SEI.Name
                                (Expr String)
                                (Database.HaskellDB.HDBRec.RecCons
                                   SEI.Time
                                   (Expr Int)
                                   (Database.HaskellDB.HDBRec.RecCons
                                      SEI.Intval (Expr Int) Database.HaskellDB.HDBRec.RecNil))'
       against inferred type `Database.HaskellDB.HDBRec.RecNil'

(I have a couple of other columns in the table) This looks really like 'by design', unfortunately :(

A: 

You're right, that does look intentional. The HaskellDB.Query docs show that insert has a type of:

insert :: (ToPrimExprs r, ShowRecRow r, InsertRec r er) => Database -> Table er -> Record r -> IO ()

In particular, the relation InsertRec r er must hold. That's defined elsewhere by the recursive type program:

InsertRec RecNil RecNil
(InsertExpr e, InsertRec r er) => InsertRec (RecCons f (e a) r) (RecCons f (Expr a) er)

The first line is the base case. The second line is an inductive case. It really does want to walk every element of er, the table. There's no short-circuit, and no support for re-ordering. But in my own tests, I have seen this work, using _default:

insQ db = insert db test_tbl1 (c1 <<- (Just 5) # c2 << _default)

So if you want a partial insert, you can always say:

insC1 db x = insert db test_tbl1 (c1 <<- (Just x) # c2 << _default)
insC2 db x = insert db test_tbl2 (c1 << _default  # c2 <<- (Just x))

I realize this isn't everything you're looking for. It looks like InsertRec can be re-written in the style of HList, to permit more generalization. That would be an excellent contribution.

Brian Sniffen