1 | /* |
---|
2 | Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. |
---|
3 | |
---|
4 | |
---|
5 | The MySQL Connector/J is licensed under the terms of the GPLv2 |
---|
6 | <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors. |
---|
7 | There are special exceptions to the terms and conditions of the GPLv2 as it is applied to |
---|
8 | this software, see the FLOSS License Exception |
---|
9 | <http://www.mysql.com/about/legal/licensing/foss-exception.html>. |
---|
10 | |
---|
11 | This program is free software; you can redistribute it and/or modify it under the terms |
---|
12 | of the GNU General Public License as published by the Free Software Foundation; version 2 |
---|
13 | of the License. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
---|
16 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
---|
17 | See the GNU General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License along with this |
---|
20 | program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth |
---|
21 | Floor, Boston, MA 02110-1301 USA |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | */ |
---|
26 | package com.mysql.jdbc; |
---|
27 | |
---|
28 | /** |
---|
29 | * EscapeTokenizer breaks up an SQL statement into SQL and escape code parts. |
---|
30 | * |
---|
31 | * @author Mark Matthews |
---|
32 | */ |
---|
33 | public class EscapeTokenizer { |
---|
34 | // ~ Instance fields |
---|
35 | // -------------------------------------------------------- |
---|
36 | |
---|
37 | private int bracesLevel = 0; |
---|
38 | |
---|
39 | private boolean emittingEscapeCode = false; |
---|
40 | |
---|
41 | private boolean inComment = false; |
---|
42 | |
---|
43 | private boolean inQuotes = false; |
---|
44 | |
---|
45 | private char lastChar = 0; |
---|
46 | |
---|
47 | private char lastLastChar = 0; |
---|
48 | |
---|
49 | private int pos = 0; |
---|
50 | |
---|
51 | private char quoteChar = 0; |
---|
52 | |
---|
53 | private boolean sawVariableUse = false; |
---|
54 | |
---|
55 | private String source = null; |
---|
56 | |
---|
57 | private int sourceLength = 0; |
---|
58 | |
---|
59 | // ~ Constructors |
---|
60 | // ----------------------------------------------------------- |
---|
61 | |
---|
62 | /** |
---|
63 | * Creates a new EscapeTokenizer object. |
---|
64 | * |
---|
65 | * @param s |
---|
66 | * the string to tokenize |
---|
67 | */ |
---|
68 | public EscapeTokenizer(String s) { |
---|
69 | this.source = s; |
---|
70 | this.sourceLength = s.length(); |
---|
71 | this.pos = 0; |
---|
72 | } |
---|
73 | |
---|
74 | // ~ Methods |
---|
75 | // ---------------------------------------------------------------- |
---|
76 | |
---|
77 | /** |
---|
78 | * Does this tokenizer have more tokens available? |
---|
79 | * |
---|
80 | * @return if this tokenizer has more tokens available |
---|
81 | */ |
---|
82 | public synchronized boolean hasMoreTokens() { |
---|
83 | return (this.pos < this.sourceLength); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Returns the next token |
---|
88 | * |
---|
89 | * @return the next token. |
---|
90 | */ |
---|
91 | public synchronized String nextToken() { |
---|
92 | StringBuffer tokenBuf = new StringBuffer(); |
---|
93 | |
---|
94 | if (this.emittingEscapeCode) { |
---|
95 | tokenBuf.append("{"); //$NON-NLS-1$ |
---|
96 | this.emittingEscapeCode = false; |
---|
97 | } |
---|
98 | |
---|
99 | for (; this.pos < this.sourceLength; this.pos++) { |
---|
100 | char c = this.source.charAt(this.pos); |
---|
101 | |
---|
102 | // Detect variable usage |
---|
103 | |
---|
104 | if (!this.inQuotes && c == '@') { |
---|
105 | this.sawVariableUse = true; |
---|
106 | } |
---|
107 | |
---|
108 | if ((c == '\'' || c == '"') && !inComment) { |
---|
109 | if (this.inQuotes && c == quoteChar) { |
---|
110 | if (this.pos + 1 < this.sourceLength) { |
---|
111 | if (this.source.charAt(this.pos + 1) == quoteChar) { |
---|
112 | // Doubled-up quote escape, if the first quote isn't already escaped |
---|
113 | if (this.lastChar != '\\') { |
---|
114 | tokenBuf.append(quoteChar); |
---|
115 | tokenBuf.append(quoteChar); |
---|
116 | this.pos++; |
---|
117 | continue; |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | if (this.lastChar != '\\') { |
---|
123 | if (this.inQuotes) { |
---|
124 | if (this.quoteChar == c) { |
---|
125 | this.inQuotes = false; |
---|
126 | } |
---|
127 | } else { |
---|
128 | this.inQuotes = true; |
---|
129 | this.quoteChar = c; |
---|
130 | } |
---|
131 | } else if (this.lastLastChar == '\\') { |
---|
132 | if (this.inQuotes) { |
---|
133 | if (this.quoteChar == c) { |
---|
134 | this.inQuotes = false; |
---|
135 | } |
---|
136 | } else { |
---|
137 | this.inQuotes = true; |
---|
138 | this.quoteChar = c; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | tokenBuf.append(c); |
---|
143 | } else if (c == '-') { |
---|
144 | if ((this.lastChar == '-') |
---|
145 | && ((this.lastLastChar != '\\') && !this.inQuotes)) { |
---|
146 | this.inComment = true; |
---|
147 | } |
---|
148 | |
---|
149 | tokenBuf.append(c); |
---|
150 | } else if ((c == '\n') || (c == '\r')) { |
---|
151 | this.inComment = false; |
---|
152 | |
---|
153 | tokenBuf.append(c); |
---|
154 | } else if (c == '{') { |
---|
155 | if (this.inQuotes || this.inComment) { |
---|
156 | tokenBuf.append(c); |
---|
157 | } else { |
---|
158 | this.bracesLevel++; |
---|
159 | |
---|
160 | if (this.bracesLevel == 1) { |
---|
161 | this.pos++; |
---|
162 | this.emittingEscapeCode = true; |
---|
163 | |
---|
164 | return tokenBuf.toString(); |
---|
165 | } |
---|
166 | |
---|
167 | tokenBuf.append(c); |
---|
168 | } |
---|
169 | } else if (c == '}') { |
---|
170 | tokenBuf.append(c); |
---|
171 | |
---|
172 | if (!this.inQuotes && !this.inComment) { |
---|
173 | this.lastChar = c; |
---|
174 | |
---|
175 | this.bracesLevel--; |
---|
176 | |
---|
177 | if (this.bracesLevel == 0) { |
---|
178 | this.pos++; |
---|
179 | |
---|
180 | return tokenBuf.toString(); |
---|
181 | } |
---|
182 | } |
---|
183 | } else { |
---|
184 | tokenBuf.append(c); |
---|
185 | } |
---|
186 | |
---|
187 | this.lastLastChar = this.lastChar; |
---|
188 | this.lastChar = c; |
---|
189 | } |
---|
190 | |
---|
191 | return tokenBuf.toString(); |
---|
192 | } |
---|
193 | |
---|
194 | boolean sawVariableUse() { |
---|
195 | return this.sawVariableUse; |
---|
196 | } |
---|
197 | } |
---|