-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_data_batch_insert_builder.go
56 lines (48 loc) · 1.37 KB
/
account_data_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
46
47
48
49
50
51
52
53
54
55
56
package history
import (
"context"
"github.com/shantanu-hashcash/go/support/db"
)
type AccountDataBatchInsertBuilder interface {
Add(data Data) error
Exec(ctx context.Context) error
Len() int
}
type accountDataBatchInsertBuilder struct {
session db.SessionInterface
builder db.FastBatchInsertBuilder
table string
}
func (q *Q) NewAccountDataBatchInsertBuilder() AccountDataBatchInsertBuilder {
return &accountDataBatchInsertBuilder{
session: q,
builder: db.FastBatchInsertBuilder{},
table: "accounts_data",
}
}
// Add adds a new account data to the batch
func (i *accountDataBatchInsertBuilder) Add(data Data) error {
ledgerKey, err := accountDataKeyToString(AccountDataKey{
AccountID: data.AccountID,
DataName: data.Name,
})
if err != nil {
return err
}
return i.builder.Row(map[string]interface{}{
"ledger_key": ledgerKey,
"account_id": data.AccountID,
"name": data.Name,
"value": data.Value,
"last_modified_ledger": data.LastModifiedLedger,
"sponsor": data.Sponsor,
})
}
// Exec writes the batch of account data to the database.
func (i *accountDataBatchInsertBuilder) 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 *accountDataBatchInsertBuilder) Len() int {
return i.builder.Len()
}