-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lirui2
committed
Jul 5, 2024
1 parent
9cb9292
commit 1574f6d
Showing
9 changed files
with
94 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package abstract | ||
|
||
type Starter interface { | ||
Init() error // 初始化 | ||
Run() error // 运行 | ||
Init() error | ||
Close() error // 关闭 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,53 @@ | ||
package database | ||
|
||
import ( | ||
sparrow "github.com/lrayt/small-sparrow" | ||
"github.com/lrayt/small-sparrow/kit/db_builder" | ||
"context" | ||
"database/sql" | ||
"github.com/lrayt/small-sparrow/builder" | ||
"github.com/lrayt/small-sparrow/core" | ||
"gorm.io/gorm" | ||
"time" | ||
) | ||
|
||
type DBProvider struct { | ||
DB *gorm.DB | ||
type DBManager struct { | ||
GormDB *gorm.DB | ||
} | ||
|
||
func NewDBProvider() (*DBProvider, error) { | ||
options := new(db_builder.Options) | ||
if err := sparrow.GConfigs().PackConf("database.scp-db", options); err != nil { | ||
return nil, err | ||
func (p *DBManager) Init() error { | ||
var ( | ||
err error | ||
options = new(builder.DBOptions) | ||
) | ||
// cfg | ||
err = core.GConfigs().PackConf("database.scp-db", options) | ||
if err != nil { | ||
return err | ||
} | ||
if db, err := db_builder.CreateGormDB(options); err != nil { | ||
return nil, err | ||
} else { | ||
return &DBProvider{DB: db}, nil | ||
// gorm db | ||
p.GormDB, err = builder.CreateGormDB(options) | ||
if err != nil { | ||
return err | ||
} | ||
// sql db | ||
var sqlDB *sql.DB | ||
sqlDB, err = p.GormDB.DB() | ||
if err != nil { | ||
return err | ||
} | ||
// ping | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) | ||
defer cancel() | ||
return sqlDB.PingContext(ctx) | ||
} | ||
|
||
func (p DBManager) Close() error { | ||
db, err := p.GormDB.DB() | ||
if err != nil { | ||
return err | ||
} | ||
return db.Close() | ||
} | ||
|
||
func NewDBManager() *DBManager { | ||
return new(DBManager) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.