Implements `io.Closer` for `DB`

Fix #18
pull/20/head
Lonre Wang 2022-04-18 22:53:01 +08:00
parent 3eb5a72123
commit 01ce253442
5 changed files with 30 additions and 0 deletions

View File

@ -38,6 +38,11 @@ func (s SQLAdapter) BeginTx(ctx context.Context) (ksql.Tx, error) {
return SQLTx{Tx: tx}, err return SQLTx{Tx: tx}, err
} }
// Close implements the io.Closer interface
func (s SQLAdapter) Close() error {
return s.DB.Close()
}
// SQLTx is used to implement the DBAdapter interface and implements // SQLTx is used to implement the DBAdapter interface and implements
// the Tx interface // the Tx interface
type SQLTx struct { type SQLTx struct {

View File

@ -42,6 +42,12 @@ func (p PGXAdapter) BeginTx(ctx context.Context) (ksql.Tx, error) {
return PGXTx{tx}, err return PGXTx{tx}, err
} }
// Close implements the io.Closer interface
func (p PGXAdapter) Close() error {
p.db.Close()
return nil
}
// PGXResult is used to implement the DBAdapter interface and implements // PGXResult is used to implement the DBAdapter interface and implements
// the Result interface // the Result interface
type PGXResult struct { type PGXResult struct {

View File

@ -38,6 +38,11 @@ func (s SQLAdapter) BeginTx(ctx context.Context) (ksql.Tx, error) {
return SQLTx{Tx: tx}, err return SQLTx{Tx: tx}, err
} }
// Close implements the io.Closer interface
func (s SQLAdapter) Close() error {
return s.DB.Close()
}
// SQLTx is used to implement the DBAdapter interface and implements // SQLTx is used to implement the DBAdapter interface and implements
// the Tx interface // the Tx interface
type SQLTx struct { type SQLTx struct {

View File

@ -38,6 +38,11 @@ func (s SQLAdapter) BeginTx(ctx context.Context) (ksql.Tx, error) {
return SQLTx{Tx: tx}, err return SQLTx{Tx: tx}, err
} }
// Close implements the io.Closer interface
func (s SQLAdapter) Close() error {
return s.DB.Close()
}
// SQLTx is used to implement the DBAdapter interface and implements // SQLTx is used to implement the DBAdapter interface and implements
// the Tx interface // the Tx interface
type SQLTx struct { type SQLTx struct {

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io"
"reflect" "reflect"
"strings" "strings"
"unicode" "unicode"
@ -890,6 +891,14 @@ func (c DB) Transaction(ctx context.Context, fn func(Provider) error) error {
} }
} }
func (c DB) Close() error {
closer, ok := c.db.(io.Closer)
if ok {
return closer.Close()
}
return nil
}
type nopScanner struct{} type nopScanner struct{}
var nopScannerValue = reflect.ValueOf(&nopScanner{}).Interface() var nopScannerValue = reflect.ValueOf(&nopScanner{}).Interface()