git.iko.soy/typeable/postgresql-simple
on master help sign in
Merge pull request #232 from MaxGabriel/additionalCITextSupport
cfb1685Leon P Smith
CHANGES.md+5
@@ -1,3 +1,8 @@
1+### Unreleased
2+
3+ * Added `ToField` instances for case-insensitive strict and lazy text.
4+ Thanks to Max Tagher for the implementation.
5+
1### Version 0.5.3.0 (2017-05-15)
6### Version 0.5.3.0 (2017-05-15)
2 * Refactored some rudimentary cursor handling code out of the
7 * Refactored some rudimentary cursor handling code out of the
3 implementation of the fold operators, into a new
8 implementation of the fold operators, into a new
postgresql-simple.cabal+1
@@ -129,6 +129,7 @@ test-suite test
129 , text
129 , text
130 , time
130 , time
131 , vector
131 , vector
132+ , case-insensitive
132
133
133 if !impl(ghc >= 7.6)
134 if !impl(ghc >= 7.6)
134 build-depends:
135 build-depends:
src/Database/PostgreSQL/Simple/ToField.hs+11
@@ -42,6 +42,8 @@ import Database.PostgreSQL.Simple.Compat (toByteString)
42
42
43import qualified Data.ByteString as SB
43import qualified Data.ByteString as SB
44import qualified Data.ByteString.Lazy as LB
44import qualified Data.ByteString.Lazy as LB
45+import Data.CaseInsensitive (CI)
46+import qualified Data.CaseInsensitive as CI
45import qualified Data.Text as ST
47import qualified Data.Text as ST
46import qualified Data.Text.Encoding as ST
48import qualified Data.Text.Encoding as ST
47import qualified Data.Text.Lazy as LT
49import qualified Data.Text.Lazy as LT
@@ -230,6 +232,15 @@ instance ToField LT.Text where
230 toField = toField . LT.toStrict
232 toField = toField . LT.toStrict
231 {-# INLINE toField #-}
233 {-# INLINE toField #-}
232
234
235+-- | citext
236+instance ToField (CI ST.Text) where
237+ toField = toField . CI.original
238+ {-# INLINE toField #-}
239+
240+-- | citext
241+instance ToField (CI LT.Text) where
242+ toField = toField . LT.toStrict . CI.original
243+ {-# INLINE toField #-}
233
244
234instance ToField UTCTime where
245instance ToField UTCTime where
235 toField = Plain . inQuotes . utcTimeToBuilder
246 toField = Plain . inQuotes . utcTimeToBuilder
test/Main.hs+16
@@ -23,6 +23,8 @@ import Data.Aeson
23import Data.ByteString (ByteString)
23import Data.ByteString (ByteString)
24import qualified Data.ByteString as B
24import qualified Data.ByteString as B
25import qualified Data.ByteString.Lazy.Char8 as BL
25import qualified Data.ByteString.Lazy.Char8 as BL
26+import Data.CaseInsensitive (CI)
27+import qualified Data.CaseInsensitive as CI
26import Data.Map (Map)
28import Data.Map (Map)
27import qualified Data.Map as Map
29import qualified Data.Map as Map
28import Data.Text(Text)
30import Data.Text(Text)
@@ -50,6 +52,7 @@ tests env = testGroup "tests"
50 , testCase "Array" . testArray
52 , testCase "Array" . testArray
51 , testCase "Array of nullables" . testNullableArray
53 , testCase "Array of nullables" . testNullableArray
52 , testCase "HStore" . testHStore
54 , testCase "HStore" . testHStore
55+ , testCase "citext" . testCIText
53 , testCase "JSON" . testJSON
56 , testCase "JSON" . testJSON
54 , testCase "Savepoint" . testSavepoint
57 , testCase "Savepoint" . testSavepoint
55 , testCase "Unicode" . testUnicode
58 , testCase "Unicode" . testUnicode
@@ -203,6 +206,19 @@ testHStore TestEnv{..} = do
203 m' <- query conn "SELECT ?::hstore" m
206 m' <- query conn "SELECT ?::hstore" m
204 [m] @?= m'
207 [m] @?= m'
205
208
209+testCIText :: TestEnv -> Assertion
210+testCIText TestEnv{..} = do
211+ execute_ conn "CREATE EXTENSION IF NOT EXISTS citext"
212+ roundTrip (CI.mk "")
213+ roundTrip (CI.mk "UPPERCASE")
214+ roundTrip (CI.mk "lowercase")
215+ where
216+ roundTrip :: (CI Text) -> Assertion
217+ roundTrip cit = do
218+ let toPostgres = Only cit
219+ fromPostgres <- query conn "SELECT ?::citext" toPostgres
220+ [toPostgres] @?= fromPostgres
221+
206testJSON :: TestEnv -> Assertion
222testJSON :: TestEnv -> Assertion
207testJSON TestEnv{..} = do
223testJSON TestEnv{..} = do
208 roundTrip (Map.fromList [] :: Map Text Text)
224 roundTrip (Map.fromList [] :: Map Text Text)