mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
simplify duplicate pgx
registration guard
The binary search is overkill here. Readability first.
This commit is contained in:
parent
c5daa3a814
commit
c2e278e5d4
@ -66,7 +66,6 @@ import (
|
||||
"math"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -87,9 +86,9 @@ func init() {
|
||||
configs: make(map[string]*pgx.ConnConfig),
|
||||
}
|
||||
|
||||
drivers := sql.Drivers()
|
||||
// if pgx driver was already registered by different pgx major version then we skip registration under the default name.
|
||||
if i := sort.SearchStrings(sql.Drivers(), "pgx"); len(drivers) >= i || drivers[i] != "pgx" {
|
||||
// if pgx driver was already registered by different pgx major version then we
|
||||
// skip registration under the default name.
|
||||
if !contains(sql.Drivers(), "pgx") {
|
||||
sql.Register("pgx", pgxDriver)
|
||||
}
|
||||
sql.Register("pgx/v5", pgxDriver)
|
||||
@ -111,6 +110,17 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO replace by slices.Contains when experimental package will be merged to stdlib
|
||||
// https://pkg.go.dev/golang.org/x/exp/slices#Contains
|
||||
func contains(list []string, y string) bool {
|
||||
for _, x := range list {
|
||||
if x == y {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// OptionOpenDB options for configuring the driver when opening a new db pool.
|
||||
type OptionOpenDB func(*connector)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user