/
table.go
116 lines (99 loc) · 2.67 KB
/
table.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package types
import (
"fmt"
"github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/common/db/table"
"github.com/33cn/chain33/types"
)
var opt = &table.Option{
Prefix: "LODB-issuance",
Name: "issuer",
Primary: "issuanceid",
Index: []string{"status"},
}
//NewIssuanceTable 新建表
func NewIssuanceTable(kvdb db.KV) *table.Table {
rowmeta := NewIssuanceRow()
table, err := table.NewTable(rowmeta, kvdb, opt)
if err != nil {
panic(err)
}
return table
}
//IssuanceRow table meta 结构
type IssuanceRow struct {
*ReceiptIssuanceID
}
//NewIssuanceRow 新建一个meta 结构
func NewIssuanceRow() *IssuanceRow {
return &IssuanceRow{ReceiptIssuanceID: &ReceiptIssuanceID{}}
}
//CreateRow 新建数据行
func (tx *IssuanceRow) CreateRow() *table.Row {
return &table.Row{Data: &ReceiptIssuanceID{}}
}
//SetPayload 设置数据
func (tx *IssuanceRow) SetPayload(data types.Message) error {
if txdata, ok := data.(*ReceiptIssuanceID); ok {
tx.ReceiptIssuanceID = txdata
return nil
}
return types.ErrTypeAsset
}
//Get 按照indexName 查询 indexValue
func (tx *IssuanceRow) Get(key string) ([]byte, error) {
if key == "issuanceid" {
return []byte(tx.IssuanceId), nil
} else if key == "status" {
return []byte(fmt.Sprintf("%2d", tx.Status)), nil
}
return nil, types.ErrNotFound
}
var optRecord = &table.Option{
Prefix: "LODB-issuance",
Name: "debt",
Primary: "debtid",
Index: []string{"status", "addr", "addr_status"},
}
// NewRecordTable 大户发行记录表
func NewRecordTable(kvdb db.KV) *table.Table {
rowmeta := NewRecordRow()
table, err := table.NewTable(rowmeta, kvdb, optRecord)
if err != nil {
panic(err)
}
return table
}
//IssuanceRecordRow table meta 结构
type IssuanceRecordRow struct {
*ReceiptIssuance
}
//NewRecordRow 新建一个meta 结构
func NewRecordRow() *IssuanceRecordRow {
return &IssuanceRecordRow{ReceiptIssuance: &ReceiptIssuance{}}
}
//CreateRow 新建数据行
func (tx *IssuanceRecordRow) CreateRow() *table.Row {
return &table.Row{Data: &ReceiptIssuance{}}
}
//SetPayload 设置数据
func (tx *IssuanceRecordRow) SetPayload(data types.Message) error {
if txdata, ok := data.(*ReceiptIssuance); ok {
tx.ReceiptIssuance = txdata
return nil
}
return types.ErrTypeAsset
}
//Get 按照indexName 查询 indexValue
func (tx *IssuanceRecordRow) Get(key string) ([]byte, error) {
if key == "debtid" {
return []byte(tx.DebtId), nil
} else if key == "status" {
return []byte(fmt.Sprintf("%2d", tx.Status)), nil
} else if key == "addr" {
return []byte(tx.AccountAddr), nil
} else if key == "addr_status" {
return []byte(fmt.Sprintf("%s:%2d", tx.AccountAddr, tx.Status)), nil
}
return nil, types.ErrNotFound
}