use of org.h2.util.ScriptReader in project h2database by h2database.
the class RunScriptCommand method update.
@Override
public int update() {
session.getUser().checkAdmin();
int count = 0;
try {
openInput();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
// if necessary, strip the BOM from the front of the file
reader.mark(1);
if (reader.read() != UTF8_BOM) {
reader.reset();
}
ScriptReader r = new ScriptReader(reader);
while (true) {
String sql = r.readStatement();
if (sql == null) {
break;
}
execute(sql);
count++;
if ((count & 127) == 0) {
checkCanceled();
}
}
r.close();
} catch (IOException e) {
throw DbException.convertIOException(e, null);
} finally {
closeIO();
}
return count;
}
use of org.h2.util.ScriptReader in project h2database by h2database.
the class TestScriptReader method testRandom.
private void testRandom() {
int len = getSize(1000, 10000);
Random random = new Random(10);
for (int i = 0; i < len; i++) {
int l = random.nextInt(10);
String[] sql = new String[l];
StringBuilder buff = new StringBuilder();
for (int j = 0; j < l; j++) {
sql[j] = randomStatement(random);
buff.append(sql[j]);
if (j < l - 1) {
buff.append(";");
}
}
String s = buff.toString();
StringReader reader = new StringReader(s);
try (ScriptReader source = new ScriptReader(reader)) {
for (int j = 0; j < l; j++) {
String e = source.readStatement();
String c = sql[j];
if (c.length() == 0 && j == l - 1) {
c = null;
}
assertEquals(c, e);
}
assertEquals(null, source.readStatement());
}
}
}
use of org.h2.util.ScriptReader in project h2database by h2database.
the class TestScriptReader method testCommon.
private void testCommon() {
String s;
ScriptReader source;
s = "$$;$$;";
source = new ScriptReader(new StringReader(s));
assertEquals("$$;$$", source.readStatement());
assertEquals(null, source.readStatement());
source.close();
s = "a;';';\";\";--;\n;/*;\n*/;//;\na;";
source = new ScriptReader(new StringReader(s));
assertEquals("a", source.readStatement());
assertEquals("';'", source.readStatement());
assertEquals("\";\"", source.readStatement());
assertEquals("--;\n", source.readStatement());
assertEquals("/*;\n*/", source.readStatement());
assertEquals("//;\na", source.readStatement());
assertEquals(null, source.readStatement());
source.close();
s = "/\n$ \n\n $';$$a$$ $\n;'";
source = new ScriptReader(new StringReader(s));
assertEquals("/\n$ \n\n $';$$a$$ $\n;'", source.readStatement());
assertEquals(null, source.readStatement());
source.close();
// check handling of unclosed block comments
s = "/*xxx";
source = new ScriptReader(new StringReader(s));
assertEquals("/*xxx", source.readStatement());
assertTrue(source.isBlockRemark());
source.close();
}
use of org.h2.util.ScriptReader in project h2database by h2database.
the class TestNestedJoins method cleanRemarks.
private static String cleanRemarks(String sql) {
ScriptReader r = new ScriptReader(new StringReader(sql));
r.setSkipRemarks(true);
sql = r.readStatement();
sql = sql.replaceAll("\\n", " ");
while (sql.contains(" ")) {
sql = sql.replaceAll(" ", " ");
}
return sql;
}
use of org.h2.util.ScriptReader in project h2database by h2database.
the class RunScript method execute.
/**
* Executes the SQL commands read from the reader against a database.
*
* @param conn the connection to a database
* @param reader the reader
* @return the last result set
*/
public static ResultSet execute(Connection conn, Reader reader) throws SQLException {
// can not close the statement because we return a result set from it
Statement stat = conn.createStatement();
ResultSet rs = null;
ScriptReader r = new ScriptReader(reader);
while (true) {
String sql = r.readStatement();
if (sql == null) {
break;
}
if (sql.trim().length() == 0) {
continue;
}
boolean resultSet = stat.execute(sql);
if (resultSet) {
if (rs != null) {
rs.close();
rs = null;
}
rs = stat.getResultSet();
}
}
return rs;
}
Aggregations