本文整理汇总了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
}
示例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)
}
}
示例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
}
示例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
}
示例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
}
示例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)
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
}
示例10: AddItem
func AddItem(db *gorm.DB, category string) (error, interface{}) {
item := models.DBTypesMap()[category]
db.NewRecord(item)
db.Create(item)
return nil, item
}
示例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"))
}
}
}
示例12: AddWhitelistedTorrent
func (t *WhiteTorrent) AddWhitelistedTorrent(db *gorm.DB) bool {
db.Create(t)
return db.NewRecord(t)
}
示例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
}
示例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
}
示例15: Insert
/*Point Insert to Sqlite*/
func (p Point) Insert(db gorm.DB) {
if db.NewRecord(p) {
db.Debug().Create(&p)
}
}