MCPcopy Index your code
hub / github.com/apache/answer / NewDB

Function NewDB

internal/base/data/data.go:56–95  ·  view source on GitHub ↗

NewDB new database instance

(debug bool, dataConf *Database)

Source from the content-addressed store, hash-verified

54
55// NewDB new database instance
56func NewDB(debug bool, dataConf *Database) (*xorm.Engine, error) {
57 if dataConf.Driver == "" {
58 dataConf.Driver = string(schemas.MYSQL)
59 }
60 if dataConf.Driver == string(schemas.SQLITE) {
61 dataConf.Driver = "sqlite"
62 dbFileDir := filepath.Dir(dataConf.Connection)
63 log.Debugf("try to create database directory %s", dbFileDir)
64 if err := dir.CreateDirIfNotExist(dbFileDir); err != nil {
65 log.Errorf("create database dir failed: %s", err)
66 }
67 dataConf.MaxOpenConn = 1
68 }
69 engine, err := xorm.NewEngine(dataConf.Driver, dataConf.Connection)
70 if err != nil {
71 return nil, err
72 }
73
74 if debug {
75 engine.ShowSQL(true)
76 } else {
77 engine.SetLogLevel(ormlog.LOG_ERR)
78 }
79
80 if err = engine.Ping(); err != nil {
81 return nil, err
82 }
83
84 if dataConf.MaxIdleConn > 0 {
85 engine.SetMaxIdleConns(dataConf.MaxIdleConn)
86 }
87 if dataConf.MaxOpenConn > 0 {
88 engine.SetMaxOpenConns(dataConf.MaxOpenConn)
89 }
90 if dataConf.ConnMaxLifeTime > 0 {
91 engine.SetConnMaxLifetime(time.Duration(dataConf.ConnMaxLifeTime) * time.Second)
92 }
93 engine.SetColumnMapper(names.GonicMapper{})
94 return engine, nil
95}
96
97// NewCache new cache instance
98func NewCache(c *CacheConf) (cache.Cache, func(), error) {

Callers 10

initDatabaseFunction · 0.92
initDatabaseFunction · 0.92
CheckDBConnectionFunction · 0.92
CheckDBTableExistFunction · 0.92
SetDefaultConfigFunction · 0.92
DumpAllDataFunction · 0.92
initDatabaseFunction · 0.92
MigrateFunction · 0.92
InitBaseInfoFunction · 0.92
initApplicationFunction · 0.92

Calls 1

CreateDirIfNotExistFunction · 0.92

Tested by 2

initDatabaseFunction · 0.74
initDatabaseFunction · 0.74