| 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.command.dml; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.nio.charset.Charset; |
| 10 |
import java.nio.charset.StandardCharsets; |
| 11 |
|
| 12 |
import org.h2.command.CommandContainer; |
| 13 |
import org.h2.command.CommandInterface; |
| 14 |
import org.h2.command.Prepared; |
| 15 |
import org.h2.engine.SessionLocal; |
| 16 |
import org.h2.message.DbException; |
| 17 |
import org.h2.result.ResultInterface; |
| 18 |
import org.h2.util.ScriptReader; |
| 19 |
import org.h2.util.StringUtils; |
| 20 |
|
| 21 |
/** |
| 22 |
* This class represents the statement |
| 23 |
* RUNSCRIPT |
| 24 |
*/ |
| 25 |
public class RunScriptCommand extends ScriptBase { |
| 26 |
|
| 27 |
/** |
| 28 |
* The byte order mark. |
| 29 |
* 0xfeff because this is the Unicode char |
| 30 |
* represented by the UTF-8 byte order mark (EF BB BF). |
| 31 |
*/ |
| 32 |
private static final char UTF8_BOM = '\uFEFF'; |
| 33 |
|
| 34 |
private Charset charset = StandardCharsets.UTF_8; |
| 35 |
|
| 36 |
private boolean quirksMode; |
| 37 |
|
| 38 |
private boolean variableBinary; |
| 39 |
|
| 40 |
private boolean from1X; |
| 41 |
|
| 42 |
public RunScriptCommand(SessionLocal session) { |
| 43 |
super(session); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public long update() { |
| 48 |
session.getUser().checkAdmin(); |
| 49 |
int count = 0; |
| 50 |
boolean oldQuirksMode = session.isQuirksMode(); |
| 51 |
boolean oldVariableBinary = session.isVariableBinary(); |
| 52 |
try { |
| 53 |
openInput(charset); |
| 54 |
// if necessary, strip the BOM from the front of the file |
| 55 |
reader.mark(1); |
| 56 |
if (reader.read() != UTF8_BOM) { |
| 57 |
reader.reset(); |
| 58 |
} |
| 59 |
if (quirksMode) { |
| 60 |
session.setQuirksMode(true); |
| 61 |
} |
| 62 |
if (variableBinary) { |
| 63 |
session.setVariableBinary(true); |
| 64 |
} |
| 65 |
ScriptReader r = new ScriptReader(reader); |
| 66 |
while (true) { |
| 67 |
String sql = r.readStatement(); |
| 68 |
if (sql == null) { |
| 69 |
break; |
| 70 |
} |
| 71 |
execute(sql); |
| 72 |
count++; |
| 73 |
if ((count & 127) == 0) { |
| 74 |
checkCanceled(); |
| 75 |
} |
| 76 |
} |
| 77 |
r.close(); |
| 78 |
} catch (IOException e) { |
| 79 |
throw DbException.convertIOException(e, null); |
| 80 |
} finally { |
| 81 |
if (quirksMode) { |
| 82 |
session.setQuirksMode(oldQuirksMode); |
| 83 |
} |
| 84 |
if (variableBinary) { |
| 85 |
session.setVariableBinary(oldVariableBinary); |
| 86 |
} |
| 87 |
closeIO(); |
| 88 |
} |
| 89 |
return count; |
| 90 |
} |
| 91 |
|
| 92 |
private void execute(String sql) { |
| 93 |
if (from1X) { |
| 94 |
sql = sql.trim(); |
| 95 |
if (sql.startsWith("--")) { |
| 96 |
int i = 2, l = sql.length(); |
| 97 |
char c; |
| 98 |
do { |
| 99 |
if (i >= l) { |
| 100 |
return; |
| 101 |
} |
| 102 |
c = sql.charAt(i++); |
| 103 |
} while (c != '\n' && c != '\r'); |
| 104 |
sql = StringUtils.trimSubstring(sql, i); |
| 105 |
} |
| 106 |
if (sql.startsWith("INSERT INTO SYSTEM_LOB_STREAM VALUES(")) { |
| 107 |
int idx = sql.indexOf(", NULL, '"); |
| 108 |
if (idx >= 0) { |
| 109 |
sql = new StringBuilder(sql.length() + 1).append(sql, 0, idx + 8).append("X'") |
| 110 |
.append(sql, idx + 9, sql.length()).toString(); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
try { |
| 115 |
Prepared command = session.prepare(sql); |
| 116 |
CommandContainer commandContainer = new CommandContainer(session, sql, command); |
| 117 |
if (commandContainer.isQuery()) { |
| 118 |
commandContainer.executeQuery(0, false); |
| 119 |
} else { |
| 120 |
commandContainer.executeUpdate(null); |
| 121 |
} |
| 122 |
} catch (DbException e) { |
| 123 |
throw e.addSQL(sql); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public void setCharset(Charset charset) { |
| 128 |
this.charset = charset; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Enables or disables the quirks mode. |
| 133 |
* |
| 134 |
* @param quirksMode |
| 135 |
* whether quirks mode should be enabled |
| 136 |
*/ |
| 137 |
public void setQuirksMode(boolean quirksMode) { |
| 138 |
this.quirksMode = quirksMode; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Changes parsing of a BINARY data type. |
| 143 |
* |
| 144 |
* @param variableBinary |
| 145 |
* {@code true} to parse BINARY as VARBINARY, {@code false} to |
| 146 |
* parse it as is |
| 147 |
*/ |
| 148 |
public void setVariableBinary(boolean variableBinary) { |
| 149 |
this.variableBinary = variableBinary; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Enables quirks for parsing scripts from H2 1.*.*. |
| 154 |
*/ |
| 155 |
public void setFrom1X() { |
| 156 |
variableBinary = quirksMode = from1X = true; |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public ResultInterface queryMeta() { |
| 161 |
return null; |
| 162 |
} |
| 163 |
|
| 164 |
@Override |
| 165 |
public int getType() { |
| 166 |
return CommandInterface.RUNSCRIPT; |
| 167 |
} |
| 168 |
|
| 169 |
} |