Specify database dialect when open database, fixes #169

This commit is contained in:
Jinzhu 2014-08-05 10:32:15 +08:00
parent 943a873051
commit 4d98c8c0bc
1 changed files with 19 additions and 4 deletions

23
main.go
View File

@ -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
}