-
Notifications
You must be signed in to change notification settings - Fork 0
/
otsAdd.go
49 lines (37 loc) · 1.15 KB
/
otsAdd.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
package otslib
import (
m "beego-tablestore/models"
"fmt"
"strconv"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
)
func BuildPutChangeMeta(c m.Comment) *tablestore.PutRowChange {
putRowChange := new(tablestore.PutRowChange)
putRowChange.TableName = CommentTableName
putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
if c.CommentId == 0 {
putRowChange.SetReturnPk()
}
meta := new(tablestore.PrimaryKey)
meta.AddPrimaryKeyColumn("ProdId", c.ProdId)
if c.CommentId > 0 {
meta.AddPrimaryKeyColumn("CommentId", c.CommentId)
} else {
meta.AddPrimaryKeyColumnWithAutoIncrement("CommentId")
}
putRowChange.PrimaryKey = meta
putRowChange.AddColumn("Content", c.Content)
return putRowChange
}
func InsertOrUpdate(commentStorage m.Comment) (int64, error) {
request := new(tablestore.PutRowRequest)
request.PutRowChange = BuildPutChangeMeta(commentStorage)
client := CreateClient()
resp, err := client.PutRow(request)
if resp.PrimaryKey.PrimaryKeys != nil {
col := resp.PrimaryKey.PrimaryKeys[1]
commentId, _ := strconv.ParseInt(fmt.Sprintf(`%d`, col.Value), 10, 64)
return commentId, err
}
return 0, err
}