on dev help sign in
Oid newtype
163dca8Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Binary.hs+31−29
@@ -276,7 +276,7 @@ instance ToParam Bool 'PGbool where toParam = K . Encoding.bool
276instance ToParam Int16 'PGint2 where toParam = K . Encoding.int2_int16
276instance ToParam Int16 'PGint2 where toParam = K . Encoding.int2_int16
277instance ToParam Int32 'PGint4 where toParam = K . Encoding.int4_int32
277instance ToParam Int32 'PGint4 where toParam = K . Encoding.int4_int32
278instance ToParam Int64 'PGint8 where toParam = K . Encoding.int8_int64
278instance 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
280instance ToParam Float 'PGfloat4 where toParam = K . Encoding.float4
280instance ToParam Float 'PGfloat4 where toParam = K . Encoding.float4
281instance ToParam Double 'PGfloat8 where toParam = K . Encoding.float8
281instance ToParam Double 'PGfloat8 where toParam = K . Encoding.float8
282instance ToParam Scientific 'PGnumeric where toParam = K . Encoding.numeric
282instance ToParam Scientific 'PGnumeric where toParam = K . Encoding.numeric
@@ -312,21 +312,23 @@ instance Aeson.ToJSON x => ToParam (Jsonb x) 'PGjsonb where
312instance (ToNullityParam x ty, ty ~ nullity pg, HasOid pg)
312instance (ToNullityParam x ty, ty ~ nullity pg, HasOid pg)
313 => ToParam (VarArray [x]) ('PGvararray ty) where
313 => ToParam (VarArray [x]) ('PGvararray ty) where
314 toParam = K
314 toParam = K
315+ . Encoding.array_foldable
315− . Encoding.array_foldable (oidOf @pg) (unK . toNullityParam @x @ty)
316+ (getOid (oidOf @pg)) (unK . toNullityParam @x @ty)
316 . getVarArray
317 . getVarArray
317instance (ToParam x pg, HasOid pg)
318instance (ToParam x pg, HasOid pg)
318 => ToParam (VarArray (Vector x)) ('PGvararray ('NotNull pg)) where
319 => ToParam (VarArray (Vector x)) ('PGvararray ('NotNull pg)) where
319 toParam = K
320 toParam = K
320− . Encoding.array_vector (oidOf @pg) (unK . toParam @x @pg)
321+ . Encoding.array_vector (getOid (oidOf @pg)) (unK . toParam @x @pg)
321 . getVarArray
322 . getVarArray
322instance (ToParam x pg, HasOid pg)
323instance (ToParam x pg, HasOid pg)
323 => ToParam (VarArray (Vector (Maybe x))) ('PGvararray ('Null pg)) where
324 => ToParam (VarArray (Vector (Maybe x))) ('PGvararray ('Null pg)) where
324 toParam = K
325 toParam = K
325− . Encoding.nullableArray_vector (oidOf @pg) (unK . toParam @x @pg)
326+ . Encoding.nullableArray_vector
327+ (getOid (oidOf @pg)) (unK . toParam @x @pg)
326 . getVarArray
328 . getVarArray
327instance (ToFixArray x dims ty, ty ~ nullity pg, HasOid pg)
329instance (ToFixArray x dims ty, ty ~ nullity pg, HasOid pg)
328 => ToParam (FixArray x) ('PGfixarray dims ty) where
330 => ToParam (FixArray x) ('PGfixarray dims ty) where
329− toParam = K . Encoding.array (oidOf @pg)
331+ toParam = K . Encoding.array (getOid (oidOf @pg))
330 . unK . unK . toFixArray @x @dims @ty . getFixArray
332 . unK . unK . toFixArray @x @dims @ty . getFixArray
331instance
333instance
332 ( IsEnumType x
334 ( IsEnumType x
@@ -380,7 +382,7 @@ instance
380 => K (Maybe Encoding.Encoding) field
382 => K (Maybe Encoding.Encoding) field
381 -> Encoding.Encoding
383 -> Encoding.Encoding
382 each (K field :: K (Maybe Encoding.Encoding) field) =
384 each (K field :: K (Maybe Encoding.Encoding) field) =
383− word32BE (aliasedOid @field)
385+ word32BE (getOid (aliasedOid @field))
384 <> case field of
386 <> case field of
385 Nothing -> int64BE (-1)
387 Nothing -> int64BE (-1)
386 Just value ->
388 Just value ->
@@ -397,32 +399,32 @@ instance
397-- >>> :set -XTypeApplications
399-- >>> :set -XTypeApplications
398-- >>> oidOf @'PGbool
400-- >>> oidOf @'PGbool
399-- 16
401-- 16
400−class HasOid (ty :: PGType) where oidOf :: Word32
402+class HasOid (ty :: PGType) where oidOf :: Oid
401−instance HasOid 'PGbool where oidOf = 16
403+instance HasOid 'PGbool where oidOf = Oid 16
402−instance HasOid 'PGint2 where oidOf = 21
404+instance HasOid 'PGint2 where oidOf = Oid 21
403−instance HasOid 'PGint4 where oidOf = 23
405+instance HasOid 'PGint4 where oidOf = Oid 23
404−instance HasOid 'PGint8 where oidOf = 20
406+instance HasOid 'PGint8 where oidOf = Oid 20
405−instance HasOid 'PGnumeric where oidOf = 1700
407+instance HasOid 'PGnumeric where oidOf = Oid 1700
406−instance HasOid 'PGfloat4 where oidOf = 700
408+instance HasOid 'PGfloat4 where oidOf = Oid 700
407−instance HasOid 'PGfloat8 where oidOf = 701
409+instance HasOid 'PGfloat8 where oidOf = Oid 701
408−instance HasOid ('PGchar n) where oidOf = 18
410+instance HasOid ('PGchar n) where oidOf = Oid 18
409−instance HasOid ('PGvarchar n) where oidOf = 1043
411+instance HasOid ('PGvarchar n) where oidOf = Oid 1043
410−instance HasOid 'PGtext where oidOf = 25
412+instance HasOid 'PGtext where oidOf = Oid 25
411−instance HasOid 'PGbytea where oidOf = 17
413+instance HasOid 'PGbytea where oidOf = Oid 17
412−instance HasOid 'PGtimestamp where oidOf = 1114
414+instance HasOid 'PGtimestamp where oidOf = Oid 1114
413−instance HasOid 'PGtimestamptz where oidOf = 1184
415+instance HasOid 'PGtimestamptz where oidOf = Oid 1184
414−instance HasOid 'PGdate where oidOf = 1082
416+instance HasOid 'PGdate where oidOf = Oid 1082
415−instance HasOid 'PGtime where oidOf = 1083
417+instance HasOid 'PGtime where oidOf = Oid 1083
416−instance HasOid 'PGtimetz where oidOf = 1266
418+instance HasOid 'PGtimetz where oidOf = Oid 1266
417−instance HasOid 'PGinterval where oidOf = 1186
419+instance HasOid 'PGinterval where oidOf = Oid 1186
418−instance HasOid 'PGuuid where oidOf = 2950
420+instance HasOid 'PGuuid where oidOf = Oid 2950
419−instance HasOid 'PGinet where oidOf = 869
421+instance HasOid 'PGinet where oidOf = Oid 869
420−instance HasOid 'PGjson where oidOf = 114
422+instance HasOid 'PGjson where oidOf = Oid 114
421−instance HasOid 'PGjsonb where oidOf = 3802
423+instance HasOid 'PGjsonb where oidOf = Oid 3802
422
424
423-- | Lifts a `HasOid` constraint to a field.
425-- | Lifts a `HasOid` constraint to a field.
424class HasAliasedOid (field :: (Symbol, NullityType)) where
426class HasAliasedOid (field :: (Symbol, NullityType)) where
425− aliasedOid :: Word32
427+ aliasedOid :: Oid
426instance HasOid ty => HasAliasedOid (alias ::: nullity ty) where
428instance HasOid ty => HasAliasedOid (alias ::: nullity ty) where
427 aliasedOid = oidOf @ty
429 aliasedOid = oidOf @ty
428
430
squeal-postgresql/src/Squeal/PostgreSQL/PG.hs+25−8
@@ -10,10 +10,12 @@ Stability: experimental
10-}
10-}
11{-# LANGUAGE
11{-# LANGUAGE
12 AllowAmbiguousTypes
12 AllowAmbiguousTypes
13+ , DeriveAnyClass
13 , DeriveFoldable
14 , DeriveFoldable
14 , DeriveFunctor
15 , DeriveFunctor
15 , DeriveGeneric
16 , DeriveGeneric
16 , DeriveTraversable
17 , DeriveTraversable
18+ , DerivingStrategies
17 , DefaultSignatures
19 , DefaultSignatures
18 , FlexibleContexts
20 , FlexibleContexts
19 , FlexibleInstances
21 , FlexibleInstances
@@ -45,6 +47,7 @@ module Squeal.PostgreSQL.PG
45 , Enumerated (..)
47 , Enumerated (..)
46 , VarArray (..)
48 , VarArray (..)
47 , FixArray (..)
49 , FixArray (..)
50+ , Oid (..)
48 -- * Type families
51 -- * Type families
49 , LabelsPG
52 , LabelsPG
50 , DimPG
53 , DimPG
@@ -105,7 +108,7 @@ type instance PG Int32 = 'PGint4
105-- | `PGint8`
108-- | `PGint8`
106type instance PG Int64 = 'PGint8
109type instance PG Int64 = 'PGint8
107-- | `PGint2`
110-- | `PGint2`
108−type instance PG Word32 = 'PGoid
111+type instance PG Oid = 'PGoid
109-- | `PGnumeric`
112-- | `PGnumeric`
110type instance PG Scientific = 'PGnumeric
113type instance PG Scientific = 'PGnumeric
111-- | `PGfloat4`
114-- | `PGfloat4`
@@ -299,7 +302,8 @@ withConnection "host=localhost port=5432 dbname=exampledb" $ do
299True
302True
300-}
303-}
301newtype Money = Money { cents :: Int64 }
304newtype 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)
303-- | `PGmoney`
307-- | `PGmoney`
304type instance PG Money = 'PGmoney
308type instance PG Money = 'PGmoney
305
309
@@ -307,7 +311,8 @@ type instance PG Money = 'PGmoney
307type it's applied to should be stored as a `PGjson`.
311type it's applied to should be stored as a `PGjson`.
308-}
312-}
309newtype Json hask = Json {getJson :: hask}
313newtype 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)
311-- | `PGjson`
316-- | `PGjson`
312type instance PG (Json hask) = 'PGjson
317type instance PG (Json hask) = 'PGjson
313
318
@@ -315,7 +320,8 @@ type instance PG (Json hask) = 'PGjson
315type it's applied to should be stored as a `PGjsonb`.
320type it's applied to should be stored as a `PGjsonb`.
316-}
321-}
317newtype Jsonb hask = Jsonb {getJsonb :: hask}
322newtype 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)
319-- | `PGjsonb`
325-- | `PGjsonb`
320type instance PG (Jsonb hask) = 'PGjsonb
326type instance PG (Jsonb hask) = 'PGjsonb
321
327
@@ -323,7 +329,8 @@ type instance PG (Jsonb hask) = 'PGjsonb
323type it's applied to should be stored as a `PGcomposite`.
329type it's applied to should be stored as a `PGcomposite`.
324-}
330-}
325newtype Composite record = Composite {getComposite :: record}
331newtype 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)
327-- | `PGcomposite` @(@`RowPG` @hask)@
334-- | `PGcomposite` @(@`RowPG` @hask)@
328type instance PG (Composite hask) = 'PGcomposite (RowPG hask)
335type instance PG (Composite hask) = 'PGcomposite (RowPG hask)
329
336
@@ -331,7 +338,8 @@ type instance PG (Composite hask) = 'PGcomposite (RowPG hask)
331type it's applied to should be stored as a `PGenum`.
338type it's applied to should be stored as a `PGenum`.
332-}
339-}
333newtype Enumerated enum = Enumerated {getEnumerated :: enum}
340newtype 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)
335-- | `PGenum` @(@`LabelsPG` @hask)@
343-- | `PGenum` @(@`LabelsPG` @hask)@
336type instance PG (Enumerated hask) = 'PGenum (LabelsPG hask)
344type instance PG (Enumerated hask) = 'PGenum (LabelsPG hask)
337
345
@@ -343,7 +351,8 @@ PG (VarArray (Vector Double)) :: PGType
343= 'PGvararray ('NotNull 'PGfloat8)
351= 'PGvararray ('NotNull 'PGfloat8)
344-}
352-}
345newtype VarArray arr = VarArray {getVarArray :: arr}
353newtype 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)
347-- | `PGvararray` @(@`NullPG` @hask)@
356-- | `PGvararray` @(@`NullPG` @hask)@
348type instance PG (VarArray (Vector hask)) = 'PGvararray (NullPG hask)
357type instance PG (VarArray (Vector hask)) = 'PGvararray (NullPG hask)
349type instance PG (VarArray [hask]) = 'PGvararray (NullPG hask)
358type instance PG (VarArray [hask]) = 'PGvararray (NullPG hask)
@@ -356,6 +365,14 @@ PG (FixArray ((Double, Double), (Double, Double))) :: PGType
356= 'PGfixarray '[2, 2] ('NotNull 'PGfloat8)
365= 'PGfixarray '[2, 2] ('NotNull 'PGfloat8)
357-}
366-}
358newtype FixArray arr = FixArray {getFixArray :: arr}
367newtype 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)
360-- | `PGfixarray` @(@`DimPG` @hask) (@`FixPG` @hask)@
370-- | `PGfixarray` @(@`DimPG` @hask) (@`FixPG` @hask)@
361type instance PG (FixArray hask) = 'PGfixarray (DimPG hask) (FixPG hask)
371type 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)