Skip to content

Commit b180345

Browse files
committed
SQL: extract types in "create type t"
https://www.postgresql.jp/document/17/html/sql-createtype.html Signed-off-by: Masatake YAMATO <yamato@redhat.com>
1 parent 1bccbf2 commit b180345

File tree

7 files changed

+109
-1
lines changed

7 files changed

+109
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
--sort=no
22
--fields=+S
33
--extras=+g
4+
--kinds-SQL=+r

Units/parser-sql.r/funcions.d/expected.tags

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
evr_array_item input.sql /^create type evr_array_item as ($/;" r
2+
n input.sql /^ n NUMERIC,$/;" E record:evr_array_item
3+
s input.sql /^ s TEXT$/;" E record:evr_array_item
4+
evr_t input.sql /^create type evr_t as ($/;" r
5+
epoch input.sql /^ epoch INT,$/;" E record:evr_t
6+
version input.sql /^ version evr_array_item[],$/;" E record:evr_t
7+
release input.sql /^ release evr_array_item[]$/;" E record:evr_t
18
evr_trigger input.sql /^CREATE FUNCTION evr_trigger() RETURNS trigger AS $\$$/;" f typeref:typename:trigger signature:()
29
empty input.sql /^create or replace FUNCTION empty(t TEXT)$/;" f typeref:typename:BOOLEAN signature:(t TEXT)
310
isalpha input.sql /^create or replace FUNCTION isalpha(ch CHAR)$/;" f typeref:typename:BOOLEAN signature:(ch CHAR)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--sort=no
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
compfoo input.sql /^CREATE TYPE compfoo AS (f1 int, f2 text);$/;" k
2+
f1 input.sql /^CREATE TYPE compfoo AS (f1 int, f2 text);$/;" E record:compfoo
3+
f2 input.sql /^CREATE TYPE compfoo AS (f1 int, f2 text);$/;" E record:compfoo
4+
getfoo input.sql /^CREATE FUNCTION getfoo() RETURNS SETOF compfoo AS $\$$/;" f typeref:typename:SETOF compfoo
5+
bug_status input.sql /^CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');$/;" k
6+
bug input.sql /^CREATE TABLE bug ($/;" t
7+
id input.sql /^ id serial,$/;" E table:bug
8+
description input.sql /^ description text,$/;" E table:bug
9+
status input.sql /^ status bug_status$/;" E table:bug
10+
float8_range input.sql /^CREATE TYPE float8_range AS RANGE (subtype = float8, subtype_diff = float8mi);$/;" k
11+
my_box_in_function input.sql /^CREATE FUNCTION my_box_in_function(cstring) RETURNS box AS $\$$/;" f typeref:typename:box
12+
my_box_out_function input.sql /^CREATE FUNCTION my_box_out_function(box) RETURNS cstring AS $\$$/;" f typeref:typename:cstring
13+
box input.sql /^CREATE TYPE box ($/;" k
14+
myboxes input.sql /^CREATE TABLE myboxes ($/;" t
15+
id input.sql /^ id integer,$/;" E table:myboxes
16+
description input.sql /^ description box$/;" E table:myboxes
17+
bigobj input.sql /^CREATE TYPE bigobj ($/;" k
18+
big_objs input.sql /^CREATE TABLE big_objs ($/;" t
19+
id input.sql /^ id integer,$/;" E table:big_objs
20+
obj input.sql /^ obj bigobj$/;" E table:big_objs
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- Taken from https://www.postgresql.jp/document/17/html/sql-createtype.html
2+
3+
CREATE TYPE compfoo AS (f1 int, f2 text);
4+
5+
CREATE FUNCTION getfoo() RETURNS SETOF compfoo AS $$
6+
SELECT fooid, fooname FROM foo
7+
$$ LANGUAGE SQL;
8+
9+
CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');
10+
11+
CREATE TABLE bug (
12+
id serial,
13+
description text,
14+
status bug_status
15+
);
16+
17+
CREATE TYPE float8_range AS RANGE (subtype = float8, subtype_diff = float8mi);
18+
19+
CREATE TYPE box;
20+
21+
CREATE FUNCTION my_box_in_function(cstring) RETURNS box AS $$
22+
... -- fix me
23+
$$ LANGUAGE SQL;
24+
25+
CREATE FUNCTION my_box_out_function(box) RETURNS cstring AS $$
26+
... -- fix me
27+
$$ LANGUAGE SQL;
28+
29+
CREATE TYPE box (
30+
INTERNALLENGTH = 16,
31+
INPUT = my_box_in_function,
32+
OUTPUT = my_box_out_function
33+
);
34+
35+
CREATE TABLE myboxes (
36+
id integer,
37+
description box
38+
);
39+
40+
CREATE TYPE bigobj (
41+
INPUT = lo_filein, OUTPUT = lo_fileout,
42+
INTERNALLENGTH = VARIABLE
43+
);
44+
CREATE TABLE big_objs (
45+
id integer,
46+
obj bigobj
47+
);

docs/news/HEAD.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ Clojure:
6666
"``clojure.core``". For the other namespaces, the parser emits
6767
"``unknown``" kind tags.
6868

69+
SQL:
70+
71+
* Add ``type`` kind to extract ``t`` in "``CREATE TYPE t ...``".
72+
6973
New parsers
7074
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7175
The following parsers have been added:

parsers/sql.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ typedef enum {
240240
SQLTAG_EVENT,
241241
SQLTAG_FUNCTION,
242242
SQLTAG_INDEX,
243+
SQLTAG_TYPE,
243244
SQLTAG_LOCAL_VARIABLE,
244245
SQLTAG_SYNONYM,
245246
SQLTAG_PROCEDURE,
@@ -270,6 +271,8 @@ static kindDefinition SqlKinds [] = {
270271
{ true, 'e', "event", "events" },
271272
{ true, 'f', "function", "functions" },
272273
{ true, 'i', "index", "indexes" },
274+
{ true, 'k', "type", "types",
275+
.version = 1 },
273276
{ false, 'l', "local", "local variables" },
274277
{ true, 'n', "synonym", "synonyms" },
275278
{ true, 'p', "procedure", "procedures" },
@@ -1714,34 +1717,59 @@ static void parseType (tokenInfo *const token)
17141717
readToken (name);
17151718
if (isType (name, TOKEN_IDENTIFIER))
17161719
{
1720+
int r = makeSqlTag (name, SQLTAG_TYPE);
1721+
17171722
readToken (token);
17181723
if (isKeyword (token, KEYWORD_is))
17191724
{
17201725
readToken (token);
1721-
switch (token->keyword)
1726+
if (isType (token, TOKEN_KEYWORD))
17221727
{
1728+
switch (token->keyword)
1729+
{
17231730
case KEYWORD_record:
17241731
case KEYWORD_object:
1732+
markCorkEntryAsPlaceholder(r, true);
17251733
makeSqlTag (name, SQLTAG_RECORD);
17261734
addToScope (token, name->string, SQLTAG_RECORD);
17271735
parseRecord (token);
17281736
break;
17291737

17301738
case KEYWORD_table:
1739+
markCorkEntryAsPlaceholder(r, true);
17311740
makeSqlTag (name, SQLTAG_TABLE);
17321741
break;
17331742

17341743
case KEYWORD_ref:
17351744
readToken (token);
17361745
if (isKeyword (token, KEYWORD_cursor))
1746+
{
1747+
markCorkEntryAsPlaceholder(r, true);
17371748
makeSqlTag (name, SQLTAG_CURSOR);
1749+
}
17381750
break;
17391751

17401752
default: break;
1753+
/* TODO: PostgreSQL can take 'enum (' and 'range (' here. */
1754+
}
1755+
}
1756+
else if (isType (token, TOKEN_OPEN_PAREN))
1757+
{
1758+
if (isLanguageKindEnabled (Lang_sql, SQLTAG_RECORD))
1759+
{
1760+
/* Uses type kind only if the record kind is disable. */
1761+
markCorkEntryAsPlaceholder(r, true);
1762+
makeSqlTag (name, SQLTAG_RECORD);
1763+
}
1764+
addToScope (token, name->string, SQLTAG_RECORD);
1765+
parseRecord (token);
17411766
}
17421767
vStringClear (token->scope);
17431768
token->scopeKind = SQLTAG_COUNT;
17441769
}
1770+
else if (isType (token, TOKEN_SEMICOLON))
1771+
/* Type declaration (or shell type); don't make a "type" tag for it. */
1772+
markCorkEntryAsPlaceholder(r, true);
17451773
}
17461774
vStringCopy(token->scope, saveScope);
17471775
token->scopeKind = saveScopeKind;

0 commit comments

Comments
 (0)