git.iko.soy/typeable/postgresql-simple
on master help sign in
Merge pull request #118 from ibotty/jsonb-support

add jsonb type

commit57e41997a2727741befd14ea6858971a841f56b1 authorLeon P Smith <leon@melding-monads.com> dateTue 26 Aug 2014, 04:58 (12 years ago) parent7a48e343c742a5
5 files changed+27−8
src/Database/PostgreSQL/Simple/BuiltinTypes.hs+8 src/Database/PostgreSQL/Simple/FromField.hs+7−8 src/Database/PostgreSQL/Simple/TypeInfo/Static.hs+10 tools/GenBuiltinTypes.hs+1 tools/GenTypeInfo.hs+1
src/Database/PostgreSQL/Simple/BuiltinTypes.hs+8
@@ -74,6 +74,7 @@ data BuiltinType
7474 | Void
7575 | UUID
7676 | JSON
77+ | JSONB
7778 deriving (Eq, Ord, Enum, Bounded, Read, Show, Typeable)
7879
7980builtin2oid :: BuiltinType -> PQ.Oid
@@ -125,6 +126,7 @@ builtin2oid typ = PQ.Oid $ case typ of
125126 Void -> 2278
126127 UUID -> 2950
127128 JSON -> 114
129+ JSONB -> 3802
128130
129131oid2builtin :: PQ.Oid -> Maybe BuiltinType
130132oid2builtin (PQ.Oid x) = case x of
@@ -175,6 +177,7 @@ oid2builtin (PQ.Oid x) = case x of
175177 2278 -> Just Void
176178 2950 -> Just UUID
177179 114 -> Just JSON
180+ 3802 -> Just JSONB
178181 _ -> Nothing
179182
180183builtin2typname :: BuiltinType -> ByteString
@@ -226,6 +229,7 @@ builtin2typname typ = case typ of
226229 Void -> void
227230 UUID -> uuid
228231 JSON -> json
232+ JSONB -> jsonb
229233
230234oid2typname :: PQ.Oid -> Maybe ByteString
231235oid2typname (PQ.Oid x) = case x of
@@ -276,6 +280,7 @@ oid2typname (PQ.Oid x) = case x of
276280 2278 -> Just void
277281 2950 -> Just uuid
278282 114 -> Just json
283+ 3802 -> Just jsonb
279284 _ -> Nothing
280285
281286bool :: ByteString
@@ -418,3 +423,6 @@ uuid = "uuid"
418423
419424json :: ByteString
420425json = "json"
426+
427+jsonb :: ByteString
428+jsonb = "jsonb"
src/Database/PostgreSQL/Simple/FromField.hs+7−8
@@ -27,11 +27,11 @@ Note that the 'Float' and 'Double' instances use attoparsec's 'double'
2727conversion routine, which sacrifices some accuracy for speed. If you
2828need accuracy, consider first converting data to a 'Scientific' or 'Rational'
2929type, and then converting to a floating-point type. If you are defining
30−your own 'Database.PostgreSQL.Simple.FromRow.FromRow' instances, this can be
31−acheived simply by
32−@'fromRational' '<$>' 'Database.PostgreSQL.Simple.FromRow.field'@, although
30+your own 'Database.PostgreSQL.Simple.FromRow.FromRow' instances, this can be
31+acheived simply by
32+@'fromRational' '<$>' 'Database.PostgreSQL.Simple.FromRow.field'@, although
3333this idiom is additionally compatible with PostgreSQL's @numeric@ type.
34−If this is unacceptable, you may find
34+If this is unacceptable, you may find
3535'Database.PostgreSQL.Simple.FromRow.fieldWith' useful.
3636
3737Because 'FromField' is a typeclass, one may provide conversions to
@@ -104,12 +104,11 @@ module Database.PostgreSQL.Simple.FromField
104104
105105#include "MachDeps.h"
106106
107−import Control.Applicative
108− ( Applicative, (<|>), (<$>), pure )
107+import Control.Applicative ( (<|>), (<$>), pure )
109108import Control.Concurrent.MVar (MVar, newMVar)
110109import Control.Exception (Exception)
111110import qualified Data.Aeson as JSON
112−import Data.Attoparsec.Char8 hiding (Result)
111+import Data.Attoparsec.ByteString.Char8 hiding (Result)
113112import Data.ByteString (ByteString)
114113import qualified Data.ByteString.Char8 as B
115114import Data.Int (Int16, Int32, Int64)
@@ -492,7 +491,7 @@ instance FromField UUID where
492491-- | json
493492instance FromField JSON.Value where
494493 fromField f mbs =
495− if typeOid f /= $(inlineTypoid TI.json)
494+ if typeOid f /= $(inlineTypoid TI.json) && typeOid f /= $(inlineTypoid TI.jsonb)
496495 then returnError Incompatible f ""
497496 else case mbs of
498497 Nothing -> returnError UnexpectedNull f ""
src/Database/PostgreSQL/Simple/TypeInfo/Static.hs+10
@@ -65,6 +65,7 @@ module Database.PostgreSQL.Simple.TypeInfo.Static
6565 , void
6666 , uuid
6767 , json
68+ , jsonb
6869 ) where
6970
7071import Database.PostgreSQL.LibPQ (Oid(..))
@@ -119,6 +120,7 @@ staticTypeInfo (Oid x) = case x of
119120 2278 -> Just void
120121 2950 -> Just uuid
121122 114 -> Just json
123+ 3802 -> Just jsonb
122124 _ -> Nothing
123125
124126bool :: TypeInfo
@@ -496,3 +498,11 @@ json = Basic {
496498 typdelim = ',',
497499 typname = "json"
498500 }
501+
502+jsonb :: TypeInfo
503+jsonb = Basic {
504+ typoid = Oid 3802,
505+ typcategory = 'U',
506+ typdelim = ',',
507+ typname = "jsonb"
508+ }
tools/GenBuiltinTypes.hs+1
@@ -85,6 +85,7 @@ record
8585void
8686uuid UUID
8787json JSON
88+jsonb JSONB
8889|]
8990
9091instance IsString Blaze.Builder where
tools/GenTypeInfo.hs+1
@@ -127,6 +127,7 @@ record
127127void
128128uuid
129129json
130+jsonb
130131|]
131132
132133instance IsString Blaze.Builder where