]> Cypherpunks repositories - gostls13.git/commitdiff
database/sql: add Drivers, returning list of registered drivers
authorRuss Cox <rsc@golang.org>
Wed, 15 Oct 2014 17:10:14 +0000 (13:10 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 15 Oct 2014 17:10:14 +0000 (13:10 -0400)
Fixes #7969.

LGTM=bradfitz
R=bradfitz
CC=golang-codereviews
https://golang.org/cl/158950043

src/database/sql/fakedb_test.go
src/database/sql/sql.go

index c7db0dd77b370a1efa5f27b31707117a3168fd78..171c322d49ac0796b6ddcd956526f68c337d3d8c 100644 (file)
@@ -10,6 +10,7 @@ import (
        "fmt"
        "io"
        "log"
+       "sort"
        "strconv"
        "strings"
        "sync"
@@ -126,6 +127,27 @@ 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
+}
+
+func TestDrivers(t *testing.T) {
+       Register("invalid", Dummy{})
+       all := Drivers()
+       if len(all) < 2 || !sort.StringsAreSorted(all) || !contains(all, "test") || !contains(all, "invalid") {
+               t.Fatalf("Drivers = %v, want sorted list with at least [invalid, test]", all)
+       }
+}
+
 // Supports dsn forms:
 //    <dbname>
 //    <dbname>;<opts>  (only currently supported option is `badConn`,
index 731b7a7f797e749362b4f9f323c7e86e715a897d..ad9179cf7d7262e1f63d29f7823c8567eca62f9a 100644 (file)
@@ -18,6 +18,7 @@ import (
        "fmt"
        "io"
        "runtime"
+       "sort"
        "sync"
 )
 
@@ -36,6 +37,16 @@ func Register(name string, driver driver.Driver) {
        drivers[name] = driver
 }
 
+// Drivers returns a sorted list of the names of the registered drivers.
+func Drivers() []string {
+       var list []string
+       for name := range drivers {
+               list = append(list, name)
+       }
+       sort.Strings(list)
+       return list
+}
+
 // RawBytes is a byte slice that holds a reference to memory owned by
 // the database itself. After a Scan into a RawBytes, the slice is only
 // valid until the next call to Next, Scan, or Close.