notice.go 2.5 KB

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