0 | module Postgres.Notification
3 | import Postgres.FFI.Utility
4 | import Postgres.Data.Conn
5 | import Postgres.Data.ResultStatus
6 | import Postgres.DB.Core
7 | import Postgres.DB.Wait
8 | import Postgres.Result
14 | PGnotify = Struct "PGnotify" [("relname", (Ptr String)), ("be_pid", Int), ("extra", (Ptr String))]
17 | record Notification where
18 | constructor MkNotification
22 | notificationChannel : PGnotify -> String
23 | notificationChannel n = prim__string_value $
getField n "relname"
25 | notificationPayload : PGnotify -> String
26 | notificationPayload n = prim__string_value $
getField n "extra"
28 | notification : PGnotify -> Notification
29 | notification n = MkNotification (notificationChannel n) (notificationPayload n)
31 | %foreign libpq "PQnotifies"
32 | prim__dbGetNextNotification : Ptr PGconn -> PrimIO (Ptr PGnotify)
41 | %foreign cHelper "notify_struct"
42 | prim__dbNotifyStruct : Ptr PGnotify -> PGnotify
44 | %foreign libpq "PQfreemem"
45 | prim__dbFreeNotifyStruct : Ptr PGnotify -> PrimIO ()
50 | notificationStruct : HasIO io => Ptr PGnotify -> io PGnotify
51 | notificationStruct ptr = let res = prim__dbNotifyStruct ptr in
52 | do primIO $
prim__dbFreeNotifyStruct ptr
55 | %foreign cHelper "is_null"
56 | prim__isNullNotifyStruct : Ptr PGnotify -> Int
58 | isNullNotification : Ptr PGnotify -> Bool
59 | isNullNotification ptr = intToBool $
prim__isNullNotifyStruct ptr
67 | pgListen : (channel: String) -> Conn -> IO ResultStatus
68 | pgListen channel conn = withExecResult conn ("LISTEN " ++ channel) (\r => pure $
pgResultStatus r)
85 | pgNextNotification : Conn -> IO (Maybe Notification)
86 | pgNextNotification (MkConn conn) = do True <- pgConsumeInput (MkConn conn)
87 | | False => pure Nothing
88 | notify <- primIO $
prim__dbGetNextNotification conn
89 | if isNullNotification notify
91 | else do derefNotify <- notificationStruct notify
92 | pure $
Just (notification derefNotify)
101 | cycle : Conn -> IO Notification
102 | cycle conn = do True <- pgWait conn
103 | | False => cycle conn
104 | Nothing <- pgNextNotification conn
119 | pgNotificationStream : Conn -> Stream (IO Notification)
120 | pgNotificationStream conn = next :: (pgNotificationStream conn) where
121 | next : IO Notification
122 | next = do Nothing <- pgNextNotification conn