mirror of https://github.com/jackc/pgx.git
simplify duplicate `pgx` registration guard
The binary search is overkill here. Readability first.pull/1511/head
parent
c5daa3a814
commit
c2e278e5d4
|
@ -66,7 +66,6 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -87,9 +86,9 @@ func init() {
|
||||||
configs: make(map[string]*pgx.ConnConfig),
|
configs: make(map[string]*pgx.ConnConfig),
|
||||||
}
|
}
|
||||||
|
|
||||||
drivers := sql.Drivers()
|
// if pgx driver was already registered by different pgx major version then we
|
||||||
// if pgx driver was already registered by different pgx major version then we skip registration under the default name.
|
// skip registration under the default name.
|
||||||
if i := sort.SearchStrings(sql.Drivers(), "pgx"); len(drivers) >= i || drivers[i] != "pgx" {
|
if !contains(sql.Drivers(), "pgx") {
|
||||||
sql.Register("pgx", pgxDriver)
|
sql.Register("pgx", pgxDriver)
|
||||||
}
|
}
|
||||||
sql.Register("pgx/v5", 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.
|
// OptionOpenDB options for configuring the driver when opening a new db pool.
|
||||||
type OptionOpenDB func(*connector)
|
type OptionOpenDB func(*connector)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue