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

ShowConstantTermsSQL for ZonedTime and UTCTime #35

Closed bssstudio closed 8 years ago

bssstudio commented 8 years ago

Hello!

I am trying to use relational record to interface a postgreSQL database and I am working with timezone field in the table.

I am able to set the type mapping for sql types to treat postgreSQL timezone (without time stamp) as UTCTime or ZonedTime:

mapFromSqlDefault :: [(String, TypeQ)]
mapFromSqlDefault =
           [
           ....
           , ("timestamp",    [t| ZonedTime |])
          ....
          ]

but I run into trouble when trying to write a query:

validAtTimeRel :: ZonedTime -> String -> Relation () ExampleTable
validAtTimeRel time tsid = relation $ do
  ptd <- query exampleTable
  wheres $ (value time) .>=. ptd ! validFrom' 
    `and'` (value time) .<. ptd ! validTo'
  return ptd

I get an error:

    No instance for (ShowConstantTermsSQL ZonedTime)
      arising from a use of ‘value’
    In the first argument of ‘(.>=.)’, namely ‘(value time)’
    In the first argument of ‘and'’, namely
      ‘(value time) .>=. ptd ! validFrom'’
    In the second argument of ‘($)’, namely
      ‘(value time) .>=. ptd ! validFrom'
       `and'`
         (value time) .<. ptd ! validTo'

Can UTCTime and ZonedTime be added to relational query? Can I make an instance of ShowConstantTermsSQL for ZonedTime and UTCTime myself?

Any help is appreciated.

bssstudio commented 8 years ago

I'm currently using the following dependencies:

  - sql-words-0.1.1.0
  - HDBC-2.4.0.1
  - HDBC-postgresql-2.3.2.3
  - HDBC-session-0.1.0.0
  - names-th-0.2.0.1
  - persistable-record-0.2.0.0
  - relational-query-0.7.0.1
  - relational-query-HDBC-0.4.0.0
  - relational-schemas-0.1.2.0

I have also checked that there are no mentioned instances in relational-query-0.8.1.0 (latest on hackage at the time of writing).

khibino commented 8 years ago

I think this is resolved by merging.

https://github.com/khibino/haskell-relational-record/pull/36