// NewXorm func NewXorm(conf *dbconf.DBConf) (*mdb.Xorm, error) { logger.Tracef("Create new mdb.Xorm, DB: %s ", conf.Database) // check args if conf == nil { return nil, fmt.Errorf("DBConf is nil") } engine, err := xorm.NewEngine("mysql", conf.GetMySqlConnURL()) if logger.CheckError(err) { return nil, err } // set engine default config //engine.ShowSQL = false //engine.ShowDebug = false //engine.ShowErr = true //engine.ShowWarn = false engine.SetTableMapper(core.SnakeMapper{}) //engine.SetColumnMapper(core.SnakeMapper{}) engine.SetColumnMapper(core.SameMapper{}) return mdb.NewXorm(engine), nil }
// NewDBFromDBConf func NewDBFromDBConf(c *dbconf.DBConf) (*sql.DB, error) { logger.Tracef("NewDBFromDBConf() Host:%s Database:%s User:%s ", c.Host, c.Database, c.User) db, errDB := sql.Open(c.Driver, c.GetMySqlConnURL()) if logger.CheckError(errDB) { logger.Errorf("Really cannot go on, open sql error.Error:%v Host:%s Database:%s", errDB, c.Host, c.Database) //panic(errDB) return db, errDB } // infact if db not work, app still can go on. if err := db.Ping(); logger.CheckError(err) { logger.Errorf("Really cannot go on, Ping db error. Host:%s Database:%s", c.Host, c.Database) // app should know how to do with this err. //panic(err) return db, err } return db, nil }
func DemoConf(conf *dbconf.DBConf) { println("==========Begin show db conf ==============") println(conf.GetConnURL()) println(conf.GetMySqlConnURL()) println("==========End.. show db conf ==============") }