Skip to content

Commit

Permalink
Merge pull request #200 from hslightdb/bugfixserveroutput
Browse files Browse the repository at this point in the history
fix dbms_output.serveroutput(true) clear the buffer.
  • Loading branch information
okbob authored Oct 26, 2022
2 parents 92be524 + e85da17 commit 1307750
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 87 deletions.
137 changes: 97 additions & 40 deletions expected/dbms_output.out
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ DECLARE
buff VARCHAR(20);
stts INTEGER;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 2');
SELECT INTO buff,stts line,status FROM DBMS_OUTPUT.GET_LINE();
Expand Down Expand Up @@ -165,9 +164,8 @@ DECLARE
buff VARCHAR(20);
stts INTEGER;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 2');
SELECT INTO buff,stts line,status FROM DBMS_OUTPUT.GET_LINE();
Expand Down Expand Up @@ -207,9 +205,9 @@ DECLARE
buff VARCHAR(20);
stts INTEGER;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();

PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 2');
SELECT INTO buff,stts line,status FROM DBMS_OUTPUT.GET_LINE();
Expand Down Expand Up @@ -246,9 +244,9 @@ DECLARE
buff VARCHAR(20);
stts INTEGER;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();

PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 2');
SELECT INTO buff,stts line,status FROM DBMS_OUTPUT.GET_LINE();
Expand Down Expand Up @@ -285,9 +283,8 @@ DECLARE
buff VARCHAR(20);
stts INTEGER;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1
');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 2');
Expand Down Expand Up @@ -322,9 +319,9 @@ DECLARE
buff VARCHAR(20);
stts INTEGER;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();

PERFORM DBMS_OUTPUT.PUT_LINE ('ORA
F
CE');
Expand Down Expand Up @@ -358,9 +355,8 @@ DECLARE
buff3 VARCHAR(20);
stts INTEGER := 10;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 2');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 3');
Expand Down Expand Up @@ -400,9 +396,8 @@ DECLARE
buff2 VARCHAR(20);
stts INTEGER := 2;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 2');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 3');
Expand Down Expand Up @@ -438,9 +433,8 @@ DECLARE
buff VARCHAR(20);
stts INTEGER := 1;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 2');
SELECT INTO buff,stts lines[1],numlines FROM DBMS_OUTPUT.GET_LINES(stts);
Expand Down Expand Up @@ -477,9 +471,8 @@ DECLARE
buff VARCHAR(20);
stts INTEGER := 1;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 2');
SELECT INTO buff,stts lines[1],numlines FROM DBMS_OUTPUT.GET_LINES(stts);
Expand Down Expand Up @@ -516,9 +509,8 @@ DECLARE
buff VARCHAR(20);
stts INTEGER := 1;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 2');
SELECT INTO buff,stts lines[1],numlines FROM DBMS_OUTPUT.GET_LINES(stts);
Expand Down Expand Up @@ -555,9 +547,8 @@ DECLARE
buff VARCHAR(20);
stts INTEGER := 1;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORA
F
CE');
Expand Down Expand Up @@ -589,9 +580,9 @@ DECLARE
buff2 VARCHAR(20);
stts INTEGER := 10;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();

PERFORM DBMS_OUTPUT.PUT ('ORA');
PERFORM DBMS_OUTPUT.NEW_LINE();
PERFORM DBMS_OUTPUT.PUT ('FCE');
Expand Down Expand Up @@ -625,9 +616,8 @@ DECLARE
buff1 VARCHAR(3000);
stts INTEGER := 10;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.ENABLE(2000);
FOR j IN 1..1999 LOOP
PERFORM DBMS_OUTPUT.PUT ('A');
Expand Down Expand Up @@ -660,9 +650,8 @@ DECLARE
buff VARCHAR(20);
stts INTEGER;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
Expand Down Expand Up @@ -732,9 +721,8 @@ DECLARE
buff VARCHAR(20);
stts INTEGER := 10;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
SELECT INTO buff,stts lines[1],numlines FROM DBMS_OUTPUT.GET_LINES(stts);
Expand Down Expand Up @@ -764,9 +752,9 @@ DECLARE
status INTEGER;
num INTEGER := 2000;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('t');
PERFORM DBMS_OUTPUT.ENABLE();

PERFORM DBMS_OUTPUT.ENABLE(2000);
PERFORM DBMS_OUTPUT.PUT ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.NEW_LINE();
Expand Down Expand Up @@ -794,9 +782,9 @@ DECLARE
buff VARCHAR(20);
stts INTEGER;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();

PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
Expand Down Expand Up @@ -831,9 +819,9 @@ DECLARE
buff VARCHAR(20);
stts INTEGER := 10;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();

PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
Expand Down Expand Up @@ -918,9 +906,9 @@ DECLARE
buff VARCHAR(20);
stts INTEGER;
BEGIN
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('f');
PERFORM DBMS_OUTPUT.ENABLE();

PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.ENABLE();
SELECT INTO buff,stts line,status FROM DBMS_OUTPUT.GET_LINE();
Expand Down Expand Up @@ -1041,3 +1029,72 @@ ORAFCE TEST 1
(1 row)

DROP FUNCTION dbms_output_test();
-- SERVEROUTPUT [4] SERVEROUTPUT('t') get_line return null
CREATE TABLE dbms_output_test (buff VARCHAR(20), status INTEGER);
CREATE FUNCTION dbms_output_test() RETURNS VOID AS $$
DECLARE
buff VARCHAR(20);
stts INTEGER;
BEGIN
PERFORM DBMS_OUTPUT.SERVEROUTPUT ('t');
PERFORM DBMS_OUTPUT.ENABLE();
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 1');
PERFORM DBMS_OUTPUT.PUT_LINE ('ORAFCE TEST 2');
SELECT INTO buff,stts line,status FROM DBMS_OUTPUT.GET_LINE();
INSERT INTO dbms_output_test VALUES (buff, stts);
SELECT INTO buff,stts line,status FROM DBMS_OUTPUT.GET_LINE();
INSERT INTO dbms_output_test VALUES (buff, stts);
PERFORM DBMS_OUTPUT.DISABLE();
PERFORM DBMS_OUTPUT.ENABLE();
END;
$$ LANGUAGE plpgsql;
SELECT dbms_output_test();
ORAFCE TEST 1
ORAFCE TEST 2
dbms_output_test
------------------

(1 row)

SELECT * FROM dbms_output_test;
buff | status
--------+--------
<NULL> | 1
<NULL> | 1
(2 rows)

DROP TABLE dbms_output_test;
DROP FUNCTION dbms_output_test();
-- test dbms_output.serveroutput(false) clear buffer
select dbms_output.serveroutput(true);
serveroutput
--------------

(1 row)

do $$
BEGIN
PERFORM DBMS_OUTPUT.PUT_LINE('1234242');
END$$;
1234242
select dbms_output.serveroutput(false);
serveroutput
--------------

(1 row)

do $$
BEGIN
PERFORM DBMS_OUTPUT.PUT_LINE('1234242');
END$$;
select dbms_output.serveroutput(true);
serveroutput
--------------

(1 row)

do $$
BEGIN
PERFORM DBMS_OUTPUT.PUT_LINE('1234242');
END$$;
1234242
22 changes: 15 additions & 7 deletions putline.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ dbms_output_enable_internal(int32 n_buf_size)
}
}

static void
dbms_output_disable_internal()
{
if (buffer)
pfree(buffer);

buffer = NULL;
buffer_size = 0;
buffer_len = 0;
buffer_get = 0;
}

PG_FUNCTION_INFO_V1(dbms_output_enable_default);

Datum
Expand Down Expand Up @@ -206,13 +218,7 @@ PG_FUNCTION_INFO_V1(dbms_output_disable);
Datum
dbms_output_disable(PG_FUNCTION_ARGS)
{
if (buffer)
pfree(buffer);

buffer = NULL;
buffer_size = 0;
buffer_len = 0;
buffer_get = 0;
dbms_output_disable_internal();
PG_RETURN_VOID();
}

Expand All @@ -224,6 +230,8 @@ dbms_output_serveroutput(PG_FUNCTION_ARGS)
is_server_output = PG_GETARG_BOOL(0);
if (is_server_output && !buffer)
dbms_output_enable_internal(BUFSIZE_DEFAULT);
else if (!is_server_output && buffer)
dbms_output_disable_internal();
PG_RETURN_VOID();
}

Expand Down
Loading

0 comments on commit 1307750

Please sign in to comment.