on dev help sign in
Merge pull request #105 from morphismtech/dev-join
d3bf5afEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+44−3
@@ -634,9 +634,50 @@ instance
634 = fmap fromRecord
634 = fmap fromRecord
635 . hsequence'
635 . hsequence'
636 . htrans (Proxy @FromField) fromField
636 . htrans (Proxy @FromField) fromField
637−instance (FromField (col ::: pgkey) (col ::: key), FromRow pgval val)
637+instance (FromField (col ::: pg) (col ::: hask))
638− => FromRow (col ::: pgkey ': pgval) (P (col ::: key), val) where
638+ => FromRow '[col ::: pg] (P (col ::: hask)) where
639− fromRow (key :* val) = (,) <$> (unComp . fromField) key <*> fromRow val
639+ fromRow (pg :* Nil) = (unComp . fromField) pg
640+instance
641+ ( row ~ Join row1 row2
642+ , FromRow row1 hask1
643+ , FromRow row2 hask2
644+ , SListI row
645+ ) => FromRow row (hask1, hask2) where
646+ fromRow row = case disjoin @row1 @row2 row of
647+ (row1, row2) -> (,) <$> fromRow row1 <*> fromRow row2
648+instance
649+ ( row ~ Join row1 row23
650+ , FromRow row1 hask1
651+ , FromRow row23 (hask2, hask3)
652+ , SListI row
653+ ) => FromRow row (hask1, hask2, hask3) where
654+ fromRow row = case disjoin @row1 @row23 row of
655+ (row1, row23) -> do
656+ hask1 <- fromRow row1
657+ (hask2, hask3) <- fromRow row23
658+ return (hask1, hask2, hask3)
659+instance
660+ ( row ~ Join row1 row234
661+ , FromRow row1 hask1
662+ , FromRow row234 (hask2, hask3, hask4)
663+ , SListI row
664+ ) => FromRow row (hask1, hask2, hask3, hask4) where
665+ fromRow row = case disjoin @row1 @row234 row of
666+ (row1, row234) -> do
667+ hask1 <- fromRow row1
668+ (hask2, hask3, hask4) <- fromRow row234
669+ return (hask1, hask2, hask3, hask4)
670+instance
671+ ( row ~ Join row1 row2345
672+ , FromRow row1 hask1
673+ , FromRow row2345 (hask2, hask3, hask4, hask5)
674+ , SListI row
675+ ) => FromRow row (hask1, hask2, hask3, hask4, hask5) where
676+ fromRow row = case disjoin @row1 @row2345 row of
677+ (row1, row2345) -> do
678+ hask1 <- fromRow row1
679+ (hask2, hask3, hask4, hask5) <- fromRow row2345
680+ return (hask1, hask2, hask3, hask4, hask5)
640
681
641-- | `Only` is a 1-tuple type, useful for encoding a single parameter with
682-- | `Only` is a 1-tuple type, useful for encoding a single parameter with
642-- `toParams` or decoding a single value with `fromRow`.
683-- `toParams` or decoding a single value with `fromRow`.
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+3−3
@@ -1217,7 +1217,7 @@ jsonbBuildArray
1217jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
1217jsonbBuildArray = unsafeVariadicFunction "jsonb_build_array"
1218
1218
1219unsafeRowFunction
1219unsafeRowFunction
1220− :: SOP.All SOP.Top elems
1220+ :: SOP.SListI elems
1221 => NP (Aliased (Expression outer grp commons schemas params from)) elems
1221 => NP (Aliased (Expression outer grp commons schemas params from)) elems
1222 -> [ByteString]
1222 -> [ByteString]
1223unsafeRowFunction =
1223unsafeRowFunction =
@@ -1232,7 +1232,7 @@ unsafeRowFunction =
1232-- argument list. The elements of the argument list must alternate between text
1232-- argument list. The elements of the argument list must alternate between text
1233-- and values.
1233-- and values.
1234jsonBuildObject
1234jsonBuildObject
1235− :: SOP.All SOP.Top elems
1235+ :: SOP.SListI elems
1236 => NP (Aliased (Expression outer grp commons schemas params from)) elems
1236 => NP (Aliased (Expression outer grp commons schemas params from)) elems
1237 -> Expression outer grp commons schemas params from (nullity 'PGjson)
1237 -> Expression outer grp commons schemas params from (nullity 'PGjson)
1238jsonBuildObject
1238jsonBuildObject
@@ -1245,7 +1245,7 @@ jsonBuildObject
1245-- variadic argument list. The elements of the argument list must alternate
1245-- variadic argument list. The elements of the argument list must alternate
1246-- between text and values.
1246-- between text and values.
1247jsonbBuildObject
1247jsonbBuildObject
1248− :: SOP.All SOP.Top elems
1248+ :: SOP.SListI elems
1249 => NP (Aliased (Expression outer grp commons schemas params from)) elems
1249 => NP (Aliased (Expression outer grp commons schemas params from)) elems
1250 -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1250 -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1251jsonbBuildObject
1251jsonbBuildObject
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+19−1
@@ -93,6 +93,7 @@ module Squeal.PostgreSQL.Schema
93 , DropIfConstraintsInvolve
93 , DropIfConstraintsInvolve
94 -- * Lists
94 -- * Lists
95 , Join
95 , Join
96+ , disjoin
96 , Elem
97 , Elem
97 , In
98 , In
98 , Length
99 , Length
@@ -632,6 +633,16 @@ type family Join xs ys where
632 Join '[] ys = ys
633 Join '[] ys = ys
633 Join (x ': xs) ys = x ': Join xs ys
634 Join (x ': xs) ys = x ': Join xs ys
634
635
636+-- | `disjoin` is a utility function for splitting an `NP` list into pieces.
637+disjoin
638+ :: forall xs ys expr. SListI xs
639+ => NP expr (Join xs ys)
640+ -> (NP expr xs, NP expr ys)
641+disjoin = case sList @xs of
642+ SNil -> \ys -> (Nil, ys)
643+ SCons -> \(x :* xsys) ->
644+ case disjoin xsys of (xs,ys) -> (x :* xs, ys)
645+
635-- | @Create alias x xs@ adds @alias ::: x@ to the end of @xs@ and is used in
646-- | @Create alias x xs@ adds @alias ::: x@ to the end of @xs@ and is used in
636-- `Squeal.PostgreSQL.Definition.createTable` statements and in @ALTER TABLE@
647-- `Squeal.PostgreSQL.Definition.createTable` statements and in @ALTER TABLE@
637-- `Squeal.PostgreSQL.Definition.addColumn`.
648-- `Squeal.PostgreSQL.Definition.addColumn`.
@@ -824,7 +835,14 @@ RowPG Person :: [(Symbol, NullityType)]
824= '["name" ::: 'NotNull 'PGtext, "age" ::: 'NotNull 'PGint4]
835= '["name" ::: 'NotNull 'PGtext, "age" ::: 'NotNull 'PGint4]
825-}
836-}
826type family RowPG (hask :: Type) :: RowType where
837type family RowPG (hask :: Type) :: RowType where
838+ RowPG (hask1, hask2) = Join (RowPG hask1) (RowPG hask2)
839+ RowPG (hask1, hask2, hask3) =
840+ Join (RowPG hask1) (RowPG (hask2, hask3))
841+ RowPG (hask1, hask2, hask3, hask4) =
842+ Join (RowPG hask1) (RowPG (hask2, hask3, hask4))
843+ RowPG (hask1, hask2, hask3, hask4, hask5) =
844+ Join (RowPG hask1) (RowPG (hask2, hask3, hask4, hask5))
827− RowPG (P (col ::: head), tail) = col ::: NullPG head ': RowPG tail
845+ RowPG (P (col ::: head)) = '[col ::: NullPG head]
828 RowPG hask = RowOf (RecordCodeOf hask)
846 RowPG hask = RowOf (RecordCodeOf hask)
829
847
830type family RowOf (fields :: [(Symbol, Type)]) :: RowType where
848type family RowOf (fields :: [(Symbol, Type)]) :: RowType where