Skip to content

Commit

Permalink
insertID: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sougou authored and michael-berlin committed May 22, 2017
1 parent 69387e3 commit a2bcdb3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
30 changes: 20 additions & 10 deletions go/vt/vtgate/engine/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func (route *Route) execDeleteEqual(vcursor VCursor, queryConstruct *queryinfo.Q
}

func (route *Route) execInsertUnsharded(vcursor VCursor, queryConstruct *queryinfo.QueryConstruct) (*sqltypes.Result, error) {
insertid, err := route.handleGenerate(vcursor, queryConstruct)
insertID, err := route.handleGenerate(vcursor, queryConstruct)
if err != nil {
return nil, fmt.Errorf("execInsertUnsharded: %v", err)
}
Expand All @@ -456,14 +456,18 @@ func (route *Route) execInsertUnsharded(vcursor VCursor, queryConstruct *queryin
return nil, fmt.Errorf("execInsertUnsharded: %v", err)
}

if insertid != 0 {
result.InsertID = uint64(insertid)
// If handleGenerate generated new values, it supercedes
// any ids that MySQL might have generated. If both generated
// values, we don't return an error because this behavior
// is required to support migration.
if insertID != 0 {
result.InsertID = uint64(insertID)
}
return result, nil
}

func (route *Route) execInsertSharded(vcursor VCursor, queryConstruct *queryinfo.QueryConstruct) (*sqltypes.Result, error) {
insertid, err := route.handleGenerate(vcursor, queryConstruct)
insertID, err := route.handleGenerate(vcursor, queryConstruct)
if err != nil {
return nil, fmt.Errorf("execInsertSharded: %v", err)
}
Expand All @@ -478,8 +482,12 @@ func (route *Route) execInsertSharded(vcursor VCursor, queryConstruct *queryinfo
return nil, fmt.Errorf("execInsertSharded: %v", err)
}

if insertid != 0 {
result.InsertID = uint64(insertid)
// If handleGenerate generated new values, it supercedes
// any ids that MySQL might have generated. If both generated
// values, we don't return an error because this behavior
// is required to support migration.
if insertID != 0 {
result.InsertID = uint64(insertID)
}
return result, nil
}
Expand Down Expand Up @@ -693,7 +701,9 @@ func (route *Route) deleteVindexEntries(vcursor VCursor, queryConstruct *queryin
return nil
}

func (route *Route) handleGenerate(vcursor VCursor, queryConstruct *queryinfo.QueryConstruct) (insertid int64, err error) {
// handleGenerate generates new values using a sequence if necessary.
// If no value was generated, it returns 0.
func (route *Route) handleGenerate(vcursor VCursor, queryConstruct *queryinfo.QueryConstruct) (insertID int64, err error) {
if route.Generate == nil {
return 0, nil
}
Expand Down Expand Up @@ -733,12 +743,12 @@ func (route *Route) handleGenerate(vcursor VCursor, queryConstruct *queryinfo.Qu
}
// If no rows are returned, it's an internal error, and the code
// must panic, which will caught and reported.
insertid, err = qr.Rows[0][0].ParseInt64()
insertID, err = qr.Rows[0][0].ParseInt64()
if err != nil {
return 0, err
}
}
cur := insertid
cur := insertID
for i, v := range resolved {
if v != nil {
queryConstruct.BindVars[SeqVarName+strconv.Itoa(i)] = v
Expand All @@ -747,7 +757,7 @@ func (route *Route) handleGenerate(vcursor VCursor, queryConstruct *queryinfo.Qu
cur++
}
}
return insertid, nil
return insertID, nil
}

func (route *Route) handlePrimary(vcursor VCursor, vindexKeys []interface{}, colVindex *vindexes.ColumnVindex, bv map[string]interface{}) (keyspaceIDs [][]byte, err error) {
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/router_dml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func TestInsertAutoincSharded(t *testing.T) {
Rows: [][]sqltypes.Value{{
sqltypes.MakeTrusted(sqltypes.Int64, []byte("1")),
}},
RowsAffected: 2,
RowsAffected: 1,
InsertID: 2,
}
sbc.SetResults([]*sqltypes.Result{wantResult})
Expand Down Expand Up @@ -516,7 +516,7 @@ func TestInsertAutoincUnsharded(t *testing.T) {
Rows: [][]sqltypes.Value{{
sqltypes.MakeTrusted(sqltypes.Int64, []byte("1")),
}},
RowsAffected: 2,
RowsAffected: 1,
InsertID: 2,
}
sbclookup.SetResults([]*sqltypes.Result{wantResult})
Expand Down

0 comments on commit a2bcdb3

Please sign in to comment.