本文整理汇总了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
}
示例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
}
示例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
}
示例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)
}
示例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{})
}
示例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...)
}
示例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
}
示例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)
}
}
}
示例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
}
示例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.
}
示例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
}
示例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{})
}
示例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)
}
}
示例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)
}
}
示例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)
}
}