Skip to content

Commit c2e278e

Browse files
prochacjackc
authored andcommitted
simplify duplicate pgx registration guard
The binary search is overkill here. Readability first.
1 parent c5daa3a commit c2e278e

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

stdlib/sql.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ import (
6666
"math"
6767
"math/rand"
6868
"reflect"
69-
"sort"
7069
"strconv"
7170
"strings"
7271
"sync"
@@ -87,9 +86,9 @@ func init() {
8786
configs: make(map[string]*pgx.ConnConfig),
8887
}
8988

90-
drivers := sql.Drivers()
91-
// if pgx driver was already registered by different pgx major version then we skip registration under the default name.
92-
if i := sort.SearchStrings(sql.Drivers(), "pgx"); len(drivers) >= i || drivers[i] != "pgx" {
89+
// if pgx driver was already registered by different pgx major version then we
90+
// skip registration under the default name.
91+
if !contains(sql.Drivers(), "pgx") {
9392
sql.Register("pgx", pgxDriver)
9493
}
9594
sql.Register("pgx/v5", pgxDriver)
@@ -111,6 +110,17 @@ func init() {
111110
}
112111
}
113112

113+
// TODO replace by slices.Contains when experimental package will be merged to stdlib
114+
// https://pkg.go.dev/golang.org/x/exp/slices#Contains
115+
func contains(list []string, y string) bool {
116+
for _, x := range list {
117+
if x == y {
118+
return true
119+
}
120+
}
121+
return false
122+
}
123+
114124
// OptionOpenDB options for configuring the driver when opening a new db pool.
115125
type OptionOpenDB func(*connector)
116126

0 commit comments

Comments
 (0)