Skip to content

Commit

Permalink
infoschema: Support for showing "AUTO_INCREMENT" in "information_sche…
Browse files Browse the repository at this point in the history
…ma.tables" (#7037)
  • Loading branch information
zimulala authored and zz-jason committed Jul 14, 2018
1 parent c1b4490 commit 419e5cf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
29 changes: 28 additions & 1 deletion infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,29 @@ func getRowCountAllTable(ctx sessionctx.Context) (map[int64]uint64, error) {
return rowCountMap, nil
}

func getAutoIncrementID(ctx sessionctx.Context, schema *model.DBInfo, tblInfo *model.TableInfo) (int64, error) {
hasAutoIncID := false
for _, col := range tblInfo.Cols() {
if mysql.HasAutoIncrementFlag(col.Flag) {
hasAutoIncID = true
break
}
}
autoIncID := tblInfo.AutoIncID
if hasAutoIncID {
is := ctx.GetSessionVars().TxnCtx.InfoSchema.(InfoSchema)
tbl, err := is.TableByName(schema.Name, tblInfo.Name)
if err != nil {
return 0, errors.Trace(err)
}
autoIncID, err = tbl.Allocator(ctx).NextGlobalAutoID(tblInfo.ID)
if err != nil {
return 0, errors.Trace(err)
}
}
return autoIncID, nil
}

func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.Datum, error) {
tableRowsMap, err := getRowCountAllTable(ctx)
if err != nil {
Expand All @@ -676,6 +699,10 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D
continue
}

autoIncID, err := getAutoIncrementID(ctx, schema, table)
if err != nil {
return nil, errors.Trace(err)
}
record := types.MakeDatums(
catalogVal, // TABLE_CATALOG
schema.Name.O, // TABLE_SCHEMA
Expand All @@ -690,7 +717,7 @@ func dataForTables(ctx sessionctx.Context, schemas []*model.DBInfo) ([][]types.D
uint64(0), // MAX_DATA_LENGTH
uint64(0), // INDEX_LENGTH
uint64(0), // DATA_FREE
table.AutoIncID, // AUTO_INCREMENT
autoIncID, // AUTO_INCREMENT
createTime, // CREATE_TIME
nil, // UPDATE_TIME
nil, // CHECK_TIME
Expand Down
9 changes: 9 additions & 0 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func (s *testSuite) TestDataForTableRowsCountField(c *C) {
tk.MustQuery("select table_rows from information_schema.tables where table_name='t'").Check(
testkit.Rows("2"))

// Test for auto increment ID.
tk.MustExec("drop table t")
tk.MustExec("create table t (c int auto_increment primary key, d int)")
tk.MustQuery("select auto_increment from information_schema.tables where table_name='t'").Check(
testkit.Rows("1"))
tk.MustExec("insert into t(c, d) values(1, 1)")
tk.MustQuery("select auto_increment from information_schema.tables where table_name='t'").Check(
testkit.Rows("30002"))

tk.MustExec("create user xxx")
tk.MustExec("flush privileges")

Expand Down

0 comments on commit 419e5cf

Please sign in to comment.