I am getting error while executing a sql file name sample_script.sql
from my java code. It is a spring application from where I am trying to run the script through java class ScriptRunner
because I wanted to run the script automatically.
sample_script.sql:
CREATE OR REPLACE FUNCTION core.getValue (id bigint, field varchar)
RETURNS text
AS $$
DECLARE json_object json;
DECLARE item json;
DECLARE val text;
BEGIN
SELECT E.json::json into json_object from customer E where E.id = id;
FOR item IN SELECT * FROM json_array_elements((json_object->>'name')::json)
LOOP
IF (item->>'data') = field
THEN
val = (item->>'values')::json->>0;
END IF;
END LOOP;
return val;
END;
$$ LANGUAGE 'plpgsql';
It is running ok from PGAdmin but when I run this file from my spring project like following it is giving error.
Java Code segment:
ScriptRunner sr = new ScriptRunner(con, false, false);
Reader reader = new BufferedReader(new FileReader("sample_script.sql"));
sr.runScript(reader);
Error Log:
Error executing: CREATE OR REPLACE FUNCTION core.getValue (id bigint, field varchar) RETURNS text AS $$ DECLARE json_object json
org.postgresql.util.PSQLException: ERROR: unterminated dollar-quoted string at or near "$$ DECLARE json_object json "
Position: 91
Also while executing the script it is removing ;
from the script like following:
CREATE OR REPLACE FUNCTION core.getValue (id bigint, field varchar) RETURNS text AS $$ DECLARE json_object json
DECLARE item json
DECLARE val text
BEGIN SELECT E.json::json into json_object from customer E where E.id = id
FOR item IN SELECT * FROM json_array_elements((json_object->>'name')::json) LOOP IF (item->>'data') = field THEN val = (item->>'values')::json->>0
END IF
END LOOP
return val
END
$$ LANGUAGE 'plpgsql'
Can anyone explain why this is happening and how to resolve it?
Note: I have seen this but it does not help on this issue.