Skip to content

Commit

Permalink
variable: add support for identity (pingcap#28449)
Browse files Browse the repository at this point in the history
  • Loading branch information
TszKitLo40 authored Sep 28, 2021
1 parent fd73942 commit b50474c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
14 changes: 14 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10338,3 +10338,17 @@ func (s *testIntegrationSuite) TestLastInsertId(c *C) {
tk.MustExec(`INSERT INTO lastinsertid VALUES (NULL);`)
tk.MustQuery("SELECT @@last_insert_id, LAST_INSERT_ID()").Check(testkit.Rows("3 3"))
}

func (s *testIntegrationSuite) TestIdentity(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
tk.MustExec(`drop table if exists identity;`)
tk.MustExec(`create table identity (id int not null primary key auto_increment);`)
tk.MustQuery("SELECT @@identity;").Check(testkit.Rows("0"))
tk.MustExec(`INSERT INTO identity VALUES (NULL);`)
tk.MustQuery("SELECT @@identity, LAST_INSERT_ID()").Check(testkit.Rows("1 1"))
tk.MustExec(`INSERT INTO identity VALUES (NULL);`)
tk.MustQuery("SELECT @@identity, LAST_INSERT_ID()").Check(testkit.Rows("2 2"))
tk.MustExec(`INSERT INTO identity VALUES (NULL);`)
tk.MustQuery("SELECT @@identity, LAST_INSERT_ID()").Check(testkit.Rows("3 3"))
}
1 change: 0 additions & 1 deletion sessionctx/variable/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,6 @@ var noopSysVars = []*SysVar{
{Scope: ScopeGlobal, Name: AvoidTemporalUpgrade, Value: Off, Type: TypeBool},
{Scope: ScopeGlobal, Name: "key_cache_age_threshold", Value: "300"},
{Scope: ScopeGlobal, Name: InnodbStatusOutput, Value: Off, Type: TypeBool, AutoConvertNegativeBool: true},
{Scope: ScopeSession, Name: "identity", Value: ""},
{Scope: ScopeGlobal | ScopeSession, Name: "min_examined_row_limit", Value: "0"},
{Scope: ScopeGlobal, Name: "sync_frm", Type: TypeBool, Value: On},
{Scope: ScopeGlobal, Name: "innodb_online_alter_log_max_size", Value: "134217728"},
Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,9 @@ var defaultSysVars = []*SysVar{
{Scope: ScopeSession, Name: LastInsertID, Value: "", skipInit: true, GetSession: func(s *SessionVars) (string, error) {
return strconv.FormatUint(s.StmtCtx.PrevLastInsertID, 10), nil
}},
{Scope: ScopeSession, Name: Identity, Value: "", skipInit: true, GetSession: func(s *SessionVars) (string, error) {
return strconv.FormatUint(s.StmtCtx.PrevLastInsertID, 10), nil
}},
{Scope: ScopeNone, Name: "have_ssl", Value: "DISABLED"},
{Scope: ScopeNone, Name: "have_openssl", Value: "DISABLED"},
{Scope: ScopeNone, Name: "ssl_ca", Value: ""},
Expand Down Expand Up @@ -2136,6 +2139,8 @@ const (
DefaultAuthPlugin = "default_authentication_plugin"
// LastInsertID is the name of 'last_insert_id' system variable.
LastInsertID = "last_insert_id"
// Identity is the name of 'identity' system variable.
Identity = "identity"
)

// GlobalVarAccessor is the interface for accessing global scope system and status variables.
Expand Down
12 changes: 12 additions & 0 deletions sessionctx/variable/sysvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,3 +734,15 @@ func TestLastInsertID(t *testing.T) {
require.NoError(t, err)
require.Equal(t, val, "21")
}

func TestIdentity(t *testing.T) {
vars := NewSessionVars()
val, err := GetSessionOrGlobalSystemVar(vars, Identity)
require.NoError(t, err)
require.Equal(t, val, "0")

vars.StmtCtx.PrevLastInsertID = 21
val, err = GetSessionOrGlobalSystemVar(vars, Identity)
require.NoError(t, err)
require.Equal(t, val, "21")
}

0 comments on commit b50474c

Please sign in to comment.