func (db *DB) UpdateBalancesWithinTransaction(pubkeys []*koblitz.PublicKey, amounts []uint64, tx *sql.Tx, coinType *coinparam.Params) (err error) {

	defer func() {
		if err != nil {
			err = fmt.Errorf("Error updating balances within transaction: \n%s", err)
			return
		}
	}()

	coinSchema, err := util.GetSchemaNameFromParam(coinType)
	if err != nil {
		return
	}

	if _, err = tx.Exec("USE " + db.balanceSchema + ";"); err != nil {
		return
	}

	for i := range amounts {
		amount := amounts[i]
		pubkey := pubkeys[i]
		currentBalanceQuery := fmt.Sprintf("SELECT balance FROM %s WHERE pubkey='%x';", coinSchema, pubkey.SerializeCompressed())
		res, queryErr := tx.Query(currentBalanceQuery)
		if queryErr != nil {
			err = queryErr
			return
		}

		// Get the first result for balance
		if res.Next() {
			var balance uint64

			if err = res.Scan(&balance); err != nil {
				return
			}

			if err = res.Close(); err != nil {
				return
			}

			// Update the balance
			// TODO: replace this name stuff, check that the name doesn't already exist on register. IMPORTANT!!
			insertBalanceQuery := fmt.Sprintf("UPDATE %s SET balance='%d' WHERE pubkey='%x';", coinSchema, balance+amount, pubkey.SerializeCompressed())
			if _, err = tx.Exec(insertBalanceQuery); err != nil {
				return
			}
		} else {
			// Create the balance
			// TODO: replace this name stuff, check that the name doesn't already exist on register. IMPORTANT!!
			insertBalanceQuery := fmt.Sprintf("INSERT INTO %s VALUES ('%s', %d);", coinSchema, pubkey.SerializeCompressed(), amount)
			if _, err = tx.Exec(insertBalanceQuery); err != nil {
				return
			}
		}
	}

	return nil
}
// UpdateBalancesWithinTransaction updates many balances, uses a transaction for all db stuff.
UpdateBalancesWithinTransaction updates many balances, uses a transaction for all db stuff.
// UpdateBalancesWithinTransaction updates many balances uses a transaction for all db stuff .
func (db *DB) UpdateBalancesWithinTransaction(pubkeys []*koblitz.PublicKey, amounts []uint64, tx *sql.Tx, coinType *coinparam.Params) (err error) {
	defer func() {
		if err != nil {
			err = fmt.Errorf("Error updating balances within transaction: \n%s", err)
			return
		}
	}()
	coinSchema, err := util.GetSchemaNameFromParam(coinType)
	if err != nil {
		return
	}
	if _, err = tx.Exec("USE " + db.balanceSchema + ";"); err != nil {
		return
	}
	for i := range amounts {
		amount := amounts[i]
		pubkey := pubkeys[i]
		currentBalanceQuery := fmt.Sprintf("SELECT balance FROM %s WHERE pubkey='%x';", coinSchema, pubkey.SerializeCompressed())
		res, queryErr := tx.Query(currentBalanceQuery)
		if queryErr != nil {
			err = queryErr
			return
		}
		if res.Next() {
			var balance uint64
			if err = res.Scan(&balance); err != nil {
				return
			}
			if err = res.Close(); err != nil {
				return
			}
			insertBalanceQuery := fmt.Sprintf("UPDATE %s SET balance='%d' WHERE pubkey='%x';", coinSchema, balance+amount, pubkey.SerializeCompressed())
			if _, err = tx.Exec(insertBalanceQuery); err != nil {
				return
			}
		} else {
			insertBalanceQuery := fmt.Sprintf("INSERT INTO %s VALUES ('%s', %d);", coinSchema, pubkey.SerializeCompressed(), amount)
			if _, err = tx.Exec(insertBalanceQuery); err != nil {
				return
			}
		}
	}
	return nil
}
func ( db * DB ) UpdateBalancesWithinTransaction ( pubkeys [ ] * koblitz . PublicKey , amounts [ ] uint64 , tx * sql . Tx , coinType * coinparam . Params ) ( err error ) { defer func ( ) { if err != nil { err = fmt . Errorf ( " \n " , err ) return } } ( ) coinSchema , err := util . GetSchemaNameFromParam ( coinType ) if err != nil { return } if _ , err = tx . Exec ( " " + db . balanceSchema + " " ) ; err != nil { return } for i := range amounts { amount := amounts [ i ] pubkey := pubkeys [ i ] currentBalanceQuery := fmt . Sprintf ( " " , coinSchema , pubkey . SerializeCompressed ( ) ) res , queryErr := tx . Query ( currentBalanceQuery ) if queryErr != nil { err = queryErr return } if res . Next ( ) { var balance uint64 if err = res . Scan ( & balance ) ; err != nil { return } if err = res . Close ( ) ; err != nil { return } insertBalanceQuery := fmt . Sprintf ( " " , coinSchema , balance + amount , pubkey . SerializeCompressed ( ) ) if _ , err = tx . Exec ( insertBalanceQuery ) ; err != nil { return } } else { insertBalanceQuery := fmt . Sprintf ( " " , coinSchema , pubkey . SerializeCompressed ( ) , amount ) if _ , err = tx . Exec ( insertBalanceQuery ) ; err != nil { return } } } return nil }
UpdateBalancesWithinTransaction updates many balances, uses a transaction for all db stuff.
UpdateBalancesWithinTransaction updates many balances uses a transaction for all db stuff .
// Get the first result for balance // Update the balance // TODO: replace this name stuff, check that the name doesn't already exist on register. IMPORTANT!! // Create the balance // TODO: replace this name stuff, check that the name doesn't already exist on register. IMPORTANT!!