...
|
...
|
@@ -91,14 +91,16 @@ func (ptr *QuerySetService) Update(ctx *domain.Context, querySetId int, queryCom |
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
// 调用底层的组装sql
|
|
|
|
|
|
// 生成Table
|
|
|
masterTable := queryComponents[0].MasterTable
|
|
|
table, err := ptr.CreateOrReFreshQuerySetTable(ctx, qs, masterTable, queryComponents)
|
|
|
table, err := ptr.CreateOrUpdateQuerySetTable(ctx, qs, masterTable, queryComponents)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
// 调用底层的组装sql
|
|
|
|
|
|
// 生成日志
|
|
|
if err = ptr.UpdateQuerySetLog(ctx, qs, queryComponents); err != nil {
|
|
|
return err
|
...
|
...
|
@@ -113,14 +115,196 @@ func (ptr *QuerySetService) Update(ctx *domain.Context, querySetId int, queryCom |
|
|
}
|
|
|
|
|
|
func (ptr *QuerySetService) UpdateQuerySetLog(ctx *domain.Context, querySet *domain.QuerySet, queryComponents []*domain.QueryComponent) error {
|
|
|
var res = make([]FastSourceLog, 0)
|
|
|
if logs := conditionsEditLog(ctx, querySet, queryComponents); len(logs) > 0 {
|
|
|
res = append(res, logs...)
|
|
|
}
|
|
|
if logs := selectsEditLog(ctx, querySet, queryComponents); len(logs) > 0 {
|
|
|
res = append(res, logs...)
|
|
|
}
|
|
|
|
|
|
for _, l := range res {
|
|
|
FastLog(ptr.transactionContext, l.LogType, l.SourceId, l.LogEntry)
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (ptr *QuerySetService) CreateOrReFreshQuerySetTable(ctx *domain.Context, querySet *domain.QuerySet, masterTable domain.QueryComponentTable, queryComponents []*domain.QueryComponent) (*domain.Table, error) {
|
|
|
func conditionsEditLog(ctx *domain.Context, querySet *domain.QuerySet, queryComponents []*domain.QueryComponent) []FastSourceLog {
|
|
|
var res = make([]FastSourceLog, 0)
|
|
|
oldQCs := domain.QueryComponentsToMapById(querySet.QueryComponents)
|
|
|
sourceId := querySet.QuerySetId
|
|
|
entry := domain.NewLogEntry(querySet.Name, querySet.Type, domain.UnKnown, ctx)
|
|
|
|
|
|
// 新增条件判断
|
|
|
addExprList := make([]string, 0)
|
|
|
editExprList := make([][]string, 0)
|
|
|
deleteExprList := make([]string, 0)
|
|
|
|
|
|
for _, item := range queryComponents {
|
|
|
if len(item.Id) == 0 {
|
|
|
continue
|
|
|
}
|
|
|
oldItem, ok := oldQCs[item.Id]
|
|
|
if !ok {
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
// 条件判断编辑情况
|
|
|
mapOldItems := domain.ConditionsToMapById(oldItem.Conditions)
|
|
|
mapNewItems := domain.ConditionsToMapById(item.Conditions)
|
|
|
for _, add := range item.Conditions {
|
|
|
if len(add.Id) != 0 {
|
|
|
continue
|
|
|
}
|
|
|
addExprList = append(addExprList, add.ExprHuman())
|
|
|
}
|
|
|
|
|
|
// 编辑
|
|
|
for _, add := range item.Conditions {
|
|
|
if len(add.Id) == 0 {
|
|
|
continue
|
|
|
}
|
|
|
v, ok := mapOldItems[add.Id]
|
|
|
if !ok {
|
|
|
continue
|
|
|
}
|
|
|
if add.Equal(v) {
|
|
|
continue
|
|
|
}
|
|
|
editExprList = append(editExprList, []string{v.ExprHuman(), add.ExprHuman()})
|
|
|
}
|
|
|
|
|
|
// 删除
|
|
|
for _, item := range oldItem.Conditions {
|
|
|
if len(item.Id) == 0 {
|
|
|
continue
|
|
|
}
|
|
|
_, ok := mapNewItems[item.Id]
|
|
|
if ok {
|
|
|
continue
|
|
|
}
|
|
|
deleteExprList = append(deleteExprList, item.ExprHuman())
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(addExprList) > 0 {
|
|
|
res = append(res, NewFastSourceLog(domain.QuerySetLog, sourceId, &EditQuerySetConditionLog{
|
|
|
LogEntry: entry,
|
|
|
OperationType: domain.AddSetCondition,
|
|
|
Sources: addExprList,
|
|
|
}))
|
|
|
}
|
|
|
|
|
|
if len(editExprList) > 0 {
|
|
|
res = append(res, NewFastSourceLog(domain.QuerySetLog, sourceId, &EditQuerySetConditionLog{
|
|
|
LogEntry: entry,
|
|
|
OperationType: domain.EditSetCondition,
|
|
|
SourceTargets: editExprList,
|
|
|
}))
|
|
|
}
|
|
|
|
|
|
if len(deleteExprList) > 0 {
|
|
|
res = append(res, NewFastSourceLog(domain.QuerySetLog, sourceId, &EditQuerySetConditionLog{
|
|
|
LogEntry: entry,
|
|
|
OperationType: domain.AddSetCondition,
|
|
|
Sources: deleteExprList,
|
|
|
}))
|
|
|
}
|
|
|
return res
|
|
|
}
|
|
|
|
|
|
func selectsEditLog(ctx *domain.Context, querySet *domain.QuerySet, queryComponents []*domain.QueryComponent) []FastSourceLog {
|
|
|
var res = make([]FastSourceLog, 0)
|
|
|
oldQCs := domain.QueryComponentsToMapById(querySet.QueryComponents)
|
|
|
//newQCs := domain.QueryComponentsToMapById(queryComponents)
|
|
|
sourceId := querySet.QuerySetId
|
|
|
entry := domain.NewLogEntry(querySet.Name, querySet.Type, domain.UnKnown, ctx)
|
|
|
|
|
|
addExprList := make([]string, 0)
|
|
|
editExprList := make([][]string, 0)
|
|
|
deleteExprList := make([]string, 0)
|
|
|
for _, item := range queryComponents {
|
|
|
if len(item.Id) == 0 {
|
|
|
continue
|
|
|
}
|
|
|
oldItem, ok := oldQCs[item.Id]
|
|
|
if !ok {
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
// 条件判断编辑情况
|
|
|
allOldSelectExpr := oldItem.AllSelectExpr()
|
|
|
mapOldConditions := domain.SelectsExprToMapById(allOldSelectExpr)
|
|
|
allNewSelectExpr := item.AllSelectExpr()
|
|
|
mapNewConditions := domain.SelectsExprToMapById(allNewSelectExpr)
|
|
|
// 新增条件判断
|
|
|
for _, item := range allNewSelectExpr {
|
|
|
if len(item.Id) != 0 {
|
|
|
continue
|
|
|
}
|
|
|
addExprList = append(addExprList, item.ExprHuman())
|
|
|
}
|
|
|
// 编辑
|
|
|
for _, item := range allNewSelectExpr {
|
|
|
if len(item.Id) == 0 {
|
|
|
continue
|
|
|
}
|
|
|
v, ok := mapOldConditions[item.Id]
|
|
|
if !ok {
|
|
|
continue
|
|
|
}
|
|
|
if item.Equal(v) {
|
|
|
continue
|
|
|
}
|
|
|
editExprList = append(editExprList, []string{v.ExprHuman(), item.ExprHuman()})
|
|
|
}
|
|
|
|
|
|
// 删除
|
|
|
for _, item := range allOldSelectExpr {
|
|
|
if len(item.Id) == 0 {
|
|
|
continue
|
|
|
}
|
|
|
_, ok := mapNewConditions[item.Id]
|
|
|
if ok {
|
|
|
continue
|
|
|
}
|
|
|
deleteExprList = append(deleteExprList, item.ExprHuman())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if len(addExprList) > 0 {
|
|
|
res = append(res, NewFastSourceLog(domain.QuerySetLog, sourceId, &EditSelectConditionLog{
|
|
|
LogEntry: entry,
|
|
|
OperationType: domain.AddSelectCondition,
|
|
|
Sources: addExprList,
|
|
|
}))
|
|
|
}
|
|
|
|
|
|
if len(editExprList) > 0 {
|
|
|
res = append(res, NewFastSourceLog(domain.QuerySetLog, sourceId, &EditSelectConditionLog{
|
|
|
LogEntry: entry,
|
|
|
OperationType: domain.EditSelectCondition,
|
|
|
SourceTargets: editExprList,
|
|
|
}))
|
|
|
}
|
|
|
|
|
|
if len(deleteExprList) > 0 {
|
|
|
res = append(res, NewFastSourceLog(domain.QuerySetLog, sourceId, &EditSelectConditionLog{
|
|
|
LogEntry: entry,
|
|
|
OperationType: domain.DeleteSelectCondition,
|
|
|
Sources: deleteExprList,
|
|
|
}))
|
|
|
}
|
|
|
return res
|
|
|
}
|
|
|
|
|
|
func (ptr *QuerySetService) CreateOrUpdateQuerySetTable(ctx *domain.Context, querySet *domain.QuerySet, masterTable domain.QueryComponentTable, queryComponents []*domain.QueryComponent) (*domain.Table, error) {
|
|
|
var (
|
|
|
err error
|
|
|
foundMasterTable *domain.Table
|
|
|
)
|
|
|
dependencyTables := querySet.GetDependencyTables(queryComponents)
|
|
|
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
|
|
|
foundMasterTable, err = tableRepository.FindOne(map[string]interface{}{"context": ctx, "tableId": masterTable.TableId})
|
|
|
if err != nil {
|
...
|
...
|
@@ -136,8 +320,24 @@ func (ptr *QuerySetService) CreateOrReFreshQuerySetTable(ctx *domain.Context, qu |
|
|
table.DataFields = masterTable.Fields
|
|
|
table.UpdatedAt = time.Now()
|
|
|
}
|
|
|
|
|
|
// 循环依赖判断
|
|
|
tableDependencyService, _ := NewTableDependencyService(ptr.transactionContext)
|
|
|
if len(dependencyTables) > 0 {
|
|
|
_, tables, err := tableRepository.Find(map[string]interface{}{"context": ctx, "tableIds": dependencyTables, "tableTypes": []string{domain.SchemaTable.ToString(), domain.SubProcessTable.ToString()}})
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
if len(tables) > 0 {
|
|
|
tree := tableDependencyService.TableDependTree(tables, querySet.QuerySetInfo.BindTableId)
|
|
|
if tableDependencyService.Detect(ctx, tree.EdgesArray()) {
|
|
|
return nil, NewCircleDependError(tableDependencyService.CircleTable(), querySet)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
table.TableInfo.ApplyOnModule = domain.ModuleAll
|
|
|
table.TableInfo.DependencyTables = querySet.GetDependencyTables(queryComponents)
|
|
|
table.TableInfo.DependencyTables = dependencyTables
|
|
|
table, err = tableRepository.Save(table)
|
|
|
if err != nil {
|
|
|
return nil, err
|
...
|
...
|
@@ -257,10 +457,19 @@ func (ptr *QuerySetService) Delete(ctx *domain.Context, querySetId int) error { |
|
|
return err
|
|
|
}
|
|
|
querySetRepository, _ := repository.NewQuerySetRepository(ptr.transactionContext)
|
|
|
tableDependencyService, _ := NewTableDependencyService(ptr.transactionContext)
|
|
|
for i := range querySets {
|
|
|
if _, err := querySetRepository.Remove(querySets[i]); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
if querySets[i].QuerySetInfo.BindTableId > 0 {
|
|
|
if err := tableDependencyService.HasDependencyError(ctx, querySets[i].QuerySetInfo.BindTableId); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
if err := dao.TableSoftDelete(ptr.transactionContext, querySets[i].QuerySetInfo.BindTableId, domain.TableType(querySets[i].Type)); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// 日志
|
|
|
if len(querySets) > 0 {
|
...
|
...
|
@@ -353,7 +562,7 @@ func insertQuerySetsByIndex(list []*domain.QuerySet, item *domain.QuerySet, inde |
|
|
func (ptr *QuerySetService) DependencyGraph(ctx *domain.Context, querySetId int) (interface{}, error) {
|
|
|
tableRepository, _ := repository.NewTableRepository(ptr.transactionContext)
|
|
|
_, tables, err := tableRepository.Find(map[string]interface{}{"context": ctx, "tableTypes": []string{domain.SchemaTable.ToString(), domain.SubProcessTable.ToString()}})
|
|
|
dependencies := make([]dependency, 0)
|
|
|
var dependencies TableDependTree = NewTableDependTree(nil, nil)
|
|
|
if err != nil {
|
|
|
return dependencies, err
|
|
|
}
|
...
|
...
|
@@ -368,89 +577,10 @@ func (ptr *QuerySetService) DependencyGraph(ctx *domain.Context, querySetId int) |
|
|
if querySet.QuerySetInfo.BindTableId == 0 {
|
|
|
return dependencies, nil
|
|
|
}
|
|
|
dependencies = makeDependencyGraph(querySet.QuerySetInfo.BindTableId, tables)
|
|
|
return dependencies, nil
|
|
|
}
|
|
|
|
|
|
func makeDependencyGraph(bindTableId int, tables []*domain.Table) []dependency {
|
|
|
dependencies := make([]dependency, 0)
|
|
|
tableMap := make(map[int]*domain.Table)
|
|
|
graph := make(map[int][]int, 0)
|
|
|
for i := range tables {
|
|
|
tableMap[tables[i].TableId] = tables[i]
|
|
|
graph[tables[i].TableId] = tables[i].TableInfo.DependencyTables
|
|
|
}
|
|
|
// parent depend
|
|
|
dependTables := []int{bindTableId}
|
|
|
|
|
|
foundDependency := make(map[string]bool, 0)
|
|
|
for {
|
|
|
if len(dependTables) == 0 {
|
|
|
break
|
|
|
}
|
|
|
parent := dependTables[0]
|
|
|
parentTable, ok := tableMap[parent]
|
|
|
if !ok {
|
|
|
continue
|
|
|
}
|
|
|
for _, dependChildId := range parentTable.TableInfo.DependencyTables {
|
|
|
dependChild, ok := tableMap[dependChildId]
|
|
|
if !ok {
|
|
|
continue
|
|
|
}
|
|
|
depend := NewDependency(parentTable.TableId, dependChild)
|
|
|
if _, ok := foundDependency[depend.String()]; !ok {
|
|
|
dependencies = append(dependencies, depend)
|
|
|
}
|
|
|
}
|
|
|
dependTables = dependTables[1:]
|
|
|
}
|
|
|
|
|
|
// dependToChild
|
|
|
return dependencies
|
|
|
}
|
|
|
|
|
|
func dependParents(tables []*domain.Table, tableMap map[int]*domain.Table, bindTableId int) []int {
|
|
|
foundDependTable := make(map[int]bool)
|
|
|
traceStack := []int{bindTableId}
|
|
|
res := []int{bindTableId}
|
|
|
for {
|
|
|
if len(traceStack) == 0 {
|
|
|
break
|
|
|
}
|
|
|
last := traceStack[0]
|
|
|
traceStack = traceStack[1:]
|
|
|
table := tableMap[last]
|
|
|
if table == nil {
|
|
|
continue
|
|
|
}
|
|
|
// for
|
|
|
if _, ok := foundDependTable[last]; !ok {
|
|
|
foundDependTable[last] = true
|
|
|
}
|
|
|
}
|
|
|
return res
|
|
|
}
|
|
|
|
|
|
func NewDependency(parentId int, child *domain.Table) dependency {
|
|
|
return dependency{
|
|
|
Id: parentId,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
type dependency struct {
|
|
|
// 标识
|
|
|
Id int `json:"id"`
|
|
|
// 表类型 MainTable:主表 SideTable:副表 SubTable:分表 ExcelTable:Excel表
|
|
|
Type string `json:"type"`
|
|
|
// 名称
|
|
|
Name string `json:"name"`
|
|
|
// 依赖的表
|
|
|
DependChildId int `json:"dependChildId"`
|
|
|
}
|
|
|
|
|
|
func (d dependency) String() string {
|
|
|
return fmt.Sprintf("%d-%d", d.Id, d.DependChildId)
|
|
|
tableDependencyService, _ := NewTableDependencyService(ptr.transactionContext)
|
|
|
dependencies = tableDependencyService.TableDependTree(tables, querySet.QuerySetInfo.BindTableId)
|
|
|
return dependencies, nil
|
|
|
}
|
|
|
|
|
|
func (ptr *QuerySetService) GetAllChild(ctx *domain.Context, querySetId int, includeSelf bool, onlyNextLevel bool) ([]*domain.QuerySet, error) {
|
...
|
...
|
|