# ==== Purpose ====
#
# Test errors/restrictions/conditions for the following variables:
#
#  - GTID_EXECUTED
#     - readable, read-only, both session and global
#  - GTID_PURGED
#     - readable, read-only, global only
#  - GTID_OWNED
#     - readable, read-only, both session and global
#  - GTID_NEXT
#     - read-writable, session only
#     - must be set to UUID:NUMBER or AUTOMATIC (when gtid_mode=ON)
#     - only settable by SUPER
#     - not settable from sub-statement
#     - not settable from statement that also invokes sub-statement
#     - not settable inside transaction
#     - not settable when some gtid is owned
#
# Note: some variables related to GTIDs are tested in other files:
#   GTID_MODE - rpl_gtid_mode.test
#   ENFORCE_GTID_CONSISTENCY - binlog_enforce_gtid_consistency.test
#   GTID_PURGED - rpl_gtid_purged_*.test
#
# ==== Implementation ====
#
# Just try all possibilities, verify that the correct error message is
# given.
#
# ==== References ====
#
# Implemented in WL#3584: Global Transaction Identifiers

--source include/have_innodb.inc
--source include/have_log_bin.inc
--source include/have_gtid.inc
--source include/have_debug.inc

--let $ida= aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
--let $idb= bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb
--let $idc= cccccccc-cccc-cccc-cccc-cccccccccccc
--let $idd= dddddddd-dddd-dddd-dddd-dddddddddddd
--let $ide= eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee
--let $idf= ffffffff-ffff-ffff-ffff-ffffffffffff
--let $tab= `SELECT '\t'`

--echo ==== Conditions for gtid_execute ====

--echo # Readable.
SELECT @@GLOBAL.GTID_EXECUTED;
SELECT @@SESSION.GTID_EXECUTED;
--echo # Read-only.
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
eval SET @@GLOBAL.GTID_EXECUTED= '$ida:1';
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
eval SET @@SESSION.GTID_EXECUTED= '$ida:2';

--echo ==== Conditions for gtid_purged ====

--echo # Readable global variable
SELECT @@GLOBAL.GTID_PURGED;
--echo # Not a session variable
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
SELECT @@SESSION.GTID_PURGED;
--echo # Read-only.
--error ER_GLOBAL_VARIABLE
eval SET @@SESSION.GTID_PURGED= '$ida:3';

--echo ==== Conditions for gtid_owned ====

--echo # Readable.
SELECT @@GLOBAL.GTID_OWNED;
SELECT @@SESSION.GTID_OWNED;
--echo # Read-only.
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
eval SET @@SESSION.GTID_OWNED= '$ida:5';
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
eval SET @@GLOBAL.GTID_OWNED= '$ida:6#1';

--echo ==== Valid and invalid values for gtid_next ====

--echo # Default should be AUTOMATIC
SELECT @@SESSION.GTID_NEXT;

SET @@SESSION.GTID_NEXT= 'AUTOMATIC';
SELECT @@SESSION.GTID_NEXT;

eval SET @@SESSION.GTID_NEXT= '$ida:10';
SELECT @@SESSION.GTID_NEXT;
ROLLBACK;

eval SET @@SESSION.GTID_NEXT= '
    $ida $tab : $tab 10
    ';
SELECT @@SESSION.GTID_NEXT;
ROLLBACK;

eval SET @@SESSION.GTID_NEXT= '$ida:0x10';
SELECT @@SESSION.GTID_NEXT;
ROLLBACK;

eval SET @@SESSION.GTID_NEXT= '$ida:010';
SELECT @@SESSION.GTID_NEXT;
ROLLBACK;

SET @@SESSION.GTID_NEXT= 'AUTOMATIC';

--error ER_WRONG_VALUE_FOR_VAR
eval SET @@SESSION.GTID_NEXT= '$ida:10#';
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_VALUE_FOR_VAR
eval SET @@SESSION.GTID_NEXT= '$ida:1-10';
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_VALUE_FOR_VAR
eval SET @@SESSION.GTID_NEXT= '$ida:1:3';
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_VALUE_FOR_VAR
eval SET @@SESSION.GTID_NEXT= '$ida:1,$ida:3';
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_VALUE_FOR_VAR
SET @@SESSION.GTID_NEXT= NULL;
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_TYPE_FOR_VAR
SET @@SESSION.GTID_NEXT= 10;
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_VALUE_FOR_VAR
SET @@SESSION.GTID_NEXT= '10';
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_VALUE_FOR_VAR
eval SET @@SESSION.GTID_NEXT= '$ida:';
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_VALUE_FOR_VAR
eval SET @@SESSION.GTID_NEXT= '$ida:0';
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_VALUE_FOR_VAR
eval SET @@SESSION.GTID_NEXT= '$ida:0x8000000000000000';
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_VALUE_FOR_VAR
eval SET @@SESSION.GTID_NEXT= '$ida:-33';
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_VALUE_FOR_VAR
eval SET @@SESSION.GTID_NEXT= '$ida:66-33';
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_VALUE_FOR_VAR
eval SET @@SESSION.GTID_NEXT= '$ida:33 33';
SELECT @@SESSION.GTID_NEXT;

--error ER_WRONG_VALUE_FOR_VAR
eval SET @@SESSION.GTID_NEXT= '$ida-33';
SELECT @@SESSION.GTID_NEXT;

--echo ==== Conditions for gtid_next ====

--echo # Initialize
--eval CREATE FUNCTION f() RETURNS INT BEGIN RETURN 1; END
CREATE TABLE t1 (a INT) ENGINE = InnoDB;
CREATE USER nonsuper@localhost;
GRANT SELECT ON *.* TO nonsuper@localhost;
--connect (nonsuper,localhost,nonsuper,,)
--connection default

--echo # Read/write session
SELECT @@SESSION.GTID_NEXT;
eval SET @@SESSION.GTID_NEXT= '$ida:7';
ROLLBACK;
--echo # not global
--error ER_INCORRECT_GLOBAL_LOCAL_VAR
SELECT @@GLOBAL.GTID_NEXT;
--error ER_LOCAL_VARIABLE
eval SET @@GLOBAL.GTID_NEXT= '$ida:8';
ROLLBACK;
SET GTID_NEXT = 'AUTOMATIC';

--echo # Can't set value from stored function
--eval CREATE FUNCTION set_var() RETURNS INT BEGIN SET @@SESSION.GTID_NEXT = '$ida:9'; RETURN 1; END
--error ER_VARIABLE_NOT_SETTABLE_IN_SF_OR_TRIGGER
SELECT set_var();
DROP FUNCTION set_var;

--echo # Can't set value from trigger
eval CREATE TRIGGER t BEFORE INSERT ON t1 FOR EACH ROW SET @@SESSION.GTID_NEXT = '$ida:10';
--error ER_VARIABLE_NOT_SETTABLE_IN_SF_OR_TRIGGER
INSERT INTO t1 VALUES (1);
DROP TRIGGER t;

--echo # Can't set value when invoking SF
--error ER_SET_STATEMENT_CANNOT_INVOKE_FUNCTION
SET @@SESSION.GTID_NEXT = f();

--echo # must be SUPER
--connection nonsuper
--error ER_SPECIFIC_ACCESS_DENIED_ERROR
eval SET @@SESSION.GTID_NEXT = '$ida:11';
--connection default

--echo # can read even if non-SUPER
SELECT @@SESSION.GTID_NEXT;

--echo # can't set inside transaction
BEGIN;
--error ER_CANT_CHANGE_GTID_NEXT_IN_TRANSACTION_WHEN_GTID_NEXT_LIST_IS_NULL
eval SET @@SESSION.GTID_NEXT = '$ida:12';
INSERT INTO t1 VALUES (4);
ROLLBACK;

--echo # can't set while owning a GTID
eval SET @@SESSION.GTID_NEXT = '$ida:13';
--error ER_CANT_SET_GTID_NEXT_WHEN_OWNING_GTID
eval SET @@SESSION.GTID_NEXT = '$ida:14';
# the above error will clean owned_gtid
eval SET @@SESSION.GTID_NEXT = 'AUTOMATIC';
ROLLBACK;

--echo # can't set while owning a GTID
eval SET @@SESSION.GTID_NEXT = '$ida:13';
--error ER_CANT_SET_GTID_NEXT_WHEN_OWNING_GTID
eval SET @@SESSION.GTID_NEXT = 'AUTOMATIC';
ROLLBACK;

eval SET @@SESSION.GTID_NEXT = '$ida:14';
--echo # can't do implicit commit while gtid_next=SID:GNO
BEGIN;
--error ER_CANT_DO_IMPLICIT_COMMIT_IN_TRX_WHEN_GTID_NEXT_IS_SET
CREATE TABLE t2 (a INT);
ROLLBACK;

eval SET GTID_NEXT = '$ida:15';
--echo # can't do implicit commit while gtid_next=SID:GNO
SET AUTOCOMMIT = 0;
INSERT INTO t1 VALUES (1);
--error ER_CANT_DO_IMPLICIT_COMMIT_IN_TRX_WHEN_GTID_NEXT_IS_SET
CREATE TABLE t2 (a INT);
ROLLBACK;

SELECT @@SESSION.GTID_NEXT;
eval SET @@SESSION.GTID_NEXT = 'AUTOMATIC';

DROP TABLE t1;
DROP FUNCTION f;
DROP USER nonsuper@localhost;

# It is (unfortunately) possible to set the value from a stored
# procedure.  No user should do this, it's just weird, but was hard
# to prevent so instead we allow it.  Although this is unsupported, we
# have to check that at least there is no crash.
--delimiter |
eval CREATE PROCEDURE p ()
BEGIN
  SET @@SESSION.GTID_NEXT = '$ida:16';
  ROLLBACK;
  SET @@SESSION.GTID_NEXT = 'AUTOMATIC';
END|
--delimiter ;
CALL p();
DROP PROCEDURE p;
