我想尝试使用esqueleto创建动态查询。理想情况下,我将有一组条件输入并输出等词查询语句。
例如
share[mkPersist sqlSettings][persistLowerCase|
MyTable
myField Int
|]
data MyCriteria
= MyFieldMin { myFieldMin :: Int }
| MyFieldMax { myFieldMax :: Int }
buildQuery :: MyCriteria -> ???
buildQuery criteria myTable =
case criteria of
MyFieldMin x -> myTable ^. MyTableMyField >=. val x -- these are equeleto query fragments
MyFieldMax x -> myTable ^. MyTableMyField <=. val x
combineQueries :: [MyCriteria] -> ?
combineQueries myCriterias =
foldr (\a b -> a &&. b) (val True) (map (\x -> buildQuery x) myCriterias)
selectStms myCriterias = do
select $ from $ \myTable -> do
where_ $ combineQueries myCriterias
return myTable
这可能与esqueleto有关吗?
编辑:好吧,我在这个问题中提供的人为设计的例子实际上可以编译,而我正在研究的实际东西不是哈哈。因此,我将对此进行更深入的研究。