|
165 | 165 | ---@field private retryAttempted number number of attempts to reconnect to the database
|
166 | 166 | ---@field private conn PGconn native connection object (do not use it directly, otherwise be careful not to store it anywhere else, otherwise closing connection will be impossible)
|
167 | 167 | ---@field private queries { push: fun(self, q: PGQuery), prepend: fun(self, q: PGQuery), pop: (fun(self): PGQuery), size: fun(self): number } list of queries
|
168 |
| ----@field private notifyCallback function? callback for NOTIFY messages |
169 | 168 | ---@field package acquired boolean
|
170 | 169 | ---@field package pool PGPool?
|
171 | 170 | local Client = {}
|
@@ -235,7 +234,10 @@ function Client:connect(callback)
|
235 | 234 | if ok then
|
236 | 235 | ---@cast conn PGconn
|
237 | 236 | self.conn = conn
|
238 |
| - pcall(self.conn.setNotifyCallback, self.conn, self.notifyCallback) |
| 237 | + self.conn:setNotifyCallback(function(channel, payload, backendPID) |
| 238 | + self:onNotify(channel, payload, backendPID) |
| 239 | + end) |
| 240 | + |
239 | 241 | xpcall(callback, ErrorNoHaltWithStack, ok)
|
240 | 242 | self:processQueue()
|
241 | 243 | else
|
@@ -481,15 +483,6 @@ function Client:pendingQueries()
|
481 | 483 | return self.queries:size()
|
482 | 484 | end
|
483 | 485 |
|
484 |
| ---- Sets a callback for NOTIFY messages |
485 |
| ----@param callback fun(channel: string, payload: string, backendPID: number) |
486 |
| -function Client:setNotifyCallback(callback) |
487 |
| - self.notifyCallback = callback |
488 |
| - if self.conn then |
489 |
| - self.conn:setNotifyCallback(callback) |
490 |
| - end |
491 |
| -end |
492 |
| - |
493 | 486 | --- Returns the database name of the connection.
|
494 | 487 | ---@return string
|
495 | 488 | function Client:db()
|
@@ -657,6 +650,15 @@ function Client:release()
|
657 | 650 | pool:processQueue() -- after client was release, we need to process pool queue
|
658 | 651 | end
|
659 | 652 |
|
| 653 | +--- This function is called when NOTIFY message is received |
| 654 | +--- |
| 655 | +--- You can set it to your own function to handle NOTIFY messages |
| 656 | +---@param channel string |
| 657 | +---@param payload string |
| 658 | +---@param backendPID number |
| 659 | +function Client:onNotify(channel, payload, backendPID) |
| 660 | +end |
| 661 | + |
660 | 662 | --- Creates a new client with given connection url
|
661 | 663 | --- ```lua
|
662 | 664 | --- local client = async_postgres.Client("postgresql://user:password@localhost:5432/database")
|
|
0 commit comments