Skip to content

feat(scannerd): 展示转换出错SQL文件内容功能 #2998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2025

Conversation

littleniannian
Copy link
Collaborator

@littleniannian littleniannian commented Mar 26, 2025

User description

关联的 issue

https://github.com/actiontech/sqle-ee/issues/2296

描述你的变更

  • 在 mybatis 和 sqlfile 命令中添加 --show-file-content 参数- 当解析文件失败时,如果设置了该参数,将打印文件内容
  • 修改了相关函数以支持新功能,并更新了命令行参数定义

确认项(pr提交后操作)

Tip

请在指定复审人之前,确认并完成以下事项,完成后✅


  • 我已完成自测
  • 我已记录完整日志方便进行诊断
  • 我已在关联的issue里补充了实现方案
  • 我已在关联的issue里补充了测试影响面
  • 我已确认了变更的兼容性,如果不兼容则在issue里标记 not_compatible
  • 我已确认了是否要更新文档,如果要更新则在issue里标记 need_update_doc


Description

  • 添加 --show-file-content 参数

  • 解析失败时显示文件内容

  • 更新扫描器相关函数

  • 修改命令行参数定义


Changes walkthrough 📝

Relevant files
Enhancement
7 files
mybatis.go
添加 `ShowFileContent` 参数                                                                   
+15/-12 
sqlfile.go
添加 `ShowFileContent` 参数                                                                   
+2/-0     
base.go
定义 `ShowFileContent` 标志                                                                   
+6/-4     
command.go
添加 `ShowFileContent` 命令行标志                                                             
+2/-0     
file.go
传递 `ShowFileContent` 参数                                                                   
+13/-4   
mybatis.go
使用 `ShowFileContent` 参数                                                                   
+24/-21 
sqlfile.go
使用 `ShowFileContent` 参数                                                                   
+4/-1     

Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • - 在 mybatis 和 sqlfile 命令中添加 --show-file-content 参数- 当解析文件失败时,如果设置了该参数,将打印文件内容
    - 修改了相关函数以支持新功能,并更新了命令行参数定义
    Copy link

    github-actions bot commented Mar 26, 2025

    PR Reviewer Guide 🔍

    (Review updated until commit 009676f)

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 Security concerns

    敏感信息泄露:
    添加了--show-file-content参数后,在解析失败时打印文件内容,可能会无意中暴露包含敏感信息的文件内容。建议对打印的内容进行过滤或限制,确保不泄露敏感数据。

    ⚡ Recommended focus areas for review

    信息泄露

    添加了打印文件内容的功能,可能会导致敏感信息泄露,建议确保打印的内容不包含敏感数据或在日志中妥善处理。

    func GetSQLFromPath(pathName string, skipErrorQuery, skipErrorFile bool, fileSuffix string, showFileContent bool) (allSQL []driverV2.Node, err error) {
    	if !path.IsAbs(pathName) {
    		pwd, err := os.Getwd()
    		if err != nil {
    			return nil, err
    		}
    		pathName = path.Join(pwd, pathName)
    	}
    
    	fileInfos, err := ioutil.ReadDir(pathName)
    	if err != nil {
    		return nil, err
    	}
    	for _, fi := range fileInfos {
    		var sqlList []driverV2.Node
    		pathJoin := path.Join(pathName, fi.Name())
    
    		if fi.IsDir() {
    			sqlList, err = GetSQLFromPath(pathJoin, skipErrorQuery, skipErrorFile, fileSuffix, showFileContent)
    		} else if strings.HasSuffix(fi.Name(), fileSuffix) {
    			sqlList, err = GetSQLFromFile(pathJoin, skipErrorQuery, fileSuffix, showFileContent)
    		}
    
    		if err != nil {
    			if skipErrorFile {
    				fmt.Printf("[parse %s file error] parse file %s error: %v\n", fileSuffix, pathJoin, err)
    			} else {
    				return nil, fmt.Errorf("parse file %s error: %v", pathJoin, err)
    			}
    		}
    		allSQL = append(allSQL, sqlList...)
    	}
    	return allSQL, err
    }
    
    func GetSQLFromFile(file string, skipErrorQuery bool, fileSuffix string, showFileContent bool) (r []driverV2.Node, err error) {
    	content, err := ReadFileContent(file)
    	if err != nil {
    		return nil, err
    	}
    	switch fileSuffix {
    	case utils.MybatisFileSuffix:
    		var sqls []string
    		var err error
    		if skipErrorQuery {
    			sqls, err = mybatisParser.ParseXMLQuery(content, mybatisParser.SkipErrorQuery, mybatisParser.RestoreOriginSql)
    		} else {
    			sqls, err = mybatisParser.ParseXMLQuery(content, mybatisParser.RestoreOriginSql)
    		}
    		if err != nil {
    			if showFileContent {
    				fmt.Printf("failed to parse xml file content: %s", content)
    			}
    			return nil, err
    		}
    		for _, sql := range sqls {
    			n, err := Parse(context.TODO(), sql)
    			if err != nil {
    				if showFileContent {
    					fmt.Printf("failed to parse xml file content: %s", content)
    				}
    				return nil, err
    			}
    			r = append(r, n...)
    		}
    	case utils.SQLFileSuffix:
    		n, err := Parse(context.TODO(), content)
    		if err != nil {
    			if showFileContent {
    				fmt.Printf("failed to parse sql file content: %s", content)
    			}

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Security
    防止敏感信息泄露

    使用 fmt.Printf 打印文件内容可能会泄露敏感信息。建议使用日志记录模块并确保内容的安全性。

    sqle/cmd/scannerd/scanners/common/file.go [67-69]

     +				if showFileContent {
    -+					fmt.Printf("failed to parse xml file content: %s", content)
    ++					logrus.Errorf("failed to parse xml file content: %s", content)
     +				}
    Suggestion importance[1-10]: 8

    __

    Why: 使用 fmt.Printf 直接打印文件内容可能会泄露敏感信息。将其替换为 logrus.Errorf 通过使用适当的日志记录模块,提升了安全性,防止潜在的数据泄露。

    Medium
    Possible issue
    确保SQL语句正确解析

    注释掉移除 -- 注释的代码可能导致解析错误。建议重新启用该行以确保 SQL 语句的正确解析。

    sqle/cmd/scannerd/scanners/common/parse.go [49]

    -+	// sqlText = regexp.MustCompile(`--.*`).ReplaceAllString(sqlText, " ")
    ++	sqlText = regexp.MustCompile(`--.*`).ReplaceAllString(sqlText, " ")
    Suggestion importance[1-10]: 7

    __

    Why: 注释掉移除 -- 注释的代码可能导致SQL解析错误。重新启用该行可以确保SQL语句被正确解析,提升代码的功能性和可靠性。

    Medium

    Copy link

    Persistent review updated to latest commit 009676f

    Copy link

    PR Code Suggestions ✨

    No code suggestions found for the PR.

    @littleniannian littleniannian changed the title feat(scannerd): 增加转换出错显示文件内容的功能 feat(scannerd): 展示转换出错SQL文件内容功能 Mar 26, 2025
    @LordofAvernus LordofAvernus merged commit 29c1d2e into main Mar 28, 2025
    4 checks passed
    @littleniannian littleniannian deleted the opt_scanner_file_log branch March 28, 2025 05:33
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    None yet
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants