git.iko.soy/typeable/postgresql-simple
on master help sign in
Change (Qualified) Identifier to use Text

The point here is to avoid encoding errors when people construct a (Qualified) Identifiers themselves, instead of going through the IsString instance. (And postgresql-simple has basically assumed the use of UTF8 so far)

commita3c2b860b3308fc2bb9986583a1b6f14f2eb16db authorLeon P Smith <leon@melding-monads.com> dateSat 22 Mar 2014, 19:30 (13 years ago) parent43edbe5
2 files changed+24−25
src/Database/PostgreSQL/Simple/ToField.hs+8−6 src/Database/PostgreSQL/Simple/Types.hs+16−19
src/Database/PostgreSQL/Simple/ToField.hs+8−6
@@ -181,15 +181,17 @@ instance ToField (Binary LB.ByteString) where
181181 {-# INLINE toField #-}
182182
183183instance ToField Identifier where
184− toField (Identifier bs) = EscapeIdentifier bs
184+ toField (Identifier bs) = EscapeIdentifier (ST.encodeUtf8 bs)
185185 {-# INLINE toField #-}
186186
187187instance ToField QualifiedIdentifier where
188− toField (QualifiedIdentifier (Just s) t) = Many [ EscapeIdentifier s
189− , Plain (fromChar '.')
190− , EscapeIdentifier t
191− ]
192− toField (QualifiedIdentifier Nothing t) = EscapeIdentifier t
188+ toField (QualifiedIdentifier (Just s) t) =
189+ Many [ EscapeIdentifier (ST.encodeUtf8 s)
190+ , Plain (fromChar '.')
191+ , EscapeIdentifier (ST.encodeUtf8 t)
192+ ]
193+ toField (QualifiedIdentifier Nothing t) =
194+ EscapeIdentifier (ST.encodeUtf8 t)
193195 {-# INLINE toField #-}
194196
195197instance ToField SB.ByteString where
src/Database/PostgreSQL/Simple/Types.hs+16−19
@@ -30,16 +30,18 @@ module Database.PostgreSQL.Simple.Types
3030 , Values(..)
3131 ) where
3232
33−import Blaze.ByteString.Builder (toByteString)
34−import Control.Arrow (first)
35−import Data.ByteString (ByteString)
36−import Data.Hashable (Hashable(hashWithSalt))
37−import Data.Monoid (Monoid(..))
38−import Data.String (IsString(..))
39−import Data.Typeable (Typeable)
33+import Blaze.ByteString.Builder (toByteString)
34+import Control.Arrow (first)
35+import Data.ByteString (ByteString)
36+import Data.Hashable (Hashable(hashWithSalt))
37+import Data.Monoid (Monoid(..))
38+import Data.String (IsString(..))
39+import Data.Typeable (Typeable)
4040import qualified Blaze.ByteString.Builder.Char.Utf8 as Utf8
4141import qualified Data.ByteString as B
42−import Database.PostgreSQL.LibPQ (Oid(..))
42+import Data.Text (Text)
43+import qualified Data.Text as T
44+import Database.PostgreSQL.LibPQ (Oid(..))
4345
4446-- | A placeholder for the SQL @NULL@ value.
4547data Null = Null
@@ -123,30 +125,25 @@ newtype Binary a = Binary {fromBinary :: a}
123125 deriving (Eq, Ord, Read, Show, Typeable, Functor)
124126
125127-- | Wrap text for use as sql identifier, i.e. a table or column name.
126−newtype Identifier = Identifier {fromIdentifier :: ByteString}
127− deriving (Eq, Ord, Read, Show, Typeable)
128−
129−instance IsString Identifier where
130− fromString = Identifier . toByteString . Utf8.fromString
128+newtype Identifier = Identifier {fromIdentifier :: Text}
129+ deriving (Eq, Ord, Read, Show, Typeable, IsString)
131130
132131instance Hashable Identifier where
133132 hashWithSalt i (Identifier t) = hashWithSalt i t
134133
135134-- | Wrap text for use as (maybe) qualified identifier, i.e. a table
136135-- with schema, or column with table.
137−data QualifiedIdentifier = QualifiedIdentifier (Maybe ByteString) ByteString
136+data QualifiedIdentifier = QualifiedIdentifier (Maybe Text) Text
138137 deriving (Eq, Ord, Read, Show, Typeable)
139138
140139instance Hashable QualifiedIdentifier where
141140 hashWithSalt i (QualifiedIdentifier q t) = hashWithSalt i (q, t)
142141
143142instance IsString QualifiedIdentifier where
144− fromString str = let (x,y) = B.break (== 46)
145− . toByteString
146− . Utf8.fromString $ str
147− in if B.null y
143+ fromString str = let (x,y) = T.break (== '.') (fromString str)
144+ in if T.null y
148145 then QualifiedIdentifier Nothing x
149− else QualifiedIdentifier (Just x) (B.tail y)
146+ else QualifiedIdentifier (Just x) (T.tail y)
150147
151148-- | Wrap a list for use as a PostgreSQL array.
152149newtype PGArray a = PGArray {fromPGArray :: [a]}