File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,26 @@ type Config struct {
22
22
MaxIdleConns int `json:"maxIdleConns,omitempty"`
23
23
ConnMaxLifetime time.Duration `json:"connMaxLifetime,omitempty"`
24
24
DefaultIsolationLevel sql.IsolationLevel `json:"-"`
25
+ Err error `json:"-"`
26
+ }
27
+
28
+ // Validate returns Config.Err if it is not nil
29
+ // or an error if the Config does not have
30
+ // a Driver, Host, or Database.
31
+ func (c * Config ) Validate () error {
32
+ if c .Err != nil {
33
+ return c .Err
34
+ }
35
+ if c .Driver == "" {
36
+ return fmt .Errorf ("missing sqldb.Config.Driver" )
37
+ }
38
+ if c .Host == "" {
39
+ return fmt .Errorf ("missing sqldb.Config.Host" )
40
+ }
41
+ if c .Database == "" {
42
+ return fmt .Errorf ("missing sqldb.Config.Database" )
43
+ }
44
+ return nil
25
45
}
26
46
27
47
// ConnectURL for connecting to a database
@@ -49,6 +69,10 @@ func (c *Config) ConnectURL() string {
49
69
// sets all Config values and performs a ping with ctx.
50
70
// The sql.DB will be returned if the ping was successful.
51
71
func (c * Config ) Connect (ctx context.Context ) (* sql.DB , error ) {
72
+ err := c .Validate ()
73
+ if err != nil {
74
+ return nil , err
75
+ }
52
76
db , err := sql .Open (c .Driver , c .ConnectURL ())
53
77
if err != nil {
54
78
return nil , err
Original file line number Diff line number Diff line change @@ -97,7 +97,7 @@ func (e connectionWithError) Stats() sql.DBStats {
97
97
}
98
98
99
99
func (e connectionWithError ) Config () * Config {
100
- return & Config {Driver : "ConnectionWithError" }
100
+ return & Config {Err : e . err }
101
101
}
102
102
103
103
func (e connectionWithError ) Now () (time.Time , error ) {
You can’t perform that action at this time.
0 commit comments