diff --git a/precompiled/crud/condition.go b/precompiled/crud/condition.go index 9c35bb5c..3ded962f 100644 --- a/precompiled/crud/condition.go +++ b/precompiled/crud/condition.go @@ -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 diff --git a/precompiled/crud/crud_service.go b/precompiled/crud/crud_service.go index 24f4082b..8bdd8201 100644 --- a/precompiled/crud/crud_service.go +++ b/precompiled/crud/crud_service.go @@ -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) } @@ -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 @@ -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) } @@ -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 @@ -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) } @@ -116,9 +115,8 @@ 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") @@ -126,7 +124,7 @@ func (service *CRUDService) Remove(table *Table, condition *Condition) (int, err 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) } @@ -140,8 +138,8 @@ 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") @@ -149,7 +147,7 @@ func (service *CRUDService) Select(table *Table, condition *Condition) ([]map[st 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) } @@ -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) { @@ -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 diff --git a/precompiled/crud/crud_service_test.go b/precompiled/crud/crud_service_test.go index 2d50e2ee..dc88c893 100644 --- a/precompiled/crud/crud_service_test.go +++ b/precompiled/crud/crud_service_test.go @@ -2,7 +2,6 @@ package crud import ( "crypto/ecdsa" - "math/rand" "strconv" "testing" @@ -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, @@ -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) } diff --git a/precompiled/crud/entry.go b/precompiled/crud/entry.go index 4467a73a..68b55000 100644 --- a/precompiled/crud/entry.go +++ b/precompiled/crud/entry.go @@ -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 } diff --git a/precompiled/crud/table.go b/precompiled/crud/table.go deleted file mode 100644 index 53210ac8..00000000 --- a/precompiled/crud/table.go +++ /dev/null @@ -1,48 +0,0 @@ -package crud - -type Table struct { - TableName string - Key string - ValueFields string - Optional string -} - -func (t *Table) GetTableName() string { - return t.TableName -} - -func (t *Table) GetKey() string { - return t.Key -} - -func (t *Table) GetValueFields() string { - return t.ValueFields -} - -func (t *Table) GetOptional() string { - return t.Optional -} - -func (t *Table) SetTableName(name string) { - t.TableName = name -} - -func (t *Table) SetKey(key string) { - t.Key = key -} - -func (t *Table) SetValueFields(value string) { - t.ValueFields = value -} - -func (t *Table) SetOptional(optional string) { - t.Optional = optional -} - -func (t *Table) GetEntry() *Entry { - return &Entry{fields: make(map[string]string)} -} - -func (t *Table) GetCondition() *Condition { - return &Condition{conditions: make(map[string]map[EnumOP]string)} -} diff --git a/precompiled/permission/permission_service.go b/precompiled/permission/permission_service.go index d1568bbc..cd9aa072 100644 --- a/precompiled/permission/permission_service.go +++ b/precompiled/permission/permission_service.go @@ -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) } diff --git a/precompiled/permission/permission_service_test.go b/precompiled/permission/permission_service_test.go index 4b34e5ec..405701fb 100644 --- a/precompiled/permission/permission_service_test.go +++ b/precompiled/permission/permission_service_test.go @@ -2,12 +2,10 @@ package permission import ( "crypto/ecdsa" - "fmt" "testing" "github.com/FISCO-BCOS/go-sdk/client" "github.com/FISCO-BCOS/go-sdk/conf" - "github.com/FISCO-BCOS/go-sdk/precompiled/crud" "github.com/ethereum/go-ethereum/crypto" ) @@ -17,28 +15,6 @@ const ( permisstionAdd = "0xFbb18d54e9Ee57529cda8c7c52242EFE879f064F" ) -func createUserTable(t *testing.T) error { - tableName := "t_test" - key := "name" - valueFields := "item_id, item_name" - table := &crud.Table{TableName: tableName, Key: key, ValueFields: valueFields} - - c := GetClient(t) - privateKey := GenerateKey(t) - service, err := crud.NewCRUDService(c, privateKey) - if err != nil { - return fmt.Errorf("init CRUDService failed: %+v", err) - } - - // create table - resultCreate, err := service.CreateTable(table) - if err != nil { - return fmt.Errorf("create table %v failed: %+v", tableName, err) - } - t.Logf("resultCreate: %d\n", resultCreate) - return nil -} - func GetClient(t *testing.T) *client.Client { // config := &conf.ParseConfig("config.toml")[0] config := &conf.Config{IsHTTP: true, ChainID: 1, IsSMCrypto: false, GroupID: 1, @@ -98,11 +74,6 @@ func TestGrant(t *testing.T) { } func TestUserTableManager(t *testing.T) { - err := createUserTable(t) - if err != nil { - t.Logf("TestUserTableManager failed: %v", err) - } - service := GetService(t) result, err := service.GrantUserTableManager(tableName, permisstionAdd)