git.iko.soy/typeable/postgresql-simple
on master help sign in
Move misplaced functions into their proper module
592fe13Leon P Smith
src/Database/PostgreSQL/Simple/FromField.hs+54
@@ -1,6 +1,7 @@
1{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
1{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
2{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
3{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
3{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
4+{-# LANGUAGE RecordWildCards #-}
4
5
5------------------------------------------------------------------------------
6------------------------------------------------------------------------------
6-- |
7-- |
@@ -126,6 +127,59 @@ class FromField a where
126 -- such a reference, and that using bytestring functions such as 'B.drop'
127 -- such a reference, and that using bytestring functions such as 'B.drop'
127 -- and 'B.takeWhile' alone will also trigger this memory leak.
128 -- and 'B.takeWhile' alone will also trigger this memory leak.
128
129
130+-- | Returns the data type name. This is the preferred way of identifying
131+-- types that do not have a stable type oid, such as types provided by
132+-- extensions to PostgreSQL.
133+--
134+-- More concretely, it returns the @typname@ column associated with the
135+-- type oid in the @pg_type@ table. First, postgresql-simple will check
136+-- built-in, static table. If the type oid is not there, postgresql-simple
137+-- will check a per-connection cache, and then finally query the database's
138+-- meta-schema.
139+
140+typename :: Field -> ByteString
141+typename = typname . typ . typeinfo
142+
143+-- | Returns the name of the column. This is often determined by a table
144+-- definition, but it can be set using an @as@ clause.
145+
146+name :: Field -> Maybe ByteString
147+name Field{..} = unsafePerformIO (PQ.fname result column)
148+
149+-- | Returns the name of the object id of the @table@ associated with the
150+-- column, if any. Returns 'Nothing' when there is no such table;
151+-- for example a computed column does not have a table associated with it.
152+-- Analogous to libpq's @PQftable@.
153+
154+tableOid :: Field -> Maybe PQ.Oid
155+tableOid Field{..} = toMaybeOid (unsafePerformIO (PQ.ftable result column))
156+ where
157+ toMaybeOid x
158+ = if x == PQ.invalidOid
159+ then Nothing
160+ else Just x
161+
162+-- | If the column has a table associated with it, this returns the number
163+-- of the associated table column. Numbering starts from 0. Analogous
164+-- to libpq's @PQftablecol@.
165+
166+tableColumn :: Field -> Int
167+tableColumn Field{..} = fromCol (unsafePerformIO (PQ.ftablecol result column))
168+ where
169+ fromCol (PQ.Col x) = fromIntegral x
170+
171+-- | This returns whether the data was returned in a binary or textual format.
172+-- Analogous to libpq's @PQfformat@.
173+
174+format :: Field -> PQ.Format
175+format Field{..} = unsafePerformIO (PQ.fformat result column)
176+
177+-- | This returns the type oid associated with the column. Analogous
178+-- to libpq's @PQftype@.
179+
180+typeOid :: Field -> PQ.Oid
181+typeOid Field{..} = unsafePerformIO (PQ.ftype result column)
182+
129instance (FromField a) => FromField (Maybe a) where
183instance (FromField a) => FromField (Maybe a) where
130 fromField _ Nothing = pure Nothing
184 fromField _ Nothing = pure Nothing
131 fromField f bs = Just <$> fromField f bs
185 fromField f bs = Just <$> fromField f bs
src/Database/PostgreSQL/Simple/Internal.hs−53
@@ -64,59 +64,6 @@ data TypeInfo = TypeInfo { typ :: !NamedOid
64 , typelem :: !(Maybe NamedOid)
64 , typelem :: !(Maybe NamedOid)
65 } deriving Show
65 } deriving Show
66
66
67−-- | Returns the data type name. This is the preferred way of identifying
68−-- types that do not have a stable type oid, such as types provided by
69−-- extensions to PostgreSQL.
70−--
71−-- More concretely, it returns the @typname@ column associated with the
72−-- type oid in the @pg_type@ table. First, postgresql-simple will check
73−-- built-in, static table. If the type oid is not there, postgresql-simple
74−-- will check a per-connection cache, and then finally query the database's
75−-- meta-schema.
76−
77−typename :: Field -> ByteString
78−typename = typname . typ . typeinfo
79−
80−-- | Returns the name of the column. This is often determined by a table
81−-- definition, but it can be set using an @as@ clause.
82−
83−name :: Field -> Maybe ByteString
84−name Field{..} = unsafePerformIO (PQ.fname result column)
85−
86−-- | Returns the name of the object id of the @table@ associated with the
87−-- column, if any. Returns 'Nothing' when there is no such table;
88−-- for example a computed column does not have a table associated with it.
89−-- Analogous to libpq's @PQftable@.
90−
91−tableOid :: Field -> Maybe PQ.Oid
92−tableOid Field{..} = toMaybeOid (unsafePerformIO (PQ.ftable result column))
93− where
94− toMaybeOid x
95− = if x == PQ.invalidOid
96− then Nothing
97− else Just x
98−
99−-- | If the column has a table associated with it, this returns the number
100−-- of the associated table column. Numbering starts from 0. Analogous
101−-- to libpq's @PQftablecol@.
102−
103−tableColumn :: Field -> Int
104−tableColumn Field{..} = fromCol (unsafePerformIO (PQ.ftablecol result column))
105− where
106− fromCol (PQ.Col x) = fromIntegral x
107−
108−-- | This returns whether the data was returned in a binary or textual format.
109−-- Analogous to libpq's @PQfformat@.
110−
111−format :: Field -> PQ.Format
112−format Field{..} = unsafePerformIO (PQ.fformat result column)
113−
114−-- | This returns the type oid associated with the column. Analogous
115−-- to libpq's @PQftype@.
116−
117−typeOid :: Field -> PQ.Oid
118−typeOid Field{..} = unsafePerformIO (PQ.ftype result column)
119−
120data Connection = Connection {
67data Connection = Connection {
121 connectionHandle :: {-# UNPACK #-} !(MVar PQ.Connection)
68 connectionHandle :: {-# UNPACK #-} !(MVar PQ.Connection)
122 , connectionObjects :: {-# UNPACK #-} !(MVar (IntMap.IntMap TypeInfo))
69 , connectionObjects :: {-# UNPACK #-} !(MVar (IntMap.IntMap TypeInfo))