]> granicus.if.org Git - postgresql/blob - src/backend/parser/keywords.c
Implement feature of new FE/BE protocol whereby RowDescription identifies
[postgresql] / src / backend / parser / keywords.c
1 /*-------------------------------------------------------------------------
2  *
3  * keywords.c
4  *        lexical token lookup for reserved words in PostgreSQL
5  *
6  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/parser/keywords.c,v 1.138 2003/04/08 23:20:02 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include <ctype.h>
18
19 #include "nodes/parsenodes.h"
20 #include "parser/keywords.h"
21 #include "parser/parse.h"
22
23 /* NB: This file is also used by pg_dump. */
24
25 /*
26  * List of (keyword-name, keyword-token-value) pairs.
27  *
28  * !!WARNING!!: This list must be sorted, because binary
29  *               search is used to locate entries.
30  */
31 static const ScanKeyword ScanKeywords[] = {
32         /* name, value */
33         {"abort", ABORT_P},
34         {"absolute", ABSOLUTE},
35         {"access", ACCESS},
36         {"action", ACTION},
37         {"add", ADD},
38         {"after", AFTER},
39         {"aggregate", AGGREGATE},
40         {"all", ALL},
41         {"alter", ALTER},
42         {"analyse", ANALYSE},           /* British spelling */
43         {"analyze", ANALYZE},
44         {"and", AND},
45         {"any", ANY},
46         {"array", ARRAY},
47         {"as", AS},
48         {"asc", ASC},
49         {"assertion", ASSERTION},
50         {"assignment", ASSIGNMENT},
51         {"at", AT},
52         {"authorization", AUTHORIZATION},
53         {"backward", BACKWARD},
54         {"before", BEFORE},
55         {"begin", BEGIN_P},
56         {"between", BETWEEN},
57         {"bigint", BIGINT},
58         {"binary", BINARY},
59         {"bit", BIT},
60         {"boolean", BOOLEAN},
61         {"both", BOTH},
62         {"by", BY},
63         {"cache", CACHE},
64         {"called", CALLED},
65         {"cascade", CASCADE},
66         {"case", CASE},
67         {"cast", CAST},
68         {"chain", CHAIN},
69         {"char", CHAR_P},
70         {"character", CHARACTER},
71         {"characteristics", CHARACTERISTICS},
72         {"check", CHECK},
73         {"checkpoint", CHECKPOINT},
74         {"class", CLASS},
75         {"close", CLOSE},
76         {"cluster", CLUSTER},
77         {"coalesce", COALESCE},
78         {"collate", COLLATE},
79         {"column", COLUMN},
80         {"comment", COMMENT},
81         {"commit", COMMIT},
82         {"committed", COMMITTED},
83         {"constraint", CONSTRAINT},
84         {"constraints", CONSTRAINTS},
85         {"conversion", CONVERSION_P},
86         {"convert", CONVERT},
87         {"copy", COPY},
88         {"create", CREATE},
89         {"createdb", CREATEDB},
90         {"createuser", CREATEUSER},
91         {"cross", CROSS},
92         {"current_date", CURRENT_DATE},
93         {"current_time", CURRENT_TIME},
94         {"current_timestamp", CURRENT_TIMESTAMP},
95         {"current_user", CURRENT_USER},
96         {"cursor", CURSOR},
97         {"cycle", CYCLE},
98         {"database", DATABASE},
99         {"day", DAY_P},
100         {"deallocate", DEALLOCATE},
101         {"dec", DEC},
102         {"decimal", DECIMAL},
103         {"declare", DECLARE},
104         {"default", DEFAULT},
105         {"deferrable", DEFERRABLE},
106         {"deferred", DEFERRED},
107         {"definer", DEFINER},
108         {"delete", DELETE_P},
109         {"delimiter", DELIMITER},
110         {"delimiters", DELIMITERS},
111         {"desc", DESC},
112         {"distinct", DISTINCT},
113         {"do", DO},
114         {"domain", DOMAIN_P},
115         {"double", DOUBLE},
116         {"drop", DROP},
117         {"each", EACH},
118         {"else", ELSE},
119         {"encoding", ENCODING},
120         {"encrypted", ENCRYPTED},
121         {"end", END_P},
122         {"escape", ESCAPE},
123         {"except", EXCEPT},
124         {"exclusive", EXCLUSIVE},
125         {"execute", EXECUTE},
126         {"exists", EXISTS},
127         {"explain", EXPLAIN},
128         {"external", EXTERNAL},
129         {"extract", EXTRACT},
130         {"false", FALSE_P},
131         {"fetch", FETCH},
132         {"first", FIRST_P},
133         {"float", FLOAT_P},
134         {"for", FOR},
135         {"force", FORCE},
136         {"foreign", FOREIGN},
137         {"forward", FORWARD},
138         {"freeze", FREEZE},
139         {"from", FROM},
140         {"full", FULL},
141         {"function", FUNCTION},
142         {"global", GLOBAL},
143         {"grant", GRANT},
144         {"group", GROUP_P},
145         {"handler", HANDLER},
146         {"having", HAVING},
147         {"hold", HOLD},
148         {"hour", HOUR_P},
149         {"ilike", ILIKE},
150         {"immediate", IMMEDIATE},
151         {"immutable", IMMUTABLE},
152         {"implicit", IMPLICIT_P},
153         {"in", IN_P},
154         {"increment", INCREMENT},
155         {"index", INDEX},
156         {"inherits", INHERITS},
157         {"initially", INITIALLY},
158         {"inner", INNER_P},
159         {"inout", INOUT},
160         {"input", INPUT},
161         {"insensitive", INSENSITIVE},
162         {"insert", INSERT},
163         {"instead", INSTEAD},
164         {"int", INT},
165         {"integer", INTEGER},
166         {"intersect", INTERSECT},
167         {"interval", INTERVAL},
168         {"into", INTO},
169         {"invoker", INVOKER},
170         {"is", IS},
171         {"isnull", ISNULL},
172         {"isolation", ISOLATION},
173         {"join", JOIN},
174         {"key", KEY},
175         {"lancompiler", LANCOMPILER},
176         {"language", LANGUAGE},
177         {"last", LAST_P},
178         {"leading", LEADING},
179         {"left", LEFT},
180         {"level", LEVEL},
181         {"like", LIKE},
182         {"limit", LIMIT},
183         {"listen", LISTEN},
184         {"load", LOAD},
185         {"local", LOCAL},
186         {"localtime", LOCALTIME},
187         {"localtimestamp", LOCALTIMESTAMP},
188         {"location", LOCATION},
189         {"lock", LOCK_P},
190         {"match", MATCH},
191         {"maxvalue", MAXVALUE},
192         {"minute", MINUTE_P},
193         {"minvalue", MINVALUE},
194         {"mode", MODE},
195         {"month", MONTH_P},
196         {"move", MOVE},
197         {"names", NAMES},
198         {"national", NATIONAL},
199         {"natural", NATURAL},
200         {"nchar", NCHAR},
201         {"new", NEW},
202         {"next", NEXT},
203         {"no", NO},
204         {"nocreatedb", NOCREATEDB},
205         {"nocreateuser", NOCREATEUSER},
206         {"none", NONE},
207         {"not", NOT},
208         {"nothing", NOTHING},
209         {"notify", NOTIFY},
210         {"notnull", NOTNULL},
211         {"null", NULL_P},
212         {"nullif", NULLIF},
213         {"numeric", NUMERIC},
214         {"of", OF},
215         {"off", OFF},
216         {"offset", OFFSET},
217         {"oids", OIDS},
218         {"old", OLD},
219         {"on", ON},
220         {"only", ONLY},
221         {"operator", OPERATOR},
222         {"option", OPTION},
223         {"or", OR},
224         {"order", ORDER},
225         {"out", OUT_P},
226         {"outer", OUTER_P},
227         {"overlaps", OVERLAPS},
228         {"overlay", OVERLAY},
229         {"owner", OWNER},
230         {"partial", PARTIAL},
231         {"password", PASSWORD},
232         {"path", PATH_P},
233         {"pendant", PENDANT},
234         {"placing", PLACING},
235         {"position", POSITION},
236         {"precision", PRECISION},
237         {"prepare", PREPARE},
238         {"preserve", PRESERVE},
239         {"primary", PRIMARY},
240         {"prior", PRIOR},
241         {"privileges", PRIVILEGES},
242         {"procedural", PROCEDURAL},
243         {"procedure", PROCEDURE},
244         {"read", READ},
245         {"real", REAL},
246         {"recheck", RECHECK},
247         {"references", REFERENCES},
248         {"reindex", REINDEX},
249         {"relative", RELATIVE},
250         {"rename", RENAME},
251         {"replace", REPLACE},
252         {"reset", RESET},
253         {"restart", RESTART},
254         {"restrict", RESTRICT},
255         {"returns", RETURNS},
256         {"revoke", REVOKE},
257         {"right", RIGHT},
258         {"rollback", ROLLBACK},
259         {"row", ROW},
260         {"rows", ROWS},
261         {"rule", RULE},
262         {"schema", SCHEMA},
263         {"scroll", SCROLL},
264         {"second", SECOND_P},
265         {"security", SECURITY},
266         {"select", SELECT},
267         {"sequence", SEQUENCE},
268         {"serializable", SERIALIZABLE},
269         {"session", SESSION},
270         {"session_user", SESSION_USER},
271         {"set", SET},
272         {"setof", SETOF},
273         {"share", SHARE},
274         {"show", SHOW},
275         {"similar", SIMILAR},
276         {"simple", SIMPLE},
277         {"smallint", SMALLINT},
278         {"some", SOME},
279         {"stable", STABLE},
280         {"start", START},
281         {"statement", STATEMENT},
282         {"statistics", STATISTICS},
283         {"stdin", STDIN},
284         {"stdout", STDOUT},
285         {"storage", STORAGE},
286         {"strict", STRICT},
287         {"substring", SUBSTRING},
288         {"sysid", SYSID},
289         {"table", TABLE},
290         {"temp", TEMP},
291         {"template", TEMPLATE},
292         {"temporary", TEMPORARY},
293         {"then", THEN},
294         {"time", TIME},
295         {"timestamp", TIMESTAMP},
296         {"to", TO},
297         {"toast", TOAST},
298         {"trailing", TRAILING},
299         {"transaction", TRANSACTION},
300         {"treat", TREAT},
301         {"trigger", TRIGGER},
302         {"trim", TRIM},
303         {"true", TRUE_P},
304         {"truncate", TRUNCATE},
305         {"trusted", TRUSTED},
306         {"type", TYPE_P},
307         {"unencrypted", UNENCRYPTED},
308         {"union", UNION},
309         {"unique", UNIQUE},
310         {"unknown", UNKNOWN},
311         {"unlisten", UNLISTEN},
312         {"until", UNTIL},
313         {"update", UPDATE},
314         {"usage", USAGE},
315         {"user", USER},
316         {"using", USING},
317         {"vacuum", VACUUM},
318         {"valid", VALID},
319         {"validator", VALIDATOR},
320         {"values", VALUES},
321         {"varchar", VARCHAR},
322         {"varying", VARYING},
323         {"verbose", VERBOSE},
324         {"version", VERSION},
325         {"view", VIEW},
326         {"volatile", VOLATILE},
327         {"when", WHEN},
328         {"where", WHERE},
329         {"with", WITH},
330         {"without", WITHOUT},
331         {"work", WORK},
332         {"write", WRITE},
333         {"year", YEAR_P},
334         {"zone", ZONE},
335 };
336
337 /*
338  * ScanKeywordLookup - see if a given word is a keyword
339  *
340  * Returns a pointer to the ScanKeyword table entry, or NULL if no match.
341  *
342  * The match is done case-insensitively.  Note that we deliberately use a
343  * dumbed-down case conversion that will only translate 'A'-'Z' into 'a'-'z',
344  * even if we are in a locale where tolower() would produce more or different
345  * translations.  This is to conform to the SQL99 spec, which says that
346  * keywords are to be matched in this way even though non-keyword identifiers
347  * receive a different case-normalization mapping.
348  */
349 const ScanKeyword *
350 ScanKeywordLookup(const char *text)
351 {
352         int                     len,
353                                 i;
354         char            word[NAMEDATALEN];
355         const ScanKeyword *low;
356         const ScanKeyword *high;
357
358         len = strlen(text);
359         /* We assume all keywords are shorter than NAMEDATALEN. */
360         if (len >= NAMEDATALEN)
361                 return NULL;
362
363         /*
364          * Apply an ASCII-only downcasing.      We must not use tolower() since it
365          * may produce the wrong translation in some locales (eg, Turkish),
366          * and we don't trust isupper() very much either.  In an ASCII-based
367          * encoding the tests against A and Z are sufficient, but we also
368          * check isupper() so that we will work correctly under EBCDIC.  The
369          * actual case conversion step should work for either ASCII or EBCDIC.
370          */
371         for (i = 0; i < len; i++)
372         {
373                 char            ch = text[i];
374
375                 if (ch >= 'A' && ch <= 'Z' && isupper((unsigned char) ch))
376                         ch += 'a' - 'A';
377                 word[i] = ch;
378         }
379         word[len] = '\0';
380
381         /*
382          * Now do a binary search using plain strcmp() comparison.
383          */
384         low = &ScanKeywords[0];
385         high = endof(ScanKeywords) - 1;
386         while (low <= high)
387         {
388                 const ScanKeyword *middle;
389                 int                     difference;
390
391                 middle = low + (high - low) / 2;
392                 difference = strcmp(middle->name, word);
393                 if (difference == 0)
394                         return middle;
395                 else if (difference < 0)
396                         low = middle + 1;
397                 else
398                         high = middle - 1;
399         }
400
401         return NULL;
402 }