check psql validity function

Solutions on MaxInterview for check psql validity function by the best coders in the world

showing results for - "check psql validity function"
Khalil
23 Feb 2019
1CREATE OR REPLACE FUNCTION is_sql(sql text) returns boolean
2    language plpgsql
3AS
4$$
5DECLARE
6    exception_message text;
7    exception_context text;
8BEGIN
9    BEGIN
10        EXECUTE E'DO $IS_SQL$ BEGIN\nRETURN;\n' || trim(trailing E'; \r\n' from sql) || E';\nEND; $IS_SQL$;';
11    EXCEPTION WHEN syntax_error THEN
12        GET STACKED DIAGNOSTICS
13            exception_message = MESSAGE_TEXT,
14            exception_context = PG_EXCEPTION_CONTEXT;
15        RAISE NOTICE '%', exception_context;
16        RAISE NOTICE '%', exception_message;
17        RETURN FALSE;
18    END;
19    RETURN TRUE;
20END
21$$;
22
23-- TEST
24SELECT is_sql('SELECTx'), is_sql('SELECT x');
25