func Connect() { if err := orm.RegisterDataBase("default", "mysql", config.GetString("dataSourceName"), 30); err != nil { glog.Errorln(err) return } orm.RegisterModel( new(model.Group), new(model.WebServer), new(model.Config), new(model.Version), ) log.Println("database connected") Db.Db, _ = orm.GetDB() }
func (this *DBDo) RDB(host, database string) error { alias := fmt.Sprintf("%s", host) if database != "" { alias = fmt.Sprintf("%s_%s", host, database) } _, err := orm.GetDB(alias) if err != nil { dsn := "" if database != "" { dsn = this.dsnd(database) } else { dsn = this.dsn() } err = orm.RegisterDataBase(alias, "mysql", dsn) if err != nil { return err } } this.orm.Using(alias) return nil }
func init() { runtime.GOMAXPROCS(Workers) beego.AppConfigPath = "../conf/app.conf" beego.ParseConfig() setting.ReadConfig() DiscuzDb = beego.AppConfig.String("importer::discuzdb") AvatarPath = beego.AppConfig.String("importer::avatarpath") if err := orm.RegisterDataBase("discuz", "mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8", setting.MySQLUser, setting.MySQLPassword, setting.MySQLHost, DiscuzDb)+"&loc=Asia%2FShanghai", 30); err != nil { fmt.Println("MySQL error:", err.Error()) return } OrmGotalk = orm.NewOrm() if db, err := orm.GetDB("discuz"); err == nil { Orm, err = orm.NewOrmWithDB("mysql", "discuz", db) if err != nil { fmt.Println("MySQL error:", err.Error()) return } } else { fmt.Println("MySQL error:", err.Error()) return } orm.RunSyncdb("default", true, false) }