(ctx context.Context, tx *sql.Tx, balances []*model.Balance)
| 949 | } |
| 950 | |
| 951 | func updateBalanceChunk(ctx context.Context, tx *sql.Tx, balances []*model.Balance) error { |
| 952 | if len(balances) == 0 { |
| 953 | return nil |
| 954 | } |
| 955 | |
| 956 | var query strings.Builder |
| 957 | query.WriteString(` |
| 958 | UPDATE ledgerforge.balances AS b |
| 959 | SET balance = v.balance, |
| 960 | credit_balance = v.credit_balance, |
| 961 | debit_balance = v.debit_balance, |
| 962 | inflight_balance = v.inflight_balance, |
| 963 | inflight_credit_balance = v.inflight_credit_balance, |
| 964 | inflight_debit_balance = v.inflight_debit_balance, |
| 965 | currency = v.currency, |
| 966 | currency_multiplier = v.currency_multiplier, |
| 967 | ledger_id = v.ledger_id, |
| 968 | created_at = v.created_at, |
| 969 | version = b.version + 1 |
| 970 | FROM (VALUES |
| 971 | `) |
| 972 | |
| 973 | args := make([]interface{}, 0, len(balances)*12) |
| 974 | for i, balance := range balances { |
| 975 | if i > 0 { |
| 976 | query.WriteString(",") |
| 977 | } |
| 978 | base := i*12 + 1 |
| 979 | query.WriteString(fmt.Sprintf( |
| 980 | "($%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d)", |
| 981 | base, |
| 982 | base+1, |
| 983 | base+2, |
| 984 | base+3, |
| 985 | base+4, |
| 986 | base+5, |
| 987 | base+6, |
| 988 | base+7, |
| 989 | base+8, |
| 990 | base+9, |
| 991 | base+10, |
| 992 | base+11, |
| 993 | )) |
| 994 | args = append(args, |
| 995 | balance.BalanceID, |
| 996 | balance.Balance.String(), |
| 997 | balance.CreditBalance.String(), |
| 998 | balance.DebitBalance.String(), |
| 999 | balance.InflightBalance.String(), |
| 1000 | balance.InflightCreditBalance.String(), |
| 1001 | balance.InflightDebitBalance.String(), |
| 1002 | balance.Currency, |
| 1003 | balance.CurrencyMultiplier, |
| 1004 | balance.LedgerID, |
| 1005 | balance.CreatedAt, |
| 1006 | balance.Version, |
| 1007 | ) |
| 1008 | } |
no test coverage detected