--echo #
--echo # MDEV-34391 Path resolution
--echo # Trigger tests with embedded PATH
--echo #

CREATE DATABASE test2;
CREATE FUNCTION test2.func() RETURNS TEXT RETURN 'test2.func';
CREATE DATABASE test3;
CREATE FUNCTION test3.func() RETURNS TEXT RETURN 'test3.func';
CREATE PROCEDURE test3.proc() SET @a:= 'hit';


CREATE TABLE t1(a INT, what text);
CREATE TABLE t2(a INT);

--echo #
--echo # PATH during creation will be used for routine resolution (1)
--echo # Test where the embedded PATH is correct
--echo #
DELIMITER $$;
SET PATH 'test2'$$
CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW
BEGIN
  set new.what=concat(new.what, ' ', func());
END;
$$
DELIMITER ;$$

INSERT INTO t1 VALUES(1, '>');
SELECT * FROM t1;

--echo #
--echo # Session PATH does not impact trigger's routine resolution
--echo #
SET PATH 'CURRENT_SCHEMA';
INSERT INTO t1 VALUES(2, '>');
SELECT * FROM t1;

DROP TRIGGER trg1;

--echo #
--echo # PATH during creation will be used for routine resolution (2)
--echo # Test where the embedded PATH is wrong
--echo #
DELIMITER $$;
CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW
BEGIN
  set new.what=concat(new.what, ' ', func());
END;
$$
DELIMITER ;$$

--error ER_SP_DOES_NOT_EXIST
INSERT INTO t1 VALUES(3, '>');
SET PATH 'test2';
--error ER_SP_DOES_NOT_EXIST
INSERT INTO t1 VALUES(4, '>');
SELECT * FROM t1;

DROP TRIGGER trg1;

--echo #
--echo # PATH during creation will be used for routine resolution (3)
--echo # Test with procedure resolution
--echo #
DELIMITER $$;
SET PATH 'test3'$$
CREATE TRIGGER trg2 BEFORE UPDATE ON t2 FOR EACH ROW
BEGIN
  CALL proc();
END;
$$
DELIMITER ;$$

INSERT INTO t2 VALUES(1);
UPDATE t2 SET a = 2;

--echo #
--echo # Session PATH does not impact trigger's routine resolution
--echo #
SET PATH 'CURRENT_SCHEMA';
UPDATE t2 SET a = 3;
SELECT * FROM t2;

--echo #
--echo # Successive triggers with different embedded PATH
--echo #
DELIMITER $$;
SET PATH 'test2'$$
CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW
BEGIN
  set new.what=concat(new.what, ' ', func());
END;
$$
SET PATH 'test3'$$
CREATE TRIGGER trg3 BEFORE INSERT ON t1 FOR EACH ROW
BEGIN
  set new.what=concat(new.what, ' ', func());
END;
$$
DELIMITER ;$$

INSERT INTO t1 VALUES(5, '>');
SELECT * FROM t1;

DROP TRIGGER trg1;
DROP TRIGGER trg3;

--echo #
--echo # When PATH is empty, trigger's routine resolution will fail
--echo # without exception
--echo #
DELIMITER $$;
SET PATH ''$$
CREATE TRIGGER trg3 BEFORE INSERT ON t1 FOR EACH ROW
BEGIN
  DO func();
END;
$$
DELIMITER ;$$

--error ER_SP_DOES_NOT_EXIST
INSERT INTO t1 VALUES(5, '>');

SET PATH 'test2';
--error ER_SP_DOES_NOT_EXIST
INSERT INTO t1 VALUES(6, '>');

USE test2;
--error ER_SP_DOES_NOT_EXIST
INSERT INTO test.t1 VALUES(7, '>');

USE test;

DROP TRIGGER trg3;

DROP TABLE t2;
DROP TABLE t1;

--echo #
--echo # When PATH is set to CURRENT_SCHEMA, trigger's routine resolution
--echo # will use the schema that was set when the trigger is created and not
--echo # the current schema at the time of execution.
--echo #
CREATE TABLE test2.t1(a INT);
CREATE TABLE test2.t2(a INT);
SET PATH 'CURRENT_SCHEMA';

USE test2;
DELIMITER $$;
CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW
BEGIN
  DO func();
END;
$$
DELIMITER ;$$
INSERT INTO t1 VALUES(1);

USE test;
INSERT INTO test2.t1 VALUES(2);

DROP DATABASE test2;
DROP DATABASE test3;
