Пример #1
0
//Connect init gorm ORM.
func (orm *AppOrm) Connect(conf interfaces.Config) error {
	var db gorm.DB
	dbUri, err := conf.Get("DatabaseUri")
	if dbUri, ok := dbUri.(string); err == nil && ok {
		log.Println(dbUri)
		db, err = gorm.Open("mysql", dbUri)
	}
	if err != nil {
		log.Fatal(err.Error())
		return err
	}
	db.DB()

	db.DB().Ping()
	db.DB().SetMaxIdleConns(10)
	db.DB().SetMaxOpenConns(100)

	// Disable table name's pluralization
	db.SingularTable(true)
	if env, err := conf.Get("env"); err == nil && env == "dev" {
		db.LogMode(true)
	}
	orm.driver = &db
	return nil
}
Пример #2
0
//ExtractDBSQL ---
func ExtractDBSQL() (gormdb chaospeddler.GormDB) {
	var err error
	var db *gorm.DB
	appEnv, _ := cfenv.Current()
	service, _ := appEnv.Services.WithName("sql-info")
	host := fmt.Sprintf("%v", service.Credentials["hostname"])
	port := fmt.Sprintf("%v", service.Credentials["port"])
	dbname := fmt.Sprintf("%v", service.Credentials["name"])
	user := fmt.Sprintf("%v", service.Credentials["username"])
	pass := fmt.Sprintf("%v", service.Credentials["password"])
	connectionString := user + ":" + pass + "@tcp(" + host + ":" + port + ")" + "/" + dbname + "?charset=utf8&parseTime=True&loc=Local"
	lo.G.Error("connection string: ", connectionString)

	if db, err = gorm.Open("mysql", connectionString); err == nil {
		db.DB()
		db.DB().Ping()
		db.DB().SetMaxIdleConns(10)
		db.DB().SetMaxOpenConns(100)
		db.SingularTable(true)
		db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(
			new(chaospeddler.ServiceInstance),
			new(chaospeddler.ServiceBinding),
		)
		gormdb = db

	} else {
		lo.G.Error("there was an error connecting to mysql: ", err)
		panic(err)
	}
	return
}
Пример #3
0
func initializeDatabase(db *gorm.DB) {
	db.SingularTable(true) // 自動生成されるテーブル名を単数形にする.

	db.CreateTable(&Property{})
	db.CreateTable(&Room{})
	//    db.AutoMigrate(&Family{}, &FamilyCredential{}) // 使いこなせないので当面自動マイグレーションはoff.

	db.LogMode(true)
	defer beginTx(db)()
}
Пример #4
0
// InitDatabase initialize new database connection
// the first argument is a driver to use, the second one - connection parameters
// Examples:
// InitDatabase("sqlite3","/var/lib/gossha/gossha.db")
// InitDatabase("sqlite3",":memory:")
// InitDatabase("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
// InitDatabase("postgres", "user=gorm dbname=gorm sslmode=disable")
// InitDatabase("postgres", "postgres://*****:*****@localhost/pqgotest?sslmode=verify-full")
func InitDatabase(driver, dbPath string, verboseLogging bool) error {
	var db *gorm.DB
	db, err := gorm.Open(driver, dbPath)
	db.LogMode(verboseLogging)
	err = db.DB().Ping()
	if err != nil {
		return err
	}
	db.DB().SetMaxIdleConns(5)
	db.DB().SetMaxOpenConns(5)
	// Disable table name's pluralization
	db.SingularTable(true)
	err = db.AutoMigrate(&User{}, &Message{}, &Key{}, &Session{}).Error
	if err != nil {
		return err
	}
	err = db.Model(&User{}).AddUniqueIndex("idx_contact_name", "name").Error
	if err != nil {
		return err
	}
	err = db.Model(&Message{}).AddIndex("idx_message", "user_id", "created_at").Error
	if err != nil {
		return err
	}
	err = db.Model(&Key{}).AddIndex("idx_key", "user_id", "created_at").Error
	if err != nil {
		return err
	}
	err = db.Model(&Key{}).AddUniqueIndex("idx_key_content", "content", "user_id", "created_at").Error
	if err != nil {
		return err
	}
	err = db.Model(&Session{}).AddIndex("idx_session", "user_id", "created_at").Error
	if err != nil {
		return err
	}
	DB = db
	return nil
}
Пример #5
0
func NewDb(Cnf DbConfig) *Db {
	var err error
	var db *gorm.DB
	switch Cnf.DriverName {
	case "sqlite3":
		db, err = gorm.Open("sqlite3", Cnf.DataSourceName)
		logger.CheckFatal(err, "Got error when connect database")
	//this.DbMap = &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
	case "mysql":
		//db, err := sql.Open("mysql", "user:password@tcp(localhost:5555)/dbname?charset=utf8&parseTime=True&loc=Local")
		db, err = gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=%s&parseTime=True&loc=Local",
			Cnf.UserName,
			Cnf.Password,
			Cnf.Host,
			Cnf.DataSourceName,
			Cnf.Encoding))
		logger.CheckFatal(err, "Got error when connect database")

	//this.DbMap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
	default:
		logger.Fatal("The types of database does not support:"+Cnf.DriverName, err)
	}

	db.DB()
	db.DB().Ping()
	db.DB().SetMaxIdleConns(10)
	db.DB().SetMaxOpenConns(100)

	// Disable table name's pluralization
	db.SingularTable(true)

	// construct a gorp DbMap

	//

	return &Db{Cnf: Cnf, db: db}
}