8000 Only protocol types left in Protocol folder · postgres-haskell/postgres-wire@6380fbb · GitHub
[go: up one dir, main page]

Skip to content

Commit 6380fbb

Browse files
Only protocol types left in Protocol folder
1 parent 034713e commit 6380fbb

File tree

7 files changed

+91
-44
lines changed

7 files changed

+91
-44
lines changed

postgres-wire.cabal

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ cabal-version: >=1.10
1616
library
1717
hs-source-dirs: src
1818
exposed-modules: Database.PostgreSQL.Protocol
19+
, Database.PostgreSQL.Connection
20+
, Database.PostgreSQL.Settings
21+
, Database.PostgreSQL.StatementStorage
22+
, Database.PostgreSQL.Types
23+
1924
, Database.PostgreSQL.Protocol.Types
20-
, Database.PostgreSQL.Protocol.Settings
21-
, Database.PostgreSQL.Protocol.Connection
2225
, Database.PostgreSQL.Protocol.Encoders
2326
, Database.PostgreSQL.Protocol.Decoders
24-
, Database.PostgreSQL.Protocol.StatementStorage
2527
build-depends: base >= 4.7 && < 5
2628
, bytestring
2729
, socket

src/Database/PostgreSQL/Protocol/Connection.hs renamed to src/Database/PostgreSQL/Connection.hs

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{-# language ExistentialQuantification #-}
66
{-# language TypeSynonymInstances #-}
77
{-# language FlexibleInstances #-}
8-
module Database.PostgreSQL.Protocol.Connection where
8+
module Database.PostgreSQL.Connection where
99

1010

1111
import qualified Data.ByteString as B
@@ -30,23 +30,38 @@ import System.Socket.Family.Unix
3030
import Data.Time.Clock.POSIX
3131
import Control.Concurrent.Chan.Unagi
3232

33-
import Database.PostgreSQL.Protocol.Settings
3433
import Database.PostgreSQL.Protocol.Encoders
3534
import Database.PostgreSQL.Protocol.Decoders
3635
import Database.PostgreSQL.Protocol.Types
37-
import Database.PostgreSQL.Protocol.StatementStorage
36+
import Database.PostgreSQL.Settings
37+
import Database.PostgreSQL.StatementStorage
38+
import Database.PostgreSQL.Types
3839

3940

4041
type UnixSocket = Socket Unix Stream Unix
4142
-- data Connecti 8000 on = Connection (Socket Inet6 Stream TCP)
4243
data Connection = Connection
4344
{ connSocket :: UnixSocket
4445
, connReceiverThread :: ThreadId
45-
, connOutChan :: OutChan ServerMessage
46+
-- Chan for only data messages
47+
, connDataOutChan :: OutChan (Either Error DataMessage)
48+
-- Chan for all messages that filter
49+
, connAllOutChan :: OutChan ServerMessage
4650
, connStatementStorage :: StatementStorage
4751
, connParameters :: ConnectionParameters
4852
}
4953

54+
newtype ServerMessageFilter = ServerMessageFilter (ServerMessage -> Bool)
55+
56+
type NotificationHandler = Notification -> IO ()
57+
58+
-- All possible errors
59+
data Error
60+
= PostgresError ErrorDesc
61+
| ImpossibleError
62+
63+
data DataMessage = DataMessage B.ByteString
64+
5065
address :: SocketAddress Unix
5166
address = fromJust $ socketAddressUnixPath "/var/run/postgresql/.s.PGSQL.5432"
5267

@@ -85,15 +100,11 @@ consStartupMessage stg = StartupMessage
85100
sendStartMessage :: UnixSocket -> StartMessage -> IO ()
86101
sendStartMessage sock msg = void $ do
87102
let smsg = toStrict . toLazyByteString $ encodeStartMessage msg
88-
-- putStrLn "sending message:"
89-
-- print smsg
90103
send sock smsg mempty
91104

92105
sendMessage :: UnixSocket -> ClientMessage -> IO ()
93106
sendMessage sock msg = void $ do
94107
let smsg = toStrict . toLazyByteString $ encodeClientMessage msg
95-
-- putStrLn "sending message:"
96-
-- print smsg
97108
send sock smsg mempty
98109

99110
readAuthMessage :: B.ByteString -> IO ()
@@ -107,33 +118,54 @@ readAuthMessage s =
107118
receiverThread :: UnixSocket -> InChan ServerMessage -> IO ()
108119
receiverThread sock chan = forever $ do
109120
r <- receive sock 4096 mempty
121+
print r
110122
go r
111123
where
112124
decoder = runGetIncremental decodeServerMessage
113125
go str = case pushChunk decoder str of
114126
BG.Done rest _ v -> do
115-
print v
116-
writeChan chan v
127+
putStrLn $ "Received: " ++ show v
117128
unless (B.null rest) $ go rest
118129
BG.Partial _ -> error "Partial"
119130
BG.Fail _ _ e -> error e
131+
dispatch :: ServerMessage -> IO ()
132+
-- dont receiving at this phase
133+
dispatch (BackendKeyData _ _) = pure ()
134+
dispatch (BindComplete) = pure ()
135+
dispatch CloseComplete = pure ()
136+
-- maybe return command result too
137+
dispatch (CommandComplete _) = pure ()
138+
dispatch r@(DataRow _) = writeChan chan r
139+
-- TODO throw error here
140+
dispatch EmptyQueryResponse = pure ()
141+
-- TODO throw error here
142+
dispatch (ErrorResponse desc) = pure ()
143+
-- TODO
144+
dispatch NoData = pure ()
145+
dispatch (NoticeResponse _) = pure ()
146+
-- TODO handle notifications
147+
dispatch (NotificationResponse n) = pure ()
148+
-- Ignore here ?
149+
dispatch (ParameterDescription _) = pure ()
150+
dispatch (ParameterStatus _ _) = pure ()
151+
dispatch (ParseComplete) = pure ()
152+
dispatch (PortalSuspended) = pure ()
153+
dispatch (ReadForQuery _) = pure ()
154+
dispatch (RowDescription _) = pure ()
120155

121156
data Query = Query
122157
{ qStatement :: B.ByteString
123158
, qOids :: V.Vector Oid
124159
, qValues :: V.Vector B.ByteString
160+
, qParamsFormat :: Format
161+
, qResultFormat :: Format
125162
} deriving (Show)
126163

127-
query1 = Query "SELECT $1 + $2" [Oid 23, Oid 23] ["1", "3"]
128-
query2 = Query "SELECT $1 + $2" [Oid 23, Oid 23] ["2", "3"]
129-
query3 = Query "SELECT $1 + $2" [Oid 23, Oid 23] ["3", "3"]
130-
query4 = Query "SELECT $1 + $2" [Oid 23, Oid 23] ["4", "3"]
131-
query5 = Query "SELECT * FROM a where v > $1 + $2 LIMIT 100" [Oid 23, Oid 23] ["5", "3"]
132-
-- query1 = QQuery "test1" "select sum(v) from a" [] []
133-
-- query2 = QQuery "test2" "select sum(v) from a" [] []
134-
-- query3 = QQuery "test3" "select sum(v) from a" [] []
135-
-- query4 = QQuery "test4" "select sum(v) from a" [] []
136-
-- query5 = QQuery "test5" "select sum(v) from a" [] []
164+
query1 = Query "SELECT $1 + $2" [Oid 23, Oid 23] ["1", "3"] Text Text
165+
query2 = Query "SELECT $1 + $2" [Oid 23, Oid 23] ["2", "3"] Text Text
166+
query3 = Query "SELECT $1 + $2" [Oid 23, Oid 23] ["3", "3"] Text Text
167+
query4 = Query "SELECT $1 + $2" [Oid 23, Oid 23] ["4", "3"] Text Text
168+
-- query5 = Query "SELECT * FROM a whereee v > $1 + $2 LIMIT 100" [Oid 23, Oid 23] ["5", "3"]
137169

138170
sendBatch :: Connection -> [Query] -> IO ()
139171
sendBatch conn qs = do
@@ -145,7 +177,8 @@ sendBatch conn qs = do
145177
let sname = StatementName ""
146178
pname = PortalName ""
147179
sendMessage s $ Parse sname (StatementSQL $ qStatement q) (qOids q)
148-
sendMessage s $ Bind pname sname Text (qValues q) Text
180+
sendMessage s $
181+
Bind pname sname (qParamsFormat q) (qValues q) (qResultFormat q)
149182
sendMessage s $ Execute pname noLimitToReceive
150183

151184

@@ -170,6 +203,9 @@ test = do
170203
-- readNextData :: Connection -> IO Data?
171204
-- readNextData = undefined
172205
--
206+
-- readNextServerMessage ?
207+
--
208+
--
173209
-- Simple Queries support or maybe dont support it
174210
-- because single text query may be send through extended protocol
175211
-- may be support for all standalone queries

src/Database/PostgreSQL/Protocol/Types.hs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
module Database.PostgreSQL.Protocol.Types where
22

3+
-- TODO
4+
-- * COPY subprotocol commands
5+
--
6+
-- * function call, is deprecated by postgres
7+
-- * AuthenticationKerberosV5 IS deprecated by postgres
8+
-- * AuthenticationSCMCredential IS deprecated since postgres 9.1
9+
-- * bind command can have different formats for parameters and results
10+
-- but we assume that there will be one format for all.
11+
312
import Data.Word (Word32, Word8)
413
import Data.Int (Int32, Int16)
514
import Data.Hashable (Hashable)
@@ -51,15 +60,6 @@ data CommandResult
5160
| CommandOk
5261
deriving (Show)
5362

54-
-- | Parameters of the current connection.
55-
-- We store only the parameters that cannot change after startup.
56-
-- For more information about additional parameters see documentation.
57-
data ConnectionParameters = ConnectionParameters
58-
{ paramServerVersion :: ServerVersion
59-
, paramServerEncoding :: ByteString -- ^ character set name
60-
, paramIntegerDatetimes :: Bool -- ^ True if integer datetimes used
61-
} deriving (Show)
62-
6363
-- | Server version contains major, minor, revision numbers.
6464
data ServerVersion = ServerVersion Word8 Word8 Word8
6565

@@ -226,12 +226,3 @@ data NoticeDesc = NoticeDesc
226226
, noticeSourceRoutine :: Maybe ByteString
227227
} deriving (Show)
228228

229-
-- TODO
230-
-- * COPY subprotocol commands
231-
-- * function call, is deprecated by postgres
232-
-- * AuthenticationKerberosV5 IS deprecated by postgres
233-
-- * AuthenticationSCMCredential IS deprecated since postgres 9.1
234-
-- * NOTICE bind command can have different formats for parameters and results
235-
-- but we assume that there will be one format for all.
236-
-- * We dont store parameters of connection that may change after startup
237-

src/Database/PostgreSQL/Protocol/Settings.hs renamed to src/Database/PostgreSQL/Settings.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{-# language OverloadedStrings #-}
22

3-
module Database.PostgreSQL.Protocol.Settings where
3+
module Database.PostgreSQL.Settings where
44

55
import Data.Word (Word16)
66
import Data.ByteString (ByteString)

src/Database/PostgreSQL/Protocol/StatementStorage.hs renamed to src/Database/PostgreSQL/StatementStorage.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Database.PostgreSQL.Protocol.StatementStorage where
1+
module Database.PostgreSQL.StatementStorage where
22

33
import qualified Data.HashTable.IO as H
44
import qualified Data.ByteString as B

src/Database/PostgreSQL/Types.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{-
2+
* We dont store parameters of connection that may change after startup
3+
-}
4+
module Database.PostgreSQL.Types where
5+
6+
import Data.ByteString (ByteString)
7+
8+
import Database.PostgreSQL.Protocol.Types
9+
10+
-- | Parameters of the current connection.
11+
-- We store only the parameters that cannot change after startup.
12+
-- For more information about additional parameters see documentation.
13+
data ConnectionParameters = ConnectionParameters
14+
{ paramServerVersion :: ServerVersion
15+
, paramServerEncoding :: ByteString -- ^ character set name
16+
, paramIntegerDatetimes :: Bool -- ^ True if integer datetimes used
17+
} deriving (Show)
18+

0 commit comments

Comments
 (0)
0