Skip to content

Commit

Permalink
Merge pull request #15 from ColdWaterLW/issue-2054
Browse files Browse the repository at this point in the history
gather parsed SQLs by file name
  • Loading branch information
sjjian authored Dec 5, 2023
2 parents ec9bc09 + 62a95c8 commit 98ff025
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions ast/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Mapper struct {
SqlNodes map[string]*SqlNode
QueryNodeIndex map[string]*QueryNode
QueryNodes []*QueryNode
FilePath string
}

func NewMapper() *Mapper {
Expand Down
16 changes: 13 additions & 3 deletions ast/mappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ func (s *Mappers) AddMapper(ms ...*Mapper) error {
return nil
}

func (s *Mappers) GetStmts(skipErrorQuery bool) ([]string, error) {
type StmtInfo struct {
FilePath string
SQL string
}

func (s *Mappers) GetStmts(skipErrorQuery bool) ([]StmtInfo, error) {
ctx := NewContext()
stmts := []string{}
stmts := []StmtInfo{}
for _, m := range s.mappers {
for id, node := range m.SqlNodes {
ctx.Sqls[fmt.Sprintf("%v.%v", m.NameSpace, id)] = node
Expand All @@ -38,7 +43,12 @@ func (s *Mappers) GetStmts(skipErrorQuery bool) ([]string, error) {
if err != nil {
return nil, fmt.Errorf("get sqls from mapper failed, namespace: %v, err: %v", m.NameSpace, err)
}
stmts = append(stmts, stmt...)
for _, sql := range stmt {
stmts = append(stmts, StmtInfo{
FilePath: m.FilePath,
SQL: sql,
})
}
}
return stmts, nil
}
14 changes: 10 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ func ParseXML(data string) (string, error) {
return stmt, nil
}

// ParseXMLs is a parser for parse all query in several XML files to []string one by one;
type XmlFile struct {
FilePath string
Content string
}

// ParseXMLs is a parser for parse all query in several XML files to []ast.StmtInfo one by one;
// you can set `skipErrorQuery` true to ignore invalid query.
func ParseXMLs(data []string, skipErrorQuery bool) ([]string, error) {
func ParseXMLs(data []XmlFile, skipErrorQuery bool) ([]ast.StmtInfo, error) {
ms := ast.NewMappers()
for i := range data {
r := strings.NewReader(data[i])
for _, data := range data {
r := strings.NewReader(data.Content)
d := xml.NewDecoder(r)
n, err := parse(d)
if err != nil {
Expand All @@ -56,6 +61,7 @@ func ParseXMLs(data []string, skipErrorQuery bool) ([]string, error) {
return nil, errors.New("the mapper is not found")
}
}
m.FilePath = data.FilePath
err = ms.AddMapper(m)
if err != nil && !skipErrorQuery {
return nil, fmt.Errorf("add mapper failed: %v", err)
Expand Down

0 comments on commit 98ff025

Please sign in to comment.