|
| 1 | +// +build go1.10 |
| 2 | + |
| 3 | +package pq |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "database/sql/driver" |
| 8 | +) |
| 9 | + |
| 10 | +// NoticeHandler returns the notice handler on the given connection, if any. A |
| 11 | +// runtime panic occurs if c is not a pq connection. This is rarely used |
| 12 | +// directly, use ConnectorNoticeHandler and ConnectorWithNoticeHandler instead. |
| 13 | +func NoticeHandler(c driver.Conn) func(*Error) { |
| 14 | + return c.(*conn).noticeHandler |
| 15 | +} |
| 16 | + |
| 17 | +// SetNoticeHandler sets the given notice handler on the given connection. A |
| 18 | +// runtime panic occurs if c is not a pq connection. A nil handler may be used |
| 19 | +// to unset it. This is rarely used directly, use ConnectorNoticeHandler and |
| 20 | +// ConnectorWithNoticeHandler instead. |
| 21 | +// |
| 22 | +// Note: Notice handlers are executed synchronously by pq meaning commands |
| 23 | +// won't continue to be processed until the handler returns. |
| 24 | +func SetNoticeHandler(c driver.Conn, handler func(*Error)) { |
| 25 | + c.(*conn).noticeHandler = handler |
| 26 | +} |
| 27 | + |
| 28 | +// NoticeHandlerConnector wraps a regular connector and sets a notice handler |
| 29 | +// on it. |
| 30 | +type NoticeHandlerConnector struct { |
| 31 | + driver.Connector |
| 32 | + noticeHandler func(*Error) |
| 33 | +} |
| 34 | + |
| 35 | +// Connect calls the underlying connector's connect method and then sets the |
| 36 | +// notice handler. |
| 37 | +func (n *NoticeHandlerConnector) Connect(ctx context.Context) (driver.Conn, error) { |
| 38 | + c, err := n.Connector.Connect(ctx) |
| 39 | + if err == nil { |
| 40 | + SetNoticeHandler(c, n.noticeHandler) |
| 41 | + } |
| 42 | + return c, err |
| 43 | +} |
| 44 | + |
| 45 | +// ConnectorNoticeHandler returns the currently set notice handler, if any. If |
| 46 | +// the given connector is not a result of ConnectorWithNoticeHandler, nil is |
| 47 | +// returned. |
| 48 | +func ConnectorNoticeHandler(c driver.Connector) func(*Error) { |
| 49 | + if c, ok := c.(*NoticeHandlerConnector); ok { |
| 50 | + return c.noticeHandler |
| 51 | + } |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +// ConnectorWithNoticeHandler creates or sets the given handler for the given |
| 56 | +// connector. If the given connector is a result of calling this function |
| 57 | +// previously, it is simply set on the given connector and returned. Otherwise, |
| 58 | +// this returns a new connector wrapping the given one and setting the notice |
| 59 | +// handler. A nil notice handler may be used to unset it. |
| 60 | +// |
| 61 | +// The returned connector is intended to be used with database/sql.OpenDB. |
| 62 | +// |
| 63 | +// Note: Notice handlers are executed synchronously by pq meaning commands |
| 64 | +// won't continue to be processed until the handler returns. |
| 65 | +func ConnectorWithNoticeHandler(c driver.Connector, handler func(*Error)) *NoticeHandlerConnector { |
| 66 | + if c, ok := c.(*NoticeHandlerConnector); ok { |
| 67 | + c.noticeHandler = handler |
| 68 | + return c |
| 69 | + } |
| 70 | + return &NoticeHandlerConnector{Connector: c, noticeHandler: handler} |
| 71 | +} |
0 commit comments