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


Golang DB.NewRecord方法代码示例

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


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

示例1: saveBinary

// SaveBinary will efficently save a binary and it's associated parts.
// Having to do this for efficency is probably a good sign that I should just
// use sqlx or something.
func saveBinary(tx *gorm.DB, b *types.Binary) error {
	var txerr error
	if tx.NewRecord(b) {
		parts := b.Parts
		b.Parts = []types.Part{}
		txerr = tx.Save(b).Error
		if txerr != nil {
			return txerr
		}
		pids := make([]int64, len(parts))
		for i, p := range parts {
			pids[i] = p.ID
		}
		txerr = tx.Model(types.Part{}).Where("id IN (?)", pids).Updates(map[string]interface{}{"binary_id": b.ID}).Error
		if txerr != nil {
			return txerr
		}
	} else {
		pids := make([]int64, len(b.Parts))
		for i, p := range b.Parts {
			pids[i] = p.ID
		}
		txerr = tx.Model(types.Part{}).Where("id IN (?)", pids).Updates(map[string]interface{}{"binary_id": b.ID}).Error
		if txerr != nil {
			return txerr
		}

	}
	return nil
}
开发者ID:hobeone,项目名称:gonab,代码行数:33,代码来源:binaries.go

示例2: setFileId

func setFileId(db *gorm.DB, pid uint, fid uint) {
	photo := TumblrPhoto{}
	db.Where("id = ?", pid).First(&photo)
	if !db.NewRecord(photo) {
		photo.FileDataID = fid
		db.Save(&photo)
	}
}
开发者ID:Jiajun-Fan,项目名称:nandu,代码行数:8,代码来源:download.go

示例3: FindOrCreatePhoto

func FindOrCreatePhoto(db *gorm.DB, path string) Photo {
	var media Photo
	db.Where(Photo{Path: path}).FirstOrInit(&media)
	if db.NewRecord(media) {
		media.RefreshAttributes()
		db.Save(&media)
	}
	return media
}
开发者ID:jaischeema,项目名称:gopic,代码行数:9,代码来源:queries.go

示例4: ormTest

func ormTest(db gorm.DB) []model.User {
	user := model.User{Name: "Jinzhu", Age: 18, Birthday: time.Now()}
	db.NewRecord(user) // => returns `true` if primary key is blank
	db.Create(&user)
	db.NewRecord(user) // => return `false` after `user` created
	users := []model.User{}
	db.First(&users)
	return users
}
开发者ID:bankaiteam,项目名称:apiPOC,代码行数:9,代码来源:server.go

示例5: CreateSummary

// CreateSummary inserts a Summary into the database
func CreateSummary(db *gorm.DB, sum Summary) (bool, Summary) {

	created := db.NewRecord(sum) // => returns `true` as primary key is blank

	if !created {
		return false, sum
	}

	db.Create(&sum)

	return true, sum
}
开发者ID:ZombieHippie,项目名称:test-gin,代码行数:13,代码来源:summary.service.go

示例6: getStop

func getStop(name string, db *gorm.DB) int64 {
	blog := TumblrBlog{}
	db.Where("name = ?", name).First(&blog)
	if db.NewRecord(blog) {
		return 0
	}
	post := TumblrPost{}
	db.Where("tumblr_blog_id = ?", blog.ID).Order("offset desc").First(&post)
	if db.NewRecord(post) {
		return 0
	}
	return int64(post.Offset)
}
开发者ID:Jiajun-Fan,项目名称:nandu,代码行数:13,代码来源:main.go

示例7: 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

示例8: 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

示例9: Validate

func (user *AwUser) Validate(t time.Time, db *gorm.DB) {
	if db.NewRecord(user) {
		user.Group = kAwGroupInvalid
		return
	}

	if user.Token == "" || user.Secret == "" {
		user.Group = kAwGroupInvalid
		db.Save(user)
		return
	}

	if user.Group > kAwGroupFree {
		if user.Expire.Before(t) {
			user.Group = kAwGroupFree
			db.Save(user)
		}
	}
}
开发者ID:Jiajun-Fan,项目名称:nandu,代码行数:19,代码来源:main.go

示例10: AddItem

func AddItem(db *gorm.DB, category string) (error, interface{}) {
	item := models.DBTypesMap()[category]
	db.NewRecord(item)
	db.Create(item)
	return nil, item
}
开发者ID:oblank,项目名称:rozklad_cdtu,代码行数:6,代码来源:database.go

示例11: Validate

// Validate does some validation to be able to store the record.
func (u *User) Validate(db *gorm.DB) {
	if !govalidator.StringLength(u.Username, "2", "255") {
		db.AddError(fmt.Errorf("Username should be longer than 2 and shorter than 255"))
	}

	if u.Username != "" {
		notFound := db.Where(
			"username = ?",
			u.Username,
		).Not(
			"id",
			u.ID,
		).First(
			&User{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Username is already present"))
		}
	}

	if u.Hash != "" {
		notFound := db.Where(
			"hash = ?",
			u.Hash,
		).Not(
			"id",
			u.ID,
		).First(
			&User{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Hash is already present"))
		}
	}

	if !govalidator.IsEmail(u.Email) {
		db.AddError(fmt.Errorf(
			"Email must be a valid email address",
		))
	}

	if u.Email != "" {
		normalized, _ := govalidator.NormalizeEmail(
			u.Email,
		)

		notFound := db.Where(
			"email = ?",
			normalized,
		).Not(
			"id",
			u.ID,
		).First(
			&User{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Email is already present"))
		}
	}

	if db.NewRecord(u) {
		if !govalidator.StringLength(u.Password, "5", "255") {
			db.AddError(fmt.Errorf("Password should be longer than 5 and shorter than 255"))
		}
	}
}
开发者ID:umschlag,项目名称:umschlag-api,代码行数:70,代码来源:user.go

示例12: AddWhitelistedTorrent

func (t *WhiteTorrent) AddWhitelistedTorrent(db *gorm.DB) bool {
	db.Create(t)
	return db.NewRecord(t)
}
开发者ID:GrappigPanda,项目名称:notorious,代码行数:4,代码来源:whiteTorrent.go

示例13: AddTask

func AddTask(db *gorm.DB, department int64) (error, models.Tasks) {
	item := models.Tasks{DepartmentId: department}
	db.NewRecord(&item)
	db.Create(&item)
	return nil, item
}
开发者ID:rtm7777,项目名称:rozklad_cdtu,代码行数:6,代码来源:tasks.go

示例14: duplicate

func duplicate(db *gorm.DB, hash string) (bool, uint) {
	file := FileData{}
	db.Where("hash = ?", hash).First(&file)
	return !db.NewRecord(file), file.ID
}
开发者ID:Jiajun-Fan,项目名称:nandu,代码行数:5,代码来源:download.go

示例15: Insert

/*Point Insert to Sqlite*/
func (p Point) Insert(db gorm.DB) {
	if db.NewRecord(p) {
		db.Debug().Create(&p)
	}
}
开发者ID:highsoul,项目名称:zmy,代码行数:6,代码来源:point.go


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