|
91 | 91 | ---@field unescapeBytea fun(self: PGconn, str: string): string
|
92 | 92 | ---@field setNotifyCallback fun(self: PGconn, callback: fun(channel: string, payload: string, backendPID: number))
|
93 | 93 | ---@field getNotifyCallback fun(self: PGconn): fun(channel: string, payload: string, backendPID: number)
|
| 94 | +---@field setNoticeCallback fun(self: PGconn, callback: fun(message: string, errdata: table)) |
| 95 | +---@field getNoticeCallback fun(self: PGconn): fun(message: string, errdata: table) |
94 | 96 | ---@field setArrayResult fun(self: PGconn, enabled: boolean)
|
95 | 97 | ---@field getArrayResult fun(self: PGconn): boolean
|
96 | 98 |
|
@@ -119,10 +121,6 @@ if async_postgres.LUA_API_VERSION ~= 1 then
|
119 | 121 | "expected 1, got " .. async_postgres.LUA_API_VERSION)
|
120 | 122 | end
|
121 | 123 |
|
122 |
| - |
123 |
| ----@class async_postgres_module : async_postgres |
124 |
| -local module = setmetatable({}, { __index = async_postgres }) |
125 |
| - |
126 | 124 | local Queue = {}
|
127 | 125 | Queue.__index = Queue
|
128 | 126 |
|
@@ -244,6 +242,9 @@ function Client:connect(callback)
|
244 | 242 | self.conn:setNotifyCallback(function(channel, payload, backendPID)
|
245 | 243 | xpcall(self.onNotify, self.errorHandler, self, channel, payload, backendPID)
|
246 | 244 | end)
|
| 245 | + self.conn:setNoticeCallback(function(message, errdata) |
| 246 | + xpcall(self.onNotice, self.errorHandler, self, message, errdata) |
| 247 | + end) |
247 | 248 |
|
248 | 249 | xpcall(callback, self.errorHandler, ok)
|
249 | 250 | self:processQueue()
|
|
663 | 664 | function Client:onNotify(channel, payload, backendPID)
|
664 | 665 | end
|
665 | 666 |
|
| 667 | +--- This **event** function is called when server sends a notice/warning message |
| 668 | +--- during a query |
| 669 | +--- |
| 670 | +--- You can set it to your own function to handle notices |
| 671 | +--- By default this function calls `ErrorNoHalt` |
| 672 | +---@param message string notice message |
| 673 | +---@param errdata table additional data |
| 674 | +function Client:onNotice(message, errdata) |
| 675 | + ErrorNoHalt(tostring(self) .. " " .. message) |
| 676 | +end |
| 677 | + |
666 | 678 | --- This **event** function is called whenever an error occurs inside connect/query callback.
|
667 | 679 | ---
|
668 | 680 | --- You can set it to your own function to handle errors.
|
|
696 | 708 | --- connectiong url format can be found at https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
|
697 | 709 | ---@param url string connection url, see libpq documentation for more information
|
698 | 710 | ---@return PGClient
|
699 |
| -function module.Client(url) |
| 711 | +function async_postgres.Client(url) |
700 | 712 | ---@class PGClient
|
701 | 713 | local client = setmetatable({
|
702 | 714 | url = url,
|
@@ -891,7 +903,7 @@ function Pool:processQueue()
|
891 | 903 | local waiters = self.queue:size()
|
892 | 904 | local threshold = clients * self.threshold
|
893 | 905 | if clients < self.max and waiters > threshold then
|
894 |
| - local client = module.Client(self.url) |
| 906 | + local client = async_postgres.Client(self.url) |
895 | 907 | client.onError = function(client, message)
|
896 | 908 | return self:onError(message)
|
897 | 909 | end
|
@@ -1079,11 +1091,12 @@ end
|
1079 | 1091 | --- Creates a new connection pool with given connection url,
|
1080 | 1092 | --- then use :connect() to get available connection,
|
1081 | 1093 | --- and then :release() to release it back to the pool
|
1082 |
| -function module.Pool(url) |
| 1094 | +---@return PGPool |
| 1095 | +function async_postgres.Pool(url) |
1083 | 1096 | ---@class PGPool
|
1084 | 1097 | local pool = setmetatable({
|
1085 | 1098 | url = url,
|
1086 |
| - clients = { module.Client(url) }, |
| 1099 | + clients = { async_postgres.Client(url) }, |
1087 | 1100 | queue = Queue.new(),
|
1088 | 1101 | max = 10,
|
1089 | 1102 | threshold = 5,
|
|
0 commit comments