Search in sources :

Example 1 with ScriptReader

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ScriptReader(org.h2.util.ScriptReader)

Example 2 with ScriptReader

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());
        }
    }
}
Also used : Random(java.util.Random) StringReader(java.io.StringReader) ScriptReader(org.h2.util.ScriptReader)

Example 3 with ScriptReader

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();
}
Also used : StringReader(java.io.StringReader) ScriptReader(org.h2.util.ScriptReader)

Example 4 with ScriptReader

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;
}
Also used : StringReader(java.io.StringReader) ScriptReader(org.h2.util.ScriptReader)

Example 5 with ScriptReader

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;
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ScriptReader(org.h2.util.ScriptReader)

Aggregations

ScriptReader (org.h2.util.ScriptReader)14 StringReader (java.io.StringReader)10 IOException (java.io.IOException)5 ResultSet (java.sql.ResultSet)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStreamReader (java.io.InputStreamReader)3 SQLException (java.sql.SQLException)3 Statement (java.sql.Statement)3 Connection (java.sql.Connection)2 DbException (org.h2.message.DbException)2 BufferedReader (java.io.BufferedReader)1 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 InputStream (java.io.InputStream)1 LineNumberReader (java.io.LineNumberReader)1 Reader (java.io.Reader)1