on dev help sign in
some cleanup
3909ad8Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+26−39
@@ -150,11 +150,9 @@ Person {name = Just "Faisal", age = Just 24}
150module Squeal.PostgreSQL.Binary
150module Squeal.PostgreSQL.Binary
151 ( -- * Encoding
151 ( -- * Encoding
152 ToParam (..)
152 ToParam (..)
153− , ToColumnParam (..)
154 , ToParams (..)
153 , ToParams (..)
155 -- * Decoding
154 -- * Decoding
156 , FromValue (..)
155 , FromValue (..)
157− , FromColumnValue (..)
158 , FromRow (..)
156 , FromRow (..)
159 -- * Only
157 -- * Only
160 , Only (..)
158 , Only (..)
@@ -266,14 +264,14 @@ instance
266instance
264instance
267 ( SListI fields
265 ( SListI fields
268 , IsRecord x xs
266 , IsRecord x xs
269− , AllZip ToAliasedParam xs fields
267+ , AllZip ToField xs fields
270 , All HasAliasedOid fields
268 , All HasAliasedOid fields
271 ) => ToParam x ('PGcomposite fields) where
269 ) => ToParam x ('PGcomposite fields) where
272 toParam =
270 toParam =
273 let
271 let
274
272
275 -- encoders :: NP (Maybe :.: P) fields -> NP (K (Maybe Encoding.Encoding)) xs
273 -- encoders :: NP (Maybe :.: P) fields -> NP (K (Maybe Encoding.Encoding)) xs
276− encoders = htrans (Proxy @ToAliasedParam) toAliasedParam
274+ encoders = htrans (Proxy @ToField) toField
277
275
278 composite
276 composite
279 :: All HasAliasedOid row
277 :: All HasAliasedOid row
@@ -314,28 +312,17 @@ class HasAliasedOid (field :: (Symbol, NullityType)) where
314instance HasOid ty => HasAliasedOid (alias ::: nullity ty) where
312instance HasOid ty => HasAliasedOid (alias ::: nullity ty) where
315 aliasedOid = oid @ty
313 aliasedOid = oid @ty
316
314
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
315+class ToNullityParam (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
316+ toNullityParam :: x -> K (Maybe Encoding.Encoding) ty
335−instance ToParam x pg => ToColumnParam x ('NotNull pg) where
317+instance ToParam x pg => ToNullityParam x ('NotNull pg) where
336− toColumnParam = K . Just . unK . toParam @x @pg
318+ toNullityParam = K . Just . unK . toParam @x @pg
337−instance ToParam x pg => ToColumnParam (Maybe x) ('Null pg) where
319+instance ToParam x pg => ToNullityParam (Maybe x) ('Null pg) where
338− toColumnParam = K . fmap (unK . toParam @x @pg)
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
339
326
340-- | A `ToParams` constraint generically sequences the encodings of `Type`s
327-- | A `ToParams` constraint generically sequences the encodings of `Type`s
341-- of the fields of a tuple or record to a row of `ColumnType`s. You should
328-- 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
352 -- >>> toParams @Tuple @Params (Tuple False (Just 0))
339 -- >>> toParams @Tuple @Params (Tuple False (Just 0))
353 -- K (Just "\NUL") :* K (Just "\NUL\NUL") :* Nil
340 -- K (Just "\NUL") :* K (Just "\NUL\NUL") :* Nil
354 toParams :: x -> NP (K (Maybe Encoding.Encoding)) tys
341 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)
356 => ToParams x tys where
343 => ToParams x tys where
357 toParams
344 toParams
358− = htrans (Proxy @ToColumnParam) (toColumnParam . unI)
345+ = htrans (Proxy @ToNullityParam) (toNullityParam . unI)
359 . unZ . unSOP . from
346 . unZ . unSOP . from
360
347
361-- | A `FromValue` constraint gives a parser from the binary format of
348-- | A `FromValue` constraint gives a parser from the binary format of
@@ -468,23 +455,23 @@ instance
468 in
455 in
469 Decoding.fn (fromRow @fields @y <=< composite p)
456 Decoding.fn (fromRow @fields @y <=< composite p)
470
457
471−-- | A `FromColumnValue` constraint lifts the `FromValue` parser
458+-- | A `FromField` constraint lifts the `FromValue` parser
472-- to a decoding of a @(Symbol, NullityType)@ to a `Type`,
459-- to a decoding of a @(Symbol, NullityType)@ to a `Type`,
473-- decoding `Null`s to `Maybe`s. You should not define instances for
460-- decoding `Null`s to `Maybe`s. You should not define instances for
474−-- `FromColumnValue`, just use the provided instances.
461+-- `FromField`, just use the provided instances.
475−class FromColumnValue (pg :: (Symbol, NullityType)) (y :: (Symbol, Type)) where
462+class FromField (pg :: (Symbol, NullityType)) (y :: (Symbol, Type)) where
476− fromColumnValue
463+ fromField
477 :: K (Maybe Strict.ByteString) pg
464 :: K (Maybe Strict.ByteString) pg
478 -> (Either Strict.Text :.: P) y
465 -> (Either Strict.Text :.: P) y
479instance FromValue pg y
466instance FromValue pg y
480− => FromColumnValue (column ::: ('NotNull pg)) (column ::: y) where
467+ => FromField (column ::: ('NotNull pg)) (column ::: y) where
481− fromColumnValue = Comp . \case
468+ fromField = Comp . \case
482− K Nothing -> Left "fromColumnValue: saw NULL when expecting NOT NULL"
469+ K Nothing -> Left "fromField: saw NULL when expecting NOT NULL"
483 K (Just bytestring) -> P <$>
470 K (Just bytestring) -> P <$>
484 Decoding.valueParser (fromValue @pg @y Proxy) bytestring
471 Decoding.valueParser (fromValue @pg @y Proxy) bytestring
485instance FromValue pg y
472instance FromValue pg y
486− => FromColumnValue (column ::: 'Null pg) (column ::: Maybe y) where
473+ => FromField (column ::: 'Null pg) (column ::: Maybe y) where
487− fromColumnValue = Comp . \case
474+ fromField = Comp . \case
488 K Nothing -> Right $ P Nothing
475 K Nothing -> Right $ P Nothing
489 K (Just bytestring) -> P . Just <$>
476 K (Just bytestring) -> P . Just <$>
490 Decoding.valueParser (fromValue @pg @y Proxy) bytestring
477 Decoding.valueParser (fromValue @pg @y Proxy) bytestring
@@ -509,12 +496,12 @@ class SListI result => FromRow (result :: RelationType) y where
509instance
496instance
510 ( SListI result
497 ( SListI result
511 , IsRecord y ys
498 , IsRecord y ys
512− , AllZip FromColumnValue result ys
499+ , AllZip FromField result ys
513 ) => FromRow result y where
500 ) => FromRow result y where
514 fromRow
501 fromRow
515 = fmap fromRecord
502 = fmap fromRecord
516 . hsequence'
503 . hsequence'
517− . htrans (Proxy @FromColumnValue) fromColumnValue
504+ . htrans (Proxy @FromField) fromField
518
505
519-- | `Only` is a 1-tuple type, useful for encoding a single parameter with
506-- | `Only` is a 1-tuple type, useful for encoding a single parameter with
520-- `toParams` or decoding a single value with `fromRow`.
507-- `toParams` or decoding a single value with `fromRow`.