Answers for "postgres check validation"

SQL
0

psql check if sql script is valid

$ pgsanity good1.sql good2.sql bad.sql
bad.sql: line 1: ERROR: syntax error at or near "bogus_token"

$ find -name '*.sql' | xargs pgsanity
./sql/bad1.sql: line 59: ERROR: syntax error at or near ";"
./sql/bad2.sql: line 41: ERROR: syntax error at or near "insert"
./sql/bad3.sql: line 57: ERROR: syntax error at or near "update"
Posted by: Guest on May-06-2021
0

check psql validity function

CREATE OR REPLACE FUNCTION is_sql(sql text) returns boolean
    language plpgsql
AS
$$
DECLARE
    exception_message text;
    exception_context text;
BEGIN
    BEGIN
        EXECUTE E'DO $IS_SQL$ BEGINnRETURN;n' || trim(trailing E'; rn' from sql) || E';nEND; $IS_SQL$;';
    EXCEPTION WHEN syntax_error THEN
        GET STACKED DIAGNOSTICS
            exception_message = MESSAGE_TEXT,
            exception_context = PG_EXCEPTION_CONTEXT;
        RAISE NOTICE '%', exception_context;
        RAISE NOTICE '%', exception_message;
        RETURN FALSE;
    END;
    RETURN TRUE;
END
$$;

-- TEST
SELECT is_sql('SELECTx'), is_sql('SELECT x');
Posted by: Guest on May-06-2021

Code answers related to "SQL"

Browse Popular Code Answers by Language