Skip to content
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

Support Update join #761

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/datasource/sql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e
)
}

func (c *Conn) GetDbVersion() string {
return c.res.GetDbVersion()
}

func (c *Conn) GetAutoCommit() bool {
return c.autoCommit
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/datasource/sql/conn_at.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (c *ATConn) QueryContext(ctx context.Context, query string, args []driver.N
NamedValues: args,
Conn: c.targetConn,
DBName: c.dbName,
DbVersion: c.GetDbVersion(),
IsSupportsSavepoints: true,
IsAutoCommit: c.GetAutoCommit(),
}
Expand Down Expand Up @@ -102,6 +103,7 @@ func (c *ATConn) ExecContext(ctx context.Context, query string, args []driver.Na
NamedValues: args,
Conn: c.targetConn,
DBName: c.dbName,
DbVersion: c.GetDbVersion(),
IsSupportsSavepoints: true,
IsAutoCommit: c.GetAutoCommit(),
}
Expand Down
98 changes: 97 additions & 1 deletion pkg/datasource/sql/exec/at/base_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ import (
"database/sql"
"database/sql/driver"
"fmt"
"seata.apache.org/seata-go/pkg/datasource/sql/undo"
"strings"

"github.com/arana-db/parser/ast"
"github.com/arana-db/parser/model"
"github.com/arana-db/parser/test_driver"
gxsort "github.com/dubbogo/gost/sort"
"github.com/pkg/errors"

"seata.apache.org/seata-go/pkg/datasource/sql/exec"
"seata.apache.org/seata-go/pkg/datasource/sql/types"
"seata.apache.org/seata-go/pkg/datasource/sql/undo"
"seata.apache.org/seata-go/pkg/datasource/sql/util"
"seata.apache.org/seata-go/pkg/util/reflectx"
)
Expand Down Expand Up @@ -98,7 +100,13 @@ func (b *baseExecutor) buildSelectArgs(stmt *ast.SelectStmt, args []driver.Named
selectArgs = make([]driver.NamedValue, 0)
)

b.traversalArgs(stmt.From.TableRefs, &selectArgsIndexs)
b.traversalArgs(stmt.Where, &selectArgsIndexs)
if stmt.GroupBy != nil {
for _, item := range stmt.GroupBy.Items {
b.traversalArgs(item, &selectArgsIndexs)
}
}
if stmt.OrderBy != nil {
for _, item := range stmt.OrderBy.Items {
b.traversalArgs(item, &selectArgsIndexs)
Expand Down Expand Up @@ -143,6 +151,16 @@ func (b *baseExecutor) traversalArgs(node ast.Node, argsIndex *[]int32) {
b.traversalArgs(exprs[i], argsIndex)
}
break
case *ast.Join:
exprs := node.(*ast.Join)
b.traversalArgs(exprs.Left, argsIndex)
if exprs.Right != nil {
b.traversalArgs(exprs.Right, argsIndex)
}
if exprs.On != nil {
b.traversalArgs(exprs.On.Expr, argsIndex)
}
break
case *test_driver.ParamMarkerExpr:
*argsIndex = append(*argsIndex, int32(node.(*test_driver.ParamMarkerExpr).Order))
break
Expand Down Expand Up @@ -230,6 +248,64 @@ func (b *baseExecutor) containsPKByName(meta *types.TableMeta, columns []string)
return matchCounter == len(pkColumnNameList)
}

func (u *baseExecutor) buildSelectFields(ctx context.Context, tableMeta *types.TableMeta, tableAliases string, inUseFields []*ast.Assignment) ([]*ast.SelectField, error) {
fields := make([]*ast.SelectField, 0, len(inUseFields))

tableName := tableAliases
if tableAliases == "" {
tableName = tableMeta.TableName
}
if undo.UndoConfig.OnlyCareUpdateColumns {
for _, column := range inUseFields {
tn := column.Column.Table.O
if tn != "" && tn != tableName {
continue
}

fields = append(fields, &ast.SelectField{
Expr: &ast.ColumnNameExpr{
Name: column.Column,
},
})
}

if len(fields) == 0 {
return fields, nil
}

// select indexes columns
for _, columnName := range tableMeta.GetPrimaryKeyOnlyName() {
fields = append(fields, &ast.SelectField{
Expr: &ast.ColumnNameExpr{
Name: &ast.ColumnName{
Table: model.CIStr{
O: tableName,
L: tableName,
},
Name: model.CIStr{
O: columnName,
L: columnName,
},
},
},
})
}
} else {
fields = append(fields, &ast.SelectField{
Expr: &ast.ColumnNameExpr{
Name: &ast.ColumnName{
Name: model.CIStr{
O: "*",
L: "*",
},
},
},
})
}

return fields, nil
}

func getSqlNullValue(value interface{}) interface{} {
if value == nil {
return nil
Expand Down Expand Up @@ -393,3 +469,23 @@ func (b *baseExecutor) buildLockKey(records *types.RecordImage, meta types.Table

return lockKeys.String()
}

func (b *baseExecutor) rowsPrepare(ctx context.Context, conn driver.Conn, selectSQL string, selectArgs []driver.NamedValue) (driver.Rows, error) {
var queryer driver.Queryer

queryerContext, ok := conn.(driver.QueryerContext)
if !ok {
queryer, ok = conn.(driver.Queryer)
}
if ok {
var err error
rows, err = util.CtxDriverQuery(ctx, queryerContext, queryer, selectSQL, selectArgs)

if err != nil {
return nil, err
}
} else {
return nil, errors.New("target conn should been driver.QueryerContext or driver.Queryer")
}
return rows, nil
}
8 changes: 6 additions & 2 deletions pkg/datasource/sql/exec/at/update_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import (
"context"
"database/sql/driver"
"fmt"
"github.com/arana-db/parser/model"
"seata.apache.org/seata-go/pkg/datasource/sql/util"
"strings"

"github.com/arana-db/parser/ast"
"github.com/arana-db/parser/format"
"github.com/arana-db/parser/model"

"seata.apache.org/seata-go/pkg/datasource/sql/datasource"
"seata.apache.org/seata-go/pkg/datasource/sql/exec"
"seata.apache.org/seata-go/pkg/datasource/sql/types"
"seata.apache.org/seata-go/pkg/datasource/sql/undo"
"seata.apache.org/seata-go/pkg/datasource/sql/util"
"seata.apache.org/seata-go/pkg/util/bytes"
"seata.apache.org/seata-go/pkg/util/log"
)
Expand All @@ -49,6 +49,10 @@ type updateExecutor struct {

// NewUpdateExecutor get update executor
func NewUpdateExecutor(parserCtx *types.ParseContext, execContent *types.ExecContext, hooks []exec.SQLHook) executor {
// Because update join cannot be clearly identified when SQL cannot be parsed
if parserCtx.UpdateStmt.TableRefs.TableRefs.Right != nil {
return NewUpdateJoinExecutor(parserCtx, execContent, hooks)
}
return &updateExecutor{parserCtx: parserCtx, execContext: execContent, baseExecutor: baseExecutor{hooks: hooks}}
}

Expand Down
Loading
Loading