on dev help sign in
some cleanup
commit3909ad8a098fc89f51a94119ebe53afd5720f3bd authorEitan Chatav <eitan.chatav@gmail.com> dateWed 11 Jul 2018, 22:27 (8 years ago) parentb856c24
1 file changed+26−39
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+26−39
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+26−39
@@ -150,11 +150,9 @@ Person {name = Just "Faisal", age = Just 24}
150150module Squeal.PostgreSQL.Binary
151151 ( -- * Encoding
152152 ToParam (..)
153− , ToColumnParam (..)
154153 , ToParams (..)
155154 -- * Decoding
156155 , FromValue (..)
157− , FromColumnValue (..)
158156 , FromRow (..)
159157 -- * Only
160158 , Only (..)
@@ -266,14 +264,14 @@ instance
266264instance
267265 ( SListI fields
268266 , IsRecord x xs
269− , AllZip ToAliasedParam xs fields
267+ , AllZip ToField xs fields
270268 , All HasAliasedOid fields
271269 ) => ToParam x ('PGcomposite fields) where
272270 toParam =
273271 let
274272
275273 -- encoders :: NP (Maybe :.: P) fields -> NP (K (Maybe Encoding.Encoding)) xs
276− encoders = htrans (Proxy @ToAliasedParam) toAliasedParam
274+ encoders = htrans (Proxy @ToField) toField
277275
278276 composite
279277 :: All HasAliasedOid row
@@ -314,28 +312,17 @@ class HasAliasedOid (field :: (Symbol, NullityType)) where
314312instance HasOid ty => HasAliasedOid (alias ::: nullity ty) where
315313 aliasedOid = oid @ty
316314
317−class ToAliasedParam (x :: (Symbol, Type)) (field :: (Symbol, NullityType)) where
318− toAliasedParam :: P x -> K (Maybe Encoding.Encoding) field
319−instance ToColumnParam x ty => ToAliasedParam (alias ::: x) (alias ::: ty) where
320− toAliasedParam (P x) = K . unK $ toColumnParam @x @ty x
321−
322−-- | A `ToColumnParam` constraint lifts the `ToParam` encoding
323−-- of a `Type` to a `NullityType`, encoding `Maybe`s to `Null`s. You should
324−-- not define instances of `ToColumnParam`, just use the provided instances.
325−class ToColumnParam (x :: Type) (ty :: NullityType) where
326− -- | >>> toColumnParam @Int16 @('NotNull 'PGint2) 0
327− -- K (Just "\NUL\NUL")
328− --
329− -- >>> toColumnParam @(Maybe Int16) @('Null 'PGint2) (Just 0)
330− -- K (Just "\NUL\NUL")
331− --
332− -- >>> toColumnParam @(Maybe Int16) @('Null 'PGint2) Nothing
333− -- K Nothing
334− toColumnParam :: x -> K (Maybe Encoding.Encoding) ty
335−instance ToParam x pg => ToColumnParam x ('NotNull pg) where
336− toColumnParam = K . Just . unK . toParam @x @pg
337−instance ToParam x pg => ToColumnParam (Maybe x) ('Null pg) where
338− toColumnParam = K . fmap (unK . toParam @x @pg)
315+class ToNullityParam (x :: Type) (ty :: NullityType) where
316+ toNullityParam :: x -> K (Maybe Encoding.Encoding) ty
317+instance ToParam x pg => ToNullityParam x ('NotNull pg) where
318+ toNullityParam = K . Just . unK . toParam @x @pg
319+instance ToParam x pg => ToNullityParam (Maybe x) ('Null pg) where
320+ toNullityParam = K . fmap (unK . toParam @x @pg)
321+
322+class ToField (x :: (Symbol, Type)) (field :: (Symbol, NullityType)) where
323+ toField :: P x -> K (Maybe Encoding.Encoding) field
324+instance ToNullityParam x ty => ToField (alias ::: x) (alias ::: ty) where
325+ toField (P x) = K . unK $ toNullityParam @x @ty x
339326
340327-- | A `ToParams` constraint generically sequences the encodings of `Type`s
341328-- of the fields of a tuple or record to a row of `ColumnType`s. You should
@@ -352,10 +339,10 @@ class SListI tys => ToParams (x :: Type) (tys :: [NullityType]) where
352339 -- >>> toParams @Tuple @Params (Tuple False (Just 0))
353340 -- K (Just "\NUL") :* K (Just "\NUL\NUL") :* Nil
354341 toParams :: x -> NP (K (Maybe Encoding.Encoding)) tys
355−instance (SListI tys, IsProductType x xs, AllZip ToColumnParam xs tys)
342+instance (SListI tys, IsProductType x xs, AllZip ToNullityParam xs tys)
356343 => ToParams x tys where
357344 toParams
358− = htrans (Proxy @ToColumnParam) (toColumnParam . unI)
345+ = htrans (Proxy @ToNullityParam) (toNullityParam . unI)
359346 . unZ . unSOP . from
360347
361348-- | A `FromValue` constraint gives a parser from the binary format of
@@ -468,23 +455,23 @@ instance
468455 in
469456 Decoding.fn (fromRow @fields @y <=< composite p)
470457
471−-- | A `FromColumnValue` constraint lifts the `FromValue` parser
458+-- | A `FromField` constraint lifts the `FromValue` parser
472459-- to a decoding of a @(Symbol, NullityType)@ to a `Type`,
473460-- decoding `Null`s to `Maybe`s. You should not define instances for
474−-- `FromColumnValue`, just use the provided instances.
475−class FromColumnValue (pg :: (Symbol, NullityType)) (y :: (Symbol, Type)) where
476− fromColumnValue
461+-- `FromField`, just use the provided instances.
462+class FromField (pg :: (Symbol, NullityType)) (y :: (Symbol, Type)) where
463+ fromField
477464 :: K (Maybe Strict.ByteString) pg
478465 -> (Either Strict.Text :.: P) y
479466instance FromValue pg y
480− => FromColumnValue (column ::: ('NotNull pg)) (column ::: y) where
481− fromColumnValue = Comp . \case
482− K Nothing -> Left "fromColumnValue: saw NULL when expecting NOT NULL"
467+ => FromField (column ::: ('NotNull pg)) (column ::: y) where
468+ fromField = Comp . \case
469+ K Nothing -> Left "fromField: saw NULL when expecting NOT NULL"
483470 K (Just bytestring) -> P <$>
484471 Decoding.valueParser (fromValue @pg @y Proxy) bytestring
485472instance FromValue pg y
486− => FromColumnValue (column ::: 'Null pg) (column ::: Maybe y) where
487− fromColumnValue = Comp . \case
473+ => FromField (column ::: 'Null pg) (column ::: Maybe y) where
474+ fromField = Comp . \case
488475 K Nothing -> Right $ P Nothing
489476 K (Just bytestring) -> P . Just <$>
490477 Decoding.valueParser (fromValue @pg @y Proxy) bytestring
@@ -509,12 +496,12 @@ class SListI result => FromRow (result :: RelationType) y where
509496instance
510497 ( SListI result
511498 , IsRecord y ys
512− , AllZip FromColumnValue result ys
499+ , AllZip FromField result ys
513500 ) => FromRow result y where
514501 fromRow
515502 = fmap fromRecord
516503 . hsequence'
517− . htrans (Proxy @FromColumnValue) fromColumnValue
504+ . htrans (Proxy @FromField) fromField
518505
519506-- | `Only` is a 1-tuple type, useful for encoding a single parameter with
520507-- `toParams` or decoding a single value with `fromRow`.