@@ -27,6 +27,7 @@ import Crypto.Hash (hash, Digest, MD5)
27
27
import Database.PostgreSQL.Protocol.Encoders
28
28
import Database.PostgreSQL.Protocol.Decoders
29
29
import Database.PostgreSQL.Protocol.Types
30
+
30
31
import Database.PostgreSQL.Driver.Settings
31
32
import Database.PostgreSQL.Driver.StatementStorage
32
33
import Database.PostgreSQL.Driver.Types
@@ -47,7 +48,7 @@ type ServerMessageFilter = ServerMessage -> Bool
47
48
48
49
type NotificationHandler = Notification -> IO ()
49
50
50
- -- All possible errors
51
+ -- All possible at errors
51
52
data Error
52
53
= PostgresError ErrorDesc
53
54
| ImpossibleError
@@ -143,7 +144,6 @@ authorize rawConn settings = do
143
144
case pushChunk (runGetIncremental decodeAuthResponse) r of
144
145
BG. Done rest _ r -> case r of
145
146
AuthenticationOk -> do
146
- putStrLn " Auth ok"
147
147
-- TODO parse parameters
148
148
pure $ Right $ parseParameters rest
149
149
AuthenticationCleartextPassword ->
@@ -162,13 +162,11 @@ authorize rawConn settings = do
162
162
performPasswordAuth
163
163
:: PasswordText -> IO (Either AuthError ConnectionParameters )
164
164
performPasswordAuth password = do
165
- putStrLn $ " sending password" ++ show password
166
165
sendMessage rawConn $ PasswordMessage password
167
166
r <- rReceive rawConn 4096
168
167
case pushChunk (runGetIncremental decodeAuthResponse) r of
169
168
BG. Done rest _ r -> case r of
170
169
AuthenticationOk -> do
171
- putStrLn " Auth ok"
172
170
pure $ Right $ parseParameters rest
173
171
AuthErrorResponse desc ->
174
172
pure $ Left $ AuthPostgresError desc
@@ -305,6 +303,7 @@ data Query = Query
305
303
, qResultFormat :: Format
306
304
} deriving (Show )
307
305
306
+ -- | Public
308
307
sendBatch :: Connection -> [Query ] -> IO ()
309
308
sendBatch conn = traverse_ sendSingle
310
309
where
@@ -317,15 +316,27 @@ sendBatch conn = traverse_ sendSingle
317
316
Bind pname sname (qParamsFormat q) (qValues q) (qResultFormat q)
318
317
sendMessage s $ Execute pname noLimitToReceive
319
318
319
+ -- | Public
320
+ sendBatchAndSync :: Connection -> [Query ] -> IO ()
321
+ sendBatchAndSync conn qs = sendBatch conn qs >> sendSync conn
322
+
323
+ -- | Public
324
+ sendBatchAndFlush :: Connection -> [Query ] -> IO ()
325
+ sendBatchAndFlush conn qs = sendBatch conn qs >> sendFlush conn
326
+
327
+ -- | Public
320
328
sendSync :: Connection -> IO ()
321
329
sendSync conn = sendMessage (connRawConnection conn) Sync
322
330
331
+ -- | Public
323
332
sendFlush :: Connection -> IO ()
324
333
sendFlush conn = sendMessage (connRawConnection conn) Flush
325
334
335
+ -- | Public
326
336
readNextData :: Connection -> IO (Either Error DataMessage )
327
337
readNextData conn = readChan $ connOutDataChan conn
328
338
339
+ -- | Public
329
340
-- SHOULD BE called after every sended `Sync` message
330
341
-- skips all messages except `ReadyForQuery`
331
342
readReadyForQuery :: Connection -> IO (Either Error () )
@@ -347,6 +358,7 @@ waitReadyForQueryCollect conn = do
347
358
ReadForQuery {} -> pure []
348
359
m -> (m: ) <$> waitReadyForQueryCollect conn
349
360
361
+ -- | Public
350
362
describeStatement
351
363
:: Connection
352
364
-> StatementSQL
@@ -367,52 +379,3 @@ describeStatement conn stmt = do
367
379
xs -> maybe (error " Impossible happened" ) (Left . PostgresError )
368
380
$ findFirstError xs
369
381
370
- query1 = Query " SELECT $1 + $2" [Oid 23 , Oid 23 ] [" 1" , " 3" ] Text Text
371
- query2 = Query " SELECT $1 + $2" [Oid 23 , Oid 23 ] [" a" , " 3" ] Text Text
372
- query3 = Query " SELECT $1 + $2" [Oid 23 , Oid 23 ] [" 3" , " 3" ] Text Text
373
- query4 = Query " SELECT $1 + $2" [Oid 23 , Oid 23 ] [" 4" , " 3" ] Text Text
374
-
375
-
376
- test :: IO ()
377
- test = do
378
- c <- connect defaultConnectionSettings
379
- sendBatch c queries
380
- sendSync c
381
- readResults c $ length queries
382
- readReadyForQuery c >>= print
383
- close c
384
- where
385
- queries = [query1, query2, query3, query4 ]
386
- readResults c 0 = pure ()
387
- readResults c n = do
388
- r <- readNextData c
389
- print r
390
- case r of
391
- Left _ -> pure ()
392
- Right _ -> readResults c $ n - 1
393
-
394
- -- sendBatchAndSync :: IsQuery a => [a] -> Connection -> IO ()
395
- -- sendBatchAndSync = undefined
396
-
397
- -- sendBatchAndFlush :: IsQuery a => [a] -> Connection -> IO ()
398
- -- sendBatchAndFlush = undefined
399
-
400
- -- internal helper
401
- -- sendBatch :: IsQuery a => [a] -> Connection -> IO ()
402
- -- sendBatch = undefined
403
-
404
-
405
- testDescribe1 :: IO ()
406
- testDescribe1 = do
407
- c <- connect defaultConnectionSettings
408
- r <- describeStatement c $ StatementSQL " start transaction"
409
- print r
410
- close c
411
-
412
- testDescribe2 :: IO ()
413
- testDescribe2 = do
414
- c <- connect defaultConnectionSettings
415
- r <- describeStatement c $ StatementSQL " select count(*) from a where v > $1"
416
- print r
417
- close c
418
-
0 commit comments