diff options
| author | apocelipes <seve3r@outlook.com> | 2024-03-29 20:09:37 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2024-04-01 12:38:07 +0000 |
| commit | 6bfaafd3c34325515e8ffbe7446b9beda3f49698 (patch) | |
| tree | d0fc352cd94d48a3be57248595398e5181699906 /src/database/sql/fakedb_test.go | |
| parent | d49a14c6092ba30d0afbdaa06a24016ff1fe2a82 (diff) | |
| download | go-6bfaafd3c34325515e8ffbe7446b9beda3f49698.tar.xz | |
database/sql: use slices to simplify the code
Change-Id: Ia198272330626271ee7d4e1ae46afca819ab2933
GitHub-Last-Rev: e713ac31638671f60cc3cf62fa514f784e834e66
GitHub-Pull-Request: golang/go#66572
Reviewed-on: https://go-review.googlesource.com/c/go/+/574995
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
Diffstat (limited to 'src/database/sql/fakedb_test.go')
| -rw-r--r-- | src/database/sql/fakedb_test.go | 50 |
1 files changed, 18 insertions, 32 deletions
diff --git a/src/database/sql/fakedb_test.go b/src/database/sql/fakedb_test.go index c6c3172b5c..3dfcd447b5 100644 --- a/src/database/sql/fakedb_test.go +++ b/src/database/sql/fakedb_test.go @@ -11,7 +11,7 @@ import ( "fmt" "io" "reflect" - "sort" + "slices" "strconv" "strings" "sync" @@ -120,12 +120,7 @@ type table struct { } func (t *table) columnIndex(name string) int { - for n, nname := range t.colname { - if name == nname { - return n - } - } - return -1 + return slices.Index(t.colname, name) } type row struct { @@ -217,15 +212,6 @@ func init() { Register("test", fdriver) } -func contains(list []string, y string) bool { - for _, x := range list { - if x == y { - return true - } - } - return false -} - type Dummy struct { driver.Driver } @@ -235,7 +221,7 @@ func TestDrivers(t *testing.T) { Register("test", fdriver) Register("invalid", Dummy{}) all := Drivers() - if len(all) < 2 || !sort.StringsAreSorted(all) || !contains(all, "test") || !contains(all, "invalid") { + if len(all) < 2 || !slices.IsSorted(all) || !slices.Contains(all, "test") || !slices.Contains(all, "invalid") { t.Fatalf("Drivers = %v, want sorted list with at least [invalid, test]", all) } } @@ -345,10 +331,8 @@ func (db *fakeDB) columnType(table, column string) (typ string, ok bool) { if !ok { return } - for n, cname := range t.colname { - if cname == column { - return t.coltype[n], true - } + if i := slices.Index(t.colname, column); i != -1 { + return t.coltype[i], true } return "", false } @@ -823,6 +807,15 @@ func (s *fakeStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (d return nil, fmt.Errorf("fakedb: unimplemented statement Exec command type of %q", s.cmd) } +func valueFromPlaceholderName(args []driver.NamedValue, name string) driver.Value { + for i := range args { + if args[i].Name == name { + return args[i].Value + } + } + return nil +} + // When doInsert is true, add the row to the table. // When doInsert is false do prep-work and error checking, but don't // actually add the row to the table. @@ -857,11 +850,8 @@ func (s *fakeStmt) execInsert(args []driver.NamedValue, doInsert bool) (driver.R val = args[argPos].Value } else { // Assign value from argument placeholder name. - for _, a := range args { - if a.Name == strvalue[1:] { - val = a.Value - break - } + if v := valueFromPlaceholderName(args, strvalue[1:]); v != nil { + val = v } } argPos++ @@ -997,12 +987,8 @@ func (s *fakeStmt) QueryContext(ctx context.Context, args []driver.NamedValue) ( if wcol.Placeholder == "?" { argValue = args[wcol.Ordinal-1].Value } else { - // Assign arg value from placeholder name. - for _, a := range args { - if a.Name == wcol.Placeholder[1:] { - argValue = a.Value - break - } + if v := valueFromPlaceholderName(args, wcol.Placeholder[1:]); v != nil { + argValue = v } } if fmt.Sprintf("%v", tcol) != fmt.Sprintf("%v", argValue) { |
