File: sqltoken.c

package info (click to toggle)
libdbd-informix-perl 2003.04-3
  • links: PTS
  • area: contrib
  • in suites: etch, etch-m68k, sarge
  • size: 1,232 kB
  • ctags: 467
  • sloc: perl: 7,349; ansic: 5,340; sh: 184; makefile: 58
file content (214 lines) | stat: -rw-r--r-- 4,782 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
/*
@(#)Purpose:         Identify SQL token in string
@(#)Author:          J Leffler
@(#)Copyright:       1998-99 Jonathan Leffler (JLSS)
@(#)Copyright:       2002    IBM
@(#)Product:         IBM Informix Database Driver for Perl Version 2003.04 (2003-03-05)
*/

/*TABSTOP=4*/

#include <assert.h>
#include <ctype.h>
#include <string.h>

#include "esqlutil.h"

#define LCURLY '{'
#define RCURLY '}'

#ifndef lint
static const char rcs[] = "@(#)$Id: sqltoken.c,v 100.2 2002/12/06 22:18:23 jleffler Exp $";
#endif

/*
** sqltoken() - get SQL token
**
** Returns pointer to start of next SQL token (keyword, string,
** punctuation) in given string, or pointer to null at end of string if
** there is none.  The end of the token is in the end parameter.
*/
char *sqltoken(char *input, char **end)
{
	char *token;
	unsigned char  c;
	unsigned char  q;

	while (*input != '\0')
	{
		while ((c = *input) != '\0' && isspace(c))
			input++;
		if ((c = *input) == LCURLY && *(input + 1) == '+')
		{
			/* Optimizer hint; treat as symbol */
			if ((token = strchr(input, RCURLY)) == 0)
				break;
			*end = token + 1;
			return input;
		}
		else if ((c = *input) == LCURLY)
		{
			/* Routine comment -- ignore */
			if ((token = strchr(input, RCURLY)) == 0)
				break;
			input = token + 1;
		}
		else if ((c == '#') || (*input == '-' && *(input + 1) == '-'))
		{
			if ((token = strchr(input + 1, '\n')) == 0)
				break;
			input = token + 1;
		}
		else if (c == '\'' || c == '"')
		{
			char *str;
			token = input;
			str = token + 1;
			q = c;
			/* Ignores newlines in quoted strings! */
			/* Does handle adjacent doubled quotes */
			while ((str = strchr(str, q)) != 0)
			{
				if (*(str + 1) != q)
				{
					*end = str + 1;
					return token;
				}
				str += 2;
			}
			break;
		}
		else if (isdigit(c) || (c == '.' && isdigit((unsigned char)input[1])))
		{
			/* Intelligent number parsing */
			/* Handles unsigned integers, fixed point, */
			/* and exponental (1E+32) notation */
			token = input;
			if (c == '.')
				input++;
			while ((c = *input++) != '\0' && isdigit(c))
				;
			if (c == '.')
			{
				while ((c = *input++) != '\0' && isdigit(c))
					;
			}
			if (c == 'e' || c == 'E')
			{
				/* Maybe exponential notation -- in fact should be... */
				if (isdigit((unsigned char)*input) ||
					((*input == '+' || *input == '-') && isdigit((unsigned char)input[1])))
				{
					if ((c = *input++) == '+' || c == '-')
						input++;
					while ((c = *input++) != '\0' && isdigit(c))
						;
				}
			}
			*end = input - 1;
			return token;
		}
		else if (isalpha(c) || c == '_')
		{
			/* Word */
			token = input;
			while ((c = *input++) != '\0' && (isalnum(c) || c == '_'))
				;
			*end = input - 1;
			return token;
		}
		else
		{
			/* Punctuation - symbols */
			token = input++;
			/* Only compound symbols known are: <> != <= >= || :: (used in IUS) */
			/* Any other punctuation character is a single token */
			if (*input != '\0' && (c == '<' || c == '!' || c == '|' || c == '>' || c == ':'))
			{
				switch (c)
				{
				case '<':
					if (*input == '>' || *input == '=')
						input++;
					break;
				case '>':
					if (*input == '=')
						input++;
					break;
				case '!':
					if (*input == '=')
						input++;
					break;
				case '|':
					if (*input == '|')
						input++;
					break;
				case ':':
					if (*input == ':')
						input++;
					break;
				default:
					assert(0);
					break;
				}
			}
			*end = input;
			return token;
		}
	}
	*end = input;
	return(input);
}

#ifdef TEST

#include <stdio.h>

#define DIM(x)	(sizeof(x)/sizeof(*(x)))

static char *input[] =
{
	"SELECT * FROM SysTables",
	"SELECT { * } Tabid FROM SysTables",
	"SELECT -- * \n Tabid FROM SysTables",
	"SELECT #- * \n Tabid FROM SysTables",
	"SELECT a+b FROM 'informix'.systables",
	"SELECT a+1 AS\"a\"\"b\",a+1.23AS'a''b2'FROM db@server:\"user\".table\n"
		"WHERE (x+2 UNITS DAY)>=(DATETIME(1998-12-23 13:12:10) YEAR TO SECOND-1 UNITS DAY)\n"
		"  AND t<+3.14159E+32\n",
	"SELECT a.--this should be in comment and invisible\n"
		"b FROM SomeDbase:{this should be in comment and invisible too}\n"
		"user.#more commentary\n\t\ttablename",
	"SELECT (a>=<=<>!=||...(b)) FROM Nowhere",
	"{cc}-1{c}+1{c}.1{c}-.1{c}+.1{}-1.2E3{c}+1.23E+4{c}-1.234e-56",
	"info columns for 'cdhdba'.cdh_user",
	"select a::type as _ from _",
	"select {+ hint} _ as _ from _",
};

int main(void)
{
	int i;
	int n;
	char *str;
	char *src;
	char *end;
	char  buffer[2048];

	for (i = 0; i < DIM(input); i++)
	{
		str = input[i];
		printf("Data: <<%s>>\n", str);
		while (*(src = sqltoken(str, &end)) != '\0' && src != end)
		{
			strncpy(buffer, src, end - src);
			buffer[end - src] = '\0';
			n++;
			printf("Token: <<%s>>\n", buffer);
			str = end;
		}
	}
	return 0;
}

#endif /* TEST */