-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts_batch_insert_builder.go
45 lines (37 loc) · 1.2 KB
/
accounts_batch_insert_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package history
import (
"context"
"github.com/shantanu-hashcash/go/support/db"
)
// AccountsBatchInsertBuilder is used to insert accounts into the accounts table
type AccountsBatchInsertBuilder interface {
Add(account AccountEntry) error
Exec(ctx context.Context) error
Len() int
}
// AccountsBatchInsertBuilder is a simple wrapper around db.FastBatchInsertBuilder
type accountsBatchInsertBuilder struct {
session db.SessionInterface
builder db.FastBatchInsertBuilder
table string
}
// NewAccountsBatchInsertBuilder constructs a new AccountsBatchInsertBuilder instance
func (q *Q) NewAccountsBatchInsertBuilder() AccountsBatchInsertBuilder {
return &accountsBatchInsertBuilder{
session: q,
builder: db.FastBatchInsertBuilder{},
table: "accounts",
}
}
// Add adds a new account to the batch
func (i *accountsBatchInsertBuilder) Add(account AccountEntry) error {
return i.builder.RowStruct(account)
}
// Exec writes the batch of accounts to the database.
func (i *accountsBatchInsertBuilder) Exec(ctx context.Context) error {
return i.builder.Exec(ctx, i.session, i.table)
}
// Len returns the number of elements in the batch
func (i *accountsBatchInsertBuilder) Len() int {
return i.builder.Len()
}