作者 yangfu
提交者 yangfu

fix: duplicate table name

... ... @@ -26,6 +26,7 @@ require (
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shopspring/decimal v1.3.1
github.com/smartystreets/goconvey v1.7.2 // indirect
github.com/sony/sonyflake v1.1.0 // indirect
github.com/stretchr/testify v1.7.1
github.com/valyala/fasthttp v1.38.0 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
... ...
... ... @@ -71,7 +71,10 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
if table.TableType == domain.SubTable.ToString() && field.Flag == domain.ManualField {
return empty, nil
}
match := cmd.Match
if !domain.SQLType(field.SQLType).IsString() {
match = ""
}
options := &starrocks.QueryOptions{
Table: table,
TableName: table.SQLName,
... ... @@ -80,7 +83,7 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
{
Condition: domain.Condition{
Field: field,
Like: cmd.Match,
Like: match,
Order: "ASC",
},
Distinct: true,
... ... @@ -107,8 +110,9 @@ func (tableService *TableService) FieldOptionalValues(ctx *domain.Context, cmd *
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
values := dataTable.OptionalValue(cmd.Match)
return map[string]interface{}{
"values": dataTable.OptionalValue(),
"total": dataTable.Total,
"values": values,
"total": len(values),
}, nil
}
... ...
... ... @@ -68,6 +68,10 @@ func (tableService *TableService) TablePreview(ctx *domain.Context, cmd *command
if err != nil {
return nil, factory.FastError(err)
}
// 计算项只返回第一个值
// if table.TableType==domain.CalculateItem.ToString() && dataTable!=nil && len(dataTable.Data)>0{
// dataTable.Data = dataTable.Data[:1]
// }
response.Load(table, dataTable, domain.ObjectMetaTable)
}
}
... ...
package domain
import "strings"
type DataTable struct {
Fields []*Field `json:"fields"`
Data [][]string `json:"data"`
... ... @@ -51,19 +53,20 @@ type Condition struct {
Order string `json:"order"`
}
func (t *DataTable) OptionalValue() []string {
//set := make(map[string]string)
func (t *DataTable) OptionalValue(args ...string) []string {
var values = make([]string, 0)
match:=""
if len(args)>0{
match = args[0]
}
if len(t.Data) > 0 && len(t.Data[0]) == 1 {
for i := range t.Data {
if len(t.Data[i]) == 0 {
continue
}
//if _, ok := set[t.Data[i][0]]; ok {
// continue
//} else {
// set[t.Data[i][0]] = ""
//}
if len(match)>0 && !strings.Contains(t.Data[i][0],match){
continue
}
values = append(values, t.Data[i][0])
}
}
... ...
... ... @@ -67,12 +67,25 @@ func (table *Table) TableIdString() string {
func (table *Table) WithContext(ctx *Context) *Table {
rand.Seed(time.Now().Unix())
table.SQLName = fmt.Sprintf("%v_t%v_c%v", table.SQLName, rand.Intn(1000000), ctx.CompanyId)
table.SQLName = fmt.Sprintf("%v_t%v_c%v", limitStringLen(table.SQLName,40), rand.Intn(1000000), limitStringLen(fmt.Sprintf("%d",ctx.CompanyId),8))
table.Context = ctx
return table
}
func limitStringLen(s string,l int)string{
result:=s
subLength :=l /2
if len(result)>l{
result = result[:subLength] + result[len(result)-subLength:]
}
return result
}
func (table *Table) WithPrefix(prefix string) *Table {
if strings.HasPrefix(table.SQLName,"_"){
table.SQLName = fmt.Sprintf("%v%v", strings.ToLower(prefix), table.SQLName)
return table
}
table.SQLName = fmt.Sprintf("%v_%v", strings.ToLower(prefix), table.SQLName)
return table
}
... ... @@ -132,3 +145,11 @@ func (t *Table) AssertTableType(types ...TableType) bool {
}
return false
}
func TableTypesToStringList(list ...TableType)[]string{
var result = make([]string,0)
for _,item:=range list{
result = append(result, item.ToString())
}
return result
}
\ No newline at end of file
... ...
package domain
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestLimitStringLen(t *testing.T){
inputs:=[]struct{
input string
length int
want string
}{
{
input: "123456789",
length: 6,
want: "123789",
},
{
input: "123456789",
length: 7,
want: "123789",
},
{
input: "123456789",
length: 10,
want: "123456789",
},
{
input: "pai_xu_ce_shi_zhu_biao_fu_zhi",
length: 20,
want: "pai_xu_ce_iao_fu_zhi",
},
}
for _,input:=range inputs{
got:=limitStringLen(input.input,input.length)
assert.Equal(t,input.want,got)
}
}
\ No newline at end of file
... ...
... ... @@ -159,7 +159,7 @@ func NewCopyTable(tableType domain.TableType, fileName string, dataFields []*dom
table.Name = fileName
table.SQLName = pin(fileName) //SQLTableName()
table.PK = PK()
if table.TableType == domain.CalculateTable.ToString() || table.TableType == domain.CalculateItem.ToString() {
if table.TableType == domain.CalculateItem.ToString() { //table.TableType == domain.CalculateTable.ToString() ||
table.PK = nil
}
table.DataFieldIndex = len(dataFields)
... ...
... ... @@ -30,7 +30,9 @@ func (ptr *GenerateMainTableService) GenerateTable(ctx *domain.Context, fileId i
return nil, fmt.Errorf("表名称重复")
}
mainTable := NewTable(domain.MainTable, tableName, table.DataFields, table.RowCount).WithContext(ctx)
mainTable := NewTable(domain.MainTable, tableName, table.DataFields, table.RowCount).
WithContext(ctx).
WithPrefix(domain.MainTable.ToString())
_, err = tableRepository.Save(mainTable)
if err != nil {
return nil, err
... ...
... ... @@ -199,6 +199,8 @@ func FastTable(table *domain.Table) (*domain.DataTable, error) {
TableName: table.SQLName,
Select: table.Fields(false),
}
options.SetDefaultOrder()
options.SetOffsetLimit(1,10000)
var dataTable *domain.DataTable
dataTable, err = FastDataTable(options)
if err != nil {
... ...
... ... @@ -40,7 +40,7 @@ func (ptr *AddTableStructService) AddTableStruct(ctx *domain.Context, parentTabl
fields = MappingFields(mainTable, fields)
dataFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.MainTableField})
manualFields := (domain.Fields)(fields).Select(map[string]interface{}{"flag": domain.ManualField})
table := NewTable(domain.SubTable, name, fields, mainTable.RowCount).WithContext(ctx)
table := NewTable(domain.SubTable, name, fields, mainTable.RowCount).WithContext(ctx).WithPrefix(string(domain.SubTable))
table.DataFieldIndex = mainTable.DataFieldIndex
table.DataFields = dataFields
table.ManualFields = manualFields
... ...
... ... @@ -2,6 +2,7 @@ package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/repository"
... ... @@ -45,7 +46,10 @@ func (ptr *CopyDataTableService) CopyTable(ctx *domain.Context, tableId int, tab
}
}
// 验证表名是否重复
duplicateTable, err := tableRepository.FindOne(map[string]interface{}{"context": ctx, "tableName": tableName})
duplicateTable, err := tableRepository.FindOne(map[string]interface{}{
"context": ctx,
"tableName": tableName,
"tableTypes":domain.TableTypesToStringList(domain.MainTable,domain.SubTable,domain.SideTable)})
if err == nil && duplicateTable != nil {
return nil, fmt.Errorf("表名称重复")
}
... ... @@ -53,7 +57,8 @@ func (ptr *CopyDataTableService) CopyTable(ctx *domain.Context, tableId int, tab
sideTable := NewCopyTable(domain.SideTable, tableName, dataFields, table.RowCount).
WithContext(ctx).
WithParentId(table.TableId).
WithDataFieldIndex(table.DataFieldIndex)
WithDataFieldIndex(table.DataFieldIndex).
WithPrefix(domain.SideTable.ToString())
if sideTable, err = tableRepository.Save(sideTable); err != nil {
return nil, err
}
... ...
... ... @@ -62,7 +62,9 @@ func (ptr *UpdateTableStructService) UpdateTableStruct(ctx *domain.Context, tabl
return nil, err
}
}
defer func() {
AsyncEvent(domain.NewEventTable(ctx, domain.TableDataEditEvent).WithTable(table))
}()
// Log
// 日志
if err = FastLog(ptr.transactionContext, domain.CommonLog, table.TableId, &SubTableEditLog{
... ...
... ... @@ -3,8 +3,8 @@ package starrocks
import (
"bytes"
"fmt"
"github.com/google/uuid"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
"gorm.io/gorm"
"html"
"html/template"
... ... @@ -15,8 +15,12 @@ func Insert(db *gorm.DB, tableName string, fields []*domain.FieldValue) error {
for _, f := range fields {
if f.Field.Flag == domain.PKField && f.Value == "" {
//continue
id, _ := uuid.NewUUID()
f.Value = id.String()
//id, _ := uuid.NewUUID()
// idString := id.String()
// 需要调用分布式id生成
id, _ := utils.NewSnowflakeId()
idString := fmt.Sprintf("%d", id)
f.Value = idString
}
value[f.Field.SQLName] = f.TypeValue()
}
... ...
... ... @@ -117,3 +117,10 @@ func TestRound(t *testing.T) {
t.Logf("%.1f", Truncate(99.99, 1))
t.Logf("%v", Truncate(99, 0))
}
func TestNewSnowflakeId(t *testing.T) {
id, _ := NewSnowflakeId()
for i := 0; i < 100; i++ {
t.Log(id / 2)
}
}
... ...