File: parsesql.c

package info (click to toggle)
pgq 3.2.6-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 836 kB
  • sloc: sql: 3,412; ansic: 1,990; python: 302; makefile: 89; sh: 2
file content (221 lines) | stat: -rw-r--r-- 4,199 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

#ifndef TEST
#include <postgres.h>
#else
#include <ctype.h>
#include <string.h>
#endif

#include "parsesql.h"

/*
 * Small SQL tokenizer.  For cases where flex/bison is overkill.
 *
 * To simplify further processing, it merges words separated
 * with dots together.  That also means it does not support
 * whitespace/comments before and after dot.
 *
 * Otherwise it's relatively compatible with main parser.
 *
 * Return value:
 * -1      - error
 *  0      - end of string
 *  1..255 - single char
 *  >255   - token code
 */
int sql_tokenizer(const char *sql, int *len_p, bool stdstr)
{
	const char *p = sql;
	int tok;

	*len_p = 0;
	if (!*p) {
		/* end */
		return 0;
	} else if (isspace(*p) || (p[0] == '-' && p[1] == '-') || (p[0] == '/' && p[1] == '*')) {
		/* whitespace */
		tok = T_SPACE;
		while (1) {
			if (p[0] == '-' && p[1] == '-') {
				/* line comment */
				while (*p && *p != '\n')
					p++;
			} else if (p[0] == '/' && p[1] == '*') {
				/* c-comment, potentially nested */
				int level = 1;
				p += 2;
				while (level) {
					if (p[0] == '*' && p[1] == '/') {
						level--;
						p += 2;
					} else if (p[0] == '/' && p[1] == '*') {
						level++;
						p += 2;
					} else if (!*p) {
						return -1;
					} else
						p++;
				}
			} else if (isspace(p[0])) {
				/* plain whitespace */
				while (isspace(p[0]))
					p++;
			} else
				break;
		}
	} else if ((p[0] == '\'' && !stdstr) || ((p[0] == 'E' || p[0] == 'e') && p[1] == '\'')) {
		/* extended string */
		tok = T_STRING;
		if (p[0] == '\'')
			p++;
		else
			p += 2;
		for (; *p; p++) {
			if (p[0] == '\'') {
				if (p[1] == '\'')
					p++;
				else
					break;
			} else if (p[0] == '\\') {
				if (!p[1])
					return -1;
				p++;
			}
		}
		if (*p++ != '\'')
			return -1;
	} else if (p[0] == '\'' && stdstr) {
		/* standard string */
		tok = T_STRING;
		for (p++; *p; p++) {
			if (p[0] == '\'') {
				if (p[1] == '\'')
					p++;
				else
					break;
			}
		}
		if (*p++ != '\'')
			return -1;
	} else if (isalpha(*p) || (*p == '_')) {
		/* plain/quoted words separated with '.' */
		tok = T_WORD;
		while (1) {
			/* plain ident */
			while (*p && (isalnum(*p) || *p == '_' || *p == '.'))
				p++;
			if (p[0] == '"') {
				/* quoted ident */
				for (p++; *p; p++) {
					if (p[0] == '"') {
						if (p[1] == '"')
							p++;
						else
							break;
					}
				}
				if (*p++ != '"')
					return -1;
			} else if (p[0] == '.') {
				tok = T_FQIDENT;
				p++;
			} else {
				break;
			}
		}
	} else if (isdigit(p[0]) || (p[0] == '.' && isdigit(p[1]))) {
		/* number */
		tok = T_NUMBER;
		while (*p) {
			if (isdigit(*p) || *p == '.') {
				p++;
			} else if ((*p == 'e' || *p == 'E')) {
				if (p[1] == '.' || p[1] == '+' || p[1] == '-') {
					p += 2;
				} else if (isdigit(p[1])) {
					p += 2;
				} else
					break;
			} else
				break;
		}
	} else if (p[0] == '$') {
		if (isdigit(p[1])) {
			/* dollar ident */
			tok = T_WORD;
			for (p += 2; *p; p++) {
				if (!isdigit(*p))
					break;
			}
		} else if (isalpha(p[1]) || p[1] == '_' || p[1] == '$') {
			/* dollar quote */
			const char *p2, *p3;
			tok = T_STRING;
			p2 = strchr(p+1, '$');
			if (!p2)
				return -1;
			p3 = ++p2;
			
			while (1) {
				p3 = strchr(p3, '$');
				if (!p3)
					return -1;
				if (strncmp(p3, p, p2 - p) == 0)
					break;
				p3++;
			}
			p = p3 + (p2 - p);
		} else
			return -1;
	} else if (*p == '.') {
		/* disallow standalone dot - seems ident parsing missed it */
		return -1;
	} else {
		/* return other symbols as-is */
		tok = *p++;
	}
	*len_p = p - sql;
	return tok;
}


#ifdef TEST

/*
 * test code
 */

const char test_sql[] =
"\r\n\t "
"-- foo\n"
"/*/**//* nested *//**/*/\n"
"select 1, .600, $1, $150, 1.44e+.1,"
" bzo.\"fo'\"\".o\".zoo.fa, "
"E'a\\\\ \\000 \\' baz ''',"
"'foo''baz' from \"quoted\"\"id\";"
"$$$$ $_$ $x$ $ $_ $_$"
;

int main(void)
{
	const char *sql = test_sql;
	int tlen;
	int tok;
	bool stdstr = false;
	while (1) {
		tok = sql_tokenizer(sql, &tlen, stdstr);
		if (tok == 0) {
			printf("EOF\n");
			break;
		} else if (tok < 0) {
			printf("ERR\n");
			return 1;
		}
		printf("tok=%d len=%d str=<%.*s>\n", tok, tlen, tlen, sql);
		sql += tlen;
	}
	return 0;
}

#endif