Skip to content

Commit acd19ea

Browse files
ymakedaqiSecloud
authored andcommitted
feat(dbm-services): 库表分析识别子查询 #10151
1 parent 4a8a0d3 commit acd19ea

File tree

5 files changed

+34
-32
lines changed

5 files changed

+34
-32
lines changed

dbm-services/mysql/db-simulation/app/config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ func IsEmptyTdbctlPodResourceConfig() bool {
185185
func loadConfig() (err error) {
186186
viper.SetConfigName("config")
187187
viper.SetConfigType("yaml")
188-
viper.AddConfigPath("$HOME/conf")
189188
viper.AddConfigPath("./conf")
189+
viper.AddConfigPath("$HOME/conf")
190190
if err = viper.ReadInConfig(); err != nil {
191191
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
192192
logger.Error("config file not found,maybe read by env")

dbm-services/mysql/db-simulation/app/syntax/parse_relation_db.go

+25-25
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ func (tf *TmysqlParseFile) DoParseRelationDbs(version string) (createDbs, relati
5050
}
5151
logger.Info("all sqlfiles download ok ~")
5252
alreadExecutedSqlfileChan := make(chan string, len(tf.Param.FileNames))
53-
5453
go func() {
5554
if err = tf.Execute(alreadExecutedSqlfileChan, version); err != nil {
5655
logger.Error("failed to execute tmysqlparse: %s", err.Error())
@@ -168,40 +167,41 @@ func (t *TmysqlParse) analyzeRelationDbs(inputfileName, mysqlVersion string) (
168167
logger.Error("json unmasrshal line:%s failed %s", string(line), err.Error())
169168
return nil, nil, nil, false, err
170169
}
171-
// 判断是否有语法错误
170+
// ErrorCode !=0 就是语法错误
172171
if res.ErrorCode != 0 {
173172
return nil, nil, nil, false, fmt.Errorf("%s", res.ErrorMsg)
174173
}
175-
if lo.IsNotEmpty(res.Command) {
176-
if res.Command == SQLTypeCreateTable {
177-
var c CreateTableResult
178-
if err = json.Unmarshal(line, &c); err != nil {
179-
logger.Error("json unmasrshal line:%s failed %s", string(line), err.Error())
180-
return nil, nil, nil, false, err
181-
}
182-
// 需要排除create table like
183-
if c.IsCreateTableLike || c.IsCreateTableSelect {
184-
return nil, nil, allCommandType, true, nil
185-
}
186-
} else {
187-
allCommandType = append(allCommandType, res.Command)
188-
}
189-
}
190-
if slices.Contains([]string{SQLTypeCreateProcedure, SQLTypeCreateFunction, SQLTypeCreateView, SQLTypeCreateTrigger,
191-
SQLTypeInsertSelect, SQLTypeRelaceSelect},
192-
res.Command) {
193-
return nil, nil, allCommandType, true, nil
194-
}
195-
if lo.IsEmpty(res.DbName) {
174+
if lo.IsEmpty(res.Command) {
196175
continue
197176
}
177+
if lo.IsNotEmpty(res.DbName) {
178+
relationDbs = append(relationDbs, res.DbName)
179+
}
198180
// create db not need dump db
199181
if slices.Contains([]string{SQLTypeCreateDb}, res.Command) {
200182
createDbs = append(createDbs, res.DbName)
201183
continue
202184
}
203-
relationDbs = append(relationDbs, res.DbName)
204-
185+
if res.HasSubQuery {
186+
return nil, relationDbs, allCommandType, true, nil
187+
}
188+
if slices.Contains([]string{SQLTypeCreateProcedure, SQLTypeCreateFunction, SQLTypeCreateView, SQLTypeCreateTrigger,
189+
SQLTypeInsertSelect, SQLTypeRelaceSelect},
190+
res.Command) {
191+
return nil, relationDbs, allCommandType, true, nil
192+
}
193+
if res.Command == SQLTypeCreateTable {
194+
var c CreateTableResult
195+
if err = json.Unmarshal(line, &c); err != nil {
196+
logger.Error("json unmasrshal line:%s failed %s", string(line), err.Error())
197+
return nil, nil, nil, false, err
198+
}
199+
// 需要排除create table like
200+
if c.IsCreateTableLike || c.IsCreateTableSelect {
201+
return nil, relationDbs, allCommandType, true, nil
202+
}
203+
}
204+
allCommandType = append(allCommandType, res.Command)
205205
}
206206
return createDbs, relationDbs, allCommandType, false, nil
207207
}

dbm-services/mysql/db-simulation/app/syntax/syntax.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,9 @@ func (t *TmysqlParse) Init() (err error) {
248248
return nil
249249
}
250250

251-
// DelTempDir TODO
251+
// DelTempDir del tempDir
252252
func (t *TmysqlParse) DelTempDir() {
253+
logger.Info("start to remove tempDir:%s", t.tmpWorkdir)
253254
if err := os.RemoveAll(t.tmpWorkdir); err != nil {
254255
logger.Warn("remove tempDir:" + t.tmpWorkdir + ".error info:" + err.Error())
255256
}

dbm-services/mysql/db-simulation/app/syntax/tmysqlpase_schema.go

+1
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ type ParseLineQueryBase struct {
232232
ErrorMsg string `json:"error_msg,omitempty"`
233233
MinMySQLVersion int `json:"min_mysql_version"`
234234
MaxMySQLVersion int `json:"max_my_sql_version"`
235+
HasSubQuery bool `json:"has_subquery,omitempty"`
235236
}
236237

237238
// IsSysDb sql modify target db is sys db

dbm-services/mysql/db-simulation/handler/syntax_check.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ func (s SyntaxHandler) ParseSQLFileRelationDb(r *gin.Context) {
252252
FileNames: param.Files,
253253
},
254254
}
255+
defer p.DelTempDir()
255256
createDbs, dbs, allCommands, dumpall, err := p.DoParseRelationDbs("")
256257
if err != nil {
257258
s.SendResponse(r, err, nil)
@@ -270,7 +271,7 @@ func (s SyntaxHandler) ParseSQLFileRelationDb(r *gin.Context) {
270271
defer p.DelTempDir()
271272
// 如果所有的命令都是alter table, dump指定库表
272273
logger.Debug("debug: %v,%d", allCommands, len(allCommands))
273-
if isAllOperateTable(allCommands) {
274+
if isAllOperateTable(allCommands) && !dumpall {
274275
relationTbls, err := p.ParseSpecialTbls("")
275276
if err != nil {
276277
s.SendResponse(r, err, nil)
@@ -320,7 +321,7 @@ func isAllOperateTable(allCommands []string) bool {
320321
return lo.Every([]string{
321322
syntax.SQLTypeAlterTable, syntax.SQLTypeUseDb,
322323
syntax.SQLTypeCreateIndex, syntax.SQLTypeDropTable,
323-
syntax.SQLTypeInsert, syntax.SQLTypeDelete,
324+
syntax.SQLTypeInsert, syntax.SQLTypeDelete, syntax.SQLTypeUpdate,
324325
syntax.SQLTypeCreateTable, syntax.SQLTypeReplace,
325326
}, allCommands)
326327
}
@@ -358,15 +359,15 @@ func (s *SyntaxHandler) ParseSQLRelationDb(r *gin.Context) {
358359
FileNames: []string{fileName},
359360
},
360361
}
361-
// defer p.DelTempDir()
362+
defer p.DelTempDir()
362363
createDbs, dbs, allCommands, dumpall, err := p.DoParseRelationDbs("")
363364
if err != nil {
364365
s.SendResponse(r, err, nil)
365366
return
366367
}
367368
// 如果所有的命令都是alter table, dump指定库表
368369
logger.Info("all command types: %v,%d", allCommands, len(allCommands))
369-
if isAllOperateTable(allCommands) {
370+
if isAllOperateTable(allCommands) && !dumpall {
370371
relationTbls, err := p.ParseSpecialTbls("")
371372
if err != nil {
372373
s.SendResponse(r, err, nil)
@@ -382,7 +383,6 @@ func (s *SyntaxHandler) ParseSQLRelationDb(r *gin.Context) {
382383
})
383384
return
384385
}
385-
386386
s.SendResponse(r, nil, gin.H{
387387
"create_dbs": createDbs,
388388
"dbs": dbs,

0 commit comments

Comments
 (0)