Browse Source

frature: add update sql parser and remove tidb parser (#264)

add update sql parser and remove tidb parser (#243)
tags/1.0.2-RC1
Jason Deng GitHub 3 years ago
parent
commit
533b3e530d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 84 additions and 464 deletions
  1. +10
    -4
      go.mod
  2. +18
    -453
      go.sum
  3. +53
    -3
      pkg/datasource/sql/exec/hook/basic_undo_builder.go
  4. +3
    -4
      pkg/datasource/sql/parser/parser_factory.go

+ 10
- 4
go.mod View File

@@ -3,24 +3,30 @@ module github.com/seata/seata-go
go 1.16

require (
cloud.google.com/go v0.93.3 // indirect
dubbo.apache.org/dubbo-go/v3 v3.0.2-0.20220508105316-b27ec53b7bab
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/agiledragon/gomonkey v2.0.2+incompatible
github.com/apache/dubbo-getty v1.4.8
github.com/arana-db/parser v0.2.4
github.com/dubbogo/gost v1.12.5
github.com/form3tech-oss/jwt-go v3.2.5+incompatible // indirect
github.com/go-sql-driver/mysql v1.6.0
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pingcap/tidb v1.1.0-beta.0.20211124132551-4a1b2e9fe5b5
github.com/pingcap/tidb/parser v0.0.0-20211124132551-4a1b2e9fe5b5
github.com/pingcap/tipb v0.0.0-20220628092852-069ef6c8fc90 // indirect
github.com/pingcap/log v0.0.0-20210906054005-afc726e70354 // indirect
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.7.1
go.uber.org/atomic v1.9.0
go.uber.org/zap v1.21.0
golang.org/x/tools v0.1.12 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
google.golang.org/genproto v0.0.0-20220630174209-ad1d48641aa7 // indirect
google.golang.org/grpc v1.47.0
google.golang.org/protobuf v1.28.0


+ 18
- 453
go.sum
File diff suppressed because it is too large
View File


+ 53
- 3
pkg/datasource/sql/exec/hook/basic_undo_builder.go View File

@@ -20,14 +20,15 @@ package exec
import (
"context"
"fmt"

"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/format"
"github.com/arana-db/parser/ast"
"github.com/arana-db/parser/format"
"github.com/seata/seata-go/pkg/common/bytes"
"github.com/seata/seata-go/pkg/common/log"
"github.com/seata/seata-go/pkg/datasource/sql/exec"
"github.com/seata/seata-go/pkg/datasource/sql/parser"
"github.com/seata/seata-go/pkg/datasource/sql/types"

_ "github.com/arana-db/parser/test_driver"
)

type BasicUndoBuilder struct {
@@ -81,3 +82,52 @@ func (u *BasicUndoBuilder) buildSelectSQLByUpdate(query string) (string, error)

return sql, nil
}

// buildSelectSQLByUpdate build select sql from update sql
func (u *BasicUndoBuilder) buildSelectSQLByInsert(query string) (string, error) {
p, err := parser.DoParser(query)
if err != nil {
return "", err
}

if p.InsertStmt == nil {
return "", fmt.Errorf("invalid Insert stmt")
}

InsertColumns := p.InsertStmt.Columns
fields := []*ast.SelectField{}

for _, column := range InsertColumns {
fields = append(fields, &ast.SelectField{
Expr: &ast.ColumnNameExpr{
Name: column,
},
})
}
insertStmtList := p.InsertStmt.Lists
var whereStmt ast.ExprNode

whereList := []ast.ExprNode{}
if len(insertStmtList) > 0 {
whereList = p.InsertStmt.Lists[0]
}

if len(whereList) > 0 {
whereStmt = whereList[0]
}

selStmt := ast.SelectStmt{
SelectStmtOpts: &ast.SelectStmtOpts{},
From: p.InsertStmt.Table,
Where: whereStmt,
Fields: &ast.FieldList{Fields: fields},
TableHints: p.InsertStmt.TableHints,
}

b := bytes.NewByteBuffer([]byte{})
selStmt.Restore(format.NewRestoreCtx(format.RestoreKeyWordUppercase, b))
sql := string(b.Bytes())
log.Infof("build select sql by insert query, sql {}", sql)

return sql, nil
}

+ 3
- 4
pkg/datasource/sql/parser/parser_factory.go View File

@@ -18,9 +18,8 @@
package parser

import (
tparser "github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
_ "github.com/pingcap/tidb/types/parser_driver"
aparser "github.com/arana-db/parser"
"github.com/arana-db/parser/ast"
"github.com/seata/seata-go/pkg/datasource/sql/types"
)

@@ -52,7 +51,7 @@ type ParseContext struct {
}

func DoParser(query string) (*ParseContext, error) {
p := tparser.New()
p := aparser.New()
stmtNode, err := p.ParseOneStmt(query, "", "")
if err != nil {
return nil, err


Loading…
Cancel
Save