Skip to content

Commit

Permalink
Refactor the CURD Precompiled interface (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
sulenn authored Jul 10, 2020
1 parent 082c254 commit 98a2043
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 155 deletions.
4 changes: 4 additions & 0 deletions precompiled/crud/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ type Condition struct {
conditions map[string]map[EnumOP]string
}

func NewCondition() *Condition {
return &Condition{conditions: make(map[string]map[EnumOP]string)}
}

func (c *Condition) EQ(key string, value string) {
newMap := make(map[EnumOP]string)
newMap[EQ] = value
Expand Down
55 changes: 24 additions & 31 deletions precompiled/crud/crud_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ func NewCRUDService(client *client.Client, privateKey *ecdsa.PrivateKey) (*CRUDS
return &CRUDService{crud: crudInstance, tableFactory: tableInstance, crudAuth: auth, client: client}, nil
}

// CreateTable returns the status of the creating that 0 represents succeed
func (service *CRUDService) CreateTable(table *Table) (int, error) {
tx, err := service.tableFactory.CreateTable(service.crudAuth, table.GetTableName(), table.GetKey(), table.GetValueFields())
func (service *CRUDService) CreateTable(tableName string, key string, valueFields string) (int, error) {
tx, err := service.tableFactory.CreateTable(service.crudAuth, tableName, key, valueFields)
if err != nil {
return -1, fmt.Errorf("CRUDService CreateTable failed: %v", err)
}
Expand All @@ -65,8 +64,8 @@ func (service *CRUDService) CreateTable(table *Table) (int, error) {
}

// Insert entry
func (service *CRUDService) Insert(table *Table, entry *Entry) (int, error) {
if len(table.GetKey()) > TableKeyMaxLength {
func (service *CRUDService) Insert(tableName string, key string, entry *Entry) (int, error) {
if len(key) > TableKeyMaxLength {
return -1, fmt.Errorf("The value of the table key exceeds the maximum limit( %d )", TableKeyMaxLength)
}
// change to string
Expand All @@ -75,7 +74,7 @@ func (service *CRUDService) Insert(table *Table, entry *Entry) (int, error) {
return -1, fmt.Errorf("change entry to json struct failed: %v", err)
}

tx, err := service.crud.Insert(service.crudAuth, table.GetTableName(), table.GetKey(), string(entryJSON[:]), table.GetOptional())
tx, err := service.crud.Insert(service.crudAuth, tableName, key, string(entryJSON[:]), "")
if err != nil {
return -1, fmt.Errorf("CRUDService Insert failed: %v", err)
}
Expand All @@ -89,8 +88,8 @@ func (service *CRUDService) Insert(table *Table, entry *Entry) (int, error) {
}

// Update entry
func (service *CRUDService) Update(table *Table, entry *Entry, condition *Condition) (int, error) {
if len(table.GetKey()) > TableKeyMaxLength {
func (service *CRUDService) Update(tableName string, key string, entry *Entry, condition *Condition) (int, error) {
if len(key) > TableKeyMaxLength {
return -1, fmt.Errorf("The value of the table key exceeds the maximum limit( %d )", TableKeyMaxLength)
}
// change to string
Expand All @@ -103,7 +102,7 @@ func (service *CRUDService) Update(table *Table, entry *Entry, condition *Condit
return -1, fmt.Errorf("change condition to json struct failed: %v", err)
}

tx, err := service.crud.Update(service.crudAuth, table.GetTableName(), table.GetKey(), string(entryJSON[:]), string(conditionJSON[:]), table.GetOptional())
tx, err := service.crud.Update(service.crudAuth, tableName, key, string(entryJSON[:]), string(conditionJSON[:]), "")
if err != nil {
return -1, fmt.Errorf("CRUDService Update failed: %v", err)
}
Expand All @@ -116,17 +115,16 @@ func (service *CRUDService) Update(table *Table, entry *Entry, condition *Condit
return handleReceipt(receipt)
}

// Remove entry
func (service *CRUDService) Remove(table *Table, condition *Condition) (int, error) {
if len(table.GetKey()) > TableKeyMaxLength {
func (service *CRUDService) Remove(tableName string, key string, condition *Condition) (int, error) {
if len(key) > TableKeyMaxLength {
return -1, fmt.Errorf("The value of the table key exceeds the maximum limit( %d )", TableKeyMaxLength)
}
conditionJSON, err := json.MarshalIndent(condition.GetConditions(), "", "\t")
if err != nil {
return -1, fmt.Errorf("change condition to json struct failed: %v", err)
}

tx, err := service.crud.Remove(service.crudAuth, table.GetTableName(), table.GetKey(), string(conditionJSON[:]), table.GetOptional())
tx, err := service.crud.Remove(service.crudAuth, tableName, key, string(conditionJSON[:]), "")
if err != nil {
return -1, fmt.Errorf("CRUDService Remove failed: %v", err)
}
Expand All @@ -140,16 +138,16 @@ func (service *CRUDService) Remove(table *Table, condition *Condition) (int, err
}

// Select entry
func (service *CRUDService) Select(table *Table, condition *Condition) ([]map[string]string, error) {
if len(table.GetKey()) > TableKeyMaxLength {
func (service *CRUDService) Select(tableName string, key string, condition *Condition) ([]map[string]string, error) {
if len(key) > TableKeyMaxLength {
return nil, fmt.Errorf("The value of the table key exceeds the maximum limit( %d )", TableKeyMaxLength)
}
conditionJSON, err := json.MarshalIndent(condition.GetConditions(), "", "\t")
if err != nil {
return nil, fmt.Errorf("change condition to json struct failed: %v", err)
}
opts := &bind.CallOpts{From: service.crudAuth.From}
result, err := service.crud.Select(opts, table.GetTableName(), table.GetKey(), string(conditionJSON[:]), table.GetOptional())
result, err := service.crud.Select(opts, tableName, key, string(conditionJSON[:]), "")
if err != nil {
return nil, fmt.Errorf("CRUDService Select failed: %v", err)
}
Expand All @@ -163,23 +161,18 @@ func (service *CRUDService) Select(table *Table, condition *Condition) ([]map[st
}

// Desc is used for Table
func (service *CRUDService) Desc(tableName string) (*Table, error) {
table := &Table{}
table.SetTableName(SysTable)
table.SetKey(UserTablePrefix + tableName)
condition := table.GetCondition()
userTable, err := service.Select(table, condition)
func (service *CRUDService) Desc(userTableName string) (string, string, error) {
tableName := SysTable
key := UserTablePrefix + userTableName
condition := NewCondition()
userTable, err := service.Select(tableName, key, condition)
if err != nil {
return nil, fmt.Errorf("select table failed: %v", err)
return "", "", fmt.Errorf("select table failed: %v", err)
}
tableInfo := &Table{}
if len(userTable) == 0 {
return nil, fmt.Errorf("The table %s does not exist", tableName)
return "", "", fmt.Errorf("the table %s does not exist", tableName)
}
tableInfo.SetTableName(tableName)
tableInfo.SetKey(userTable[0]["key_field"])
tableInfo.SetValueFields(userTable[0]["value_field"])
return table, nil
return userTable[0]["key_field"], userTable[0]["value_field"], nil
}

func handleReceipt(receipt *types.Receipt) (int, error) {
Expand All @@ -191,8 +184,8 @@ func handleReceipt(receipt *types.Receipt) (int, error) {
if output != "0x" {
i := new(big.Int)
var flag bool
i, flag = i.SetString(output[2:len(output)], 16)
if flag == false {
i, flag = i.SetString(output[2:], 16)
if !flag {
return -1, fmt.Errorf("handleReceipt: convert receipt output to int failed")
}
return int(i.Uint64()), nil
Expand Down
102 changes: 56 additions & 46 deletions precompiled/crud/crud_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package crud

import (
"crypto/ecdsa"
"math/rand"
"strconv"
"testing"

Expand All @@ -11,6 +10,12 @@ import (
"github.com/ethereum/go-ethereum/crypto"
)

const (
tableName = "t_test"
key = "name"
valueFields = "item_id, item_name"
)

func GetClient(t *testing.T) *client.Client {
// config := &conf.ParseConfig("config.toml")[0]
config := &conf.Config{IsHTTP: true, ChainID: 1, IsSMCrypto: false, GroupID: 1,
Expand Down Expand Up @@ -41,82 +46,87 @@ func GetService(t *testing.T) *CRUDService {
return service
}

func TestCRUD(t *testing.T) {
tableName := "t_test" + strconv.Itoa(rand.Intn(100000))
key := "name"
valueFields := "item_id, item_name"
table := &Table{TableName: tableName, Key: key, ValueFields: valueFields}

func TestCreateTable(t *testing.T) {
service := GetService(t)

// create table
resultCreate, err := service.CreateTable(table)
resultCreate, err := service.CreateTable(tableName, key, valueFields)
if err != nil {
t.Fatalf("create table failed: %v", err)
}
t.Logf("resultCreate: %d\n", resultCreate)
}

func TestInsert(t *testing.T) {
service := GetService(t)

// insert records
var insertResults int
insertEntry := NewEntry()
for i := 1; i <= 5; i++ {
insertEnrty := table.GetEntry()
insertEnrty.Put("item_id", "1")
insertEnrty.Put("item_name", "apple"+strconv.Itoa(i))
table.SetKey("fruit")
insertResult, err := service.Insert(table, insertEnrty)
insertEntry.Put("item_id", "1")
insertEntry.Put("item_name", "apple"+strconv.Itoa(i))
insertResult, err := service.Insert(tableName, "fruit", insertEntry)
if err != nil {
t.Fatalf("insert table faied: %v", err)
t.Fatalf("insert table failed: %v", err)
}
insertResults += insertResult
}
t.Logf("insertResults: %d\n", insertResults)
}

func TestSelect(t *testing.T) {
service := GetService(t)

// select records
condition1 := table.GetCondition()
condition1.EQ("item_id", "1")
condition1.Limit(1)
condition := NewCondition()
condition.EQ("item_id", "1")
condition.Limit(5)

resultSelect1, err := service.Select(table, condition1)
resultSelect, err := service.Select(tableName, "fruit", condition)
if err != nil {
t.Fatalf("select table faied: %v", err)
t.Fatalf("select table failed: %v", err)
}
t.Logf("resultSelect1 :\n")
t.Logf("%s\n", resultSelect1[0]["name"])
t.Logf("%s\n", resultSelect1[0]["item_id"])
t.Logf("%s\n", resultSelect1[0]["item_name"])
t.Logf("resultSelect :\n")
t.Logf("%d", len(resultSelect))
for i := 0; i < len(resultSelect); i++ {
t.Logf("resultSelect[%d]'s name is:%s\n", i, resultSelect[i]["name"])
t.Logf("resultSelect[%d]'s item_id is:%s\n", i, resultSelect[i]["item_id"])
t.Logf("resultSelect[%d]'s item_name is:%s\n", i, resultSelect[i]["item_name"])
}
}

// update records
updateEntry := table.GetEntry()
func TestUpdate(t *testing.T) {
service := GetService(t)

updateEntry := NewEntry()
updateEntry.Put("item_id", "1")
updateEntry.Put("item_name", "orange")
updateCondition := table.GetCondition()
updateCondition := NewCondition()
updateCondition.EQ("item_id", "1")
updateResult, err := service.Update(table, updateEntry, updateCondition)
updateResult, err := service.Update(tableName, "fruit", updateEntry, updateCondition)
if err != nil {
t.Fatalf("update table failed: %v", err)
}
t.Logf("updateResult: %d", updateResult)
}

// select records
condition2 := table.GetCondition()
condition2.EQ("item_id", "1")
condition2.Limit(1)
func TestRemove(t *testing.T) {
service := GetService(t)

resultSelect2, err := service.Select(table, condition2)
removeCondition := NewCondition()
removeCondition.EQ("item_id", "1")
removeResult, err := service.Remove(tableName, "fruit", removeCondition)
if err != nil {
t.Fatalf("select table faied: %v", err)
t.Fatalf("remove table failed: %v", err)
}
t.Logf("resultSelect2 :\n")
t.Logf("%s\n", resultSelect2[0]["name"])
t.Logf("%s\n", resultSelect2[0]["item_id"])
t.Logf("%s\n", resultSelect2[0]["item_name"])
t.Logf("removeResult: %d\n", removeResult)
}

// remove records
removeCondition := table.GetCondition()
removeCondition.EQ("item_id", "1")
removeResult, err := service.Remove(table, removeCondition)
func TestDesc(t *testing.T) {
service := GetService(t)

keyField, valueField, err := service.Desc(tableName)
if err != nil {
t.Fatalf("remove table faied: %v", err)
t.Fatalf("query table info by tableName failed: %v", err)
}
t.Logf("removeResult: %d\n", removeResult)
t.Logf("keyFiled is:%s\n", keyField)
t.Logf("valueField is:%s\n", valueField)
}
4 changes: 4 additions & 0 deletions precompiled/crud/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ type Entry struct {
fields map[string]string
}

func NewEntry() *Entry {
return &Entry{fields: make(map[string]string)}
}

func (e *Entry) GetFields() map[string]string {
return e.fields
}
Expand Down
48 changes: 0 additions & 48 deletions precompiled/crud/table.go

This file was deleted.

2 changes: 1 addition & 1 deletion precompiled/permission/permission_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (service *PermissionService) GrantUserTableManager(tableName string, grantr
if err != nil {
return "", fmt.Errorf("PermissionService create CRUDService failed: %v", err)
}
_, err = crudService.Desc(tableName)
_, _, err = crudService.Desc(tableName)
if err != nil {
return "", fmt.Errorf("GrantUserTableManager failed: %v", err)
}
Expand Down
Loading

0 comments on commit 98a2043

Please sign in to comment.