diff --git a/gollygorm2/DbOpen.go b/gollygorm2/DbOpen.go new file mode 100644 index 0000000..535d0e2 --- /dev/null +++ b/gollygorm2/DbOpen.go @@ -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 +}