mariadb/mysql-test/main/sp-package-code.test
Alexander Barkov aed9c656a9 MDEV-32101 CREATE PACKAGE [BODY] for sql_mode=DEFAULT
This patch adds PACKAGE support with SQL/PSM dialect for sql_mode=DEFAULT:

- CREATE PACKAGE
- DROP PACKAGE
- CREATE PACKAGE BODY
- DROP PACKAGE BODY
- Package function and procedure invocation from outside of the package:
    -- using two step identifiers
    SELECT pkg.f1();
    CALL pkg.p1()

    -- using three step identifiers
    SELECT db.pkg.f1();
    CALL db.pkg.p1();

This is a non-standard MariaDB extension.

However, later this code can be used to implement
the SQL Standard and DB2 dialects of CREATE MODULE.
2023-12-18 13:34:55 +04:00

200 lines
3.4 KiB
Text

-- source include/have_debug.inc
--echo #
--echo # MDEV-32101 CREATE PACKAGE [BODY] for sql_mode=DEFAULT
--echo #
DELIMITER $$;
CREATE PACKAGE pkg1
PROCEDURE p1();
FUNCTION f1() RETURNS INT;
END;
$$
CREATE PACKAGE BODY pkg1
PROCEDURE p1()
BEGIN SELECT 1;
END;
FUNCTION f1() RETURNS INT RETURN 1;
END;
$$
DELIMITER ;$$
SHOW PROCEDURE CODE pkg1.p1;
SHOW FUNCTION CODE pkg1.f1;
SHOW PACKAGE BODY CODE pkg1;
DROP PACKAGE pkg1;
DELIMITER $$;
CREATE PACKAGE pkg1
PROCEDURE p1();
FUNCTION f1() RETURNS INT;
PROCEDURE p2show();
PROCEDURE p2public();
FUNCTION f2public() RETURNS TEXT;
END;
$$
CREATE PACKAGE BODY pkg1
DECLARE a INT DEFAULT 10;
PROCEDURE p1()
BEGIN
DECLARE b INT DEFAULT 20;
SET b=a;
SET b=a+1;
SET a=b;
SET a=b+1;
SET a=a+1;
SET @a=@a+2;
SELECT f1() FROM DUAL;
END;
FUNCTION f1() RETURNS INT
BEGIN
RETURN a;
END;
PROCEDURE p2private()
BEGIN
SELECT 'This is p2private';
END;
PROCEDURE p2public()
BEGIN
SELECT 'This is p2public';
END;
FUNCTION f2private() RETURNS TEXT
BEGIN
RETURN 'This is f2private';
END;
FUNCTION f2public() RETURNS TEXT
BEGIN
RETURN 'This is f2public';
END;
PROCEDURE p2show()
BEGIN
SHOW FUNCTION CODE f2public;
SHOW FUNCTION CODE f2private;
SHOW PROCEDURE CODE p2public;
SHOW PROCEDURE CODE p2private;
SHOW PROCEDURE CODE p2show;
END;
-- Initialization section
SET a=a+1;
BEGIN
DECLARE b INT;
SET b=a;
SET b=a+1;
SET a=b;
SET a=b+1;
END;
END;
$$
DELIMITER ;$$
SHOW PROCEDURE CODE pkg1.p1;
SHOW FUNCTION CODE pkg1.f1;
SHOW PACKAGE BODY CODE pkg1;
CALL pkg1.p2show;
DROP PACKAGE pkg1;
CREATE TABLE t1 (a INT);
DELIMITER $$;
CREATE PACKAGE pkg1
PROCEDURE p1();
END;
$$
CREATE PACKAGE BODY pkg1
DECLARE a TYPE OF t1.a DEFAULT 10;
PROCEDURE p1()
BEGIN
DECLARE b TYPE OF t1.a DEFAULT 20;
SET b=a;
SET b=a+1;
SET b=b+1;
SET a=b;
SET a=b+1;
SET a=a+1;
END;
-- Initialization section
SET a=a+1;
BEGIN
DECLARE b TYPE OF t1.a;
SET b=a;
SET b=a+1;
SET a=b;
SET a=b+1;
END;
END;
$$
DELIMITER ;$$
SHOW PROCEDURE CODE pkg1.p1;
SHOW PACKAGE BODY CODE pkg1;
DROP PACKAGE pkg1;
DROP TABLE t1;
DELIMITER $$;
CREATE PACKAGE pkg1
PROCEDURE p1();
END;
$$
CREATE PACKAGE BODY pkg1
DECLARE a ROW(a INT,b TEXT) DEFAULT ROW(10,'x10');
PROCEDURE p1()
BEGIN
DECLARE b ROW(a INT,b TEXT) DEFAULT ROW(20,'x20');
SET b=a;
SET a=b;
SET b.a=a.a+1;
SET a.a=b.a+1;
SET a.a=a.a+1;
END;
-- Initialization section
SET a.a:=a.a+1;
BEGIN
DECLARE b ROW(a INT,b TEXT) DEFAULT ROW(30,'x30');
SET b=a;
SET b.a=a.a+1;
SET a=b;
SET a.a=b.a+1;
END;
END;
$$
DELIMITER ;$$
SHOW PROCEDURE CODE pkg1.p1;
SHOW PACKAGE BODY CODE pkg1;
DROP PACKAGE pkg1;
CREATE TABLE t1 (a INT, b TEXT);
DELIMITER $$;
CREATE PACKAGE pkg1
PROCEDURE p1();
END;
$$
CREATE PACKAGE BODY pkg1
DECLARE a ROW TYPE OF t1 DEFAULT ROW(10,'x10');
PROCEDURE p1()
BEGIN
DECLARE b ROW TYPE OF t1 DEFAULT ROW(20,'x20');
SET b=a;
SET a=b;
SET b.a=a.a+1;
SET a.a=b.a+1;
SET a.a=a.a+1;
END;
-- Initialization section
SET a.a=a.a+1;
BEGIN
DECLARE b ROW TYPE OF t1 DEFAULT ROW(30,'x30');
SET b=a;
SET b.a=a.a+1;
SET a=b;
SET a.a=b.a+1;
END;
END;
$$
DELIMITER ;$$
SHOW PROCEDURE CODE pkg1.p1;
SHOW PACKAGE BODY CODE pkg1;
DROP PACKAGE pkg1;
DROP TABLE t1;