on dev help sign in
Oid newtype
commit163dca82d53bf816bb50afd0e98cbe107a333bcf authorEitan Chatav <eitan@EEM109LMBP-1.local> dateWed 21 Aug 2019, 11:43 (7 years ago) parentf0f3e4f
2 files changed+56−37
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+31−29 squeal-postgresql/src/Squeal/PostgreSQL/PG.hs+25−8
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+31−29
@@ -276,7 +276,7 @@ instance ToParam Bool 'PGbool where toParam = K . Encoding.bool
276276instance ToParam Int16 'PGint2 where toParam = K . Encoding.int2_int16
277277instance ToParam Int32 'PGint4 where toParam = K . Encoding.int4_int32
278278instance ToParam Int64 'PGint8 where toParam = K . Encoding.int8_int64
279−instance ToParam Word32 'PGoid where toParam = K . Encoding.int4_word32
279+instance ToParam Oid 'PGoid where toParam = K . Encoding.int4_word32 . getOid
280280instance ToParam Float 'PGfloat4 where toParam = K . Encoding.float4
281281instance ToParam Double 'PGfloat8 where toParam = K . Encoding.float8
282282instance ToParam Scientific 'PGnumeric where toParam = K . Encoding.numeric
@@ -312,21 +312,23 @@ instance Aeson.ToJSON x => ToParam (Jsonb x) 'PGjsonb where
312312instance (ToNullityParam x ty, ty ~ nullity pg, HasOid pg)
313313 => ToParam (VarArray [x]) ('PGvararray ty) where
314314 toParam = K
315− . Encoding.array_foldable (oidOf @pg) (unK . toNullityParam @x @ty)
315+ . Encoding.array_foldable
316+ (getOid (oidOf @pg)) (unK . toNullityParam @x @ty)
316317 . getVarArray
317318instance (ToParam x pg, HasOid pg)
318319 => ToParam (VarArray (Vector x)) ('PGvararray ('NotNull pg)) where
319320 toParam = K
320− . Encoding.array_vector (oidOf @pg) (unK . toParam @x @pg)
321+ . Encoding.array_vector (getOid (oidOf @pg)) (unK . toParam @x @pg)
321322 . getVarArray
322323instance (ToParam x pg, HasOid pg)
323324 => ToParam (VarArray (Vector (Maybe x))) ('PGvararray ('Null pg)) where
324325 toParam = K
325− . Encoding.nullableArray_vector (oidOf @pg) (unK . toParam @x @pg)
326+ . Encoding.nullableArray_vector
327+ (getOid (oidOf @pg)) (unK . toParam @x @pg)
326328 . getVarArray
327329instance (ToFixArray x dims ty, ty ~ nullity pg, HasOid pg)
328330 => ToParam (FixArray x) ('PGfixarray dims ty) where
329− toParam = K . Encoding.array (oidOf @pg)
331+ toParam = K . Encoding.array (getOid (oidOf @pg))
330332 . unK . unK . toFixArray @x @dims @ty . getFixArray
331333instance
332334 ( IsEnumType x
@@ -380,7 +382,7 @@ instance
380382 => K (Maybe Encoding.Encoding) field
381383 -> Encoding.Encoding
382384 each (K field :: K (Maybe Encoding.Encoding) field) =
383− word32BE (aliasedOid @field)
385+ word32BE (getOid (aliasedOid @field))
384386 <> case field of
385387 Nothing -> int64BE (-1)
386388 Just value ->
@@ -397,32 +399,32 @@ instance
397399-- >>> :set -XTypeApplications
398400-- >>> oidOf @'PGbool
399401-- 16
400−class HasOid (ty :: PGType) where oidOf :: Word32
401−instance HasOid 'PGbool where oidOf = 16
402−instance HasOid 'PGint2 where oidOf = 21
403−instance HasOid 'PGint4 where oidOf = 23
404−instance HasOid 'PGint8 where oidOf = 20
405−instance HasOid 'PGnumeric where oidOf = 1700
406−instance HasOid 'PGfloat4 where oidOf = 700
407−instance HasOid 'PGfloat8 where oidOf = 701
408−instance HasOid ('PGchar n) where oidOf = 18
409−instance HasOid ('PGvarchar n) where oidOf = 1043
410−instance HasOid 'PGtext where oidOf = 25
411−instance HasOid 'PGbytea where oidOf = 17
412−instance HasOid 'PGtimestamp where oidOf = 1114
413−instance HasOid 'PGtimestamptz where oidOf = 1184
414−instance HasOid 'PGdate where oidOf = 1082
415−instance HasOid 'PGtime where oidOf = 1083
416−instance HasOid 'PGtimetz where oidOf = 1266
417−instance HasOid 'PGinterval where oidOf = 1186
418−instance HasOid 'PGuuid where oidOf = 2950
419−instance HasOid 'PGinet where oidOf = 869
420−instance HasOid 'PGjson where oidOf = 114
421−instance HasOid 'PGjsonb where oidOf = 3802
402+class HasOid (ty :: PGType) where oidOf :: Oid
403+instance HasOid 'PGbool where oidOf = Oid 16
404+instance HasOid 'PGint2 where oidOf = Oid 21
405+instance HasOid 'PGint4 where oidOf = Oid 23
406+instance HasOid 'PGint8 where oidOf = Oid 20
407+instance HasOid 'PGnumeric where oidOf = Oid 1700
408+instance HasOid 'PGfloat4 where oidOf = Oid 700
409+instance HasOid 'PGfloat8 where oidOf = Oid 701
410+instance HasOid ('PGchar n) where oidOf = Oid 18
411+instance HasOid ('PGvarchar n) where oidOf = Oid 1043
412+instance HasOid 'PGtext where oidOf = Oid 25
413+instance HasOid 'PGbytea where oidOf = Oid 17
414+instance HasOid 'PGtimestamp where oidOf = Oid 1114
415+instance HasOid 'PGtimestamptz where oidOf = Oid 1184
416+instance HasOid 'PGdate where oidOf = Oid 1082
417+instance HasOid 'PGtime where oidOf = Oid 1083
418+instance HasOid 'PGtimetz where oidOf = Oid 1266
419+instance HasOid 'PGinterval where oidOf = Oid 1186
420+instance HasOid 'PGuuid where oidOf = Oid 2950
421+instance HasOid 'PGinet where oidOf = Oid 869
422+instance HasOid 'PGjson where oidOf = Oid 114
423+instance HasOid 'PGjsonb where oidOf = Oid 3802
422424
423425-- | Lifts a `HasOid` constraint to a field.
424426class HasAliasedOid (field :: (Symbol, NullityType)) where
425− aliasedOid :: Word32
427+ aliasedOid :: Oid
426428instance HasOid ty => HasAliasedOid (alias ::: nullity ty) where
427429 aliasedOid = oidOf @ty
428430
squeal-postgresql/src/Squeal/PostgreSQL/PG.hs+25−8
@@ -10,10 +10,12 @@ Stability: experimental
1010-}
1111{-# LANGUAGE
1212 AllowAmbiguousTypes
13+ , DeriveAnyClass
1314 , DeriveFoldable
1415 , DeriveFunctor
1516 , DeriveGeneric
1617 , DeriveTraversable
18+ , DerivingStrategies
1719 , DefaultSignatures
1820 , FlexibleContexts
1921 , FlexibleInstances
@@ -45,6 +47,7 @@ module Squeal.PostgreSQL.PG
4547 , Enumerated (..)
4648 , VarArray (..)
4749 , FixArray (..)
50+ , Oid (..)
4851 -- * Type families
4952 , LabelsPG
5053 , DimPG
@@ -105,7 +108,7 @@ type instance PG Int32 = 'PGint4
105108-- | `PGint8`
106109type instance PG Int64 = 'PGint8
107110-- | `PGint2`
108−type instance PG Word32 = 'PGoid
111+type instance PG Oid = 'PGoid
109112-- | `PGnumeric`
110113type instance PG Scientific = 'PGnumeric
111114-- | `PGfloat4`
@@ -299,7 +302,8 @@ withConnection "host=localhost port=5432 dbname=exampledb" $ do
299302True
300303-}
301304newtype Money = Money { cents :: Int64 }
302− deriving (Eq, Ord, Show, Read, GHC.Generic)
305+ deriving stock (Eq, Ord, Show, Read, GHC.Generic)
306+ deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
303307-- | `PGmoney`
304308type instance PG Money = 'PGmoney
305309
@@ -307,7 +311,8 @@ type instance PG Money = 'PGmoney
307311type it's applied to should be stored as a `PGjson`.
308312-}
309313newtype Json hask = Json {getJson :: hask}
310− deriving (Eq, Ord, Show, Read, GHC.Generic)
314+ deriving stock (Eq, Ord, Show, Read, GHC.Generic)
315+ deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
311316-- | `PGjson`
312317type instance PG (Json hask) = 'PGjson
313318
@@ -315,7 +320,8 @@ type instance PG (Json hask) = 'PGjson
315320type it's applied to should be stored as a `PGjsonb`.
316321-}
317322newtype Jsonb hask = Jsonb {getJsonb :: hask}
318− deriving (Eq, Ord, Show, Read, GHC.Generic)
323+ deriving stock (Eq, Ord, Show, Read, GHC.Generic)
324+ deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
319325-- | `PGjsonb`
320326type instance PG (Jsonb hask) = 'PGjsonb
321327
@@ -323,7 +329,8 @@ type instance PG (Jsonb hask) = 'PGjsonb
323329type it's applied to should be stored as a `PGcomposite`.
324330-}
325331newtype Composite record = Composite {getComposite :: record}
326− deriving (Eq, Ord, Show, Read, GHC.Generic)
332+ deriving stock (Eq, Ord, Show, Read, GHC.Generic)
333+ deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
327334-- | `PGcomposite` @(@`RowPG` @hask)@
328335type instance PG (Composite hask) = 'PGcomposite (RowPG hask)
329336
@@ -331,7 +338,8 @@ type instance PG (Composite hask) = 'PGcomposite (RowPG hask)
331338type it's applied to should be stored as a `PGenum`.
332339-}
333340newtype Enumerated enum = Enumerated {getEnumerated :: enum}
334− deriving (Eq, Ord, Show, Read, GHC.Generic)
341+ deriving stock (Eq, Ord, Show, Read, GHC.Generic)
342+ deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
335343-- | `PGenum` @(@`LabelsPG` @hask)@
336344type instance PG (Enumerated hask) = 'PGenum (LabelsPG hask)
337345
@@ -343,7 +351,8 @@ PG (VarArray (Vector Double)) :: PGType
343351= 'PGvararray ('NotNull 'PGfloat8)
344352-}
345353newtype VarArray arr = VarArray {getVarArray :: arr}
346− deriving (Eq, Ord, Show, Read, GHC.Generic)
354+ deriving stock (Eq, Ord, Show, Read, GHC.Generic)
355+ deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
347356-- | `PGvararray` @(@`NullPG` @hask)@
348357type instance PG (VarArray (Vector hask)) = 'PGvararray (NullPG hask)
349358type instance PG (VarArray [hask]) = 'PGvararray (NullPG hask)
@@ -356,6 +365,14 @@ PG (FixArray ((Double, Double), (Double, Double))) :: PGType
356365= 'PGfixarray '[2, 2] ('NotNull 'PGfloat8)
357366-}
358367newtype FixArray arr = FixArray {getFixArray :: arr}
359− deriving (Eq, Ord, Show, Read, GHC.Generic)
368+ deriving stock (Eq, Ord, Show, Read, GHC.Generic)
369+ deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
360370-- | `PGfixarray` @(@`DimPG` @hask) (@`FixPG` @hask)@
361371type instance PG (FixArray hask) = 'PGfixarray (DimPG hask) (FixPG hask)
372+
373+{- | Object identifiers (`Oid`s) are used internally by PostgreSQL
374+as primary keys for various system tables.
375+-}
376+newtype Oid = Oid { getOid :: Word32 }
377+ deriving stock (Eq, Ord, Show, Read, GHC.Generic)
378+ deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)