Exemple #1
0
func SaveAddress(addr *Address) (*Address, error) {
	_, err := db.Exec(
		`INSERT INTO address (`+AddressModel.FieldsInsert+`)
         VALUES (`+AddressModel.Placeholders+`)`,
		addr,
	)
	return addr, err
}
Exemple #2
0
func UpdateUserSetTOTPConfirmed(userId int64) {
	_, err := db.Exec(
		`UPDATE auth_user
         SET totp_conf=1
         WHERE id=?`,
		userId,
	)
	if err != nil {
		panic(err)
	}
}
Exemple #3
0
func UpdateUserSetEmailConfirmed(emailCode string) {
	_, err := db.Exec(
		`UPDATE auth_user
         SET email_conf=1
         WHERE email_code=?`,
		emailCode,
	)
	if err != nil {
		panic(err)
	}
}
Exemple #4
0
func SaveBlock(b *Block) *Block {
	if b.Time == 0 {
		b.Time = time.Now().Unix()
	}
	_, err := db.Exec(
		`INSERT INTO block (`+BlockModel.FieldsInsert+`)
         VALUES (`+BlockModel.Placeholders+`)`,
		b,
	)
	if err != nil {
		panic(err)
	}
	return b
}
Exemple #5
0
func SaveBetaSignup(signup *BetaSignup) *BetaSignup {
	if signup.Time == 0 {
		signup.Time = time.Now().Unix()
	}
	_, err := db.Exec(
		`INSERT INTO beta_signup (`+BetaSignupModel.FieldsInsert+`)
         VALUES (`+BetaSignupModel.Placeholders+`)`,
		signup,
	)
	if err != nil {
		panic(err)
	}
	return signup
}
Exemple #6
0
func Set(key, value string) {
	_, err := db.Exec(
		`INSERT INTO kvstore (key_, value)
         VALUES (?, ?)`,
		key, value,
	)
	switch db.GetErrorType(err) {
	case nil:
		return
	case db.ERR_DUPLICATE_ENTRY:
		// Update instead
		_, err := db.Exec(
			`UPDATE kvstore SET value=? WHERE key=?`,
			value, key,
		)
		if err != nil {
			panic(err)
		}
		return
	default:
		panic(err)
	}
}
Exemple #7
0
func UpdateBlockStatus(hash string, oldStatus, newStatus uint32) {
	now := time.Now().Unix()
	res, err := db.Exec(
		`UPDATE block
         SET status=?, updated=?
         WHERE status=? AND hash=?`,
		newStatus, now, oldStatus, hash,
	)
	if err != nil {
		panic(err)
	}
	count, err := res.RowsAffected()
	if int(count) != 1 {
		panic(NewError("Expected to update 1 block's status, but none changed"))
	}
	if err != nil {
		panic(err)
	}
}
func TestPriceLog(t *testing.T) {
	logger := &PriceLogger{Market: RandId(12)}
	logger.Initialize()

	defer func() {
		db.Exec(`
             DELETE FROM exchange_price_log
             WHERE market=?`,
			logger.Market,
		)
	}()

	// Add some entries.
	//             Type, Amount,  Price,  Time
	logger.AddTrade("B", 100, 100, 0) // Minute 0
	logger.AddTrade("B", 100, 99, 10)
	logger.AddTrade("B", 100, 102, 20)

	prices := logger.LoadPrices(60*1, 0, 60*1)
	if len(prices) != 0 {
		t.Fatalf("Expected 0 prices, got %v", len(prices))
	}
	prices = logger.LoadPrices(60*5, 0, 60*5)
	if len(prices) != 0 {
		t.Fatalf("Expected 0 prices, got %v", len(prices))
	}

	logger.AddTrade("B", 100, 105, 60) // Minute 1

	prices = logger.LoadPrices(60*1, 0, 60*1)
	if len(prices) != 1 {
		t.Fatalf("Expected 1 prices, got %v", len(prices))
	}
	checkPlog(t, prices[0], 99, 102, 100, 102, 0, 300)

	logger.AddTrade("B", 100, 104, 60*2) // Minute 2

	prices = logger.LoadPrices(60*1, 0, 60*2)
	if len(prices) != 2 {
		t.Fatalf("Expected 2 prices, got %v", len(prices))
	}
	checkPlog(t, prices[0], 99, 102, 100, 102, 0, 300)
	checkPlog(t, prices[1], 105, 105, 105, 105, 0, 100)
	prices = logger.LoadPrices(60*5, 0, 60*5)
	if len(prices) != 1 {
		t.Fatalf("Expected 1 prices, got %v", len(prices))
	}

	logger.AddTrade("B", 100, 100, 60*6) // Minute 6

	prices = logger.LoadPrices(60*1, 0, 60*6)
	if len(prices) != 3 {
		t.Fatalf("Expected 3 prices, got %v", len(prices))
	}
	checkPlog(t, prices[0], 99, 102, 100, 102, 0, 300)
	checkPlog(t, prices[1], 105, 105, 105, 105, 0, 100)
	checkPlog(t, prices[2], 104, 104, 104, 104, 0, 100)
	prices = logger.LoadPrices(60*5, 0, 60*5)
	if len(prices) != 1 {
		t.Fatalf("Expected 1 prices, got %v", len(prices))
	}
	checkPlog(t, prices[0], 99, 105, 100, 104, 0, 500)
}