simplify duplicate `pgx` registration guard

The binary search is overkill here.
Readability first.
pull/1511/head
Tomáš Procházka 2023-02-13 19:44:54 +01:00 committed by Jack Christensen
parent c5daa3a814
commit c2e278e5d4
1 changed files with 14 additions and 4 deletions

View File

@ -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)