...

Source file src/github.com/lib/pq/notice_example_test.go

Documentation: github.com/lib/pq

     1  //go:build go1.10
     2  // +build go1.10
     3  
     4  package pq_test
     5  
     6  import (
     7  	"database/sql"
     8  	"fmt"
     9  	"log"
    10  
    11  	"github.com/lib/pq"
    12  )
    13  
    14  func ExampleConnectorWithNoticeHandler() {
    15  	name := ""
    16  	// Base connector to wrap
    17  	base, err := pq.NewConnector(name)
    18  	if err != nil {
    19  		log.Fatal(err)
    20  	}
    21  	// Wrap the connector to simply print out the message
    22  	connector := pq.ConnectorWithNoticeHandler(base, func(notice *pq.Error) {
    23  		fmt.Println("Notice sent: " + notice.Message)
    24  	})
    25  	db := sql.OpenDB(connector)
    26  	defer db.Close()
    27  	// Raise a notice
    28  	sql := "DO language plpgsql $$ BEGIN RAISE NOTICE 'test notice'; END $$"
    29  	if _, err := db.Exec(sql); err != nil {
    30  		log.Fatal(err)
    31  	}
    32  	// Output:
    33  	// Notice sent: test notice
    34  }
    35  

View as plain text