-- Пачка 5.2. Те же кейсы, что на стенде: вывод Java в SQL.
|
CREATE OR REPLACE FUNCTION dbms_java_set_output_to_sql(
|
id varchar2, stmt varchar2, bindings varchar2,
|
no_newline_stmt varchar2 DEFAULT NULL,
|
no_newline_bindings varchar2 DEFAULT NULL,
|
newline_only_stmt varchar2 DEFAULT NULL,
|
newline_only_bindings varchar2 DEFAULT NULL,
|
maximum_line_segment_length number DEFAULT 0,
|
allow_replace number DEFAULT 1,
|
from_stdout number DEFAULT 1,
|
from_stderr number DEFAULT 1,
|
include_newlines number DEFAULT 0,
|
eager number DEFAULT 0) RETURNS varchar2
|
AS 'dbms_java_set_output_to_sql' LANGUAGE internal;
|
CREATE OR REPLACE FUNCTION dbms_java_remove_output_to_sql(id varchar2) RETURNS varchar2
|
AS 'dbms_java_remove_output_to_sql' LANGUAGE internal;
|
CREATE OR REPLACE FUNCTION dbms_java_enable_output_to_sql(id varchar2) RETURNS varchar2
|
AS 'dbms_java_enable_output_to_sql' LANGUAGE internal;
|
CREATE OR REPLACE FUNCTION dbms_java_disable_output_to_sql(id varchar2) RETURNS varchar2
|
AS 'dbms_java_disable_output_to_sql' LANGUAGE internal;
|
CREATE OR REPLACE FUNCTION dbms_java_query_output_to_sql(id varchar2) RETURNS varchar2
|
AS 'dbms_java_query_output_to_sql' LANGUAGE internal;
|
CREATE OR REPLACE FUNCTION dbms_java_endsession() RETURNS varchar2
|
AS 'dbms_java_endsession' LANGUAGE internal;
|
CREATE OR REPLACE FUNCTION dbms_java_endsession_and_related_state() RETURNS varchar2
|
AS 'dbms_java_endsession_and_related_state' LANGUAGE internal;
|
DO $$
|
BEGIN
|
CALL dbms_java_dropjava('rjt.OutSql');
|
EXCEPTION WHEN OTHERS THEN NULL;
|
END $$;
|
DROP FUNCTION IF EXISTS rjt_out_hi();
|
NOTICE: function rjt_out_hi() does not exist, skipping
|
DROP FUNCTION IF EXISTS rjt_out_err();
|
NOTICE: function rjt_out_err() does not exist, skipping
|
DROP TABLE IF EXISTS rjt_out_sql;
|
NOTICE: table "rjt_out_sql" does not exist, skipping
|
DO $$
|
BEGIN
|
PERFORM dbms_java_remove_output_to_sql('rjt.sql');
|
PERFORM dbms_java_remove_output_to_sql('rjt.rep');
|
PERFORM dbms_java_remove_output_to_sql('rjt.std');
|
PERFORM dbms_java_remove_output_to_sql('rjt.seg');
|
PERFORM dbms_java_endsession_and_related_state();
|
END $$;
|
CREATE TABLE rjt_out_sql (txt varchar2(200));
|
CALL dbms_java_create_source('rjt.OutSql', $src$
|
package rjt;
|
public class OutSql {
|
public static String hi() {
|
System.out.println("hello-sql");
|
return "ok";
|
}
|
public static String err() {
|
System.err.println("hello-err");
|
return "ok";
|
}
|
}
|
$src$, current_schema()::varchar2, true);
|
CREATE FUNCTION rjt_out_hi() RETURNS varchar2
|
LANGUAGE java AS 'rjt.OutSql.hi() return java.lang.String';
|
CREATE FUNCTION rjt_out_err() RETURNS varchar2
|
LANGUAGE java AS 'rjt.OutSql.err() return java.lang.String';
|
DO $$
|
DECLARE
|
msg text;
|
buf text;
|
got text;
|
n int;
|
BEGIN
|
RAISE NOTICE 'Expected: NULL|ok|hello-sql|1';
|
DELETE FROM rjt_out_sql;
|
msg := dbms_java_set_output_to_sql(
|
'rjt.sql',
|
'insert into rjt_out_sql(txt) values (:1)',
|
'TEXT');
|
BEGIN
|
buf := rjt_out_hi();
|
EXCEPTION
|
WHEN OTHERS THEN
|
buf := SQLERRM;
|
END;
|
SELECT COUNT(*) INTO n FROM rjt_out_sql;
|
SELECT coalesce(MAX(rjt_out_sql.txt), 'NONE') INTO got FROM rjt_out_sql;
|
RAISE NOTICE 'Actual: %', coalesce(msg, 'NULL') || '|' || coalesce(buf, 'NULL')
|
|| '|' || got || '|' || n;
|
END $$;
|
NOTICE: Expected: NULL|ok|hello-sql|1
|
NOTICE: Actual: NULL|ok|hello-sql|1
|
DO $$
|
DECLARE
|
msg text;
|
BEGIN
|
RAISE NOTICE 'Expected: Description of output to sql specification named rjt.sql| output from STDOUT and STDERR no max line length|default statement: insert into rjt_out_sql(txt) values (:1)|bindings: TEXT|';
|
msg := dbms_java_query_output_to_sql('rjt.sql');
|
RAISE NOTICE 'Actual: %', coalesce(replace(replace(substr(msg, 1, 300), chr(10), '|'), chr(13), ''), 'NULL');
|
END $$;
|
NOTICE: Expected: Description of output to sql specification named rjt.sql| output from STDOUT and STDERR no max line length|default statement: insert into rjt_out_sql(txt) values (:1)|bindings: TEXT|
|
NOTICE: Actual: Description of output to sql specification named rjt.sql| output from STDOUT and STDERR no max line length|default statement: insert into rjt_out_sql(txt) values (:1)|bindings: TEXT|
|
DO $$
|
DECLARE
|
msg text;
|
n int;
|
BEGIN
|
RAISE NOTICE 'Expected: NULL|0';
|
DELETE FROM rjt_out_sql;
|
msg := dbms_java_disable_output_to_sql('rjt.sql');
|
BEGIN
|
PERFORM rjt_out_hi();
|
EXCEPTION
|
WHEN OTHERS THEN
|
msg := coalesce(msg, '') || ' call=' || SQLERRM;
|
END;
|
SELECT COUNT(*) INTO n FROM rjt_out_sql;
|
RAISE NOTICE 'Actual: %', coalesce(msg, 'NULL') || '|' || n;
|
END $$;
|
NOTICE: Expected: NULL|0
|
NOTICE: Actual: NULL|0
|
DO $$
|
DECLARE
|
msg text;
|
n int;
|
BEGIN
|
RAISE NOTICE 'Expected: NULL|1';
|
DELETE FROM rjt_out_sql;
|
msg := dbms_java_enable_output_to_sql('rjt.sql');
|
BEGIN
|
PERFORM rjt_out_hi();
|
EXCEPTION
|
WHEN OTHERS THEN
|
msg := coalesce(msg, 'NULL') || ' call=' || SQLERRM;
|
END;
|
SELECT COUNT(*) INTO n FROM rjt_out_sql;
|
RAISE NOTICE 'Actual: %', coalesce(msg, 'NULL') || '|' || n;
|
END $$;
|
NOTICE: Expected: NULL|1
|
NOTICE: Actual: NULL|1
|
DO $$
|
DECLARE
|
q text;
|
BEGIN
|
RAISE NOTICE 'Expected: Description of output to sql specification named rjt.sql| output from STDOUT and STDERR no max line length|default statement: insert into rjt_out_sql(txt) values (:1)|bindings: TEXT|';
|
PERFORM dbms_java_endsession();
|
q := dbms_java_query_output_to_sql('rjt.sql');
|
RAISE NOTICE 'Actual: %', coalesce(replace(replace(substr(q, 1, 200), chr(10), '|'), chr(13), ''), 'NULL');
|
END $$;
|
NOTICE: Expected: Description of output to sql specification named rjt.sql| output from STDOUT and STDERR no max line length|default statement: insert into rjt_out_sql(txt) values (:1)|bindings: TEXT|
|
NOTICE: Actual: Description of output to sql specification named rjt.sql| output from STDOUT and STDERR no max line length|default statement: insert into rjt_out_sql(txt) values (:1)|bindings: TEXT|
|
DO $$
|
DECLARE
|
q text;
|
BEGIN
|
RAISE NOTICE 'Expected: output to sql specification "rjt.sql" not found';
|
PERFORM dbms_java_endsession_and_related_state();
|
q := dbms_java_query_output_to_sql('rjt.sql');
|
RAISE NOTICE 'Actual: %', coalesce(replace(replace(substr(q, 1, 200), chr(10), '|'), chr(13), ''), 'NULL');
|
END $$;
|
NOTICE: Expected: output to sql specification "rjt.sql" not found
|
NOTICE: Actual: output to sql specification "rjt.sql" not found
|
DO $$
|
DECLARE
|
msg text;
|
q text;
|
BEGIN
|
RAISE NOTICE 'Expected: NULL|output to sql specification "rjt.sql" not found';
|
msg := dbms_java_set_output_to_sql(
|
'rjt.sql',
|
'insert into rjt_out_sql(txt) values (:1)',
|
'TEXT');
|
msg := dbms_java_remove_output_to_sql('rjt.sql');
|
q := dbms_java_query_output_to_sql('rjt.sql');
|
RAISE NOTICE 'Actual: %', coalesce(msg, 'NULL') || '|'
|
|| coalesce(replace(replace(substr(q, 1, 160), chr(10), '|'), chr(13), ''), 'NULL');
|
END $$;
|
NOTICE: Expected: NULL|output to sql specification "rjt.sql" not found
|
NOTICE: Actual: NULL|output to sql specification "rjt.sql" not found
|
DO $$
|
DECLARE
|
a text;
|
b text;
|
c text;
|
d text;
|
BEGIN
|
RAISE NOTICE 'Expected: output to sql specification "rjt.missing" not found|output to sql specification "rjt.missing" not found|output to sql specification "rjt.missing" not found|output to sql specification "rjt.missing" not found';
|
a := dbms_java_remove_output_to_sql('rjt.missing');
|
b := dbms_java_enable_output_to_sql('rjt.missing');
|
c := dbms_java_disable_output_to_sql('rjt.missing');
|
d := dbms_java_query_output_to_sql('rjt.missing');
|
RAISE NOTICE 'Actual: %', coalesce(a, 'NULL') || '|' || coalesce(b, 'NULL')
|
|| '|' || coalesce(c, 'NULL') || '|' || coalesce(d, 'NULL');
|
END $$;
|
NOTICE: Expected: output to sql specification "rjt.missing" not found|output to sql specification "rjt.missing" not found|output to sql specification "rjt.missing" not found|output to sql specification "rjt.missing" not found
|
NOTICE: Actual: output to sql specification "rjt.missing" not found|output to sql specification "rjt.missing" not found|output to sql specification "rjt.missing" not found|output to sql specification "rjt.missing" not found
|
DO $$
|
DECLARE
|
msg text;
|
BEGIN
|
RAISE NOTICE 'Expected: ID must not be null';
|
BEGIN
|
msg := dbms_java_set_output_to_sql(NULL, 'insert into rjt_out_sql(txt) values (:1)', 'TEXT');
|
EXCEPTION
|
WHEN OTHERS THEN
|
msg := SQLERRM;
|
END;
|
RAISE NOTICE 'Actual: %', coalesce(replace(replace(substr(msg, 1, 180), chr(10), '|'), chr(13), ''), 'NULL');
|
END $$;
|
NOTICE: Expected: ID must not be null
|
NOTICE: Actual: ID must not be null
|
DO $$
|
DECLARE
|
msg text;
|
BEGIN
|
RAISE NOTICE 'Expected: ID must not be null';
|
BEGIN
|
msg := dbms_java_set_output_to_sql('', 'insert into rjt_out_sql(txt) values (:1)', 'TEXT');
|
EXCEPTION
|
WHEN OTHERS THEN
|
msg := SQLERRM;
|
END;
|
RAISE NOTICE 'Actual: %', coalesce(replace(replace(substr(msg, 1, 180), chr(10), '|'), chr(13), ''), 'NULL');
|
END $$;
|
NOTICE: Expected: ID must not be null
|
NOTICE: Actual: ID must not be null
|
DO $$
|
DECLARE
|
msg text;
|
BEGIN
|
RAISE NOTICE 'Expected: specification with this name already exists';
|
msg := dbms_java_set_output_to_sql(
|
'rjt.rep',
|
'insert into rjt_out_sql(txt) values (:1)',
|
'TEXT');
|
msg := dbms_java_set_output_to_sql(
|
'rjt.rep',
|
'insert into rjt_out_sql(txt) values (:1)',
|
'TEXT',
|
NULL, NULL, NULL, NULL, 0, 0);
|
RAISE NOTICE 'Actual: %', coalesce(replace(replace(substr(msg, 1, 180), chr(10), '|'), chr(13), ''), 'NULL');
|
END $$;
|
NOTICE: Expected: specification with this name already exists
|
NOTICE: Actual: specification with this name already exists
|
DO $$
|
DECLARE
|
msg text;
|
n int;
|
got text;
|
BEGIN
|
RAISE NOTICE 'Expected: NULL|1|hello-err';
|
DELETE FROM rjt_out_sql;
|
msg := dbms_java_remove_output_to_sql('rjt.sql');
|
msg := dbms_java_remove_output_to_sql('rjt.rep');
|
msg := dbms_java_set_output_to_sql(
|
id => 'rjt.std',
|
stmt => 'insert into rjt_out_sql(txt) values (:1)',
|
bindings => 'TEXT',
|
from_stdout => 0);
|
BEGIN
|
PERFORM rjt_out_hi();
|
PERFORM rjt_out_err();
|
EXCEPTION
|
WHEN OTHERS THEN
|
msg := coalesce(msg, 'NULL') || ' call=' || SQLERRM;
|
END;
|
SELECT COUNT(*) INTO n FROM rjt_out_sql;
|
SELECT coalesce(string_agg(rjt_out_sql.txt, ',' ORDER BY ctid), 'NONE') INTO got FROM rjt_out_sql;
|
RAISE NOTICE 'Actual: %', coalesce(msg, 'NULL') || '|' || n || '|' || got;
|
END $$;
|
NOTICE: Expected: NULL|1|hello-err
|
NOTICE: Actual: NULL|1|hello-err
|
DO $$
|
DECLARE
|
msg text;
|
got text;
|
BEGIN
|
RAISE NOTICE 'Expected: NULL|hell,o-sq,l';
|
DELETE FROM rjt_out_sql;
|
msg := dbms_java_remove_output_to_sql('rjt.std');
|
msg := dbms_java_remove_output_to_sql('rjt.rep');
|
msg := dbms_java_set_output_to_sql(
|
'rjt.seg',
|
'insert into rjt_out_sql(txt) values (:1)',
|
'TEXT',
|
NULL, NULL, NULL, NULL,
|
4);
|
BEGIN
|
PERFORM rjt_out_hi();
|
EXCEPTION
|
WHEN OTHERS THEN
|
msg := coalesce(msg, 'NULL') || ' call=' || SQLERRM;
|
END;
|
SELECT coalesce(string_agg(rjt_out_sql.txt, ',' ORDER BY ctid), 'NONE') INTO got FROM rjt_out_sql;
|
RAISE NOTICE 'Actual: %', coalesce(msg, 'NULL') || '|' || got;
|
END $$;
|
NOTICE: Expected: NULL|hell,o-sq,l
|
NOTICE: Actual: NULL|hell,o-sq,l
|
DO $$
|
BEGIN
|
PERFORM dbms_java_remove_output_to_sql('rjt.sql');
|
PERFORM dbms_java_remove_output_to_sql('rjt.rep');
|
PERFORM dbms_java_remove_output_to_sql('rjt.std');
|
PERFORM dbms_java_remove_output_to_sql('rjt.seg');
|
END $$;
|
DROP FUNCTION rjt_out_hi();
|
DROP FUNCTION rjt_out_err();
|
DROP TABLE rjt_out_sql;
|
CALL dbms_java_dropjava('rjt.OutSql');
|