离开加入Opaleye

时间:2017-10-20 16:07:08

标签: haskell opaleye

我一直在尝试在项目中使用Opaleye运行左连接,但我无法编译代码。我从两个"模型开始#34;表示相关的表:

首先:

data ModelA' a b = Model { primA :: a, foreignA :: b }
type ModelA = ModelA' UUID UUID
type ModelAColumn = ModelA' (Column PGUuid) (Column (Nullable PGUuid))

$(makeAdaptorAndInstance "pModelA" ''ModelA')

table :: Table ModelAColumn ModelAColumn
table = Opaleye.table "model_a" $ pModelA (ModelA (tableColumn "uuid") (tableColumn "foreign"))

还有:

data ModelB' a b = Model { primB :: a, valB :: b }
type ModelB = ModelB' UUID String
type ModelBColumn = ModelB' (Column PGUuid) (Column PGText)

$(makeAdaptorAndInstance "pModelB" ''ModelB')

table :: Table ModelBColumn ModelBColumn
table = Opaleye.table "model_b" $ pModelB (ModelB (tableColumn "uuid") (tableColumn "val"))

正如类型所反映的那样, ModelA 可以没有 ModelB 关联。

我需要一个查询来获取由foreignA == primB上的表之间的左连接给出的(ModelA,Maybe ModelB)对。我期待它看起来像:

doJoin :: Connection -> IO [(ModelA, Maybe ModelB)]
doJoin conn = runQuery conn query
  where
    query :: Query (ModelAColumn, Maybe ModelBColumn)
    query = leftJoin (queryTable ModelA.table) (queryTable ModelB.table) (\(ma, mb) -> foreignA ma .== primB mb)

但这不起作用。我还尝试了多种变体,特别是我在查询中替换了类型签名,以明确说明右侧列的可为空性:

query :: Query (ModelAColumn, (Column (Nullable PGUuid), Column (Nullable PGText))

但这失败了:

  

没有Data.Profunctor.Product.Default.Class.Default的实例   Opaleye.Internal.Join.NullMaker ModelBColumn(Column(Nullable   PGUuid),专栏(Nullable PGText)。

如何在Opaleye中进行此查询?

3 个答案:

答案 0 :(得分:3)

这里有一些误解。我在下面制作了一个完整的工作版本。

首先,leftJoin的返回类型不是

Query (ModelAColumn, Maybe ModelBColumn)

你必须做

type ModelBNullableColumn = ModelB' (Column (Nullable PGUuid))
                                    (Column (Nullable PGText))

然后使用

Query (ModelAColumn, ModelBNullableColumn)

其次,runQuery的返回类型不是

IO [(ModelA, Maybe ModelB)]

你必须做

type ModelBMaybe = ModelB' (Maybe UUID) (Maybe String)

并使用

IO [(ModelA, ModelBMaybe)]

这些差异的原因是NullableMaybe必须直接应用于ModelBColumnModelB中的每一列和值,而不是整个值

(还有一些奇怪的语法错误,如

ModelA { tableColumn "uuid", tableColumn "foreign" }

这意味着您的代码无法编译。我也解决了这些问题。)

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}

import           Opaleye hiding (table)
import qualified Opaleye
import Data.Profunctor.Product.TH
import Database.PostgreSQL.Simple hiding (Query)
import Data.UUID

data ModelA' a b = ModelA { primA :: a, foreignA :: b }
type ModelA = ModelA' UUID (Maybe UUID)
type ModelAColumn = ModelA' (Column PGUuid) (Column (Nullable PGUuid))

$(makeAdaptorAndInstance "pModelA" ''ModelA')

modelAtable :: Table ModelAColumn ModelAColumn
modelAtable = Opaleye.table "model_a" $ pModelA ModelA { primA = tableColumn "uuid", foreignA = tableColumn "foreign" }

data ModelB' a b = ModelB { primB :: a, valB :: b }
type ModelB = ModelB' UUID String
type ModelBMaybe = ModelB' (Maybe UUID) (Maybe String)
type ModelBColumn = ModelB' (Column PGUuid) (Column PGText)
type ModelBNullableColumn = ModelB' (Column (Nullable PGUuid)) (Column (Nullable PGText))

$(makeAdaptorAndInstance "pModelB" ''ModelB')

modelBtable :: Table ModelBColumn ModelBColumn
modelBtable = Opaleye.table "model_b" $ pModelB ModelB { primB = tableColumn "uuid", valB = tableColumn "val" }

doJoin :: Connection -> IO [(ModelA, ModelBMaybe)]
doJoin conn = runQuery conn query
  where
    query :: Query (ModelAColumn, ModelBNullableColumn)
    query = leftJoin (queryTable modelAtable) (queryTable modelBtable) (\(ma, mb) -> matchNullable (pgBool False) (.== primB mb) (foreignA ma))

main :: IO ()
main = return ()

答案 1 :(得分:0)

尝试更改以下内容:

query :: Query (ModelAColumn, Maybe ModelBColumn)

query :: Query ((Column PGUuid) (Column (Nullable PGUuid))
               , (Column (Nullable PGUuid)) (Column (Nullable PGText)))

尝试进行类型检查,不要在其上调用runQuery。一旦有效,请回到剩下的解决方案。

答案 2 :(得分:0)

您需要使用Functional Join,特别是leftJoinF

查看此https://hackage.haskell.org/package/opaleye-0.5.4.0/docs/Opaleye-FunctionalJoin.html

您需要在两个表上提供select的结果作为它的第4和第5个参数。 相当于foreignA == primB作为第三个参数。

对于您的第二个参数,您必须指定一个默认值,只要返回的Maybe ModelBNothing

,就应该使用该值