MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / insertLineageOutboxesInTx

Function insertLineageOutboxesInTx

database/lineage.go:170–248  ·  view source on GitHub ↗
(ctx context.Context, tx *sql.Tx, outboxes []*model.LineageOutbox)

Source from the content-addressed store, hash-verified

168}
169
170func insertLineageOutboxesInTx(ctx context.Context, tx *sql.Tx, outboxes []*model.LineageOutbox) error {
171 if len(outboxes) == 0 {
172 return nil
173 }
174
175 var query strings.Builder
176 query.WriteString(`
177 INSERT INTO ledgerforge.lineage_outbox
178 (transaction_id, source_balance_id, destination_balance_id, provider, lineage_type, payload, status, max_attempts, created_at, inflight)
179 VALUES
180 `)
181
182 args := make([]interface{}, 0, len(outboxes)*10)
183 now := time.Now()
184 for i, outbox := range outboxes {
185 if outbox == nil {
186 continue
187 }
188 if len(args) > 0 {
189 query.WriteString(",")
190 }
191 base := len(args) + 1
192 query.WriteString(fmt.Sprintf("($%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d,$%d)", base, base+1, base+2, base+3, base+4, base+5, base+6, base+7, base+8, base+9))
193 args = append(args,
194 outbox.TransactionID,
195 outbox.SourceBalanceID,
196 outbox.DestinationBalanceID,
197 outbox.Provider,
198 outbox.LineageType,
199 outbox.Payload,
200 model.OutboxStatusPending,
201 outbox.MaxAttempts,
202 now.Add(time.Duration(i)*time.Nanosecond),
203 outbox.Inflight,
204 )
205 }
206
207 if len(args) == 0 {
208 return nil
209 }
210
211 query.WriteString(" RETURNING id, transaction_id")
212 rows, err := tx.QueryContext(ctx, query.String(), args...)
213 if err != nil {
214 return apierror.NewAPIError(apierror.ErrInternalServer, "Failed to insert lineage outbox entries", err)
215 }
216 defer func() { _ = rows.Close() }()
217
218 outboxesByTransactionID := make(map[string]*model.LineageOutbox, len(outboxes))
219 for _, outbox := range outboxes {
220 if outbox == nil {
221 continue
222 }
223 outboxesByTransactionID[outbox.TransactionID] = outbox
224 }
225
226 inserted := 0
227 for rows.Next() {

Calls 2

NewAPIErrorFunction · 0.92
CloseMethod · 0.45

Tested by

no test coverage detected