From 4d98c8c0bc430f2babd45b3088007841814a0c40 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 5 Aug 2014 10:32:15 +0800 Subject: [PATCH] Specify database dialect when open database, fixes #169 --- main.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 951f989a..a13bf6f2 100644 --- a/main.go +++ b/main.go @@ -23,11 +23,26 @@ type DB struct { source string } -func Open(driver, source string) (DB, error) { +func Open(dialect string, drivesources ...string) (DB, error) { + var db DB var err error - db := DB{dialect: NewDialect(driver), tagIdentifier: "sql", logger: defaultLogger, callback: DefaultCallback, source: source} - db.db, err = sql.Open(driver, source) - db.parent = &db + var driver = dialect + var source string + + if len(drivesources) == 0 { + err = errors.New("invalid database source") + } else { + if len(drivesources) == 1 { + source = drivesources[0] + } else if len(drivesources) >= 2 { + driver = drivesources[0] + source = drivesources[1] + } + + db = DB{dialect: NewDialect(dialect), tagIdentifier: "sql", logger: defaultLogger, callback: DefaultCallback, source: source} + db.db, err = sql.Open(driver, source) + db.parent = &db + } return db, err }