From b02e5309af680dfcba4a302b3800a861cffe9135 Mon Sep 17 00:00:00 2001 From: bb7133 Date: Thu, 27 Sep 2018 10:26:29 +0800 Subject: [PATCH] fix stylecheck fix #4754, fix #3839 --- ddl/ddl.go | 2 +- ddl/ddl_api.go | 12 ++++++------ ddl/table.go | 8 ++++---- executor/ddl.go | 3 ++- executor/show.go | 24 ++++++++++++------------ types/field_type.go | 5 ++++- types/field_type_test.go | 24 ++++++++++++++++++++++++ 7 files changed, 53 insertions(+), 25 deletions(-) diff --git a/ddl/ddl.go b/ddl/ddl.go index 614aeedc72b10..56cecd50e27b8 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -201,7 +201,7 @@ type DDL interface { DropSchema(ctx sessionctx.Context, schema model.CIStr) error CreateTable(ctx sessionctx.Context, stmt *ast.CreateTableStmt, withSelect bool) (int64, error) CreateTableWithLike(ctx sessionctx.Context, ident, referIdent ast.Ident, ifNotExists bool) (int64, error) - DropTable(ctx sessionctx.Context, tableIdent ast.Ident, tableId int64) (err error) + DropTable(ctx sessionctx.Context, tableIdent ast.Ident, tableID int64) (err error) CreateIndex(ctx sessionctx.Context, tableIdent ast.Ident, unique bool, indexName model.CIStr, columnNames []*ast.IndexColName, indexOption *ast.IndexOption) error DropIndex(ctx sessionctx.Context, tableIdent ast.Ident, indexName model.CIStr) error diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 5e790ecc219d8..c4cbd3499e691 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1879,24 +1879,24 @@ func (d *ddl) RenameIndex(ctx sessionctx.Context, ident ast.Ident, spec *ast.Alt } // DropTable will proceed even if some table in the list does not exists. -func (d *ddl) DropTable(ctx sessionctx.Context, ti ast.Ident, tableId int64) (err error) { +func (d *ddl) DropTable(ctx sessionctx.Context, ti ast.Ident, tableID int64) (err error) { is := d.GetInformationSchema(ctx) schema, ok := is.SchemaByName(ti.Schema) if !ok { return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(ti.Schema) } - if tableId == 0 { - tb, err := is.TableByName(ti.Schema, ti.Name) - if err != nil { + if tableID == 0 { + tb, e := is.TableByName(ti.Schema, ti.Name) + if e != nil { return errors.Trace(infoschema.ErrTableNotExists.GenWithStackByArgs(ti.Schema, ti.Name)) } - tableId = tb.Meta().ID + tableID = tb.Meta().ID } job := &model.Job{ SchemaID: schema.ID, - TableID: tableId, + TableID: tableID, Type: model.ActionDropTable, BinlogInfo: &model.HistoryInfo{}, } diff --git a/ddl/table.go b/ddl/table.go index 50cd8a7a14ba0..1e460fdb4bff7 100644 --- a/ddl/table.go +++ b/ddl/table.go @@ -66,7 +66,7 @@ func onCreateTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) // TODO: Add restrictions to this operation. go splitTableRegion(d.store, tbInfo.ID) } - ver, err := updateVersionAndTableInfo(t, job, tbInfo, originalState != tbInfo.State) + ver, err = updateVersionAndTableInfo(t, job, tbInfo, originalState != tbInfo.State) if err != nil { job.State = model.JobStateCancelled return ver, err @@ -81,9 +81,9 @@ func onCreateTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) } } -func onRevealTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) { +func onRevealTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, err error) { tbInfo := &model.TableInfo{} - if err := job.DecodeArgs(tbInfo); err != nil { + if err = job.DecodeArgs(tbInfo); err != nil { // Invalid arguments, cancel this job. job.State = model.JobStateCancelled return ver, errors.Trace(err) @@ -96,7 +96,7 @@ func onRevealTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) tbInfo.State = model.StatePublic tbInfo.UpdateTS = t.StartTS // Finish this job. - ver, err := updateVersionAndTableInfo(t, job, tbInfo, originalState != tbInfo.State) + ver, err = updateVersionAndTableInfo(t, job, tbInfo, originalState != tbInfo.State) if err != nil { job.State = model.JobStateCancelled return ver, err diff --git a/executor/ddl.go b/executor/ddl.go index 71db92127b568..88d56aea92a7d 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -88,7 +88,8 @@ func (e *DDLExec) Next(ctx context.Context, chk *chunk.Chunk) (err error) { case *ast.CreateDatabaseStmt: err = e.executeCreateDatabase(x) case *ast.CreateTableStmt: - tableID, err := e.executeCreateTable(x) + var tableID int64 + tableID, err = e.executeCreateTable(x) if err != nil { return errors.Trace(e.toErr(err)) } diff --git a/executor/show.go b/executor/show.go index 50682d50608ac..1a16fe07d6337 100644 --- a/executor/show.go +++ b/executor/show.go @@ -421,19 +421,19 @@ func (e *ShowExec) fetchShowIndex() error { subPart = col.Length } e.appendRow([]interface{}{ - tb.Meta().Name.O, // Table - nonUniq, // Non_unique - idx.Meta().Name.O, // Key_name - i + 1, // Seq_in_index - col.Name.O, // Column_name - "A", // Collation - 0, // Cardinality - subPart, // Sub_part - nil, // Packed - "YES", // Null + tb.Meta().Name.O, // Table + nonUniq, // Non_unique + idx.Meta().Name.O, // Key_name + i + 1, // Seq_in_index + col.Name.O, // Column_name + "A", // Collation + 0, // Cardinality + subPart, // Sub_part + nil, // Packed + "YES", // Null idx.Meta().Tp.String(), // Index_type - "", // Comment - idx.Meta().Comment, // Index_comment + "", // Comment + idx.Meta().Comment, // Index_comment }) } } diff --git a/types/field_type.go b/types/field_type.go index 3e0ab740994c2..0eb043644f8b8 100644 --- a/types/field_type.go +++ b/types/field_type.go @@ -40,7 +40,10 @@ func NewFieldType(tp byte) *FieldType { } } +// Clone makes a deep copy of FieldType func (ft *FieldType) Clone() *FieldType { + elems := make([]string, len(ft.Elems)) + copy(elems, ft.Elems) return &FieldType{ Tp: ft.Tp, Flag: ft.Flag, @@ -48,7 +51,7 @@ func (ft *FieldType) Clone() *FieldType { Decimal: ft.Decimal, Charset: ft.Charset, Collate: ft.Collate, - Elems: ft.Elems, + Elems: elems, } } diff --git a/types/field_type_test.go b/types/field_type_test.go index 04ec2e4b90aba..83d92da13a558 100644 --- a/types/field_type_test.go +++ b/types/field_type_test.go @@ -392,3 +392,27 @@ func (s *testFieldTypeSuite) TestAggregateEvalType(c *C) { } } } + +func (s *testFieldTypeSuite) TestClone(c *C) { + defer testleak.AfterTest(c)() + + ft := NewFieldType(mysql.TypeDecimal) + c.Assert(ft.Equal(ft.Clone()), IsTrue) + + ft = NewFieldType(mysql.TypeLong) + ft.Flen = 5 + ft.Flag = mysql.UnsignedFlag | mysql.ZerofillFlag + c.Assert(ft.Equal(ft.Clone()), IsTrue) + + ft = NewFieldType(mysql.TypeFloat) + ft.Flen = 7 + ft.Decimal = 3 + ft.Charset = charset.CharsetASCII + ft.Collate = charset.CharsetBin + ft.Elems = []string{"a", "b", "c"} + c.Assert(ft.Equal(ft.Clone()), IsTrue) + + ftc := ft.Clone() + ftc.Elems = append(ftc.Elems, "d") + c.Assert(ft.Equal(ftc), IsFalse) +}