Skip to content

Commit

Permalink
gorm 2 support in gollygorm2 package
Browse files Browse the repository at this point in the history
  • Loading branch information
craigmj committed Oct 17, 2020
1 parent 4796b2c commit cf76f40
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions gollygorm2/DbOpen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gollygorm2

import (
"gorm.io/gorm"

"github.com/craigmj/golly"
)

// DbOpen is identical to golly.DbOpen, but returns a gorm database connection instead.
func DbOpen(driverName, datasourceName string) (*gorm.DB, error) {
var db *gorm.DB
var err error
if err := golly.Run(func() error {
db, err = gorm.Open(driverName, datasourceName)
if nil != err {
if nil != golly.ErrorLog {
golly.ErrorLog(err)
}
return err
}
if err = db.DB().Ping(); nil != err {
db.Close()
if nil != err && nil != golly.ErrorLog {
golly.ErrorLog(err)
}
return err
}
return nil
}); nil != err {
return nil, err
}
return db, nil
}

0 comments on commit cf76f40

Please sign in to comment.