| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util; |
| 7 |
|
| 8 |
import java.io.Closeable; |
| 9 |
import java.io.IOException; |
| 10 |
import java.io.Reader; |
| 11 |
import java.util.Arrays; |
| 12 |
import org.h2.engine.Constants; |
| 13 |
import org.h2.message.DbException; |
| 14 |
|
| 15 |
/** |
| 16 |
* This class can split SQL scripts to single SQL statements. |
| 17 |
* Each SQL statement ends with the character ';', however it is ignored |
| 18 |
* in comments and quotes. |
| 19 |
*/ |
| 20 |
public class ScriptReader implements Closeable { |
| 21 |
|
| 22 |
private final Reader reader; |
| 23 |
private char[] buffer; |
| 24 |
|
| 25 |
/** |
| 26 |
* The position in the buffer of the next char to be read |
| 27 |
*/ |
| 28 |
private int bufferPos; |
| 29 |
|
| 30 |
/** |
| 31 |
* The position in the buffer of the statement start |
| 32 |
*/ |
| 33 |
private int bufferStart = -1; |
| 34 |
|
| 35 |
/** |
| 36 |
* The position in the buffer of the last available char |
| 37 |
*/ |
| 38 |
private int bufferEnd; |
| 39 |
|
| 40 |
/** |
| 41 |
* True if we have read past the end of file |
| 42 |
*/ |
| 43 |
private boolean endOfFile; |
| 44 |
|
| 45 |
/** |
| 46 |
* True if we are inside a comment |
| 47 |
*/ |
| 48 |
private boolean insideRemark; |
| 49 |
|
| 50 |
/** |
| 51 |
* Only valid if insideRemark is true. True if we are inside a block |
| 52 |
* comment, false if we are inside a line comment |
| 53 |
*/ |
| 54 |
private boolean blockRemark; |
| 55 |
|
| 56 |
/** |
| 57 |
* True if comments should be skipped completely by this reader. |
| 58 |
*/ |
| 59 |
private boolean skipRemarks; |
| 60 |
|
| 61 |
/** |
| 62 |
* The position in buffer of start of comment |
| 63 |
*/ |
| 64 |
private int remarkStart; |
| 65 |
|
| 66 |
/** |
| 67 |
* Create a new SQL script reader from the given reader |
| 68 |
* |
| 69 |
* @param reader the reader |
| 70 |
*/ |
| 71 |
public ScriptReader(Reader reader) { |
| 72 |
this.reader = reader; |
| 73 |
buffer = new char[Constants.IO_BUFFER_SIZE * 2]; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Close the underlying reader. |
| 78 |
*/ |
| 79 |
@Override |
| 80 |
public void close() { |
| 81 |
try { |
| 82 |
reader.close(); |
| 83 |
} catch (IOException e) { |
| 84 |
throw DbException.convertIOException(e, null); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Read a statement from the reader. This method returns null if the end has |
| 90 |
* been reached. |
| 91 |
* |
| 92 |
* @return the SQL statement or null |
| 93 |
*/ |
| 94 |
public String readStatement() { |
| 95 |
if (endOfFile) { |
| 96 |
return null; |
| 97 |
} |
| 98 |
try { |
| 99 |
return readStatementLoop(); |
| 100 |
} catch (IOException e) { |
| 101 |
throw DbException.convertIOException(e, null); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
private String readStatementLoop() throws IOException { |
| 106 |
bufferStart = bufferPos; |
| 107 |
int c = read(); |
| 108 |
while (true) { |
| 109 |
if (c < 0) { |
| 110 |
endOfFile = true; |
| 111 |
if (bufferPos - 1 == bufferStart) { |
| 112 |
return null; |
| 113 |
} |
| 114 |
break; |
| 115 |
} else if (c == ';') { |
| 116 |
break; |
| 117 |
} |
| 118 |
switch (c) { |
| 119 |
case '$': { |
| 120 |
c = read(); |
| 121 |
if (c == '$' && (bufferPos - bufferStart < 3 || buffer[bufferPos - 3] <= ' ')) { |
| 122 |
// dollar quoted string |
| 123 |
while (true) { |
| 124 |
c = read(); |
| 125 |
if (c < 0) { |
| 126 |
break; |
| 127 |
} |
| 128 |
if (c == '$') { |
| 129 |
c = read(); |
| 130 |
if (c < 0) { |
| 131 |
break; |
| 132 |
} |
| 133 |
if (c == '$') { |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
c = read(); |
| 139 |
} |
| 140 |
break; |
| 141 |
} |
| 142 |
case '\'': |
| 143 |
while (true) { |
| 144 |
c = read(); |
| 145 |
if (c < 0) { |
| 146 |
break; |
| 147 |
} |
| 148 |
if (c == '\'') { |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
c = read(); |
| 153 |
break; |
| 154 |
case '"': |
| 155 |
while (true) { |
| 156 |
c = read(); |
| 157 |
if (c < 0) { |
| 158 |
break; |
| 159 |
} |
| 160 |
if (c == '\"') { |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
c = read(); |
| 165 |
break; |
| 166 |
case '/': { |
| 167 |
c = read(); |
| 168 |
if (c == '*') { |
| 169 |
// block comment |
| 170 |
startRemark(true); |
| 171 |
int level = 1; |
| 172 |
while (true) { |
| 173 |
c = read(); |
| 174 |
if (c < 0) { |
| 175 |
break; |
| 176 |
} |
| 177 |
if (c == '*') { |
| 178 |
c = read(); |
| 179 |
if (c < 0) { |
| 180 |
clearRemark(); |
| 181 |
break; |
| 182 |
} |
| 183 |
if (c == '/') { |
| 184 |
if (--level == 0) { |
| 185 |
endRemark(); |
| 186 |
break; |
| 187 |
} |
| 188 |
} |
| 189 |
} else if (c == '/') { |
| 190 |
c = read(); |
| 191 |
if (c < 0) { |
| 192 |
clearRemark(); |
| 193 |
break; |
| 194 |
} |
| 195 |
if (c == '*') { |
| 196 |
level++; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
c = read(); |
| 201 |
} else if (c == '/') { |
| 202 |
// single line comment |
| 203 |
startRemark(false); |
| 204 |
while (true) { |
| 205 |
c = read(); |
| 206 |
if (c < 0) { |
| 207 |
clearRemark(); |
| 208 |
break; |
| 209 |
} |
| 210 |
if (c == '\r' || c == '\n') { |
| 211 |
endRemark(); |
| 212 |
break; |
| 213 |
} |
| 214 |
} |
| 215 |
c = read(); |
| 216 |
} |
| 217 |
break; |
| 218 |
} |
| 219 |
case '-': { |
| 220 |
c = read(); |
| 221 |
if (c == '-') { |
| 222 |
// single line comment |
| 223 |
startRemark(false); |
| 224 |
while (true) { |
| 225 |
c = read(); |
| 226 |
if (c < 0) { |
| 227 |
clearRemark(); |
| 228 |
break; |
| 229 |
} |
| 230 |
if (c == '\r' || c == '\n') { |
| 231 |
endRemark(); |
| 232 |
break; |
| 233 |
} |
| 234 |
} |
| 235 |
c = read(); |
| 236 |
} |
| 237 |
break; |
| 238 |
} |
| 239 |
default: { |
| 240 |
c = read(); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
return new String(buffer, bufferStart, bufferPos - 1 - bufferStart); |
| 245 |
} |
| 246 |
|
| 247 |
private void startRemark(boolean block) { |
| 248 |
blockRemark = block; |
| 249 |
remarkStart = bufferPos - 2; |
| 250 |
insideRemark = true; |
| 251 |
} |
| 252 |
|
| 253 |
private void endRemark() { |
| 254 |
clearRemark(); |
| 255 |
insideRemark = false; |
| 256 |
} |
| 257 |
|
| 258 |
private void clearRemark() { |
| 259 |
if (skipRemarks) { |
| 260 |
Arrays.fill(buffer, remarkStart, bufferPos, ' '); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
private int read() throws IOException { |
| 265 |
if (bufferPos >= bufferEnd) { |
| 266 |
return readBuffer(); |
| 267 |
} |
| 268 |
return buffer[bufferPos++]; |
| 269 |
} |
| 270 |
|
| 271 |
private int readBuffer() throws IOException { |
| 272 |
if (endOfFile) { |
| 273 |
return -1; |
| 274 |
} |
| 275 |
int keep = bufferPos - bufferStart; |
| 276 |
if (keep > 0) { |
| 277 |
char[] src = buffer; |
| 278 |
if (keep + Constants.IO_BUFFER_SIZE > src.length) { |
| 279 |
// protect against NegativeArraySizeException |
| 280 |
if (src.length >= Integer.MAX_VALUE / 2) { |
| 281 |
throw new IOException("Error in parsing script, " + |
| 282 |
"statement size exceeds 1G, " + |
| 283 |
"first 80 characters of statement looks like: " + |
| 284 |
new String(buffer, bufferStart, 80)); |
| 285 |
} |
| 286 |
buffer = new char[src.length * 2]; |
| 287 |
} |
| 288 |
System.arraycopy(src, bufferStart, buffer, 0, keep); |
| 289 |
} |
| 290 |
remarkStart -= bufferStart; |
| 291 |
bufferStart = 0; |
| 292 |
bufferPos = keep; |
| 293 |
int len = reader.read(buffer, keep, Constants.IO_BUFFER_SIZE); |
| 294 |
if (len == -1) { |
| 295 |
// ensure bufferPos > bufferEnd |
| 296 |
bufferEnd = -1024; |
| 297 |
endOfFile = true; |
| 298 |
// ensure the right number of characters are read |
| 299 |
// in case the input buffer is still used |
| 300 |
bufferPos++; |
| 301 |
return -1; |
| 302 |
} |
| 303 |
bufferEnd = keep + len; |
| 304 |
return buffer[bufferPos++]; |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Check if this is the last statement, and if the single line or block |
| 309 |
* comment is not finished yet. |
| 310 |
* |
| 311 |
* @return true if the current position is inside a remark |
| 312 |
*/ |
| 313 |
public boolean isInsideRemark() { |
| 314 |
return insideRemark; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* If currently inside a remark, this method tells if it is a block comment |
| 319 |
* (true) or single line comment (false) |
| 320 |
* |
| 321 |
* @return true if inside a block comment |
| 322 |
*/ |
| 323 |
public boolean isBlockRemark() { |
| 324 |
return blockRemark; |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* If comments should be skipped completely by this reader. |
| 329 |
* |
| 330 |
* @param skipRemarks true if comments should be skipped |
| 331 |
*/ |
| 332 |
public void setSkipRemarks(boolean skipRemarks) { |
| 333 |
this.skipRemarks = skipRemarks; |
| 334 |
} |
| 335 |
|
| 336 |
} |