git.iko.soy/typeable/postgresql-simple
on master help sign in
Merge pull request #232 from MaxGabriel/additionalCITextSupport

Add additional case-insensitive text support

commitcfb16854ac1487f23efa9a08c66502d3a8348c74 authorLeon P Smith <leon.p.smith+github@gmail.com> dateSat 25 Nov 2017, 21:07 (9 years ago) parentad504f29b21525
4 files changed+33
CHANGES.md+5 postgresql-simple.cabal+1 src/Database/PostgreSQL/Simple/ToField.hs+11 test/Main.hs+16
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+
16### Version 0.5.3.0 (2017-05-15)
27 * Refactored some rudimentary cursor handling code out of the
38 implementation of the fold operators, into a new
postgresql-simple.cabal+1
@@ -129,6 +129,7 @@ test-suite test
129129 , text
130130 , time
131131 , vector
132+ , case-insensitive
132133
133134 if !impl(ghc >= 7.6)
134135 build-depends:
src/Database/PostgreSQL/Simple/ToField.hs+11
@@ -42,6 +42,8 @@ import Database.PostgreSQL.Simple.Compat (toByteString)
4242
4343import qualified Data.ByteString as SB
4444import qualified Data.ByteString.Lazy as LB
45+import Data.CaseInsensitive (CI)
46+import qualified Data.CaseInsensitive as CI
4547import qualified Data.Text as ST
4648import qualified Data.Text.Encoding as ST
4749import qualified Data.Text.Lazy as LT
@@ -230,6 +232,15 @@ instance ToField LT.Text where
230232 toField = toField . LT.toStrict
231233 {-# INLINE toField #-}
232234
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 #-}
233244
234245instance ToField UTCTime where
235246 toField = Plain . inQuotes . utcTimeToBuilder
test/Main.hs+16
@@ -23,6 +23,8 @@ import Data.Aeson
2323import Data.ByteString (ByteString)
2424import qualified Data.ByteString as B
2525import qualified Data.ByteString.Lazy.Char8 as BL
26+import Data.CaseInsensitive (CI)
27+import qualified Data.CaseInsensitive as CI
2628import Data.Map (Map)
2729import qualified Data.Map as Map
2830import Data.Text(Text)
@@ -50,6 +52,7 @@ tests env = testGroup "tests"
5052 , testCase "Array" . testArray
5153 , testCase "Array of nullables" . testNullableArray
5254 , testCase "HStore" . testHStore
55+ , testCase "citext" . testCIText
5356 , testCase "JSON" . testJSON
5457 , testCase "Savepoint" . testSavepoint
5558 , testCase "Unicode" . testUnicode
@@ -203,6 +206,19 @@ testHStore TestEnv{..} = do
203206 m' <- query conn "SELECT ?::hstore" m
204207 [m] @?= m'
205208
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+
206222testJSON :: TestEnv -> Assertion
207223testJSON TestEnv{..} = do
208224 roundTrip (Map.fromList [] :: Map Text Text)