···337337For most queries, you should use the `conn.query(...)`, `conn.row(...)` or `conn.exec(...)` methods. For queries with parameters, these methods look like:
338338339339```zig
340340-var stmt = try Stmt.init(conn, opts)
340340+var stmt = try Stmt.init(conn, opts);
341341errdefer stmt.deinit();
342342343343try stmt.prepare(sql, null);
···518518519519When reading a `JSON` or `JSONB` column with `[]u8`, the serialized JSON will be returned.
520520521521+### PgLSN, xid8, xid
522522+PgLSN and xid8 can be bound and read as i64.
523523+524524+xid can be bound and read as i32.
525525+521526## Listen / Notify
522527You can create a `pg.Listener` either from an existing `Pool` or directly.
523528···538543});
539544540545// add 1 or more channels to listen to
541541-try listener.listen("chan_1");
542542-try listener.listen("chan_2");
546546+try listener.listen("chan_1", .{});
547547+try listener.listen("chan_2", .{});
543548544549// .next() blocks until there's a notification or an error
545550while (listener.next()) |notification| {
···564569565570// same as above
566571```
572572+573573+### Listen Timeout
574574+When calling `listen`, a timeout in milliseconds can be specified:
575575+576576+```zig
577577+try listener.listen("chan_1", .{});
578578+```
579579+580580+If multiple calls to `listen` are made, the last timeout will be used. If no message is received in `timeout` milliseconds, `next()` will return `null` and `listener.err.?.err == error.WouldBlock`.
567581568582## Reconnects
569583A listener will not automatically reconnect on error/disconnect. The pub/sub nature of LISTEN/NOTIFY mean that delivery is at-most-once and auto-reconnecting can hide that fact. Put the above code in a `while (true) {...}` loop.
+8-3
src/listener.zig
···7878 }
7979 }
80808181- pub fn listen(self: *Listener, channel: []const u8) !void {
8181+ const ListenOpts = struct {
8282+ timeout: u32 = 0,
8383+ };
8484+ pub fn listen(self: *Listener, channel: []const u8, opts: ListenOpts) !void {
8285 // LISTEN doesn't support parameterized queries. It has to be a simple query.
8386 // We don't use proto.Query because we want to quote the identifier.
8487···135138 else => return error.UnexpectedDBMessage,
136139 }
137140 }
141141+142142+ try self._reader.startFlow(null, opts.timeout);
138143 }
139144140145 pub fn next(self: *Listener) ?NotificationResponse {
···209214}
210215211216fn testListener(l: *Listener) !void {
212212- try l.listen("chan-1");
213213- try l.listen("chan_2");
217217+ try l.listen("chan-1", .{});
218218+ try l.listen("chan_2", .{});
214219215220 const thrd = try std.Thread.spawn(.{}, testNotifier, .{});
216221 {