// InitDb initializes the database. func InitDb() { dbName := utils.Cfg.MustValue("db", "name") dbPwd := utils.Cfg.MustValue("db", "pwd_"+runtime.GOOS) // Register database. qbs.Register("mysql", fmt.Sprintf("%v:%v@%v/%v?charset=utf8&parseTime=true", utils.Cfg.MustValue("db", "user"), dbPwd, utils.Cfg.MustValue("db", "host"), dbName), dbName, qbs.NewMysql()) // Connect to database. q := connDb() defer q.Close() mg, err := setMg() if err != nil { panic("models.init -> " + err.Error()) } defer mg.Close() // Create data tables. mg.CreateTableIfNotExists(new(hv.PkgInfo)) mg.CreateTableIfNotExists(new(PkgTag)) mg.CreateTableIfNotExists(new(PkgRock)) mg.CreateTableIfNotExists(new(PkgExam)) mg.CreateTableIfNotExists(new(PkgDecl)) mg.CreateTableIfNotExists(new(PkgDoc)) mg.CreateTableIfNotExists(new(PkgFunc)) mg.CreateTableIfNotExists(new(PkgImport)) beego.Trace("Initialized database ->", dbName) }
//连接数据库 func connectDb(driver, dbname, user, password, host string) { params := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true", user, password, host, dbname) qbs.Register(driver, params, dbname, qbs.NewMysql()) err := initTable() if err != nil { fmt.Println(err) } }
func CreateUserTable() error { db, err := OpenDb() if err != nil { return err } migration := qbs.NewMigration(db, "qbs_test", qbs.NewMysql()) defer migration.Close() return migration.CreateTableIfNotExists(new(User)) }
func GetQbs() (q *qbs.Qbs, err error) { db := qbs.GetFreeDB() if db == nil { db, err = OpenDb() if err != nil { return nil, err } } q = qbs.New(db, qbs.NewMysql()) return q, nil }
func SetMg() (mg *qbs.Migration, err error) { db, err := OpenDb(dbtypeset) switch { case dbtypeset == "sqlite": mg = qbs.NewMigration(db, DbName, qbs.NewSqlite3()) case dbtypeset == "mysql": mg = qbs.NewMigration(db, DbName, qbs.NewMysql()) case dbtypeset == "pgsql": mg = qbs.NewMigration(db, DbName, qbs.NewPostgres()) } return mg, err }
// 初始化数据库 func RegisterDb() { qbs.Register("mysql", DBSPEC, DBNAME, qbs.NewMysql()) // 创建数据库 m, err := qbs.GetMigration() if err != nil { panic(err) } defer m.Close() m.CreateTableIfNotExists(new(models.User)) m.CreateTableIfNotExists(new(models.Project)) m.CreateTableIfNotExists(new(models.UserCode)) m.CreateTableIfNotExists(new(models.UserStatus)) m.CreateTableIfNotExists(new(models.OAuth)) }
func init() { st := NewSuite("qbs") st.InitF = func() { st.AddBenchmark("Insert", 2000*ORM_MULTI, QbsInsert) st.AddBenchmark("MultiInsert 100 row", 500*ORM_MULTI, QbsInsertMulti) st.AddBenchmark("Update", 2000*ORM_MULTI, QbsUpdate) st.AddBenchmark("Read", 4000*ORM_MULTI, QbsRead) st.AddBenchmark("MultiRead limit 100", 2000*ORM_MULTI, QbsReadSlice) qbs.Register("mysql", ORM_SOURCE, "model", qbs.NewMysql()) qbs.ChangePoolSize(ORM_MAX_IDLE) qbs.SetConnectionLimit(ORM_MAX_CONN, true) qo, _ = qbs.GetQbs() } }
func init() { revel.OnAppStart(func() { var ( found bool spec []string dialect qbs.Dialect ) if Driver, found = revel.Config.String("db.driver"); !found { revel.ERROR.Fatal("No db.driver found.") } switch strings.ToLower(Driver) { case "mysql": dialect = qbs.NewMysql() case "postgres": dialect = qbs.NewPostgres() case "sqlite3": dialect = qbs.NewSqlite3() } // Start building the spec from available config options if User, found = revel.Config.String("db.user"); found { spec = append(spec, User) if Password, found = revel.Config.String("db.password"); found { spec = append(spec, fmt.Sprintf(":%v", Password)) } spec = append(spec, "@") } if Protocol, found = revel.Config.String("db.protocol"); found { spec = append(spec, Protocol) if Address, found = revel.Config.String("db.address"); found { spec = append(spec, fmt.Sprintf("(%v)", Address)) } } if DbName, found = revel.Config.String("db.dbname"); !found { revel.ERROR.Fatal("No db.dbname found.") } spec = append(spec, fmt.Sprintf("/%v", DbName)) if Params, found = revel.Config.String("db.params"); found { spec = append(spec, fmt.Sprintf("?%v", Params)) } qbs.Register(Driver, strings.Join(spec, ""), DbName, dialect) Db, err = qbs.GetQbs() defer Db.Close() }) }
func Init() { var found bool if Driver, found = revel.Config.String("db.driver"); !found { revel.ERROR.Fatal("No db.driver found.") } if Spec, found = revel.Config.String("db.spec"); !found { revel.ERROR.Fatal("No db.spec found.") } // QBS uses snake case by default; override the name convention. qbs.ColumnNameToFieldName = noConvert qbs.FieldNameToColumnName = noConvert qbs.TableNameToStructName = noConvert qbs.StructNameToTableName = noConvert qbs.Register(Driver, Spec, "", qbs.NewMysql()) }
func InitQbs(maxConn int) { var ( found bool driver, spec string ) if driver, found = revel.Config.String("db.driver"); !found { revel.ERROR.Fatal("No db.driver found.") } if spec, found = revel.Config.String("db.spec"); !found { revel.ERROR.Fatal("No db.spec found.") } // QBS uses snake case by default; override the name convention. qbs.ColumnNameToFieldName = noConvert qbs.FieldNameToColumnName = noConvert qbs.TableNameToStructName = noConvert qbs.StructNameToTableName = noConvert qbs.Register(driver, spec, "", qbs.NewMysql()) qbs.ChangePoolSize(maxConn) }
func ConnDb() (q *qbs.Qbs, err error) { db := qbs.GetFreeDB() if db == nil { db, err = OpenDb(dbtypeset) if err != nil { return nil, err } } switch { case dbtypeset == "sqlite": q = qbs.New(db, qbs.NewSqlite3()) case dbtypeset == "mysql": q = qbs.New(db, qbs.NewMysql()) case dbtypeset == "pgsql": q = qbs.New(db, qbs.NewPostgres()) } return q, nil }
func RegisterDb() { switch { case dbtypeset == "sqlite": qbs.Register("sqlite3", "./data/sqlite.db", "", qbs.NewSqlite3()) case dbtypeset == "mysql": qbs.Register("mysql", "qbs_test@/qbs_test?charset=utf8&parseTime=true&loc=Local", "dbname", qbs.NewMysql()) case dbtypeset == "pgsql": qbs.Register("postgres", "user=postgres password=jn!@#$%^&* dbname=pgsql sslmode=disable", "pgsql", qbs.NewPostgres()) } }
func RegisterDb() { qbs.Register("mysql", "qbs_test@/qbs_test?charset=utf8&parseTime=true&loc=Local", "qbs_test", qbs.NewMysql()) }
func RegisterDb(driver, dbname, user, password, host string) { qbs.Register(driver, fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true", user, password, host, dbname), dbname, qbs.NewMysql()) err := CreateTabel() if err != nil { fmt.Println(err) } }
func RegisterDb() { switch { case dbtypeset == "sqlite": qbs.Register("sqlite3", "./data/sqlite.db", "", qbs.NewSqlite3()) case dbtypeset == "mysql": qbs.Register("mysql", "qbs_test@/qbs_test?charset=utf8&parseTime=true&loc=Local", "dbname", qbs.NewMysql()) case dbtypeset == "pgsql": qbs.Register("postgres", "qbs_test@/qbs_test?charset=utf8&parseTime=true&loc=Local", "dbname", qbs.NewPostgres()) } }