Skip to content

Commit 29c1d2e

Browse files
Merge pull request #2998 from actiontech/opt_scanner_file_log
feat(scannerd): 展示转换出错SQL文件内容功能
2 parents 6000de8 + 009676f commit 29c1d2e

File tree

7 files changed

+66
-42
lines changed

7 files changed

+66
-42
lines changed

sqle/cmd/scannerd/cmd/mybatis.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,26 @@ import (
1717
)
1818

1919
var (
20-
dir string
21-
skipErrorQuery bool
22-
skipErrorXml bool
23-
dbTypeXml string
24-
instNameXml string
25-
schemaNameXml string
20+
dir string
21+
skipErrorQuery bool
22+
skipErrorXml bool
23+
dbTypeXml string
24+
instNameXml string
25+
schemaNameXml string
26+
ShowFileContent bool
2627

2728
mybatisCmd = &cobra.Command{
2829
Use: scannerCmd.TypeMySQLMybatis,
2930
Short: "Parse MyBatis XML file",
3031
Run: func(cmd *cobra.Command, args []string) {
3132
param := &mybatis.Params{
32-
XMLDir: dir,
33-
SkipErrorQuery: skipErrorQuery,
34-
SkipErrorXml: skipErrorXml,
35-
DbType: dbTypeXml,
36-
InstName: instNameXml,
37-
SchemaName: schemaNameXml,
33+
XMLDir: dir,
34+
SkipErrorQuery: skipErrorQuery,
35+
SkipErrorXml: skipErrorXml,
36+
DbType: dbTypeXml,
37+
InstName: instNameXml,
38+
SchemaName: schemaNameXml,
39+
ShowFileContent: ShowFileContent,
3840
}
3941
log := logrus.WithField("scanner", "mybatis")
4042
client := scanner.NewSQLEClient(time.Second*time.Duration(rootCmdFlags.timeout), rootCmdFlags.host, rootCmdFlags.port).WithToken(rootCmdFlags.token).WithProject(rootCmdFlags.project)
@@ -65,6 +67,7 @@ func init() {
6567
mybatisCmd.Flags().StringVarP(mybatis.StringFlagFn[scannerCmd.FlagDbType](&dbTypeXml))
6668
mybatisCmd.Flags().StringVarP(mybatis.StringFlagFn[scannerCmd.FlagInstanceName](&instNameXml))
6769
mybatisCmd.Flags().StringVarP(mybatis.StringFlagFn[scannerCmd.FlagSchemaName](&schemaNameXml))
70+
mybatisCmd.Flags().BoolVarP(mybatis.BoolFlagFn[scannerCmd.FlagShowFileContent](&ShowFileContent))
6871

6972
for _, requiredFlag := range mybatis.RequiredFlags {
7073
_ = mybatisCmd.MarkFlagRequired(requiredFlag)

sqle/cmd/scannerd/cmd/sqlfile.go

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var (
3333
DbType: dbTypeSqlFile,
3434
InstName: instNameSqlFile,
3535
SchemaName: schemaNameSqlFile,
36+
ShowFileContent: ShowFileContent,
3637
}
3738
log := logrus.WithField("scanner", "sqlFile")
3839
client := scanner.NewSQLEClient(time.Second*time.Duration(rootCmdFlags.timeout), rootCmdFlags.host, rootCmdFlags.port).WithToken(rootCmdFlags.token).WithProject(rootCmdFlags.project)
@@ -62,6 +63,7 @@ func init() {
6263
sqlFileCmd.Flags().StringVarP(sqlfile.StringFlagFn[scannerCmd.FlagDbType](&dbTypeSqlFile))
6364
sqlFileCmd.Flags().StringVarP(sqlfile.StringFlagFn[scannerCmd.FlagInstanceName](&instNameSqlFile))
6465
sqlFileCmd.Flags().StringVarP(sqlfile.StringFlagFn[scannerCmd.FlagSchemaName](&schemaNameSqlFile))
66+
sqlFileCmd.Flags().BoolVarP(sqlfile.BoolFlagFn[scannerCmd.FlagShowFileContent](&ShowFileContent))
6567

6668
for _, requiredFlag := range sqlfile.RequiredFlags {
6769
_ = sqlFileCmd.MarkFlagRequired(requiredFlag)

sqle/cmd/scannerd/command/base.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ const (
3232
FlagProject string = "project"
3333
FlagProjectSort string = "J"
3434
// mybatis
35-
FlagSkipErrorQuery string = "skip-error-query"
36-
FlagSkipErrorQuerySort string = "S"
37-
FlagSkipErrorXml string = "skip-error-xml"
38-
FlagSkipErrorXmlSort string = "X"
35+
FlagSkipErrorQuery string = "skip-error-query"
36+
FlagSkipErrorQuerySort string = "S"
37+
FlagSkipErrorXml string = "skip-error-xml"
38+
FlagSkipErrorXmlSort string = "X"
39+
FlagShowFileContent string = "show-file-content"
40+
FlagShowFileContentSort string = "L"
3941
// sqlfile
4042
FlagSkipErrorSqlFile string = "skip-error-sql-file"
4143
FlagSkipErrorSqlFileSort string = "S"

sqle/cmd/scannerd/command/command.go

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func init() {
3838
myBatis.addBoolFlag(FlagSkipErrorQuery, FlagSkipErrorQuerySort, false,
3939
"skip the statement that the scanner failed to parse from within the xml file")
4040
myBatis.addBoolFlag(FlagSkipErrorXml, FlagSkipErrorXmlSort, false, "skip the xml file that failed to parse")
41+
myBatis.addBoolFlag(FlagShowFileContent, FlagShowFileContentSort, false, "show xml file")
4142
myBatis.addRequiredFlag(FlagDirectory)
4243
}
4344

@@ -58,5 +59,6 @@ func init() {
5859
sqlFile.addStringFlag(FlagDbType, FlagDbTypeSort, EmptyDefaultValue, "database type")
5960
sqlFile.addStringFlag(FlagInstanceName, FlagInstanceNameSort, EmptyDefaultValue, "instance name")
6061
sqlFile.addStringFlag(FlagSchemaName, FlagSchemaNameSort, EmptyDefaultValue, "schema name")
62+
sqlFile.addBoolFlag(FlagShowFileContent, FlagShowFileContentSort, false, "show sql file")
6163
sqlFile.addRequiredFlag(FlagDirectory)
6264
}

sqle/cmd/scannerd/scanners/common/file.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/actiontech/sqle/sqle/utils"
1515
)
1616

17-
func GetSQLFromPath(pathName string, skipErrorQuery, skipErrorFile bool, fileSuffix string) (allSQL []driverV2.Node, err error) {
17+
func GetSQLFromPath(pathName string, skipErrorQuery, skipErrorFile bool, fileSuffix string, showFileContent bool) (allSQL []driverV2.Node, err error) {
1818
if !path.IsAbs(pathName) {
1919
pwd, err := os.Getwd()
2020
if err != nil {
@@ -32,9 +32,9 @@ func GetSQLFromPath(pathName string, skipErrorQuery, skipErrorFile bool, fileSuf
3232
pathJoin := path.Join(pathName, fi.Name())
3333

3434
if fi.IsDir() {
35-
sqlList, err = GetSQLFromPath(pathJoin, skipErrorQuery, skipErrorFile, fileSuffix)
35+
sqlList, err = GetSQLFromPath(pathJoin, skipErrorQuery, skipErrorFile, fileSuffix, showFileContent)
3636
} else if strings.HasSuffix(fi.Name(), fileSuffix) {
37-
sqlList, err = GetSQLFromFile(pathJoin, skipErrorQuery, fileSuffix)
37+
sqlList, err = GetSQLFromFile(pathJoin, skipErrorQuery, fileSuffix, showFileContent)
3838
}
3939

4040
if err != nil {
@@ -49,7 +49,7 @@ func GetSQLFromPath(pathName string, skipErrorQuery, skipErrorFile bool, fileSuf
4949
return allSQL, err
5050
}
5151

52-
func GetSQLFromFile(file string, skipErrorQuery bool, fileSuffix string) (r []driverV2.Node, err error) {
52+
func GetSQLFromFile(file string, skipErrorQuery bool, fileSuffix string, showFileContent bool) (r []driverV2.Node, err error) {
5353
content, err := ReadFileContent(file)
5454
if err != nil {
5555
return nil, err
@@ -64,18 +64,27 @@ func GetSQLFromFile(file string, skipErrorQuery bool, fileSuffix string) (r []dr
6464
sqls, err = mybatisParser.ParseXMLQuery(content, mybatisParser.RestoreOriginSql)
6565
}
6666
if err != nil {
67+
if showFileContent {
68+
fmt.Printf("failed to parse xml file content: %s", content)
69+
}
6770
return nil, err
6871
}
6972
for _, sql := range sqls {
7073
n, err := Parse(context.TODO(), sql)
7174
if err != nil {
75+
if showFileContent {
76+
fmt.Printf("failed to parse xml file content: %s", content)
77+
}
7278
return nil, err
7379
}
7480
r = append(r, n...)
7581
}
7682
case utils.SQLFileSuffix:
7783
n, err := Parse(context.TODO(), content)
7884
if err != nil {
85+
if showFileContent {
86+
fmt.Printf("failed to parse sql file content: %s", content)
87+
}
7988
return nil, err
8089
}
8190
r = append(r, n...)

sqle/cmd/scannerd/scanners/mybatis/mybatis.go

+24-21
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,41 @@ type MyBatis struct {
1515
l *logrus.Entry
1616
c *scanner.Client
1717

18-
xmlDir string
19-
skipErrorQuery bool
20-
skipErrorXml bool
21-
dbType string
22-
instName string
23-
schemaName string
18+
xmlDir string
19+
skipErrorQuery bool
20+
skipErrorXml bool
21+
dbType string
22+
instName string
23+
schemaName string
24+
showFileContent bool
2425
}
2526

2627
type Params struct {
27-
XMLDir string
28-
SkipErrorQuery bool
29-
SkipErrorXml bool
30-
DbType string
31-
InstName string
32-
SchemaName string
28+
XMLDir string
29+
SkipErrorQuery bool
30+
SkipErrorXml bool
31+
DbType string
32+
InstName string
33+
SchemaName string
34+
ShowFileContent bool
3335
}
3436

3537
func New(params *Params, l *logrus.Entry, c *scanner.Client) (*MyBatis, error) {
3638
return &MyBatis{
37-
xmlDir: params.XMLDir,
38-
skipErrorQuery: params.SkipErrorQuery,
39-
skipErrorXml: params.SkipErrorXml,
40-
dbType: params.DbType,
41-
instName: params.InstName,
42-
schemaName: params.SchemaName,
43-
l: l,
44-
c: c,
39+
xmlDir: params.XMLDir,
40+
skipErrorQuery: params.SkipErrorQuery,
41+
skipErrorXml: params.SkipErrorXml,
42+
dbType: params.DbType,
43+
instName: params.InstName,
44+
schemaName: params.SchemaName,
45+
showFileContent: params.ShowFileContent,
46+
l: l,
47+
c: c,
4548
}, nil
4649
}
4750

4851
func (mb *MyBatis) Run(ctx context.Context) error {
49-
sqls, err := common.GetSQLFromPath(mb.xmlDir, mb.skipErrorQuery, mb.skipErrorXml, utils.MybatisFileSuffix)
52+
sqls, err := common.GetSQLFromPath(mb.xmlDir, mb.skipErrorQuery, mb.skipErrorXml, utils.MybatisFileSuffix, mb.showFileContent)
5053
if err != nil {
5154
return err
5255
}

sqle/cmd/scannerd/scanners/sql_file/sqlfile.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type SQLFile struct {
2222
dbType string
2323
instName string
2424
schemaName string
25+
showFileContent bool
2526
}
2627

2728
type Params struct {
@@ -31,6 +32,7 @@ type Params struct {
3132
DbType string
3233
InstName string
3334
SchemaName string
35+
ShowFileContent bool
3436
}
3537

3638
func New(params *Params, l *logrus.Entry, c *scanner.Client) (*SQLFile, error) {
@@ -42,11 +44,12 @@ func New(params *Params, l *logrus.Entry, c *scanner.Client) (*SQLFile, error) {
4244
schemaName: params.SchemaName,
4345
l: l,
4446
c: c,
47+
showFileContent: params.ShowFileContent,
4548
}, nil
4649
}
4750

4851
func (sf *SQLFile) Run(ctx context.Context) error {
49-
sqls, err := common.GetSQLFromPath(sf.sqlDir, false, sf.skipErrorSqlFile, utils.SQLFileSuffix)
52+
sqls, err := common.GetSQLFromPath(sf.sqlDir, false, sf.skipErrorSqlFile, utils.SQLFileSuffix, sf.showFileContent)
5053
if err != nil {
5154
return fmt.Errorf("failed to get sql from path: %v", err)
5255
}

0 commit comments

Comments
 (0)