khibino / haskell-relational-record

This repository includes a joined query generator based on typefull relational algebra, and mapping tools between SQL values list and Haskell record type.
234 stars 36 forks source link

Provide table name at runtime when building relations #57

Open bssstudio opened 6 years ago

bssstudio commented 6 years ago

Hi! In my use case, I have multiple tables with schema that is exactly the same. What I would like to do is to provide the table name at runtime. I would use one table to generate the definitions with template haskell, but when building a relation, I would provide a different name.

nRel :: String -> Relation () MyData
nRel name = relationName name $ do
  query $ MyData.myData
  ...

Is this currently possible, or might this be a feature request?

igrep commented 6 years ago

Providing a table name as a String can break the type safety of this library. So you should not do that.

I have multiple tables with schema that is exactly the same.

I guess you should make use of HasProjection type class. Given tables with columns foo :: String and bar :: Int, you can write the query like this.

(Updated due to a type error)

nRel
  :: (HasProjection "foo" a String, HasProjection "bar" a Int)
  => Relation () a -> Relation () a
nRel tbl = relation $ do
  t <- query tbl
  wheres $ t ! #bar .=. value "some bar value"
  ...

(You may have to enable DataKinds, OverloadedLabels, and the other related extensions).