git.iko.soy/typeable/postgresql-simple
on master help sign in
Fix TypeInfo for the _record type

This issue was exposed in #153. I don't like the fact that this change means the information in the static table doesn't entirely agree with what would otherwise be fetched dynamically by the Database.PostgreSQL.Simple.TypeInfo module, but this does appear to be a special case, and handling it here does seem to be less worse then handling it there. Plus, now GenTypeInfo should be able to deal with array types, so we probably should strongly consider moving at least some of them into the static table.

commit28f469b287a6dfedf9779438028bceb0ae1f8976 authorLeon P Smith <leon@melding-monads.com> dateWed 15 Jul 2015, 15:53 (11 years ago) parent2e1ce34
2 files changed+43−11
src/Database/PostgreSQL/Simple/TypeInfo/Static.hs+11 tools/GenTypeInfo.hs+32−11
src/Database/PostgreSQL/Simple/TypeInfo/Static.hs+11
@@ -63,6 +63,7 @@ module Database.PostgreSQL.Simple.TypeInfo.Static
6363 , refcursor
6464 , record
6565 , void
66+ , array_record
6667 , uuid
6768 , json
6869 , jsonb
@@ -118,6 +119,7 @@ staticTypeInfo (Oid x) = case x of
118119 1790 -> Just refcursor
119120 2249 -> Just record
120121 2278 -> Just void
122+ 2287 -> Just array_record
121123 2950 -> Just uuid
122124 114 -> Just json
123125 3802 -> Just jsonb
@@ -483,6 +485,15 @@ void = Basic {
483485 typname = "void"
484486 }
485487
488+array_record :: TypeInfo
489+array_record = Array {
490+ typoid = Oid 2287,
491+ typcategory = 'P',
492+ typdelim = ',',
493+ typname = "_record",
494+ typelem = record
495+ }
496+
486497uuid :: TypeInfo
487498uuid = Basic {
488499 typoid = Oid 2950,
tools/GenTypeInfo.hs+32−11
@@ -125,6 +125,7 @@ numeric
125125refcursor
126126record
127127void
128+_record array_record
128129uuid
129130json
130131jsonb
@@ -187,6 +188,35 @@ renderElem byOid elemOid
187188 Nothing -> error ("oid not found: " ++ show elemOid)
188189 Just x -> "Just " ++ bs (typname x)
189190
191+renderTypeInfo :: OidMap -> TypeInfo -> TypeName -> Blaze.Builder
192+renderTypeInfo byOid info name
193+ | typcategory info == 'A' || typname info == "_record" =
194+ let (Just typelem_info) = Map.lookup (typelem info) byOid
195+ (Just typelem_hs_name) = lookup (typname typelem_info) typeNames
196+ in concat
197+ [ "\n"
198+ , bs (hs name), " :: TypeInfo\n"
199+ , bs (hs name), " = Array {\n"
200+ , " typoid = ", fromString (show (typoid info)), ",\n"
201+ , " typcategory = '", Blaze.fromChar (typcategory info), "',\n"
202+ , " typdelim = '", Blaze.fromChar (typdelim info), "',\n"
203+ , " typname = \"", bs (typname info), "\",\n"
204+ , " typelem = ", bs typelem_hs_name, "\n"
205+ , " }\n"
206+ ]
207+ | typcategory info == 'R' = undefined
208+ | otherwise =
209+ concat
210+ [ "\n"
211+ , bs (hs name), " :: TypeInfo\n"
212+ , bs (hs name), " = Basic {\n"
213+ , " typoid = ", fromString (show (typoid info)), ",\n"
214+ , " typcategory = '", Blaze.fromChar (typcategory info), "',\n"
215+ , " typdelim = '", Blaze.fromChar (typdelim info), "',\n"
216+ , " typname = \"", bs (typname info), "\"\n"
217+ , " }\n"
218+ ]
219+
190220-- FIXME: add in any names that we need that we didn't specify, (i.e.
191221-- the "unknowns" in getTypeInfos
192222-- and munge them into a valid haskell identifier if needed.
@@ -237,15 +267,6 @@ staticTypeInfo (Oid x) = case x of
237267 ++ [longstring|
238268 _ -> Nothing
239269|]
240− ++ concat [concat
241− [ "\n"
242− , bs (hs name), " :: TypeInfo\n"
243− , bs (hs name), " = Basic {\n"
244− , " typoid = ", fromString (show typoid), ",\n"
245− , " typcategory = '", Blaze.fromChar typcategory, "',\n"
246− , " typdelim = '", Blaze.fromChar typdelim, "',\n"
247− , " typname = \"", bs typname, "\"\n"
248− , " }\n"
249− ]
270+ ++ concat [ renderTypeInfo byOid typeInfo name
250271 | name <- getNames byName names
251− , let (Just (TypeInfo{..})) = Map.lookup (pg name) byName])
272+ , let (Just typeInfo) = Map.lookup (pg name) byName])