Example #1
0
// 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
}
Example #2
0
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)
	}
}
Example #3
0
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
}
Example #4
0
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
}
Example #5
0
// 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
}
Example #6
0
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)
}
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)
	}
}
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)
	}
}
Example #9
0
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)
		}
	}
}
Example #10
0
func AddItem(db *gorm.DB, category string) (error, interface{}) {
	item := models.DBTypesMap()[category]
	db.NewRecord(item)
	db.Create(item)
	return nil, item
}
Example #11
0
// 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"))
		}
	}
}
Example #12
0
func (t *WhiteTorrent) AddWhitelistedTorrent(db *gorm.DB) bool {
	db.Create(t)
	return db.NewRecord(t)
}
Example #13
0
func AddTask(db *gorm.DB, department int64) (error, models.Tasks) {
	item := models.Tasks{DepartmentId: department}
	db.NewRecord(&item)
	db.Create(&item)
	return nil, item
}
Example #14
0
func duplicate(db *gorm.DB, hash string) (bool, uint) {
	file := FileData{}
	db.Where("hash = ?", hash).First(&file)
	return !db.NewRecord(file), file.ID
}
Example #15
0
/*Point Insert to Sqlite*/
func (p Point) Insert(db gorm.DB) {
	if db.NewRecord(p) {
		db.Debug().Create(&p)
	}
}