From 0d54677e138512cd5f403c6e48eaf51d9a511cf4 Mon Sep 17 00:00:00 2001 From: Baiju Muthukadan Date: Fri, 7 Oct 2016 17:12:29 +0530 Subject: [PATCH 1/2] if no arguments, return error at once --- main.go | 60 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/main.go b/main.go index 52a536d0..6f7e625e 100644 --- a/main.go +++ b/main.go @@ -44,40 +44,40 @@ func Open(dialect string, args ...interface{}) (*DB, error) { if len(args) == 0 { err = errors.New("invalid database source") - } else { - var source string - var dbSQL sqlCommon + return nil, err + } + var source string + var dbSQL sqlCommon - switch value := args[0].(type) { - case string: - var driver = dialect - if len(args) == 1 { - source = value - } else if len(args) >= 2 { - driver = value - source = args[1].(string) - } - dbSQL, err = sql.Open(driver, source) - case sqlCommon: - source = reflect.Indirect(reflect.ValueOf(value)).FieldByName("dsn").String() - dbSQL = value + switch value := args[0].(type) { + case string: + var driver = dialect + if len(args) == 1 { + source = value + } else if len(args) >= 2 { + driver = value + source = args[1].(string) } + dbSQL, err = sql.Open(driver, source) + case sqlCommon: + source = reflect.Indirect(reflect.ValueOf(value)).FieldByName("dsn").String() + dbSQL = value + } - db = DB{ - dialect: newDialect(dialect, dbSQL.(*sql.DB)), - logger: defaultLogger, - callbacks: DefaultCallback, - source: source, - values: map[string]interface{}{}, - db: dbSQL, - } - db.parent = &db + db = DB{ + dialect: newDialect(dialect, dbSQL.(*sql.DB)), + logger: defaultLogger, + callbacks: DefaultCallback, + source: source, + values: map[string]interface{}{}, + db: dbSQL, + } + db.parent = &db - if err == nil { - err = db.DB().Ping() // Send a ping to make sure the database connection is alive. - if err != nil { - db.DB().Close() - } + if err == nil { + err = db.DB().Ping() // Send a ping to make sure the database connection is alive. + if err != nil { + db.DB().Close() } } From bd513dd58068e31025a12cb75e0dc6bdfa61cb65 Mon Sep 17 00:00:00 2001 From: Baiju Muthukadan Date: Sat, 8 Oct 2016 21:52:15 +0530 Subject: [PATCH 2/2] test case for single parameter for Open --- main_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/main_test.go b/main_test.go index 0e129dde..716471a2 100644 --- a/main_test.go +++ b/main_test.go @@ -754,6 +754,16 @@ func TestDdlErrors(t *testing.T) { } } +func TestOpenWithOneParameter(t *testing.T) { + db, err := gorm.Open("dialect") + if db != nil { + t.Error("Open with one parameter returned non nil for db") + } + if err == nil { + t.Error("Open with one parameter returned err as nil") + } +} + func BenchmarkGorm(b *testing.B) { b.N = 2000 for x := 0; x < b.N; x++ {