当前位置: 首页>>代码示例>>Golang>>正文


Golang DB.Set方法代码示例

本文整理汇总了Golang中github.com/jinzhu/gorm.DB.Set方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.Set方法的具体用法?Golang DB.Set怎么用?Golang DB.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/jinzhu/gorm.DB的用法示例。


在下文中一共展示了DB.Set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: CreateTimestampTable

// CreateTimestampTable creates the DB table for TUFFile
func CreateTimestampTable(db gorm.DB) error {
	query := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8").CreateTable(&TimestampKey{})
	if query.Error != nil {
		return query.Error
	}
	return nil
}
开发者ID:rogaha,项目名称:notary,代码行数:8,代码来源:models.go

示例2: ExtractDBSQL

//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
}
开发者ID:xchapter7x,项目名称:chaospeddler,代码行数:32,代码来源:main.go

示例3: Discard

func (createResourcePublishInterface) Discard(db *gorm.DB, event publish.PublishEventInterface) error {
	if event, ok := event.(*publish.PublishEvent); ok {
		var product Product
		db.Set("publish:draft_mode", true).First(&product, event.Argument)
		pb.Discard(&product)
	}
	return nil
}
开发者ID:8legd,项目名称:qor-qor,代码行数:8,代码来源:publish_event_test.go

示例4: Add

func (handler publishJoinTableHandler) Add(h gorm.JoinTableHandlerInterface, db *gorm.DB, source1 interface{}, source2 interface{}) error {
	// production mode
	if !isDraftMode(db) {
		if err := handler.JoinTableHandler.Add(h, db.Set(publishDraftMode, true), source1, source2); err != nil {
			return err
		}
	}
	return handler.JoinTableHandler.Add(h, db, source1, source2)
}
开发者ID:yunhor,项目名称:qor,代码行数:9,代码来源:many_2_many.go

示例5: createTables

// Create all tables
func createTables(db *gorm.DB) {
	db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.User{})
	db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.Item{})
	db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.UserFavoriteItem{})
	db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.ItemImpression{})
	db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.ItemTag{})
	db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.Tag{})
	db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").CreateTable(&models.UserTag{})
}
开发者ID:gophergala2016,项目名称:source,代码行数:10,代码来源:scheme.go

示例6: Delete

func (handler publishJoinTableHandler) Delete(h gorm.JoinTableHandlerInterface, db *gorm.DB, sources ...interface{}) error {
	// production mode
	if !IsDraftMode(db) {
		if err := handler.JoinTableHandler.Delete(h, db.Set(publishDraftMode, true), sources...); err != nil {
			return err
		}
	}
	return handler.JoinTableHandler.Delete(h, db, sources...)
}
开发者ID:qor,项目名称:publish,代码行数:9,代码来源:many_2_many.go

示例7: CreateTUFTable

// CreateTUFTable creates the DB table for TUFFile
func CreateTUFTable(db gorm.DB) error {
	// TODO: gorm
	query := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8").CreateTable(&TUFFile{})
	if query.Error != nil {
		return query.Error
	}
	query = db.Model(&TUFFile{}).AddUniqueIndex(
		"idx_gun", "gun", "role", "version")
	return query.Error
}
开发者ID:endophage,项目名称:notary,代码行数:11,代码来源:sql_models.go

示例8: checkBrandPosition

func checkBrandPosition(db *gorm.DB, t *testing.T, description string) {
	var brands []Brand
	if err := db.Set("l10n:mode", "locale").Find(&brands).Error; err != nil {
		t.Errorf("no error should happen when find brands, but got %v", err)
	}
	for i, brand := range brands {
		if brand.Position != i+1 {
			t.Errorf("Brand %v(%v)'s position should be %v after %v, but got %v", brand.ID, brand.LanguageCode, i+1, description, brand.Position)
		}
	}
}
开发者ID:8legd,项目名称:qor-sorting,代码行数:11,代码来源:l10n_test.go

示例9: CreateKeyTable

// CreateKeyTable creates the DB table for TUFFile
func CreateKeyTable(db gorm.DB) error {
	query := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8").CreateTable(&Key{})
	if query.Error != nil {
		return query.Error
	}
	query = db.Model(&Key{}).AddUniqueIndex(
		"idx_gun_role", "gun", "role")
	if query.Error != nil {
		return query.Error
	}
	return nil
}
开发者ID:NathanMcCauley,项目名称:notary,代码行数:13,代码来源:models.go

示例10: migrateDb

func migrateDb(db gorm.DB) {
	// Create table
	db.CreateTable(&model.User{})
	db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&model.User{})

	// Automating Migration
	db.AutoMigrate(&model.User{})
	db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&model.User{})
	// Feel free to change your struct, AutoMigrate will keep your database up-to-date.
	// AutoMigrate will ONLY add *new columns* and *new indexes*,
	// WON'T update current column's type or delete unused columns, to protect your data.
	// If the table is not existing, AutoMigrate will create the table automatically.
}
开发者ID:bankaiteam,项目名称:apiPOC,代码行数:13,代码来源:server.go

示例11: move

func move(db *gorm.DB, value sortingInterface, pos int) (err error) {
	var startedTransaction bool
	var tx = db.Set("publish:publish_event", true)
	if t := tx.Begin(); t.Error == nil {
		startedTransaction = true
		tx = t
	}

	scope := db.NewScope(value)
	for _, field := range scope.PrimaryFields() {
		if field.DBName != "id" {
			tx = tx.Where(fmt.Sprintf("%s = ?", field.DBName), field.Field.Interface())
		}
	}

	currentPos := value.GetPosition()

	var results *gorm.DB
	if pos > 0 {
		results = tx.Model(newModel(value)).
			Where("position > ? AND position <= ?", currentPos, currentPos+pos).
			UpdateColumn("position", gorm.Expr("position - ?", 1))
	} else {
		results = tx.Model(newModel(value)).
			Where("position < ? AND position >= ?", currentPos, currentPos+pos).
			UpdateColumn("position", gorm.Expr("position + ?", 1))
	}

	if err = results.Error; err == nil {
		var rowsAffected = int(results.RowsAffected)
		if pos < 0 {
			rowsAffected = -rowsAffected
		}
		value.SetPosition(currentPos + rowsAffected)
		err = tx.Model(value).UpdateColumn("position", gorm.Expr("position + ?", rowsAffected)).Error
	}

	// Create Publish Event
	createPublishEvent(tx, value)

	if startedTransaction {
		if err == nil {
			tx.Commit()
		} else {
			tx.Rollback()
		}
	}
	return err
}
开发者ID:NZAOM,项目名称:qor,代码行数:49,代码来源:sorting.go

示例12: MigrationsUp

func MigrationsUp(db *gorm.DB) {
	log.Println("Starting migrations")

	// Drop table
	db.DropTable(&User{}, &Phone{}, &Email{}, &Language{})

	// Create table
	//db.CreateTable(&User{})
	db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&User{}, &Phone{}, &Email{}, &Language{})

	// ModifyColumn
	//db.Model(&User{}).ModifyColumn("description", "text")

	// DropColumn
	//db.Model(&User{}).DropColumn("description")

	// Automating Migration
	db.AutoMigrate(&User{}, &Phone{}, &Email{}, &Language{})
}
开发者ID:thiagoao-dev,项目名称:GoLang-SimpleApi,代码行数:19,代码来源:migrations.go

示例13: initializeGridPieceTable

func initializeGridPieceTable(db gorm.DB) {
	db.CreateTable(&GridPiece{})
	db.Set("gorm:taple_operations", "ENGINE=InnoDB").CreateTable(&GridPiece{})

	pieces := []GridPiece{
		GridPiece{Type: BlackRook, Y: 1, X: 1},
		GridPiece{Type: BlackKnight, Y: 1, X: 2},
		GridPiece{Type: BlackBishop, Y: 1, X: 3},
		GridPiece{Type: BlackQueen, Y: 1, X: 4},
		GridPiece{Type: BlackKing, Y: 1, X: 5},
		GridPiece{Type: BlackBishop, Y: 1, X: 6},
		GridPiece{Type: BlackKnight, Y: 1, X: 7},
		GridPiece{Type: BlackRook, Y: 1, X: 8},
		GridPiece{Type: BlackPawn, Y: 2, X: 1},
		GridPiece{Type: BlackPawn, Y: 2, X: 2},
		GridPiece{Type: BlackPawn, Y: 2, X: 3},
		GridPiece{Type: BlackPawn, Y: 2, X: 4},
		GridPiece{Type: BlackPawn, Y: 2, X: 5},
		GridPiece{Type: BlackPawn, Y: 2, X: 6},
		GridPiece{Type: BlackPawn, Y: 2, X: 7},
		GridPiece{Type: BlackPawn, Y: 2, X: 8},
		GridPiece{Type: WhiteRook, Y: 8, X: 1},
		GridPiece{Type: WhiteKnight, Y: 8, X: 2},
		GridPiece{Type: WhiteBishop, Y: 8, X: 3},
		GridPiece{Type: WhiteQueen, Y: 8, X: 4},
		GridPiece{Type: WhiteKing, Y: 8, X: 5},
		GridPiece{Type: WhiteBishop, Y: 8, X: 6},
		GridPiece{Type: WhiteKnight, Y: 8, X: 7},
		GridPiece{Type: WhiteRook, Y: 8, X: 8},
		GridPiece{Type: WhitePawn, Y: 7, X: 1},
		GridPiece{Type: WhitePawn, Y: 7, X: 2},
		GridPiece{Type: WhitePawn, Y: 7, X: 3},
		GridPiece{Type: WhitePawn, Y: 7, X: 4},
		GridPiece{Type: WhitePawn, Y: 7, X: 5},
		GridPiece{Type: WhitePawn, Y: 7, X: 6},
		GridPiece{Type: WhitePawn, Y: 7, X: 7},
		GridPiece{Type: WhitePawn, Y: 7, X: 8},
	}
	for _, piece := range pieces {
		db.NewRecord(piece)
		db.Create(&piece)
	}
}
开发者ID:anarchang,项目名称:chess-board-web,代码行数:43,代码来源:chess-board-web.go

示例14: initializePieceTable

func initializePieceTable(db gorm.DB) {
	db.CreateTable(&Piece{})
	db.Set("gorm:taple_operations", "ENGINE=InnoDB").CreateTable(&Piece{})

	pieces := []Piece{
		Piece{Type: BlackRook, Top: 82, Left: 79},
		Piece{Type: BlackKnight, Top: 82, Left: 154},
		Piece{Type: BlackBishop, Top: 82, Left: 234},
		Piece{Type: BlackQueen, Top: 82, Left: 308},
		Piece{Type: BlackKing, Top: 82, Left: 383},
		Piece{Type: BlackBishop, Top: 82, Left: 458},
		Piece{Type: BlackKnight, Top: 82, Left: 533},
		Piece{Type: BlackRook, Top: 82, Left: 608},
		Piece{Type: BlackPawn, Top: 2, Left: 79},
		Piece{Type: BlackPawn, Top: 160, Left: 154},
		Piece{Type: BlackPawn, Top: 160, Left: 234},
		Piece{Type: BlackPawn, Top: 160, Left: 308},
		Piece{Type: BlackPawn, Top: 160, Left: 383},
		Piece{Type: BlackPawn, Top: 160, Left: 458},
		Piece{Type: BlackPawn, Top: 160, Left: 533},
		Piece{Type: BlackPawn, Top: 160, Left: 608},
		Piece{Type: WhiteRook, Top: 611, Left: 79},
		Piece{Type: WhiteKnight, Top: 611, Left: 154},
		Piece{Type: WhiteBishop, Top: 611, Left: 234},
		Piece{Type: WhiteQueen, Top: 611, Left: 308},
		Piece{Type: WhiteKing, Top: 611, Left: 383},
		Piece{Type: WhiteBishop, Top: 611, Left: 458},
		Piece{Type: WhiteKnight, Top: 611, Left: 533},
		Piece{Type: WhiteRook, Top: 611, Left: 608},
		Piece{Type: WhitePawn, Top: 533, Left: 79},
		Piece{Type: WhitePawn, Top: 533, Left: 154},
		Piece{Type: WhitePawn, Top: 533, Left: 234},
		Piece{Type: WhitePawn, Top: 533, Left: 308},
		Piece{Type: WhitePawn, Top: 533, Left: 383},
		Piece{Type: WhitePawn, Top: 533, Left: 458},
		Piece{Type: WhitePawn, Top: 533, Left: 533},
		Piece{Type: WhitePawn, Top: 533, Left: 608},
	}
	for _, piece := range pieces {
		db.NewRecord(piece)
		db.Create(&piece)
	}
}
开发者ID:anarchang,项目名称:chess-board-web,代码行数:43,代码来源:chess-board-web.go

示例15: checkHasProductInLocale

func checkHasProductInLocale(db *gorm.DB, locale string, t *testing.T) {
	var count int
	if db.Set("l10n:locale", locale).Count(&count); count != 1 {
		t.Errorf("should has only one product for locale %v, but found %v", locale, count)
	}
}
开发者ID:strogo,项目名称:qor,代码行数:6,代码来源:curd_test.go


注:本文中的github.com/jinzhu/gorm.DB.Set方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。