从Esqueleto查询中的时间戳字段中提取年份

时间:2018-06-13 13:16:21

标签: sql haskell esqueleto

我正在尝试使用来自Esqueleto的unsafeSqlExtractSubField来创建一个从日期中提取年份的文件,例如:

data ReportRow = ReportRow Text

userSignupData :: MonadIO m => Key User -> SqlPersistT m [ReportRow]
userSignupData _ = fmap toRow <$> select (from query)
  where
    query s =
      pure $ (extractYear $ s ^. UserCreatedAt)

    toRow yearVal = ReportRow (showText . unValue $ yearVal)

extractYear :: UnsafeSqlFunctionArgument a => a -> SqlExpr (Value Integer)
extractYear =
  unsafeSqlExtractSubField "year"

showText :: Show a => a -> Text
showText = T.pack . show

但我收到错误:

Could not deduce
  (UnsafeSqlFunctionArgument
    (expr0 (Value UTCTime)))

  arising from a use of ‘query’
from the context: MonadIO m
  bound by the type signature for:
    userSignupData :: forall (m :: * -> *).
                      MonadIO m =>
                      Key User -> SqlPersistT m [ReportRow]

The type variable ‘expr0’ is ambiguous

   |
20 | userSignupData _ = fmap toRow <$> select (from query)
   |                                                ^^^^^

我是否需要在此为UnsafeSqlFunctionArgument定义UTCTime的实例,或者我是否想将方形钉固定在圆孔中?

我没有回答说我可以在haskell级别提取日期,我想在查询中获取年份,以便我可以在里面执行SQL GROUP BY查询。

0 个答案:

没有答案