Skip to content

Commit c63147d

Browse files
committed
Add a function pg_get_keywords() to let clients find out the set of keywords
known to the SQL parser. Dave Page
1 parent e3d9dce commit c63147d

File tree

7 files changed

+109
-9
lines changed

7 files changed

+109
-9
lines changed

doc/src/sgml/func.sgml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.437 2008/05/19 18:08:15 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.438 2008/07/03 20:58:46 tgl Exp $ -->
22

33
<chapter id="functions">
44
<title>Functions and Operators</title>
@@ -11484,6 +11484,10 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1148411484
<primary>format_type</primary>
1148511485
</indexterm>
1148611486

11487+
<indexterm>
11488+
<primary>pg_get_keywords</primary>
11489+
</indexterm>
11490+
1148711491
<indexterm>
1148811492
<primary>pg_get_viewdef</primary>
1148911493
</indexterm>
@@ -11538,6 +11542,11 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1153811542
<entry><type>text</type></entry>
1153911543
<entry>get SQL name of a data type</entry>
1154011544
</row>
11545+
<row>
11546+
<entry><literal><function>pg_get_keywords</function>()</literal></entry>
11547+
<entry><type>setof record</type></entry>
11548+
<entry>get list of SQL keywords and their categories</entry>
11549+
</row>
1154111550
<row>
1154211551
<entry><literal><function>pg_get_constraintdef</function>(<parameter>constraint_oid</parameter>)</literal></entry>
1154311552
<entry><type>text</type></entry>
@@ -11633,6 +11642,16 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1163311642
for the type modifier if no specific modifier is known.
1163411643
</para>
1163511644

11645+
<para>
11646+
<function>pg_get_keywords</function> returns a set of records describing
11647+
the SQL keywords recognized by the server. The <structfield>word</> column
11648+
contains the keyword. The <structfield>catcode</> column contains a
11649+
category code: <literal>U</> for unreserved, <literal>C</> for column name,
11650+
<literal>T</> for type or function name, or <literal>R</> for reserved.
11651+
The <structfield>catdesc</> column contains a possibly-localized string
11652+
describing the category.
11653+
</para>
11654+
1163611655
<para>
1163711656
<function>pg_get_constraintdef</function>,
1163811657
<function>pg_get_indexdef</function>, <function>pg_get_ruledef</function>,

src/backend/parser/keywords.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/keywords.c,v 1.197 2008/05/21 19:51:01 meskes Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/keywords.c,v 1.198 2008/07/03 20:58:46 tgl Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -41,7 +41,7 @@
4141
* !!WARNING!!: This list must be sorted by ASCII name, because binary
4242
* search is used to locate entries.
4343
*/
44-
static const ScanKeyword ScanKeywords[] = {
44+
const ScanKeyword ScanKeywords[] = {
4545
/* name, value, category */
4646
{"abort", ABORT_P, UNRESERVED_KEYWORD},
4747
{"absolute", ABSOLUTE_P, UNRESERVED_KEYWORD},
@@ -428,6 +428,9 @@ static const ScanKeyword ScanKeywords[] = {
428428
{"zone", ZONE, UNRESERVED_KEYWORD},
429429
};
430430

431+
/* End of ScanKeywords, for use elsewhere */
432+
const ScanKeyword *LastScanKeyword = endof(ScanKeywords);
433+
431434
/*
432435
* ScanKeywordLookup - see if a given word is a keyword
433436
*

src/backend/utils/adt/misc.c

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.62 2008/04/17 20:56:41 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.63 2008/07/03 20:58:46 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -20,10 +20,12 @@
2020
#include <math.h>
2121

2222
#include "access/xact.h"
23+
#include "catalog/pg_type.h"
2324
#include "catalog/pg_tablespace.h"
2425
#include "commands/dbcommands.h"
2526
#include "funcapi.h"
2627
#include "miscadmin.h"
28+
#include "parser/keywords.h"
2729
#include "postmaster/syslogger.h"
2830
#include "storage/fd.h"
2931
#include "storage/pmsignal.h"
@@ -322,3 +324,72 @@ pg_sleep(PG_FUNCTION_ARGS)
322324

323325
PG_RETURN_VOID();
324326
}
327+
328+
/* Function to return the list of grammar keywords */
329+
Datum
330+
pg_get_keywords(PG_FUNCTION_ARGS)
331+
{
332+
FuncCallContext *funcctx;
333+
334+
if (SRF_IS_FIRSTCALL())
335+
{
336+
MemoryContext oldcontext;
337+
TupleDesc tupdesc;
338+
339+
funcctx = SRF_FIRSTCALL_INIT();
340+
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
341+
342+
tupdesc = CreateTemplateTupleDesc(3, false);
343+
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "word",
344+
TEXTOID, -1, 0);
345+
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "catcode",
346+
CHAROID, -1, 0);
347+
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "catdesc",
348+
TEXTOID, -1, 0);
349+
350+
funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
351+
352+
MemoryContextSwitchTo(oldcontext);
353+
}
354+
355+
funcctx = SRF_PERCALL_SETUP();
356+
357+
if (&ScanKeywords[funcctx->call_cntr] < LastScanKeyword)
358+
{
359+
char *values[3];
360+
HeapTuple tuple;
361+
362+
/* cast-away-const is ugly but alternatives aren't much better */
363+
values[0] = (char *) ScanKeywords[funcctx->call_cntr].name;
364+
365+
switch (ScanKeywords[funcctx->call_cntr].category)
366+
{
367+
case UNRESERVED_KEYWORD:
368+
values[1] = "U";
369+
values[2] = _("Unreserved");
370+
break;
371+
case COL_NAME_KEYWORD:
372+
values[1] = "C";
373+
values[2] = _("Column name");
374+
break;
375+
case TYPE_FUNC_NAME_KEYWORD:
376+
values[1] = "T";
377+
values[2] = _("Type or function name");
378+
break;
379+
case RESERVED_KEYWORD:
380+
values[1] = "R";
381+
values[2] = _("Reserved");
382+
break;
383+
default: /* shouldn't be possible */
384+
values[1] = NULL;
385+
values[2] = NULL;
386+
break;
387+
}
388+
389+
tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
390+
391+
SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
392+
}
393+
394+
SRF_RETURN_DONE(funcctx);
395+
}

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.464 2008/06/24 17:58:27 tgl Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.465 2008/07/03 20:58:46 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200806241
56+
#define CATALOG_VERSION_NO 200807031
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.503 2008/06/17 19:10:56 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.504 2008/07/03 20:58:46 tgl Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -2288,6 +2288,9 @@ DESCR("deparse an encoded expression");
22882288
DATA(insert OID = 1665 ( pg_get_serial_sequence PGNSP PGUID 12 1 0 f f t f s 2 25 "25 25" _null_ _null_ _null_ pg_get_serial_sequence - _null_ _null_ ));
22892289
DESCR("name of sequence for a serial column");
22902290

2291+
DATA(insert OID = 1686 ( pg_get_keywords PGNSP PGUID 12 10 400 f f t t s 0 2249 "" "{25,18,25}" "{o,o,o}" "{word,catcode,catdesc}" pg_get_keywords - _null_ _null_ ));
2292+
DESCR("list of SQL keywords");
2293+
22912294

22922295
/* Generic referential integrity constraint triggers */
22932296
DATA(insert OID = 1644 ( RI_FKey_check_ins PGNSP PGUID 12 1 0 f f t f v 0 2279 "" _null_ _null_ _null_ RI_FKey_check_ins - _null_ _null_ ));

src/include/parser/keywords.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/parser/keywords.h,v 1.24 2008/01/01 19:45:58 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/parser/keywords.h,v 1.25 2008/07/03 20:58:46 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -28,6 +28,9 @@ typedef struct ScanKeyword
2828
int16 category; /* see codes above */
2929
} ScanKeyword;
3030

31+
extern const ScanKeyword ScanKeywords[];
32+
extern const ScanKeyword *LastScanKeyword;
33+
3134
extern const ScanKeyword *ScanKeywordLookup(const char *text);
3235

3336
#endif /* KEYWORDS_H */

src/include/utils/builtins.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.317 2008/06/17 19:10:56 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.318 2008/07/03 20:58:47 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -411,6 +411,7 @@ extern Datum pg_reload_conf(PG_FUNCTION_ARGS);
411411
extern Datum pg_tablespace_databases(PG_FUNCTION_ARGS);
412412
extern Datum pg_rotate_logfile(PG_FUNCTION_ARGS);
413413
extern Datum pg_sleep(PG_FUNCTION_ARGS);
414+
extern Datum pg_get_keywords(PG_FUNCTION_ARGS);
414415

415416
/* oid.c */
416417
extern Datum oidin(PG_FUNCTION_ARGS);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy