m4dc4p / haskelldb

A library for building re-usable and composable SQL queries.
BSD 3-Clause "New" or "Revised" License
102 stars 17 forks source link

Compatibility with the seimgroup (<>) operator from Prelude & minor export change #31

Closed useronym closed 5 years ago

useronym commented 5 years ago

We define our own instance of Args for use with the PSQL function date_part:

instance Args (String -> Expr a -> Expr b) where
  arg_ n@"date_part" [] =
      \p (Expr d) -> Expr $ FunExpr n [ConstExpr $ StringLit p,d]
  arg_ n _ = error $ "Invalid function name: " ++ n

date_part :: String -> Expr (Maybe CalendarTime) -> Expr Integer
date_part = func "date_part"

Then, we can use it like so in projections:

  project
    $ F.id << event!F.id
    # F.year << date_part "year" (event!F.startDate)

There might be a better way to achieve this, I'm not sure. I'm not terribly fluent with haskelldb, but I was migrating a project from GHC 6.12 to 8.4.4 and it seems at some point arg_ became private, thus breaking our code.

m4dc4p commented 5 years ago

You only need to use the func function to define custom functions - it takes any number of arguments - they just have to be convertable to Expr.

date_part is actually an example in the func docs: https://github.com/m4dc4p/haskelldb/blob/master/src/Database/HaskellDB/Query.hs#L565.

However, I'm glad to merge the <> changes, if you'd like to update this PR.

useronym commented 5 years ago

Alright I see, thanks! I removed the commit.