taldir

Directory service to resolve wallet mailboxes by messenger addresses
Log | Files | Refs | Submodules | README | LICENSE

notice.go (2528B)


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