GetBalancesByIDsLite retrieves multiple balances by their IDs in a single query. Returns a map of balance_id to Balance for easy lookup. Balances that are not found are simply not included in the result map. Parameters: - ctx context.Context: The context for the operation. - ids []string: The list
(ctx context.Context, ids []string)
| 463 | // - map[string]*model.Balance: A map of balance_id to Balance. |
| 464 | // - error: Returns an error in case of database failures. |
| 465 | func (d Datasource) GetBalancesByIDsLite(ctx context.Context, ids []string) (map[string]*model.Balance, error) { |
| 466 | if len(ids) == 0 { |
| 467 | return make(map[string]*model.Balance), nil |
| 468 | } |
| 469 | |
| 470 | rows, err := d.Conn.QueryContext(ctx, ` |
| 471 | SELECT balance_id, indicator, currency, currency_multiplier, ledger_id, balance, credit_balance, debit_balance, inflight_balance, inflight_credit_balance, inflight_debit_balance, created_at, version, track_fund_lineage, COALESCE(allocation_strategy, 'FIFO') as allocation_strategy, COALESCE(identity_id, '') as identity_id |
| 472 | FROM ledgerforge.balances |
| 473 | WHERE balance_id = ANY($1) |
| 474 | `, pq.Array(ids)) |
| 475 | if err != nil { |
| 476 | return nil, apierror.NewAPIError(apierror.ErrInternalServer, "Failed to query balances", err) |
| 477 | } |
| 478 | defer func() { _ = rows.Close() }() |
| 479 | |
| 480 | result := make(map[string]*model.Balance) |
| 481 | |
| 482 | for rows.Next() { |
| 483 | var balance model.Balance |
| 484 | var balanceValue, creditBalanceValue, debitBalanceValue string |
| 485 | var inflightBalanceValue, inflightCreditBalanceValue, inflightDebitBalanceValue string |
| 486 | var indicator sql.NullString |
| 487 | var allocationStrategy sql.NullString |
| 488 | |
| 489 | err := rows.Scan( |
| 490 | &balance.BalanceID, |
| 491 | &indicator, |
| 492 | &balance.Currency, |
| 493 | &balance.CurrencyMultiplier, |
| 494 | &balance.LedgerID, |
| 495 | &balanceValue, |
| 496 | &creditBalanceValue, |
| 497 | &debitBalanceValue, |
| 498 | &inflightBalanceValue, |
| 499 | &inflightCreditBalanceValue, |
| 500 | &inflightDebitBalanceValue, |
| 501 | &balance.CreatedAt, |
| 502 | &balance.Version, |
| 503 | &balance.TrackFundLineage, |
| 504 | &allocationStrategy, |
| 505 | &balance.IdentityID, |
| 506 | ) |
| 507 | if err != nil { |
| 508 | logrus.WithError(err).Error("balance batch scan failed") |
| 509 | continue |
| 510 | } |
| 511 | |
| 512 | if indicator.Valid { |
| 513 | balance.Indicator = indicator.String |
| 514 | } |
| 515 | if allocationStrategy.Valid { |
| 516 | balance.AllocationStrategy = allocationStrategy.String |
| 517 | } else { |
| 518 | balance.AllocationStrategy = "FIFO" |
| 519 | } |
| 520 | |
| 521 | // Parse big.Int values |
| 522 | var parseErr error |