Skip to content

Commit 940cf38

Browse files
Change ByteString to Encode in Bind message
1 parent a2536fd commit 940cf38

File tree

8 files changed

+29
-22
lines changed

8 files changed

+29
-22
lines changed

bench/Codecs.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ encodeMessage params = runEncode $
8282
bindMessage = Bind (PortalName "") stmtName Binary
8383
(encodedParams params) Binary
8484
encodedParams (a, b, c, d, e, f, g) =
85-
[ Just . runEncode $ PE.bool a
86-
, Just . runEncode $ PE.bytea b
87-
, Just . runEncode $ PE.float8 c
88-
, Just . runEncode $ PE.interval d
89-
, Just . runEncode $ PE.numeric e
90-
, Just . runEncode $ PE.timestamptz f
91-
, Just . runEncode $ PE.uuid g
85+
[ Just $ PE.bool a
86+
, Just $ PE.bytea b
87+
, Just $ PE.float8 c
88+
, Just $ PE.interval d
89+
, Just $ PE.numeric e
90+
, Just $ PE.timestamptz f
91+
, Just $ PE.uuid g
9292
]
9393
parseMessage = Parse stmtName stmt oids
9494
stmtName = StatementName "_pw_statement_0010"

src/Database/PostgreSQL/Driver/Query.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import Database.PostgreSQL.Driver.StatementStorage
3232
-- Public
3333
data Query = Query
3434
{ qStatement :: B.ByteString
35-
, qValues :: [(Oid, Maybe B.ByteString)]
35+
, qValues :: [(Oid, Maybe Encode)]
3636
, qParamsFormat :: Format
3737
, qResultFormat :: Format
3838
, qCachePolicy :: CachePolicy

src/Database/PostgreSQL/Protocol/Encoders.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ encodeClientMessage Terminate
8080
-- | Encodes single data values. Length `-1` indicates a NULL parameter value.
8181
-- No value bytes follow in the NULL case.
8282
{-# INLINE encodeValue #-}
83-
encodeValue :: Maybe B.ByteString -> Encode
83+
encodeValue :: Maybe Encode -> Encode
8484
encodeValue Nothing = putWord32BE (-1)
85-
encodeValue (Just v) = putWord32BE (fromIntegral $ B.length v)
86-
<> putByteString v
85+
encodeValue (Just v) = putWord32BE (fromIntegral $ getEncodeLen v) <> v
8786

8887
{-# INLINE encodeFormat #-}
8988
encodeFormat :: Format -> Encode

src/Database/PostgreSQL/Protocol/Store/Encode.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ instance Monoid Encode where
2020
{-# INLINE mappend #-}
2121
(Encode len1 f1) `mappend` (Encode len2 f2) = Encode (len1 + len2) (f1 *> f2)
2222

23+
instance Show Encode where
24+
show (Encode len _) = "Encode instance of length " ++ show len
25+
2326
{-# INLINE getEncodeLen #-}
2427
getEncodeLen :: Encode -> Int
2528
getEncodeLen (Encode len _) = len

src/Database/PostgreSQL/Protocol/Types.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Data.Hashable (Hashable)
1616
import Data.ByteString as B(ByteString)
1717
import qualified Data.ByteString.Lazy as BL(ByteString)
1818
import Data.Vector (Vector)
19+
import Database.PostgreSQL.Protocol.Store.Encode (Encode)
1920

2021
-- Common
2122
newtype Oid = Oid { unOid :: Word32 } deriving (Show, Eq)
@@ -128,9 +129,9 @@ data AuthResponse
128129
data ClientMessage
129130
= Bind !PortalName !StatementName
130131
!Format -- parameter format code, one format for all
131-
![Maybe ByteString] -- the values of parameters, Nothing
132+
![Maybe Encode] -- the values of parameters, Nothing
132133
-- is recognized as NULL
133-
!Format -- to apply code to all result columns
134+
!Format -- to apply code to all result columns
134135
-- Postgres use one command `close` for closing both statements and
135136
-- portals, but we distinguish them
136137
| CloseStatement !StatementName

tests/Codecs/QuickCheck.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ makeCodecProperty
3535
-> Oid -> (a -> Encode) -> PD.FieldDecoder a
3636
-> a -> Property
3737
makeCodecProperty c oid encoder fd v = monadicIO $ do
38-
let bs = runEncode $ encoder v
39-
q = Query "SELECT $1" [(oid, Just bs)]
38+
let q = Query "SELECT $1" [(oid, Just $ encoder v)]
4039
Binary Binary AlwaysCache
4140
decoder = PD.dataRowHeader *> PD.getNonNullable fd
4241
r <- run $ do
@@ -58,8 +57,7 @@ makeCodecEncodeProperty
5857
-> (a -> String)
5958
-> a -> Property
6059
makeCodecEncodeProperty c oid queryString encoder fPrint v = monadicIO $ do
61-
let bs = runEncode $ encoder v
62-
q = Query queryString [(oid, Just bs)]
60+
let q = Query queryString [(oid, Just $ encoder v)]
6361
Binary Text AlwaysCache
6462
decoder = PD.dataRowHeader *> PD.getNonNullable PD.bytea
6563
r <- run $ do

tests/Driver.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Database.PostgreSQL.Protocol.Store.Decode
2323
import Database.PostgreSQL.Protocol.Decoders
2424

2525
import Database.PostgreSQL.Protocol.Codecs.Decoders
26+
import Database.PostgreSQL.Protocol.Codecs.Encoders as PE
2627

2728
import Connection
2829

@@ -45,11 +46,13 @@ testDriver = testGroup "Driver"
4546
]
4647

4748
makeQuery1 :: B.ByteString -> Query
48-
makeQuery1 n = Query "SELECT $1" [(Oid 23, Just n)] Text Text AlwaysCache
49+
makeQuery1 n = Query "SELECT $1" [(Oid 23, Just $ PE.bytea n )]
50+
Text Text AlwaysCache
4951

5052
makeQuery2 :: B.ByteString -> B.ByteString -> Query
5153
makeQuery2 n1 n2 = Query "SELECT $1 + $2"
52-
[(Oid 23, Just n1), (Oid 23, Just n2)] Text Text AlwaysCache
54+
[(Oid 23, Just $ PE.bytea n1), (Oid 23, Just $ PE.bytea n2)]
55+
Text Text AlwaysCache
5356

5457
fromRight :: Either e a -> a
5558
fromRight (Right v) = v
@@ -140,8 +143,10 @@ checkInvalidResult conn n = readNextData conn >>=
140143
testInvalidBatch :: IO ()
141144
testInvalidBatch = do
142145
let rightQuery = makeQuery1 "5"
143-
q1 = Query "SEL $1" [(Oid 23, Just "5")] Text Text NeverCache
144-
q2 = Query "SELECT $1" [(Oid 23, Just "a")] Text Text NeverCache
146+
q1 = Query "SEL $1" [(Oid 23, Just $ PE.bytea "5")]
147+
Text Text NeverCache
148+
q2 = Query "SELECT $1" [(Oid 23, Just $ PE.bytea "a")]
149+
Text Text NeverCache
145150
q4 = Query "SELECT $1" [] Text Text NeverCache
146151

147152
assertInvalidBatch "Parse error" [q1]

tests/Protocol.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Database.PostgreSQL.Driver.StatementStorage
1212
import Database.PostgreSQL.Driver.Query
1313
import Database.PostgreSQL.Driver.Error
1414
import Database.PostgreSQL.Protocol.Types
15+
import Database.PostgreSQL.Protocol.Codecs.Encoders as PE
1516

1617
import Connection
1718

@@ -50,7 +51,7 @@ testExtendedQuery = withConnectionCommonAll $ \c -> do
5051
statement = StatementSQL "SELECT $1 + $2"
5152
sendMessage rawConn $ Parse sname statement [Oid 23, Oid 23]
5253
sendMessage rawConn $
53-
Bind pname sname Text [Just "1", Just "2"] Text
54+
Bind pname sname Text [Just $ PE.bytea "1", Just $ PE.bytea "2"] Text
5455
sendMessage rawConn $ Execute pname noLimitToReceive
5556
sendMessage rawConn $ DescribeStatement sname
5657
sendMessage rawConn $ DescribePortal pname

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy