git.iko.soy/typeable/postgresql-simple
on master help sign in
Move misplaced functions into their proper module
commit592fe131248121780801f37de9b8ce6809ba96fc authorLeon P Smith <leon@melding-monads.com> dateMon 7 Jan 2013, 19:12 (14 years ago) parent84c189a
2 files changed+54−53
src/Database/PostgreSQL/Simple/FromField.hs+54 src/Database/PostgreSQL/Simple/Internal.hs−53
src/Database/PostgreSQL/Simple/FromField.hs+54
@@ -1,6 +1,7 @@
11{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
22{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
33{-# LANGUAGE PatternGuards, ScopedTypeVariables #-}
4+{-# LANGUAGE RecordWildCards #-}
45
56------------------------------------------------------------------------------
67-- |
@@ -126,6 +127,59 @@ class FromField a where
126127 -- such a reference, and that using bytestring functions such as 'B.drop'
127128 -- and 'B.takeWhile' alone will also trigger this memory leak.
128129
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+
129183instance (FromField a) => FromField (Maybe a) where
130184 fromField _ Nothing = pure Nothing
131185 fromField f bs = Just <$> fromField f bs
src/Database/PostgreSQL/Simple/Internal.hs−53
@@ -64,59 +64,6 @@ data TypeInfo = TypeInfo { typ :: !NamedOid
6464 , typelem :: !(Maybe NamedOid)
6565 } deriving Show
6666
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−
12067data Connection = Connection {
12168 connectionHandle :: {-# UNPACK #-} !(MVar PQ.Connection)
12269 , connectionObjects :: {-# UNPACK #-} !(MVar (IntMap.IntMap TypeInfo))