-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.go
177 lines (162 loc) · 4.58 KB
/
crud.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package simplets
import (
"fmt"
"reflect"
. "github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
)
var ErrConditionCheckFail = fmt.Errorf("OTSConditionCheckFail")
var ErrObjectNotExist = fmt.Errorf("OTSObjectNotExist")
func GetRow(client *TableStoreClient, r interface{}) (bool, error) {
getRowRequest := new(GetRowRequest)
criteria := new(SingleRowQueryCriteria)
v := reflect.ValueOf(r).Elem()
t := v.Type()
pk, fields, table := generateInfo(v, t)
criteria.PrimaryKey = pk
getRowRequest.SingleRowQueryCriteria = criteria
getRowRequest.SingleRowQueryCriteria.TableName = table
getRowRequest.SingleRowQueryCriteria.MaxVersion = 1
getResp, err := client.GetRow(getRowRequest)
if err != nil {
return false, err
}
if getResp.PrimaryKey.PrimaryKeys == nil {
return false, nil
}
columns := getResp.Columns
fillColsToFieldInfos(columns, fields)
fillStructFromFields(t, v, fields)
return true, nil
}
func getSupportedValue(i interface{}) interface{} {
value := reflect.ValueOf(i)
kind := value.Kind()
var v interface{}
switch kind {
case reflect.Bool:
v = value.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v = value.Int()
case reflect.String:
v = value.String()
case reflect.Float32, reflect.Float64:
v = value.Float()
case reflect.Slice:
if value.Type() == typeOfBytes {
v = value.Bytes()
}
default:
}
return v
}
func PutRow(client *TableStoreClient, r interface{}, setters ...Option) error {
opts := &Options{}
for _, s := range setters {
s(opts)
}
v := reflect.ValueOf(r).Elem()
t := v.Type()
rowRequest := new(PutRowRequest)
rowChange := new(PutRowChange)
pk, fields, table := generateInfo(v, t)
rowChange.TableName = table
rowChange.PrimaryKey = pk
rowChange.ReturnType = ReturnType_RT_PK
for col, field := range fields {
if field.isPk || (field.isZero && !opts.storeZeroValue) {
continue
}
if field.isPrefixCol {
for col, value := range field.values {
rowChange.AddColumn(field.columnPrefix+col, getSupportedValue(value))
}
} else {
rowChange.AddColumn(col, getSupportedValue(field.value))
}
}
rowChange.SetCondition(opts.rowExistence)
if opts.columnFilter != nil {
rowChange.SetColumnCondition(opts.columnFilter)
}
rowRequest.PutRowChange = rowChange
resp, err := client.PutRow(rowRequest)
if err != nil {
return err
}
fillPKsToFieldInfos(resp.PrimaryKey, fields)
fillStructFromFields(t, v, fields)
return err
}
func UpdateRow(client *TableStoreClient, r interface{}, setters ...Option) error {
opts := &Options{}
for _, s := range setters {
s(opts)
}
v := reflect.ValueOf(r).Elem()
t := v.Type()
rowRequest := new(UpdateRowRequest)
rowChange := new(UpdateRowChange)
pk, fields, table := generateInfo(v, t)
rowChange.TableName = table
rowChange.PrimaryKey = pk
var incColumnsToReturn []string
for col, field := range fields {
if field.isPk || (field.isZero && !opts.storeZeroValue) {
continue
}
if field.isPrefixCol {
for col, value := range field.values {
if field.isAtomicIncCol {
rowChange.IncrementColumn(field.columnPrefix+col, getSupportedValue(value).(int64))
incColumnsToReturn = append(incColumnsToReturn, field.columnPrefix+col)
} else {
rowChange.PutColumn(field.columnPrefix+col, getSupportedValue(value))
}
}
} else {
if field.isAtomicIncCol {
rowChange.IncrementColumn(col, field.value.(int64))
incColumnsToReturn = append(incColumnsToReturn, col)
} else {
rowChange.PutColumn(col, getSupportedValue(field.value))
}
}
}
if len(incColumnsToReturn) > 0 {
rowChange.ReturnType = ReturnType_RT_AFTER_MODIFY
rowChange.ColumnNamesToReturn = incColumnsToReturn
}
rowChange.SetCondition(opts.rowExistence)
if opts.columnFilter != nil {
rowChange.SetColumnCondition(opts.columnFilter)
}
rowRequest.UpdateRowChange = rowChange
resp, err := client.UpdateRow(rowRequest)
if err != nil {
return substantiateError(err)
}
columns := resp.Columns
fillColsToFieldInfos(columns, fields)
fillStructFromFields(t, v, fields)
return nil
}
func DeleteRow(client *TableStoreClient, r interface{}, setters ...Option) error {
opts := &Options{}
for _, s := range setters {
s(opts)
}
v := reflect.ValueOf(r).Elem()
t := v.Type()
pk, _, table := generateInfo(v, t)
rowRequest := new(DeleteRowRequest)
rowChange := new(DeleteRowChange)
rowChange.TableName = table
rowChange.PrimaryKey = pk
rowChange.SetCondition(opts.rowExistence)
if opts.columnFilter != nil {
rowChange.SetColumnCondition(opts.columnFilter)
}
rowRequest.DeleteRowChange = rowChange
_, err := client.DeleteRow(rowRequest)
return substantiateError(err)
}