notice.go 2.5 KB

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