正在显示
13 个修改的文件
包含
257 行增加
和
49 行删除
| @@ -4,9 +4,12 @@ import ( | @@ -4,9 +4,12 @@ import ( | ||
| 4 | "context" | 4 | "context" |
| 5 | "fmt" | 5 | "fmt" |
| 6 | "github.com/beego/beego/v2/task" | 6 | "github.com/beego/beego/v2/task" |
| 7 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
| 7 | "github.com/linmadan/egglib-go/utils/xtime" | 8 | "github.com/linmadan/egglib-go/utils/xtime" |
| 8 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory" | 9 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/application/factory" |
| 9 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain" | 10 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain" |
| 11 | + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/dao" | ||
| 12 | + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/starrocks" | ||
| 10 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log" | 13 | "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log" |
| 11 | "io/fs" | 14 | "io/fs" |
| 12 | "os" | 15 | "os" |
| @@ -28,6 +31,9 @@ func (crontabService *CrontabService) initTask() { | @@ -28,6 +31,9 @@ func (crontabService *CrontabService) initTask() { | ||
| 28 | 31 | ||
| 29 | autoRemovePublicDownloadFile := task.NewTask("定时清理缓存文件", "0 20 */1 * * *", AutoRemovePublicDownloadFile) | 32 | autoRemovePublicDownloadFile := task.NewTask("定时清理缓存文件", "0 20 */1 * * *", AutoRemovePublicDownloadFile) |
| 30 | task.AddTask("autoRemovePublicDownloadFile", autoRemovePublicDownloadFile) | 33 | task.AddTask("autoRemovePublicDownloadFile", autoRemovePublicDownloadFile) |
| 34 | + | ||
| 35 | + autoRemoveTemporaryTable := task.NewTask("定时清理临时表", "0 57 */1 * * *", AutoRemoveTemporaryTable) | ||
| 36 | + task.AddTask("autoRemoveTemporaryTable", autoRemoveTemporaryTable) | ||
| 31 | } | 37 | } |
| 32 | 38 | ||
| 33 | func (crontabService *CrontabService) StartCrontabTask() { | 39 | func (crontabService *CrontabService) StartCrontabTask() { |
| @@ -115,3 +121,52 @@ func AutoRemovePublicDownloadFile(ctx context.Context) error { | @@ -115,3 +121,52 @@ func AutoRemovePublicDownloadFile(ctx context.Context) error { | ||
| 115 | } | 121 | } |
| 116 | return nil | 122 | return nil |
| 117 | } | 123 | } |
| 124 | + | ||
| 125 | +func AutoRemoveTemporaryTable(ctx context.Context) error { | ||
| 126 | + defer func() { | ||
| 127 | + if r := recover(); r != nil { | ||
| 128 | + log.Logger.Error(fmt.Sprintf("%v", r), map[string]interface{}{"task": "定时清理过期临时文件记录"}) | ||
| 129 | + } | ||
| 130 | + }() | ||
| 131 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 132 | + if err != nil { | ||
| 133 | + return err | ||
| 134 | + } | ||
| 135 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 136 | + return err | ||
| 137 | + } | ||
| 138 | + defer func() { | ||
| 139 | + if err != nil { | ||
| 140 | + log.Logger.Error("【定时清理临时表】 失败:" + err.Error()) | ||
| 141 | + } | ||
| 142 | + transactionContext.RollbackTransaction() | ||
| 143 | + }() | ||
| 144 | + | ||
| 145 | + log.Logger.Debug("【定时清理临时表】 启动") | ||
| 146 | + end := xtime.New(time.Now()).BeginningOfDay().Add(-time.Hour * 12) | ||
| 147 | + begin := end.AddDate(0, 0, -7) | ||
| 148 | + | ||
| 149 | + tableRepository, _, _ := factory.FastPgTable(transactionContext, 0) | ||
| 150 | + _, tables, err := tableRepository.Find(map[string]interface{}{"beginTime": begin, "endTime": end, "tableTypes": []string{domain.TemporaryTable.ToString()}}) | ||
| 151 | + if err != nil { | ||
| 152 | + return nil | ||
| 153 | + } | ||
| 154 | + for i, t := range tables { | ||
| 155 | + if err = dao.TableDelete(transactionContext.(*pgTransaction.TransactionContext), t.TableId, domain.TemporaryTable); err != nil { | ||
| 156 | + log.Logger.Error(err.Error()) | ||
| 157 | + return nil | ||
| 158 | + } | ||
| 159 | + log.Logger.Info(fmt.Sprintf("序号:%d 清理临时表 %v", i, t.SQLName)) | ||
| 160 | + if err = starrocks.DropView(starrocks.DB, t.SQLName); err != nil { | ||
| 161 | + log.Logger.Error(err.Error()) | ||
| 162 | + return nil | ||
| 163 | + } | ||
| 164 | + } | ||
| 165 | + //if err = dao.TableDeleteByTime(transactionContext.(*pgTransaction.TransactionContext), domain.TemporaryTable, begin, end); err != nil { | ||
| 166 | + // return err | ||
| 167 | + //} | ||
| 168 | + if err = transactionContext.CommitTransaction(); err != nil { | ||
| 169 | + return err | ||
| 170 | + } | ||
| 171 | + return nil | ||
| 172 | +} |
| @@ -344,6 +344,35 @@ func (querySetService *QuerySetService) UpdateQuerySet(ctx *domain.Context, upda | @@ -344,6 +344,35 @@ func (querySetService *QuerySetService) UpdateQuerySet(ctx *domain.Context, upda | ||
| 344 | return struct{}{}, nil | 344 | return struct{}{}, nil |
| 345 | } | 345 | } |
| 346 | 346 | ||
| 347 | +func (querySetService *QuerySetService) PreviewPrepare(ctx *domain.Context, updateQuerySetCommand *command.UpdateQuerySetCommand) (interface{}, error) { | ||
| 348 | + if err := updateQuerySetCommand.ValidateCommand(); err != nil { | ||
| 349 | + return nil, application.ThrowError(application.ARG_ERROR, err.Error()) | ||
| 350 | + } | ||
| 351 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 352 | + if err != nil { | ||
| 353 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 354 | + } | ||
| 355 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 356 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 357 | + } | ||
| 358 | + defer func() { | ||
| 359 | + transactionContext.RollbackTransaction() | ||
| 360 | + }() | ||
| 361 | + svr, _ := factory.FastQuerySetServices(transactionContext) | ||
| 362 | + table, err := svr.PreviewPrepare(ctx, updateQuerySetCommand.QuerySetId, updateQuerySetCommand.QueryComponents) | ||
| 363 | + if err != nil { | ||
| 364 | + return nil, factory.FastError(err) | ||
| 365 | + } | ||
| 366 | + | ||
| 367 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 368 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 369 | + } | ||
| 370 | + return map[string]interface{}{ | ||
| 371 | + "objectId": table.TableId, | ||
| 372 | + "sqlName": table.SQLName, | ||
| 373 | + }, nil | ||
| 374 | +} | ||
| 375 | + | ||
| 347 | func NewQuerySetService(options map[string]interface{}) *QuerySetService { | 376 | func NewQuerySetService(options map[string]interface{}) *QuerySetService { |
| 348 | newQuerySetService := &QuerySetService{} | 377 | newQuerySetService := &QuerySetService{} |
| 349 | return newQuerySetService | 378 | return newQuerySetService |
| @@ -523,11 +523,8 @@ func (tableService *TableService) CheckRowDuplicateV2(ctx *domain.Context, cmd * | @@ -523,11 +523,8 @@ func (tableService *TableService) CheckRowDuplicateV2(ctx *domain.Context, cmd * | ||
| 523 | } | 523 | } |
| 524 | 524 | ||
| 525 | querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0) | 525 | querySetRepository, _, _ := factory.FastPgQuerySet(transactionContext, 0) |
| 526 | - querySet, err := querySetRepository.FindOne(map[string]interface{}{"BindTableId": table.TableId, "context": ctx}) | ||
| 527 | - if err != nil { | ||
| 528 | - return nil, factory.FastError(err) | ||
| 529 | - } | ||
| 530 | - if len(querySet.QueryComponents) == 1 { | 526 | + querySet, _ := querySetRepository.FindOne(map[string]interface{}{"BindTableId": table.TableId, "context": ctx}) |
| 527 | + if querySet != nil && len(querySet.QueryComponents) == 1 { | ||
| 531 | return defaultResponse, nil | 528 | return defaultResponse, nil |
| 532 | } | 529 | } |
| 533 | 530 |
| @@ -75,10 +75,11 @@ var ( | @@ -75,10 +75,11 @@ var ( | ||
| 75 | ) | 75 | ) |
| 76 | 76 | ||
| 77 | var ( | 77 | var ( |
| 78 | - MainTable TableType = "MainTable" | ||
| 79 | - SideTable TableType = "SideTable" | ||
| 80 | - SubTable TableType = "SubTable" | ||
| 81 | - ExcelTable TableType = "ExcelTable" | 78 | + MainTable TableType = "MainTable" |
| 79 | + SideTable TableType = "SideTable" | ||
| 80 | + SubTable TableType = "SubTable" | ||
| 81 | + ExcelTable TableType = "ExcelTable" | ||
| 82 | + TemporaryTable TableType = "TemporaryTable" | ||
| 82 | ) | 83 | ) |
| 83 | 84 | ||
| 84 | var ( | 85 | var ( |
| @@ -265,7 +265,7 @@ func (gateway ApiByteLib) FormulasGenerate(param domain.ReqFormulasGenerate) (*d | @@ -265,7 +265,7 @@ func (gateway ApiByteLib) FormulasGenerate(param domain.ReqFormulasGenerate) (*d | ||
| 265 | } | 265 | } |
| 266 | 266 | ||
| 267 | func (gateway ApiByteLib) FormulasClear(param domain.ReqFormulasClear) (*domain.DataFormulasClear, error) { | 267 | func (gateway ApiByteLib) FormulasClear(param domain.ReqFormulasClear) (*domain.DataFormulasClear, error) { |
| 268 | - url := gateway.Host() + "/formulas/generate" | 268 | + url := gateway.Host() + "/formulas/clear" |
| 269 | method := "post" | 269 | method := "post" |
| 270 | var data FormulasGenerateResponse | 270 | var data FormulasGenerateResponse |
| 271 | request := FormulasClearRequest{ | 271 | request := FormulasClearRequest{ |
| @@ -36,15 +36,15 @@ type ( | @@ -36,15 +36,15 @@ type ( | ||
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | FormulaDataHandleRule struct { | 38 | FormulaDataHandleRule struct { |
| 39 | - RuleType int `json:"ruleType"` //Int, 规则类型 1.拆分赋值2.正常赋值 | ||
| 40 | - FormulaJoinCondition FormulaJoinCondition `json:"formulaJoinCondition"` // 拆分赋值 | ||
| 41 | - AssignValueCondition AssignValueCondition `json:"assignValueCondition"` // 正常赋值 | 39 | + RuleType int `json:"ruleType"` //Int, 规则类型 1.拆分赋值2.正常赋值 |
| 40 | + FormulaJoinCondition *FormulaJoinCondition `json:"formulaJoinCondition"` // 拆分赋值 | ||
| 41 | + AssignValueCondition *AssignValueCondition `json:"assignValueCondition"` // 正常赋值 | ||
| 42 | } | 42 | } |
| 43 | // FormulaJoinCondition 拆分链接条件 | 43 | // FormulaJoinCondition 拆分链接条件 |
| 44 | FormulaJoinCondition struct { | 44 | FormulaJoinCondition struct { |
| 45 | - ConditionLeftField FormulaField `json:"conditionLeftField"` | ||
| 46 | - ConditionRightField FormulaField `json:"conditionRightField"` | ||
| 47 | - JoinAssignValues []AssignValueCondition `json:"joinAssignValues"` | 45 | + ConditionLeftField FormulaField `json:"conditionLeftField"` |
| 46 | + ConditionRightField FormulaField `json:"conditionRightField"` | ||
| 47 | + JoinAssignValues []*AssignValueCondition `json:"joinAssignValues"` | ||
| 48 | } | 48 | } |
| 49 | // AssignValueCondition 拆分规则 | 49 | // AssignValueCondition 拆分规则 |
| 50 | AssignValueCondition struct { | 50 | AssignValueCondition struct { |
| @@ -170,10 +170,11 @@ func NewFormulaDataHandleRule(s domain.SelectExprGroup) FormulaDataHandleRule { | @@ -170,10 +170,11 @@ func NewFormulaDataHandleRule(s domain.SelectExprGroup) FormulaDataHandleRule { | ||
| 170 | return res | 170 | return res |
| 171 | } | 171 | } |
| 172 | 172 | ||
| 173 | -func NewFormulaJoinCondition(s domain.SelectExprGroup) FormulaJoinCondition { | ||
| 174 | - var res = FormulaJoinCondition{ | 173 | +func NewFormulaJoinCondition(s domain.SelectExprGroup) *FormulaJoinCondition { |
| 174 | + var res = &FormulaJoinCondition{ | ||
| 175 | ConditionLeftField: NewFormulaField(s.FieldLeft, true), | 175 | ConditionLeftField: NewFormulaField(s.FieldLeft, true), |
| 176 | ConditionRightField: NewFormulaField(s.FieldRight), | 176 | ConditionRightField: NewFormulaField(s.FieldRight), |
| 177 | + JoinAssignValues: make([]*AssignValueCondition, 0), | ||
| 177 | } | 178 | } |
| 178 | for _, sub := range s.SubSelects { | 179 | for _, sub := range s.SubSelects { |
| 179 | res.JoinAssignValues = append(res.JoinAssignValues, NewAssignValueCondition(sub)) | 180 | res.JoinAssignValues = append(res.JoinAssignValues, NewAssignValueCondition(sub)) |
| @@ -181,8 +182,8 @@ func NewFormulaJoinCondition(s domain.SelectExprGroup) FormulaJoinCondition { | @@ -181,8 +182,8 @@ func NewFormulaJoinCondition(s domain.SelectExprGroup) FormulaJoinCondition { | ||
| 181 | return res | 182 | return res |
| 182 | } | 183 | } |
| 183 | 184 | ||
| 184 | -func NewAssignValueCondition(selectExpr domain.SelectExpr) AssignValueCondition { | ||
| 185 | - var res = AssignValueCondition{ | 185 | +func NewAssignValueCondition(selectExpr domain.SelectExpr) *AssignValueCondition { |
| 186 | + var res = &AssignValueCondition{ | ||
| 186 | ConditionLeftField: NewFormulaField(selectExpr.FieldLeft, true), | 187 | ConditionLeftField: NewFormulaField(selectExpr.FieldLeft, true), |
| 187 | ConditionRightField: NewFormulaField(selectExpr.FieldRight), | 188 | ConditionRightField: NewFormulaField(selectExpr.FieldRight), |
| 188 | } | 189 | } |
| @@ -17,3 +17,9 @@ func TableSoftDelete(ptr *pgTransaction.TransactionContext, tableId int, tableTy | @@ -17,3 +17,9 @@ func TableSoftDelete(ptr *pgTransaction.TransactionContext, tableId int, tableTy | ||
| 17 | _, err := ptr.PgTx.Exec(sql, time.Now(), tableId, tableType.ToString()) | 17 | _, err := ptr.PgTx.Exec(sql, time.Now(), tableId, tableType.ToString()) |
| 18 | return err | 18 | return err |
| 19 | } | 19 | } |
| 20 | + | ||
| 21 | +func TableDeleteByTime(ptr *pgTransaction.TransactionContext, tableType domain.TableType, begin time.Time, end time.Time) error { | ||
| 22 | + sql := "delete from metadata.tables where table_type = ? and created_at >=? and created_at< ?" | ||
| 23 | + _, err := ptr.PgTx.Exec(sql, tableType.ToString(), begin, end) | ||
| 24 | + return err | ||
| 25 | +} |
| @@ -162,6 +162,7 @@ func NewCopyTable(tableType domain.TableType, fileName string, dataFields []*dom | @@ -162,6 +162,7 @@ func NewCopyTable(tableType domain.TableType, fileName string, dataFields []*dom | ||
| 162 | table.CreatedAt = time.Now() | 162 | table.CreatedAt = time.Now() |
| 163 | table.UpdatedAt = time.Now() | 163 | table.UpdatedAt = time.Now() |
| 164 | table.RowCount = rowCount | 164 | table.RowCount = rowCount |
| 165 | + table.TableInfo = domain.NewTableInfo() | ||
| 165 | return table | 166 | return table |
| 166 | } | 167 | } |
| 167 | 168 |
| @@ -136,6 +136,60 @@ func (ptr *QuerySetService) Update(ctx *domain.Context, querySetId int, queryCom | @@ -136,6 +136,60 @@ func (ptr *QuerySetService) Update(ctx *domain.Context, querySetId int, queryCom | ||
| 136 | return nil | 136 | return nil |
| 137 | } | 137 | } |
| 138 | 138 | ||
| 139 | +func (ptr *QuerySetService) PreviewPrepare(ctx *domain.Context, querySetId int, queryComponents []*domain.QueryComponent) (*domain.Table, error) { | ||
| 140 | + querySetRepository, _ := repository.NewQuerySetRepository(ptr.transactionContext) | ||
| 141 | + querySet, err := querySetRepository.FindOne(map[string]interface{}{"querySetId": querySetId}) | ||
| 142 | + tableRepository, _ := repository.NewTableRepository(ptr.transactionContext) | ||
| 143 | + if err != nil { | ||
| 144 | + return nil, err | ||
| 145 | + } | ||
| 146 | + if queryComponentsHasEdit(ctx, querySet, queryComponents) && querySet.QuerySetInfo.BindTableId > 0 { | ||
| 147 | + if t, _ := tableRepository.FindOne(map[string]interface{}{"context": ctx, "tableId": querySet.QuerySetInfo.BindTableId}); t != nil { | ||
| 148 | + return t, nil | ||
| 149 | + } | ||
| 150 | + } | ||
| 151 | + // 验证 | ||
| 152 | + if err = ptr.validQueryComponents(queryComponents); err != nil { | ||
| 153 | + return nil, err | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + var ( | ||
| 157 | + foundMasterTable *domain.Table | ||
| 158 | + ) | ||
| 159 | + // 生成Table | ||
| 160 | + masterTable := queryComponents[0].MasterTable | ||
| 161 | + dependencyTables := querySet.GetDependencyTables(queryComponents) | ||
| 162 | + foundMasterTable, err = tableRepository.FindOne(map[string]interface{}{"context": ctx, "tableId": masterTable.TableId}) | ||
| 163 | + if err != nil { | ||
| 164 | + return nil, err | ||
| 165 | + } | ||
| 166 | + masterTable = domain.NewQueryComponentTable(foundMasterTable) | ||
| 167 | + var table *domain.Table = NewCopyTable(domain.TableType(domain.TemporaryTable), querySet.Name, domain.RangeFields(masterTable.Fields, domain.ChangeFieldFlag), 0). | ||
| 168 | + WithContext(ctx). | ||
| 169 | + WithPrefix(strings.ToLower(string(domain.TemporaryTable))) | ||
| 170 | + // 循环依赖判断 | ||
| 171 | + if err = ptr.validDependentCircle(ctx, querySet, queryComponents); err != nil { | ||
| 172 | + return nil, err | ||
| 173 | + } | ||
| 174 | + | ||
| 175 | + table.TableInfo.ApplyOnModule = domain.ModuleAll | ||
| 176 | + table.TableInfo.DependencyTables = dependencyTables | ||
| 177 | + table, err = tableRepository.Save(table) | ||
| 178 | + if err != nil { | ||
| 179 | + return nil, err | ||
| 180 | + } | ||
| 181 | + | ||
| 182 | + // 调用底层的组装sql | ||
| 183 | + if _, err = ByteCore.FormulasGenerate(domain.ReqFormulasGenerate{ | ||
| 184 | + QuerySet: querySet, | ||
| 185 | + Table: table, | ||
| 186 | + QueryComponents: queryComponents, | ||
| 187 | + }); err != nil { | ||
| 188 | + return nil, err | ||
| 189 | + } | ||
| 190 | + return table, nil | ||
| 191 | +} | ||
| 192 | + | ||
| 139 | func (ptr *QuerySetService) validQueryComponents(queryComponents []*domain.QueryComponent) error { | 193 | func (ptr *QuerySetService) validQueryComponents(queryComponents []*domain.QueryComponent) error { |
| 140 | if len(queryComponents) == 0 { | 194 | if len(queryComponents) == 0 { |
| 141 | return nil | 195 | return nil |
| @@ -154,6 +208,42 @@ func (ptr *QuerySetService) validQueryComponents(queryComponents []*domain.Query | @@ -154,6 +208,42 @@ func (ptr *QuerySetService) validQueryComponents(queryComponents []*domain.Query | ||
| 154 | return nil | 208 | return nil |
| 155 | } | 209 | } |
| 156 | 210 | ||
| 211 | +func (ptr *QuerySetService) validDependentCircle(ctx *domain.Context, querySet *domain.QuerySet, queryComponents []*domain.QueryComponent) error { | ||
| 212 | + tableRepository, _ := repository.NewTableRepository(ptr.transactionContext) | ||
| 213 | + var validTables = make([]*domain.Table, 0) | ||
| 214 | + dependencyTables := querySet.GetDependencyTables(queryComponents) | ||
| 215 | + if len(dependencyTables) > 0 { | ||
| 216 | + _, tables, err := tableRepository.Find(map[string]interface{}{"context": ctx, "tableIds": dependencyTables}) | ||
| 217 | + if err != nil { | ||
| 218 | + return err | ||
| 219 | + } | ||
| 220 | + tableMap := make(map[int]*domain.Table) | ||
| 221 | + for i := range tables { | ||
| 222 | + tableMap[tables[i].TableId] = tables[i] | ||
| 223 | + } | ||
| 224 | + for _, c := range queryComponents { | ||
| 225 | + if t, ok := tableMap[c.MasterTable.TableId]; ok { | ||
| 226 | + c.MasterTable = domain.NewQueryComponentTable(t) | ||
| 227 | + } | ||
| 228 | + } | ||
| 229 | + for _, t := range validTables { | ||
| 230 | + if t.TableType == domain.SchemaTable.ToString() || t.TableType == domain.SubProcessTable.ToString() { | ||
| 231 | + validTables = append(validTables, t) | ||
| 232 | + } | ||
| 233 | + } | ||
| 234 | + } | ||
| 235 | + | ||
| 236 | + // 循环依赖判断 | ||
| 237 | + tableDependencyService, _ := NewTableDependencyService(ptr.transactionContext) | ||
| 238 | + if len(validTables) > 0 { | ||
| 239 | + tree := tableDependencyService.TableDependTree(validTables, querySet.QuerySetInfo.BindTableId) | ||
| 240 | + if tableDependencyService.Detect(ctx, tree.EdgesArray()) { | ||
| 241 | + return NewCircleDependError(tableDependencyService.CircleTable(), querySet) | ||
| 242 | + } | ||
| 243 | + } | ||
| 244 | + return nil | ||
| 245 | +} | ||
| 246 | + | ||
| 157 | func (ptr *QuerySetService) UpdateQuerySetLog(ctx *domain.Context, querySet *domain.QuerySet, queryComponents []*domain.QueryComponent) error { | 247 | func (ptr *QuerySetService) UpdateQuerySetLog(ctx *domain.Context, querySet *domain.QuerySet, queryComponents []*domain.QueryComponent) error { |
| 158 | var res = make([]FastSourceLog, 0) | 248 | var res = make([]FastSourceLog, 0) |
| 159 | if logs := conditionsEditLog(ctx, querySet, queryComponents); len(logs) > 0 { | 249 | if logs := conditionsEditLog(ctx, querySet, queryComponents); len(logs) > 0 { |
| @@ -254,10 +344,14 @@ func conditionsEditLog(ctx *domain.Context, querySet *domain.QuerySet, queryComp | @@ -254,10 +344,14 @@ func conditionsEditLog(ctx *domain.Context, querySet *domain.QuerySet, queryComp | ||
| 254 | return res | 344 | return res |
| 255 | } | 345 | } |
| 256 | 346 | ||
| 347 | +func queryComponentsHasEdit(ctx *domain.Context, querySet *domain.QuerySet, queryComponents []*domain.QueryComponent) bool { | ||
| 348 | + logs := selectsEditLog(ctx, querySet, queryComponents) | ||
| 349 | + return len(logs) == 0 | ||
| 350 | +} | ||
| 351 | + | ||
| 257 | func selectsEditLog(ctx *domain.Context, querySet *domain.QuerySet, queryComponents []*domain.QueryComponent) []FastSourceLog { | 352 | func selectsEditLog(ctx *domain.Context, querySet *domain.QuerySet, queryComponents []*domain.QueryComponent) []FastSourceLog { |
| 258 | var res = make([]FastSourceLog, 0) | 353 | var res = make([]FastSourceLog, 0) |
| 259 | oldQCs := domain.QueryComponentsToMapById(querySet.QueryComponents) | 354 | oldQCs := domain.QueryComponentsToMapById(querySet.QueryComponents) |
| 260 | - //newQCs := domain.QueryComponentsToMapById(queryComponents) | ||
| 261 | sourceId := querySet.QuerySetId | 355 | sourceId := querySet.QuerySetId |
| 262 | entry := domain.NewLogEntry(querySet.Name, querySet.Type, domain.UnKnown, ctx) | 356 | entry := domain.NewLogEntry(querySet.Name, querySet.Type, domain.UnKnown, ctx) |
| 263 | 357 | ||
| @@ -351,7 +445,7 @@ func (ptr *QuerySetService) CreateOrUpdateQuerySetTable(ctx *domain.Context, que | @@ -351,7 +445,7 @@ func (ptr *QuerySetService) CreateOrUpdateQuerySetTable(ctx *domain.Context, que | ||
| 351 | return nil, err | 445 | return nil, err |
| 352 | } | 446 | } |
| 353 | masterTable = domain.NewQueryComponentTable(foundMasterTable) | 447 | masterTable = domain.NewQueryComponentTable(foundMasterTable) |
| 354 | - var table *domain.Table = NewTable(domain.TableType(querySet.Type), querySet.Name, domain.RangeFields(masterTable.Fields, domain.ChangeFieldFlag), 0).WithContext(ctx).WithPrefix(strings.ToLower(querySet.Type)) | 448 | + var table *domain.Table = NewCopyTable(domain.TableType(querySet.Type), querySet.Name, domain.RangeFields(masterTable.Fields, domain.ChangeFieldFlag), 0).WithContext(ctx).WithPrefix(strings.ToLower(querySet.Type)) |
| 355 | if querySet.QuerySetInfo.BindTableId > 0 { | 449 | if querySet.QuerySetInfo.BindTableId > 0 { |
| 356 | table, err = tableRepository.FindOne(map[string]interface{}{"context": ctx, "tableId": querySet.QuerySetInfo.BindTableId}) | 450 | table, err = tableRepository.FindOne(map[string]interface{}{"context": ctx, "tableId": querySet.QuerySetInfo.BindTableId}) |
| 357 | if err != nil { | 451 | if err != nil { |
| @@ -360,36 +454,41 @@ func (ptr *QuerySetService) CreateOrUpdateQuerySetTable(ctx *domain.Context, que | @@ -360,36 +454,41 @@ func (ptr *QuerySetService) CreateOrUpdateQuerySetTable(ctx *domain.Context, que | ||
| 360 | table.DataFields = masterTable.Fields | 454 | table.DataFields = masterTable.Fields |
| 361 | table.UpdatedAt = time.Now() | 455 | table.UpdatedAt = time.Now() |
| 362 | } | 456 | } |
| 363 | - var validTables = make([]*domain.Table, 0) | ||
| 364 | - if len(dependencyTables) > 0 { | ||
| 365 | - _, tables, err := tableRepository.Find(map[string]interface{}{"context": ctx, "tableIds": dependencyTables}) | ||
| 366 | - if err != nil { | ||
| 367 | - return nil, err | ||
| 368 | - } | ||
| 369 | - tableMap := make(map[int]*domain.Table) | ||
| 370 | - for i := range tables { | ||
| 371 | - tableMap[tables[i].TableId] = tables[i] | ||
| 372 | - } | ||
| 373 | - for _, c := range queryComponents { | ||
| 374 | - if t, ok := tableMap[c.MasterTable.TableId]; ok { | ||
| 375 | - c.MasterTable = domain.NewQueryComponentTable(t) | ||
| 376 | - } | ||
| 377 | - } | ||
| 378 | - for _, t := range validTables { | ||
| 379 | - if t.TableType == domain.SchemaTable.ToString() || t.TableType == domain.SubProcessTable.ToString() { | ||
| 380 | - validTables = append(validTables, t) | ||
| 381 | - } | ||
| 382 | - } | ||
| 383 | - } | ||
| 384 | 457 | ||
| 385 | // 循环依赖判断 | 458 | // 循环依赖判断 |
| 386 | - tableDependencyService, _ := NewTableDependencyService(ptr.transactionContext) | ||
| 387 | - if len(validTables) > 0 { | ||
| 388 | - tree := tableDependencyService.TableDependTree(validTables, querySet.QuerySetInfo.BindTableId) | ||
| 389 | - if tableDependencyService.Detect(ctx, tree.EdgesArray()) { | ||
| 390 | - return nil, NewCircleDependError(tableDependencyService.CircleTable(), querySet) | ||
| 391 | - } | 459 | + if err = ptr.validDependentCircle(ctx, querySet, queryComponents); err != nil { |
| 460 | + return nil, err | ||
| 392 | } | 461 | } |
| 462 | + //var validTables = make([]*domain.Table, 0) | ||
| 463 | + //if len(dependencyTables) > 0 { | ||
| 464 | + // _, tables, err := tableRepository.Find(map[string]interface{}{"context": ctx, "tableIds": dependencyTables}) | ||
| 465 | + // if err != nil { | ||
| 466 | + // return nil, err | ||
| 467 | + // } | ||
| 468 | + // tableMap := make(map[int]*domain.Table) | ||
| 469 | + // for i := range tables { | ||
| 470 | + // tableMap[tables[i].TableId] = tables[i] | ||
| 471 | + // } | ||
| 472 | + // for _, c := range queryComponents { | ||
| 473 | + // if t, ok := tableMap[c.MasterTable.TableId]; ok { | ||
| 474 | + // c.MasterTable = domain.NewQueryComponentTable(t) | ||
| 475 | + // } | ||
| 476 | + // } | ||
| 477 | + // for _, t := range validTables { | ||
| 478 | + // if t.TableType == domain.SchemaTable.ToString() || t.TableType == domain.SubProcessTable.ToString() { | ||
| 479 | + // validTables = append(validTables, t) | ||
| 480 | + // } | ||
| 481 | + // } | ||
| 482 | + //} | ||
| 483 | + // | ||
| 484 | + //// 循环依赖判断 | ||
| 485 | + //tableDependencyService, _ := NewTableDependencyService(ptr.transactionContext) | ||
| 486 | + //if len(validTables) > 0 { | ||
| 487 | + // tree := tableDependencyService.TableDependTree(validTables, querySet.QuerySetInfo.BindTableId) | ||
| 488 | + // if tableDependencyService.Detect(ctx, tree.EdgesArray()) { | ||
| 489 | + // return nil, NewCircleDependError(tableDependencyService.CircleTable(), querySet) | ||
| 490 | + // } | ||
| 491 | + //} | ||
| 393 | 492 | ||
| 394 | table.TableInfo.ApplyOnModule = domain.ModuleAll | 493 | table.TableInfo.ApplyOnModule = domain.ModuleAll |
| 395 | table.TableInfo.DependencyTables = dependencyTables | 494 | table.TableInfo.DependencyTables = dependencyTables |
| @@ -182,6 +182,9 @@ func (repository *TableRepository) Find(queryOptions map[string]interface{}) (in | @@ -182,6 +182,9 @@ func (repository *TableRepository) Find(queryOptions map[string]interface{}) (in | ||
| 182 | query.Where(`table_info->'dependencyTables' @> '[?]'`, v.(int)) | 182 | query.Where(`table_info->'dependencyTables' @> '[?]'`, v.(int)) |
| 183 | } | 183 | } |
| 184 | 184 | ||
| 185 | + query.SetWhereByQueryOption("created_at >= ?", "beginTime") | ||
| 186 | + query.SetWhereByQueryOption("created_at < ?", "endTime") | ||
| 187 | + | ||
| 185 | //query.SetOffsetAndLimit(20) | 188 | //query.SetOffsetAndLimit(20) |
| 186 | query.SetOrderDirect("table_id", "DESC") | 189 | query.SetOrderDirect("table_id", "DESC") |
| 187 | if count, err := query.SelectAndCount(); err != nil { | 190 | if count, err := query.SelectAndCount(); err != nil { |
| @@ -65,3 +65,10 @@ func Delete(db *gorm.DB, tableName string, fields []*domain.FieldValue) error { | @@ -65,3 +65,10 @@ func Delete(db *gorm.DB, tableName string, fields []*domain.FieldValue) error { | ||
| 65 | tx := db.Exec(sql) | 65 | tx := db.Exec(sql) |
| 66 | return tx.Error | 66 | return tx.Error |
| 67 | } | 67 | } |
| 68 | + | ||
| 69 | +//DROP VIEW IF EXISTS | ||
| 70 | + | ||
| 71 | +func DropView(db *gorm.DB, tableName string) error { | ||
| 72 | + tx := db.Exec("DROP VIEW IF EXISTS " + tableName) | ||
| 73 | + return tx.Error | ||
| 74 | +} |
| @@ -29,6 +29,14 @@ func (controller *QuerySetController) UpdateQuerySet() { | @@ -29,6 +29,14 @@ func (controller *QuerySetController) UpdateQuerySet() { | ||
| 29 | controller.Response(data, err) | 29 | controller.Response(data, err) |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | +func (controller *QuerySetController) PreviewPrepare() { | ||
| 33 | + querySetService := service.NewQuerySetService(nil) | ||
| 34 | + updateQuerySetCommand := &command.UpdateQuerySetCommand{} | ||
| 35 | + Must(controller.Unmarshal(updateQuerySetCommand)) | ||
| 36 | + data, err := querySetService.PreviewPrepare(ParseContext(controller.BaseController), updateQuerySetCommand) | ||
| 37 | + controller.Response(data, err) | ||
| 38 | +} | ||
| 39 | + | ||
| 32 | func (controller *QuerySetController) GetQuerySet() { | 40 | func (controller *QuerySetController) GetQuerySet() { |
| 33 | querySetService := service.NewQuerySetService(nil) | 41 | querySetService := service.NewQuerySetService(nil) |
| 34 | getQuerySetQuery := &query.GetQuerySetQuery{} | 42 | getQuerySetQuery := &query.GetQuerySetQuery{} |
| @@ -18,4 +18,5 @@ func init() { | @@ -18,4 +18,5 @@ func init() { | ||
| 18 | web.Router("/data/query-sets/move", &controllers.QuerySetController{}, "Post:Move") | 18 | web.Router("/data/query-sets/move", &controllers.QuerySetController{}, "Post:Move") |
| 19 | web.Router("/data/query-sets/rename", &controllers.QuerySetController{}, "Post:Rename") | 19 | web.Router("/data/query-sets/rename", &controllers.QuerySetController{}, "Post:Rename") |
| 20 | web.Router("/data/query-sets/search", &controllers.QuerySetController{}, "Post:SearchQuerySet") | 20 | web.Router("/data/query-sets/search", &controllers.QuerySetController{}, "Post:SearchQuerySet") |
| 21 | + web.Router("/data/query-sets/preview-prepare", &controllers.QuerySetController{}, "Post:PreviewPrepare") | ||
| 21 | } | 22 | } |
-
请 注册 或 登录 后发表评论