xref: /Universal-ctags/docs/man/ctags-lang-sql.7.rst (revision 49ac2085b4ee182d46990ef9a258c45aedb59788)
1.. _ctags-lang-sql(7):
2
3==============================================================
4ctags-lang-sql
5==============================================================
6
7The man page of the SQL parser for Universal Ctags
8
9:Version: 5.9.0
10:Manual group: Universal Ctags
11:Manual section: 7
12
13SYNOPSIS
14--------
15|	**ctags** ... [--extras={guest}] --languages=+SQL ...
16
17
18DESCRIPTION
19-----------
20The SQL parser supports various SQL dialects. PostgreSQL is one of them.
21
22PostgreSQL allows user-defined functions to be written in other
23languages (*procedural languages*) besides SQL and C [PL]_.
24
25The SQL parser makes tags for language objects in the user-defined
26functions written in the procedural languages if the ``guest`` extra
27is enabled.
28
29The SQL parser looks for a token coming after ``LANGUAGE`` keyword in
30the source code to choose a proper guest parser.
31
32.. code-block:: SQL
33
34   ... LANGUAGE plpythonu AS '... user-defined function ' ...
35   ... AS $$ user-defined function $$ LANGUAGE plv8 ...
36
37In the above examples, ``plpythonu`` and ``plv8`` are the names of
38procedural languages. The SQL parser trims `pl` at the start and `u`
39at the end of the name before finding a ctags parser.  For
40``plpythonu`` and ``plv8``, the SQL parser extracts ``python`` and
41``v8`` as the candidates of guest parsers.
42
43For ``plpythonu``, ctags can run its Python parser.  ctags doesn't
44have a parser named ``v8``. However, the JavaScript parser in ctags has
45``v8`` as an alias. So ctags can run the JavaScript parser as the
46guest parser for ``plv8``.
47
48EXAMPLES
49--------
50tagging code including a user-defined function in a string literal [GH3006]_:
51
52"input.sql"
53
54.. code-block:: SQL
55
56	CREATE OR REPLACE FUNCTION fun1() RETURNS VARCHAR AS '
57	DECLARE
58		test1_var1 VARCHAR(64) := $$ABC$$;
59		test1_var2 VARCHAR(64) := $xyz$XYZ$xyz$;
60		test1_var3     INTEGER := 1;
61	BEGIN
62		RETURN  TO_CHAR(test_var3, ''000'') || test1_var1 || test1_var2;
63	END;
64	' LANGUAGE plpgsql;
65
66"output.tags"
67with "--options=NONE -o - --sort=no --extras=+{guest} input.sql"
68
69.. code-block:: tags
70
71	fun1	input.sql	/^CREATE OR REPLACE FUNCTION fun1() RETURNS VARCHAR AS '$/;"	f
72	test1_var1	input.sql	/^	test1_var1 VARCHAR(64) := $$ABC$$;$/;"	v
73	test1_var2	input.sql	/^	test1_var2 VARCHAR(64) := $xyz$XYZ$xyz$;$/;"	v
74	test1_var3	input.sql	/^	test1_var3     INTEGER := 1;$/;"	v
75
76tagging code including a user-defined function in a dollar quote [GH3006]_:
77
78"input.sql"
79
80.. code-block:: SQL
81
82	CREATE OR REPLACE FUNCTION fun2() RETURNS VARCHAR LANGUAGE plpgsql AS $$
83	DECLARE
84		test2_var1 VARCHAR(64) := 'ABC2';
85		test2_var2 VARCHAR(64) := 'XYZ2';
86		test2_var3        INTEGER := 2;
87	BEGIN
88		RETURN  TO_CHAR(test2_var3, '000') || test2_var1 || test2_var2;
89	END;
90	$$;
91
92"output.tags"
93with "--options=NONE -o - --sort=no --extras=+{guest} input.sql"
94
95.. code-block:: tags
96
97	fun2	input.sql	/^CREATE OR REPLACE FUNCTION fun2() RETURNS VARCHAR LANGUAGE plpgsql AS $\$$/;"	f
98	test2_var1	input.sql	/^	test2_var1 VARCHAR(64) := 'ABC2';$/;"	v
99	test2_var2	input.sql	/^	test2_var2 VARCHAR(64) := 'XYZ2';$/;"	v
100	test2_var3	input.sql	/^	test2_var3        INTEGER := 2;$/;"	v
101
102tagging code including a user-defined written in JavaScript:
103
104.. code-block:: SQL
105
106	-- Derived from https://github.com/plv8/plv8/blob/r3.0alpha/sql/plv8.sql
107	CREATE FUNCTION test(keys text[], vals text[]) RETURNS text AS
108	$$
109		var o = {};
110		for (var i = 0; i < keys.length; i++)
111			o[keys[i]] = vals[i];
112		return JSON.stringify(o);
113	$$
114	LANGUAGE plv8 IMMUTABLE STRICT;
115
116"output.tags"
117with "--options=NONE -o - --sort=no --extras=+{guest} input.sql"
118
119.. code-block:: tags
120
121	test	input.sql	/^CREATE FUNCTION test(keys text[], vals text[]) RETURNS text AS$/;"	f
122	o	input.sql	/^	var o = {};$/;"	v
123
124KNOWN BUGS
125----------
126Escape sequences (`''`) in a string literal may make a guest parser confused.
127
128SEE ALSO
129--------
130:ref:`ctags(1) <ctags(1)>`, :ref:`ctags-client-tools(7) <ctags-client-tools(7)>`
131
132REFERENCES
133----------
134
135.. [PL] PostgreSQL 9.5.25 Documentation, "Chapter 39. Procedural Languages", https://www.postgresql.org/docs/9.5/xplang.html
136
137.. [GH3006] @bagl's comment submitted to https://github.com/universal-ctags/ctags/issues/3006
138