考虑以下代码,可以找到一组坐标:
data Coord = Coord { lat :: Float
, lon :: Float
}
instance FromRow Coord where
fromRow = Coord
<$> field
<*> field
findSomePoints :: Connection -> Int -> IO [Coord]
findSomePoints = undefined
然后我想为一组命名的坐标定义数据类型:
data Path = Path { name :: String
, points :: [Coord]
}
instance FromRow Path where
fromRow = Path
<$> field
<*> -- PROBLEM: would like to call something like `findSomePoints conn field` here...
findPath :: Connection -> Int -> IO Path
findPath = undefined
不幸的是,我不知道如何通过查询来组合数据类型(在我的情况下,Path
和Coord
)。这样的事情是否有可能(以及如何实现?)。
答案 0 :(得分:2)
我将为数据库中的每个表编写一个数据类型和一个FromRow
实例,并将数据库代码与更改模式代码分开。 PathRow
和[Coord]
是您可以直接从数据库获得的东西。 makePath :: PathRow -> [Coord] -> Path
用于构建所需的嵌套结构,根本不需要与数据库进行交互。然后findPath
可以按照这些片段来实现。