Skip to content

Commit 18a5dfd

Browse files
authored
Merge pull request #3 from actiontech/main
sync
2 parents 98ff025 + 4709390 commit 18a5dfd

File tree

388 files changed

+99676
-111892
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

388 files changed

+99676
-111892
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
/.idea/

ast/data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (d *IBatisData) scanVariable() error {
301301
if err != nil {
302302
return err
303303
}
304-
if r == '$' {
304+
if r == '$' || r == '}' {
305305
break
306306
}
307307
}

ast/data_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ func TestMyBatisScan(t *testing.T) {
3838
}
3939

4040
func TestIBatisScan(t *testing.T) {
41-
d := NewIBatisData([]byte("asd #include_target#$v$asdasd"))
41+
d := NewIBatisData([]byte("asd #include_target#$v$asdasd${vb}"))
4242
err := d.ScanData()
4343
if err != nil {
4444
t.Errorf("parse error: %v", err)
4545
return
4646
}
47-
if len(d.Nodes) != 4 {
47+
if len(d.Nodes) != 5 {
4848
t.Errorf("data length is 3, actual is %d", len(d.Nodes))
4949
//return
5050
}
@@ -68,4 +68,9 @@ func TestIBatisScan(t *testing.T) {
6868
t.Errorf("expect data is \"asdasd\", actual is %s", actual)
6969
return
7070
}
71+
actual = d.Nodes[4].String()
72+
if actual != "$" {
73+
t.Errorf("expect data is \"$\", actual is %s", actual)
74+
return
75+
}
7176
}

ast/dynamic.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,40 @@ func (n *IterateStmt) GetStmt(ctx *Context) (string, error) {
347347
return buff.String(), nil
348348
}
349349

350+
type StrStmt struct {
351+
*ChildrenNode
352+
}
353+
354+
func NewStrStmt() *StrStmt {
355+
n := &StrStmt{}
356+
n.ChildrenNode = NewNode()
357+
return n
358+
}
359+
360+
func (s *StrStmt) Scan(start *xml.StartElement) error {
361+
return nil
362+
}
363+
364+
func (s *StrStmt) GetStmt(ctx *Context) (string, error) {
365+
buff := bytes.Buffer{}
366+
for _, a := range s.Children {
367+
data, err := a.GetStmt(ctx)
368+
if err != nil {
369+
return "", err
370+
}
371+
// 如果解析出来的语句仅仅只有一个变量,不处理,直接跳过,因为没有意义
372+
// <str type="Str"><![CDATA[${setID}]]></str>
373+
if data == "?" {
374+
continue
375+
}
376+
377+
buff.WriteString(" ")
378+
buff.WriteString(data)
379+
buff.WriteString(" ")
380+
}
381+
return buff.String(), nil
382+
}
383+
350384
type DynamicStmt struct {
351385
*ChildrenNode
352386
prepEnd string
@@ -396,3 +430,73 @@ func (n *DynamicStmt) GetStmt(ctx *Context) (string, error) {
396430
}
397431
return buff.String(), nil
398432
}
433+
434+
type AndNodeStmt struct {
435+
*ChildrenNode
436+
}
437+
438+
func NewAndNode() *AndNodeStmt {
439+
n := &AndNodeStmt{}
440+
n.ChildrenNode = NewNode()
441+
return n
442+
}
443+
444+
func (a *AndNodeStmt) Scan(start *xml.StartElement) error {
445+
return nil
446+
}
447+
448+
func (a *AndNodeStmt) AddChildren(ns ...Node) error {
449+
a.Children = append(a.Children, ns...)
450+
return nil
451+
}
452+
453+
func (a *AndNodeStmt) GetStmt(ctx *Context) (string, error) {
454+
andList := make([]string, 0, len(a.Children))
455+
for _, a := range a.Children {
456+
data, err := a.GetStmt(ctx)
457+
if err != nil {
458+
return "", err
459+
}
460+
buff := bytes.Buffer{}
461+
buff.WriteString("(")
462+
buff.WriteString(data)
463+
buff.WriteString(")")
464+
andList = append(andList, buff.String())
465+
}
466+
return strings.Join(andList, "AND"), nil
467+
}
468+
469+
type OrNodeStmt struct {
470+
*ChildrenNode
471+
}
472+
473+
func NewOrNode() *OrNodeStmt {
474+
n := &OrNodeStmt{}
475+
n.ChildrenNode = NewNode()
476+
return n
477+
}
478+
479+
func (a *OrNodeStmt) Scan(start *xml.StartElement) error {
480+
return nil
481+
}
482+
483+
func (a *OrNodeStmt) AddChildren(ns ...Node) error {
484+
a.Children = append(a.Children, ns...)
485+
return nil
486+
}
487+
488+
func (a *OrNodeStmt) GetStmt(ctx *Context) (string, error) {
489+
orList := make([]string, 0, len(a.Children))
490+
for _, a := range a.Children {
491+
data, err := a.GetStmt(ctx)
492+
if err != nil {
493+
return "", err
494+
}
495+
buff := bytes.Buffer{}
496+
buff.WriteString("(")
497+
buff.WriteString(data)
498+
buff.WriteString(")")
499+
orList = append(orList, buff.String())
500+
}
501+
return strings.Join(orList, "OR"), nil
502+
}

ast/mapper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ func (m *Mapper) GetStmt(ctx *Context) (string, error) {
6969
return strings.TrimSuffix(buff.String(), "\n"), nil
7070
}
7171

72-
func (m *Mapper) GetStmts(ctx *Context, skipErrorQuery bool) ([]string, error) {
73-
var stmts []string
72+
func (m *Mapper) GetStmts(ctx *Context, skipErrorQuery bool) ([]StmtInfo, error) {
73+
var stmts []StmtInfo
7474
if len(ctx.Sqls) == 0 {
7575
ctx.Sqls = m.SqlNodes
7676
}
7777
ctx.DefaultNamespace = m.NameSpace
7878
for _, a := range m.QueryNodes {
7979
data, err := a.GetStmt(ctx)
8080
if err == nil {
81-
stmts = append(stmts, data)
81+
stmts = append(stmts, StmtInfo{SQL: data, StartLine: a.StartLine})
8282
continue
8383
}
8484
if skipErrorQuery {

ast/mappers.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ func (s *Mappers) AddMapper(ms ...*Mapper) error {
2424
}
2525

2626
type StmtInfo struct {
27-
FilePath string
28-
SQL string
27+
FilePath string
28+
StartLine uint64
29+
SQL string
2930
}
3031

3132
func (s *Mappers) GetStmts(skipErrorQuery bool) ([]StmtInfo, error) {
@@ -43,10 +44,11 @@ func (s *Mappers) GetStmts(skipErrorQuery bool) ([]StmtInfo, error) {
4344
if err != nil {
4445
return nil, fmt.Errorf("get sqls from mapper failed, namespace: %v, err: %v", m.NameSpace, err)
4546
}
46-
for _, sql := range stmt {
47+
for _, info := range stmt {
4748
stmts = append(stmts, StmtInfo{
48-
FilePath: m.FilePath,
49-
SQL: sql,
49+
FilePath: m.FilePath,
50+
StartLine: info.StartLine,
51+
SQL: info.SQL,
5052
})
5153
}
5254
}

ast/query.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import (
99

1010
type QueryNode struct {
1111
*ChildrenNode
12-
Id string
13-
Type string
12+
Id string
13+
Type string
14+
StartLine uint64
1415
}
1516

16-
func NewQueryNode() *QueryNode {
17-
n := &QueryNode{}
17+
func NewQueryNode(startLine uint64) *QueryNode {
18+
n := &QueryNode{
19+
StartLine: startLine,
20+
}
1821
n.ChildrenNode = NewNode()
1922
return n
2023
}

go.mod

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
module github.com/actiontech/mybatis-mapper-2-sql
22

3-
go 1.14
3+
go 1.17
44

55
require (
66
github.com/pingcap/parser v3.0.12+incompatible
77
github.com/pingcap/tidb v0.0.0-20200312110807-8c4696b3f340 // v3.0.12
8-
github.com/stretchr/testify v1.3.0
8+
)
9+
10+
require github.com/stretchr/testify v1.3.0
11+
12+
require (
13+
github.com/StackExchange/wmi v0.0.0-20180725035823-b12b22c5341f // indirect
14+
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
15+
github.com/davecgh/go-spew v1.1.1 // indirect
16+
github.com/go-ole/go-ole v1.2.1 // indirect
17+
github.com/golang/protobuf v1.2.0 // indirect
18+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
19+
github.com/pingcap/errors v0.11.4 // indirect
20+
github.com/pingcap/log v0.0.0-20190715063458-479153f07ebd // indirect
21+
github.com/pingcap/tipb v0.0.0-20191120045257-1b9900292ab6 // indirect
22+
github.com/pmezard/go-difflib v1.0.0 // indirect
23+
github.com/remyoudompheng/bigfft v0.0.0-20190512091148-babf20351dd7 // indirect
24+
github.com/shirou/gopsutil v2.18.10+incompatible // indirect
25+
github.com/sirupsen/logrus v1.2.0 // indirect
26+
go.uber.org/atomic v1.3.2 // indirect
27+
go.uber.org/multierr v1.1.0 // indirect
28+
go.uber.org/zap v1.9.1 // indirect
29+
golang.org/x/crypto v0.0.0-20190909091759-094676da4a83 // indirect
30+
golang.org/x/sys v0.15.0 // indirect
31+
golang.org/x/text v0.3.0 // indirect
32+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
933
)

0 commit comments

Comments
 (0)