Postgresql-simple

时间:2018-07-17 20:20:25

标签: haskell postgresql-simple

考虑以下代码,可以找到一组坐标:

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

不幸的是,我不知道如何通过查询来组合数据类型(在我的情况下,PathCoord)。这样的事情是否有可能(以及如何实现?)。

1 个答案:

答案 0 :(得分:2)

我将为数据库中的每个表编写一个数据类型和一个FromRow实例,并将数据库代码与更改模式代码分开。 PathRow[Coord]是您可以直接从数据库获得的东西。 makePath :: PathRow -> [Coord] -> Path用于构建所需的嵌套结构,根本不需要与数据库进行交互。然后findPath可以按照这些片段来实现。