git.iko.soy/typeable/postgresql-simple
on master help sign in
Implement new TypeInfo system and fix array conversion
commit136a6f89822fa355c9400f60110d96c8050bcb5e authorLeon P Smith <leon@melding-monads.com> dateMon 25 Mar 2013, 16:07 (14 years ago) parentfb39887
9 files changed+161−165
postgresql-simple.cabal+2 src/Database/PostgreSQL/Simple/FromField.hs+33−15 src/Database/PostgreSQL/Simple/FromRow.hs+1−1 src/Database/PostgreSQL/Simple/FromRow.hs-boot+1 src/Database/PostgreSQL/Simple/Internal.hs+3−8 src/Database/PostgreSQL/Simple/TypeInfo.hs+35−28 src/Database/PostgreSQL/Simple/TypeInfo/Static.hs+50−101 src/Database/PostgreSQL/Simple/TypeInfo/Types.hs+30 tools/GenTypeInfo.hs+6−12
postgresql-simple.cabal+2
@@ -35,6 +35,7 @@ Library
3535 Database.PostgreSQL.Simple.ToRow
3636 Database.PostgreSQL.Simple.Transaction
3737 Database.PostgreSQL.Simple.TypeInfo
38+ Database.PostgreSQL.Simple.TypeInfo.Static
3839 Database.PostgreSQL.Simple.Types
3940 Database.PostgreSQL.Simple.Errors
4041-- Other-modules:
@@ -43,6 +44,7 @@ Library
4344 Other-modules:
4445 Database.PostgreSQL.Simple.Compat
4546 Database.PostgreSQL.Simple.Time.Implementation
47+ Database.PostgreSQL.Simple.TypeInfo.Types
4648
4749 Build-depends:
4850 attoparsec >= 0.8.5.3,
src/Database/PostgreSQL/Simple/FromField.hs+33−15
@@ -73,7 +73,9 @@ module Database.PostgreSQL.Simple.FromField
7373
7474 , Field
7575 , typename
76− , typeinfo
76+ , TypeInfo(..)
77+ , typeInfo
78+ , typeInfoByOid
7779 , name
7880 , tableOid
7981 , tableColumn
@@ -94,7 +96,6 @@ import Data.ByteString (ByteString)
9496import qualified Data.ByteString.Char8 as B
9597import Data.Int (Int16, Int32, Int64)
9698import Data.List (foldl')
97−import Data.Maybe (fromMaybe)
9899import Data.Ratio (Ratio)
99100import Data.Time ( UTCTime, ZonedTime, LocalTime, Day, TimeOfDay )
100101import Data.Typeable (Typeable, typeOf)
@@ -106,9 +107,9 @@ import Database.PostgreSQL.Simple.BuiltinTypes
106107import Database.PostgreSQL.Simple.Compat
107108import Database.PostgreSQL.Simple.Ok
108109import Database.PostgreSQL.Simple.Types (Binary(..), Null(..))
109−import Database.PostgreSQL.Simple.TypeInfo
110+import Database.PostgreSQL.Simple.TypeInfo as TypeInfo
110111import Database.PostgreSQL.Simple.Time
111−import Database.PostgreSQL.Simple.Arrays
112+import Database.PostgreSQL.Simple.Arrays as Arrays
112113import qualified Database.PostgreSQL.LibPQ as PQ
113114import qualified Data.ByteString as SB
114115import qualified Data.ByteString.Char8 as B8
@@ -179,12 +180,16 @@ class FromField a where
179180-- meta-schema.
180181
181182typename :: Field -> Conversion ByteString
182−typename field = typname . typ <$> typeinfo field
183+typename field = typname <$> typeInfo field
183184
184−typeinfo :: Field -> Conversion TypeInfo
185−typeinfo Field{..} = Conversion $ \conn -> do
185+typeInfo :: Field -> Conversion TypeInfo
186+typeInfo Field{..} = Conversion $ \conn -> do
186187 Ok <$> (getTypeInfo conn =<< PQ.ftype result column)
187188
189+typeInfoByOid :: PQ.Oid -> Conversion TypeInfo
190+typeInfoByOid oid = Conversion $ \conn -> do
191+ Ok <$> (getTypeInfo conn oid)
192+
188193-- | Returns the name of the column. This is often determined by a table
189194-- definition, but it can be set using an @as@ clause.
190195
@@ -205,7 +210,7 @@ tableOid Field{..} = toMaybeOid (unsafeDupablePerformIO (PQ.ftable result column
205210 else Just x
206211
207212-- | If the column has a table associated with it, this returns the number
208−-- of the associated table column. Numbering starts from 0. Analogous
213+-- off the associated table column. Numbering starts from 0. Analogous
209214-- to libpq's @PQftablecol@.
210215
211216tableColumn :: Field -> Int
@@ -352,14 +357,27 @@ instance (FromField a, FromField b) => FromField (Either a b) where
352357 <|> (Left <$> fromField f dat)
353358
354359instance (FromField a, Typeable a) => FromField (Vector a) where
355− fromField f dat = either (returnError ConversionFailed f)
356− (V.fromList <$>)
357− (parseOnly (fromArray ',' f) (maybe "" id dat))
358−
359−fromArray :: (FromField a) => Char -> Field -> Parser (Conversion [a])
360−fromArray delim f = sequence . (parseIt <$>) <$> array delim
360+ fromField f mdat = do
361+ info <- typeInfo f
362+ case info of
363+ TypeInfo.Array{} ->
364+ case mdat of
365+ Nothing -> returnError UnexpectedNull f ""
366+ Just dat -> do
367+ case parseOnly (fromArray info f) dat of
368+ Left err -> returnError ConversionFailed f err
369+ Right conv -> V.fromList <$> conv
370+ _ -> returnError Incompatible f ""
371+
372+fromArray :: (FromField a)
373+ => TypeInfo -> Field -> Parser (Conversion [a])
374+fromArray typeInfo f = sequence . (parseIt <$>) <$> array delim
361375 where
362− parseIt item = (fromField f . Just . fmt delim) item
376+ delim = typdelim (typelem typeInfo)
377+ fElem = f{ typeOid = typoid (typelem typeInfo) }
378+ parseIt item = (fromField f' . Just . fmt delim) item
379+ where f' | Arrays.Array _ <- item = f
380+ | otherwise = fElem
363381
364382newtype Compat = Compat Word64
365383
src/Database/PostgreSQL/Simple/FromRow.hs+1−1
@@ -79,7 +79,7 @@ getTypeInfoByCol Row{..} col =
7979 Ok <$> getTypeInfo conn oid
8080
8181getTypenameByCol :: Row -> PQ.Column -> Conversion ByteString
82−getTypenameByCol row col = typname . typ <$> getTypeInfoByCol row col
82+getTypenameByCol row col = typname <$> getTypeInfoByCol row col
8383
8484fieldWith :: FieldParser a -> RowParser a
8585fieldWith fieldP = RP $ do
src/Database/PostgreSQL/Simple/FromRow.hs-boot+1
@@ -5,3 +5,4 @@ import {-# SOURCE #-} Database.PostgreSQL.Simple.FromField
55class FromRow a
66
77instance (FromField a, FromField b, FromField c, FromField d) => FromRow (a,b,c,d)
8+instance (FromField a, FromField b, FromField c, FromField d, FromField e) => FromRow (a,b,c,d,e)
src/Database/PostgreSQL/Simple/Internal.hs+3−8
@@ -39,6 +39,7 @@ import qualified Database.PostgreSQL.LibPQ as PQ
3939import Database.PostgreSQL.LibPQ(ExecStatus(..))
4040import Database.PostgreSQL.Simple.Ok
4141import Database.PostgreSQL.Simple.Types (Query(..))
42+import Database.PostgreSQL.Simple.TypeInfo.Types(TypeInfo)
4243import Control.Monad.Trans.State.Strict
4344import Control.Monad.Trans.Reader
4445
@@ -56,17 +57,11 @@ data Field = Field {
5657 -- to libpq's @PQftype@.
5758 }
5859
59−data NamedOid = NamedOid { typoid :: !PQ.Oid
60− , typname :: !ByteString
61− } deriving Show
62−
63−data TypeInfo = TypeInfo { typ :: !NamedOid
64− , typelem :: !(Maybe NamedOid)
65− } deriving Show
60+type TypeInfoCache = IntMap.IntMap TypeInfo
6661
6762data Connection = Connection {
6863 connectionHandle :: {-# UNPACK #-} !(MVar PQ.Connection)
69− , connectionObjects :: {-# UNPACK #-} !(MVar (IntMap.IntMap TypeInfo))
64+ , connectionObjects :: {-# UNPACK #-} !(MVar TypeInfoCache)
7065 }
7166
7267data SqlError = SqlError {
src/Database/PostgreSQL/Simple/TypeInfo.hs+35−28
@@ -12,10 +12,10 @@
1212
1313module Database.PostgreSQL.Simple.TypeInfo
1414 ( getTypeInfo
15− , NamedOid(..)
1615 , TypeInfo(..)
1716 ) where
1817
18+import qualified Data.ByteString.Char8 as B8
1919import qualified Data.IntMap as IntMap
2020import Control.Concurrent.MVar
2121import Control.Exception (throw)
@@ -24,33 +24,40 @@ import qualified Database.PostgreSQL.LibPQ as PQ
2424import {-# SOURCE #-} Database.PostgreSQL.Simple
2525import Database.PostgreSQL.Simple.Internal
2626import Database.PostgreSQL.Simple.Types
27−import Database.PostgreSQL.Simple.BuiltinTypes
27+import Database.PostgreSQL.Simple.TypeInfo.Types
28+import Database.PostgreSQL.Simple.TypeInfo.Static
2829
2930getTypeInfo :: Connection -> PQ.Oid -> IO TypeInfo
3031getTypeInfo conn@Connection{..} oid =
31− case oid2typname oid of
32− Just name -> return $! TypeInfo { typ = NamedOid oid name
33− , typelem = Nothing
34− }
35− Nothing -> modifyMVar connectionObjects $ \oidmap -> do
36− case IntMap.lookup (oid2int oid) oidmap of
37− Just typeinfo -> return (oidmap, typeinfo)
38− Nothing -> do
39− names <- query conn "SELECT p.oid, p.typname, c.oid, c.typname\
40− \ FROM pg_type AS p LEFT OUTER JOIN pg_type AS c\
41− \ ON c.oid = p.typelem\
42− \ WHERE p.oid = ?"
43− (Only oid)
44− typinf <- case names of
45− [] -> return $ throw (fatalError "invalid type oid")
46− [(pOid, pTypName, mbCOid, mbCTypName)] ->
47− return $! TypeInfo { typ = NamedOid pOid pTypName
48− , typelem = do
49− cOid <- mbCOid
50− cTypName <- mbCTypName
51− return $ NamedOid cOid cTypName
52− }
53− _ -> fail "typename query returned more than one result"
54− -- oid is a primary key, so the query should
55− -- never return more than one result
56− return (IntMap.insert (oid2int oid) typinf oidmap, typinf)
32+ case staticTypeInfo oid of
33+ Just name -> return name
34+ Nothing -> modifyMVar connectionObjects $ getTypeInfo' conn oid
35+
36+getTypeInfo' :: Connection -> PQ.Oid -> TypeInfoCache
37+ -> IO (TypeInfoCache, TypeInfo)
38+getTypeInfo' conn oid oidmap =
39+ case IntMap.lookup (oid2int oid) oidmap of
40+ Just typeinfo -> return (oidmap, typeinfo)
41+ Nothing -> do
42+ names <- query conn "SELECT oid, typcategory, typdelim, typname, typelem\
43+ \ FROM pg_type WHERE oid = ?"
44+ (Only oid)
45+ (oidmap', typeInfo) <-
46+ case names of
47+ [] -> return $ throw (fatalError "invalid type oid")
48+ [(typoid, typcategory_, typdelim_, typname, typelem_)] -> do
49+ let !typcategory = B8.index typcategory_ 0
50+ !typdelim = B8.index typdelim_ 0
51+ if typcategory == 'A'
52+ then do
53+ (oidmap', typelem) <- getTypeInfo' conn typelem_ oidmap
54+ let !typeInfo = Array{..}
55+ return (oidmap', typeInfo)
56+ else do
57+ let !typeInfo = Basic{..}
58+ return (oidmap, typeInfo)
59+ _ -> fail "typename query returned more than one result"
60+ -- oid is a primary key, so the query should
61+ -- never return more than one result
62+ let !oidmap'' = IntMap.insert (oid2int oid) typeInfo oidmap'
63+ return $! (oidmap'', typeInfo)
src/Database/PostgreSQL/Simple/TypeInfo/Static.hs+50−101
@@ -6,6 +6,9 @@
66-- Maintainer: Leon P Smith <leon@melding-monads.com>
77-- Stability: experimental
88--
9+-- Note that this module is semi-internal, and you probably want to use
10+-- Database.PostgreSQL.Simple.TypeInfo instead.
11+--
912------------------------------------------------------------------------------
1013
1114-- Note that this file is generated by tools/GenTypeInfo.hs, and should
@@ -62,16 +65,8 @@ module Database.PostgreSQL.Simple.TypeInfo.Static
6265 , uuid
6366 ) where
6467
65−import Data.ByteString (ByteString)
6668import Database.PostgreSQL.LibPQ (Oid(..))
67−
68−data TypeInfo = TypeInfo
69− { typoid :: {-# UNPACK #-} !Oid
70− , typcategory :: {-# UNPACK #-} !Char
71− , typdelim :: {-# UNPACK #-} !Char
72− , typelem :: !(Maybe TypeInfo)
73− , typname :: !ByteString
74− }
69+import Database.PostgreSQL.Simple.TypeInfo.Types
7570
7671staticTypeInfo :: Oid -> Maybe TypeInfo
7772staticTypeInfo (Oid x) = case x of
@@ -124,415 +119,369 @@ staticTypeInfo (Oid x) = case x of
124119 _ -> Nothing
125120
126121bool :: TypeInfo
127−bool = TypeInfo {
122+bool = Basic {
128123 typoid = Oid 16,
129124 typcategory = 'B',
130125 typdelim = ',',
131− typelem = Nothing,
132126 typname = "bool"
133127 }
134128
135129bytea :: TypeInfo
136−bytea = TypeInfo {
130+bytea = Basic {
137131 typoid = Oid 17,
138132 typcategory = 'U',
139133 typdelim = ',',
140− typelem = Nothing,
141134 typname = "bytea"
142135 }
143136
144137char :: TypeInfo
145−char = TypeInfo {
138+char = Basic {
146139 typoid = Oid 18,
147140 typcategory = 'S',
148141 typdelim = ',',
149− typelem = Nothing,
150142 typname = "char"
151143 }
152144
153145name :: TypeInfo
154−name = TypeInfo {
146+name = Basic {
155147 typoid = Oid 19,
156148 typcategory = 'S',
157149 typdelim = ',',
158− typelem = Just char,
159150 typname = "name"
160151 }
161152
162153int8 :: TypeInfo
163−int8 = TypeInfo {
154+int8 = Basic {
164155 typoid = Oid 20,
165156 typcategory = 'N',
166157 typdelim = ',',
167− typelem = Nothing,
168158 typname = "int8"
169159 }
170160
171161int2 :: TypeInfo
172−int2 = TypeInfo {
162+int2 = Basic {
173163 typoid = Oid 21,
174164 typcategory = 'N',
175165 typdelim = ',',
176− typelem = Nothing,
177166 typname = "int2"
178167 }
179168
180169int4 :: TypeInfo
181−int4 = TypeInfo {
170+int4 = Basic {
182171 typoid = Oid 23,
183172 typcategory = 'N',
184173 typdelim = ',',
185− typelem = Nothing,
186174 typname = "int4"
187175 }
188176
189177regproc :: TypeInfo
190−regproc = TypeInfo {
178+regproc = Basic {
191179 typoid = Oid 24,
192180 typcategory = 'N',
193181 typdelim = ',',
194− typelem = Nothing,
195182 typname = "regproc"
196183 }
197184
198185text :: TypeInfo
199−text = TypeInfo {
186+text = Basic {
200187 typoid = Oid 25,
201188 typcategory = 'S',
202189 typdelim = ',',
203− typelem = Nothing,
204190 typname = "text"
205191 }
206192
207193oid :: TypeInfo
208−oid = TypeInfo {
194+oid = Basic {
209195 typoid = Oid 26,
210196 typcategory = 'N',
211197 typdelim = ',',
212− typelem = Nothing,
213198 typname = "oid"
214199 }
215200
216201tid :: TypeInfo
217−tid = TypeInfo {
202+tid = Basic {
218203 typoid = Oid 27,
219204 typcategory = 'U',
220205 typdelim = ',',
221− typelem = Nothing,
222206 typname = "tid"
223207 }
224208
225209xid :: TypeInfo
226−xid = TypeInfo {
210+xid = Basic {
227211 typoid = Oid 28,
228212 typcategory = 'U',
229213 typdelim = ',',
230− typelem = Nothing,
231214 typname = "xid"
232215 }
233216
234217cid :: TypeInfo
235−cid = TypeInfo {
218+cid = Basic {
236219 typoid = Oid 29,
237220 typcategory = 'U',
238221 typdelim = ',',
239− typelem = Nothing,
240222 typname = "cid"
241223 }
242224
243225xml :: TypeInfo
244−xml = TypeInfo {
226+xml = Basic {
245227 typoid = Oid 142,
246228 typcategory = 'U',
247229 typdelim = ',',
248− typelem = Nothing,
249230 typname = "xml"
250231 }
251232
252233point :: TypeInfo
253−point = TypeInfo {
234+point = Basic {
254235 typoid = Oid 600,
255236 typcategory = 'G',
256237 typdelim = ',',
257− typelem = Just float8,
258238 typname = "point"
259239 }
260240
261241lseg :: TypeInfo
262−lseg = TypeInfo {
242+lseg = Basic {
263243 typoid = Oid 601,
264244 typcategory = 'G',
265245 typdelim = ',',
266− typelem = Just point,
267246 typname = "lseg"
268247 }
269248
270249path :: TypeInfo
271−path = TypeInfo {
250+path = Basic {
272251 typoid = Oid 602,
273252 typcategory = 'G',
274253 typdelim = ',',
275− typelem = Nothing,
276254 typname = "path"
277255 }
278256
279257box :: TypeInfo
280−box = TypeInfo {
258+box = Basic {
281259 typoid = Oid 603,
282260 typcategory = 'G',
283261 typdelim = ';',
284− typelem = Just point,
285262 typname = "box"
286263 }
287264
288265polygon :: TypeInfo
289−polygon = TypeInfo {
266+polygon = Basic {
290267 typoid = Oid 604,
291268 typcategory = 'G',
292269 typdelim = ',',
293− typelem = Nothing,
294270 typname = "polygon"
295271 }
296272
297273line :: TypeInfo
298−line = TypeInfo {
274+line = Basic {
299275 typoid = Oid 628,
300276 typcategory = 'G',
301277 typdelim = ',',
302− typelem = Just float8,
303278 typname = "line"
304279 }
305280
306281cidr :: TypeInfo
307−cidr = TypeInfo {
282+cidr = Basic {
308283 typoid = Oid 650,
309284 typcategory = 'I',
310285 typdelim = ',',
311− typelem = Nothing,
312286 typname = "cidr"
313287 }
314288
315289float4 :: TypeInfo
316−float4 = TypeInfo {
290+float4 = Basic {
317291 typoid = Oid 700,
318292 typcategory = 'N',
319293 typdelim = ',',
320− typelem = Nothing,
321294 typname = "float4"
322295 }
323296
324297float8 :: TypeInfo
325−float8 = TypeInfo {
298+float8 = Basic {
326299 typoid = Oid 701,
327300 typcategory = 'N',
328301 typdelim = ',',
329− typelem = Nothing,
330302 typname = "float8"
331303 }
332304
333305abstime :: TypeInfo
334−abstime = TypeInfo {
306+abstime = Basic {
335307 typoid = Oid 702,
336308 typcategory = 'D',
337309 typdelim = ',',
338− typelem = Nothing,
339310 typname = "abstime"
340311 }
341312
342313reltime :: TypeInfo
343−reltime = TypeInfo {
314+reltime = Basic {
344315 typoid = Oid 703,
345316 typcategory = 'T',
346317 typdelim = ',',
347− typelem = Nothing,
348318 typname = "reltime"
349319 }
350320
351321tinterval :: TypeInfo
352−tinterval = TypeInfo {
322+tinterval = Basic {
353323 typoid = Oid 704,
354324 typcategory = 'T',
355325 typdelim = ',',
356− typelem = Nothing,
357326 typname = "tinterval"
358327 }
359328
360329unknown :: TypeInfo
361−unknown = TypeInfo {
330+unknown = Basic {
362331 typoid = Oid 705,
363332 typcategory = 'X',
364333 typdelim = ',',
365− typelem = Nothing,
366334 typname = "unknown"
367335 }
368336
369337circle :: TypeInfo
370−circle = TypeInfo {
338+circle = Basic {
371339 typoid = Oid 718,
372340 typcategory = 'G',
373341 typdelim = ',',
374− typelem = Nothing,
375342 typname = "circle"
376343 }
377344
378345money :: TypeInfo
379−money = TypeInfo {
346+money = Basic {
380347 typoid = Oid 790,
381348 typcategory = 'N',
382349 typdelim = ',',
383− typelem = Nothing,
384350 typname = "money"
385351 }
386352
387353macaddr :: TypeInfo
388−macaddr = TypeInfo {
354+macaddr = Basic {
389355 typoid = Oid 829,
390356 typcategory = 'U',
391357 typdelim = ',',
392− typelem = Nothing,
393358 typname = "macaddr"
394359 }
395360
396361inet :: TypeInfo
397−inet = TypeInfo {
362+inet = Basic {
398363 typoid = Oid 869,
399364 typcategory = 'I',
400365 typdelim = ',',
401− typelem = Nothing,
402366 typname = "inet"
403367 }
404368
405369bpchar :: TypeInfo
406−bpchar = TypeInfo {
370+bpchar = Basic {
407371 typoid = Oid 1042,
408372 typcategory = 'S',
409373 typdelim = ',',
410− typelem = Nothing,
411374 typname = "bpchar"
412375 }
413376
414377varchar :: TypeInfo
415−varchar = TypeInfo {
378+varchar = Basic {
416379 typoid = Oid 1043,
417380 typcategory = 'S',
418381 typdelim = ',',
419− typelem = Nothing,
420382 typname = "varchar"
421383 }
422384
423385date :: TypeInfo
424−date = TypeInfo {
386+date = Basic {
425387 typoid = Oid 1082,
426388 typcategory = 'D',
427389 typdelim = ',',
428− typelem = Nothing,
429390 typname = "date"
430391 }
431392
432393time :: TypeInfo
433−time = TypeInfo {
394+time = Basic {
434395 typoid = Oid 1083,
435396 typcategory = 'D',
436397 typdelim = ',',
437− typelem = Nothing,
438398 typname = "time"
439399 }
440400
441401timestamp :: TypeInfo
442−timestamp = TypeInfo {
402+timestamp = Basic {
443403 typoid = Oid 1114,
444404 typcategory = 'D',
445405 typdelim = ',',
446− typelem = Nothing,
447406 typname = "timestamp"
448407 }
449408
450409timestamptz :: TypeInfo
451−timestamptz = TypeInfo {
410+timestamptz = Basic {
452411 typoid = Oid 1184,
453412 typcategory = 'D',
454413 typdelim = ',',
455− typelem = Nothing,
456414 typname = "timestamptz"
457415 }
458416
459417interval :: TypeInfo
460−interval = TypeInfo {
418+interval = Basic {
461419 typoid = Oid 1186,
462420 typcategory = 'T',
463421 typdelim = ',',
464− typelem = Nothing,
465422 typname = "interval"
466423 }
467424
468425timetz :: TypeInfo
469−timetz = TypeInfo {
426+timetz = Basic {
470427 typoid = Oid 1266,
471428 typcategory = 'D',
472429 typdelim = ',',
473− typelem = Nothing,
474430 typname = "timetz"
475431 }
476432
477433bit :: TypeInfo
478−bit = TypeInfo {
434+bit = Basic {
479435 typoid = Oid 1560,
480436 typcategory = 'V',
481437 typdelim = ',',
482− typelem = Nothing,
483438 typname = "bit"
484439 }
485440
486441varbit :: TypeInfo
487−varbit = TypeInfo {
442+varbit = Basic {
488443 typoid = Oid 1562,
489444 typcategory = 'V',
490445 typdelim = ',',
491− typelem = Nothing,
492446 typname = "varbit"
493447 }
494448
495449numeric :: TypeInfo
496−numeric = TypeInfo {
450+numeric = Basic {
497451 typoid = Oid 1700,
498452 typcategory = 'N',
499453 typdelim = ',',
500− typelem = Nothing,
501454 typname = "numeric"
502455 }
503456
504457refcursor :: TypeInfo
505−refcursor = TypeInfo {
458+refcursor = Basic {
506459 typoid = Oid 1790,
507460 typcategory = 'U',
508461 typdelim = ',',
509− typelem = Nothing,
510462 typname = "refcursor"
511463 }
512464
513465record :: TypeInfo
514−record = TypeInfo {
466+record = Basic {
515467 typoid = Oid 2249,
516468 typcategory = 'P',
517469 typdelim = ',',
518− typelem = Nothing,
519470 typname = "record"
520471 }
521472
522473void :: TypeInfo
523−void = TypeInfo {
474+void = Basic {
524475 typoid = Oid 2278,
525476 typcategory = 'P',
526477 typdelim = ',',
527− typelem = Nothing,
528478 typname = "void"
529479 }
530480
531481uuid :: TypeInfo
532−uuid = TypeInfo {
482+uuid = Basic {
533483 typoid = Oid 2950,
534484 typcategory = 'U',
535485 typdelim = ',',
536− typelem = Nothing,
537486 typname = "uuid"
538487 }
src/Database/PostgreSQL/Simple/TypeInfo/Types.hsnew file+30
@@ -0,0 +1,30 @@
1+------------------------------------------------------------------------------
2+-- |
3+-- Module: Database.PostgreSQL.Simple.TypeInfo.Types
4+-- Copyright: (c) 2013 Leon P Smith
5+-- License: BSD3
6+-- Maintainer: Leon P Smith <leon@melding-monads.com>
7+-- Stability: experimental
8+--
9+------------------------------------------------------------------------------
10+
11+module Database.PostgreSQL.Simple.TypeInfo.Types where
12+
13+import Data.ByteString(ByteString)
14+import Database.PostgreSQL.LibPQ(Oid)
15+
16+data TypeInfo
17+
18+ = Basic { typoid :: {-# UNPACK #-} !Oid
19+ , typcategory :: {-# UNPACK #-} !Char
20+ , typdelim :: {-# UNPACK #-} !Char
21+ , typname :: !ByteString
22+ }
23+
24+ | Array { typoid :: {-# UNPACK #-} !Oid
25+ , typcategory :: {-# UNPACK #-} !Char
26+ , typdelim :: {-# UNPACK #-} !Char
27+ , typname :: !ByteString
28+ , typelem :: !TypeInfo
29+ }
30+ deriving (Show)
tools/GenTypeInfo.hs+6−12
@@ -209,6 +209,9 @@ renderFile byOid byName names = ([longstring|
209209-- Maintainer: Leon P Smith <leon@melding-monads.com>
210210-- Stability: experimental
211211--
212+-- Note that this module is semi-internal, and you probably want to use
213+-- Database.PostgreSQL.Simple.TypeInfo instead.
214+--
212215------------------------------------------------------------------------------
213216
214217-- Note that this file is generated by tools/GenTypeInfo.hs, and should
@@ -217,20 +220,12 @@ renderFile byOid byName names = ([longstring|
217220module Database.PostgreSQL.Simple.TypeInfo.Static
218221 ( TypeInfo(..)
219222 , staticTypeInfo
220−|] ++ concat [ " , " ++ bs (hs name) ++ "\n"
223+|] ++ concat [ " , " ++ bs (hs name) ++ "\n"
221224 | name <- names ] ++ [longstring|
222225 ) where
223226
224−import Data.ByteString (ByteString)
225227import Database.PostgreSQL.LibPQ (Oid(..))
226−
227−data TypeInfo = TypeInfo
228− { typoid :: {-# UNPACK #-} !Oid
229− , typcategory :: {-# UNPACK #-} !Char
230− , typdelim :: {-# UNPACK #-} !Char
231− , typelem :: !(Maybe TypeInfo)
232− , typname :: !ByteString
233− }
228+import Database.PostgreSQL.Simple.TypeInfo.Types
234229
235230staticTypeInfo :: Oid -> Maybe TypeInfo
236231staticTypeInfo (Oid x) = case x of
@@ -244,11 +239,10 @@ staticTypeInfo (Oid x) = case x of
244239 ++ concat [concat
245240 [ "\n"
246241 , bs (hs name), " :: TypeInfo\n"
247− , bs (hs name), " = TypeInfo {\n"
242+ , bs (hs name), " = Basic {\n"
248243 , " typoid = ", fromString (show typoid), ",\n"
249244 , " typcategory = '", Blaze.fromChar typcategory, "',\n"
250245 , " typdelim = '", Blaze.fromChar typdelim, "',\n"
251− , " typelem = ", renderElem byOid typelem, ",\n"
252246 , " typname = \"", bs typname, "\"\n"
253247 , " }\n"
254248 ]