forked from rin-nas/postgresql-patterns-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_sql.sql
More file actions
24 lines (23 loc) · 686 Bytes
/
Copy pathis_sql.sql
File metadata and controls
24 lines (23 loc) · 686 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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$ BEGIN\nRETURN;\n' || trim(trailing E'; \r\n' 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
$$;
-- проверяем, что работает
SELECT is_sql('SELECTx'), is_sql('SELECT x');