Search in sources :

Example 1 with RunScript

use of org.h2.tools.RunScript in project h2database by h2database.

the class Parser method parsePrepared.

private Prepared parsePrepared() {
    int start = lastParseIndex;
    Prepared c = null;
    String token = currentToken;
    if (token.length() == 0) {
        c = new NoOperation(session);
    } else {
        char first = token.charAt(0);
        switch(first) {
            case '?':
                // read the ? as a parameter
                readTerm();
                // this is an 'out' parameter - set a dummy value
                parameters.get(0).setValue(ValueNull.INSTANCE);
                read("=");
                read("CALL");
                c = parseCall();
                break;
            case '(':
                c = parseSelect();
                break;
            case 'a':
            case 'A':
                if (readIf("ALTER")) {
                    c = parseAlter();
                } else if (readIf("ANALYZE")) {
                    c = parseAnalyze();
                }
                break;
            case 'b':
            case 'B':
                if (readIf("BACKUP")) {
                    c = parseBackup();
                } else if (readIf("BEGIN")) {
                    c = parseBegin();
                }
                break;
            case 'c':
            case 'C':
                if (readIf("COMMIT")) {
                    c = parseCommit();
                } else if (readIf("CREATE")) {
                    c = parseCreate();
                } else if (readIf("CALL")) {
                    c = parseCall();
                } else if (readIf("CHECKPOINT")) {
                    c = parseCheckpoint();
                } else if (readIf("COMMENT")) {
                    c = parseComment();
                }
                break;
            case 'd':
            case 'D':
                if (readIf("DELETE")) {
                    c = parseDelete();
                } else if (readIf("DROP")) {
                    c = parseDrop();
                } else if (readIf("DECLARE")) {
                    // support for DECLARE GLOBAL TEMPORARY TABLE...
                    c = parseCreate();
                } else if (readIf("DEALLOCATE")) {
                    c = parseDeallocate();
                }
                break;
            case 'e':
            case 'E':
                if (readIf("EXPLAIN")) {
                    c = parseExplain();
                } else if (readIf("EXECUTE")) {
                    c = parseExecute();
                }
                break;
            case 'f':
            case 'F':
                if (isToken("FROM")) {
                    c = parseSelect();
                }
                break;
            case 'g':
            case 'G':
                if (readIf("GRANT")) {
                    c = parseGrantRevoke(CommandInterface.GRANT);
                }
                break;
            case 'h':
            case 'H':
                if (readIf("HELP")) {
                    c = parseHelp();
                }
                break;
            case 'i':
            case 'I':
                if (readIf("INSERT")) {
                    c = parseInsert();
                }
                break;
            case 'm':
            case 'M':
                if (readIf("MERGE")) {
                    c = parseMerge();
                }
                break;
            case 'p':
            case 'P':
                if (readIf("PREPARE")) {
                    c = parsePrepare();
                }
                break;
            case 'r':
            case 'R':
                if (readIf("ROLLBACK")) {
                    c = parseRollback();
                } else if (readIf("REVOKE")) {
                    c = parseGrantRevoke(CommandInterface.REVOKE);
                } else if (readIf("RUNSCRIPT")) {
                    c = parseRunScript();
                } else if (readIf("RELEASE")) {
                    c = parseReleaseSavepoint();
                } else if (readIf("REPLACE")) {
                    c = parseReplace();
                }
                break;
            case 's':
            case 'S':
                if (isToken("SELECT")) {
                    c = parseSelect();
                } else if (readIf("SET")) {
                    c = parseSet();
                } else if (readIf("SAVEPOINT")) {
                    c = parseSavepoint();
                } else if (readIf("SCRIPT")) {
                    c = parseScript();
                } else if (readIf("SHUTDOWN")) {
                    c = parseShutdown();
                } else if (readIf("SHOW")) {
                    c = parseShow();
                }
                break;
            case 't':
            case 'T':
                if (readIf("TRUNCATE")) {
                    c = parseTruncate();
                }
                break;
            case 'u':
            case 'U':
                if (readIf("UPDATE")) {
                    c = parseUpdate();
                } else if (readIf("USE")) {
                    c = parseUse();
                }
                break;
            case 'v':
            case 'V':
                if (readIf("VALUES")) {
                    c = parseValues();
                }
                break;
            case 'w':
            case 'W':
                if (readIf("WITH")) {
                    c = parseWithStatementOrQuery();
                }
                break;
            case ';':
                c = new NoOperation(session);
                break;
            default:
                throw getSyntaxError();
        }
        if (indexedParameterList != null) {
            for (int i = 0, size = indexedParameterList.size(); i < size; i++) {
                if (indexedParameterList.get(i) == null) {
                    indexedParameterList.set(i, new Parameter(i));
                }
            }
            parameters = indexedParameterList;
        }
        if (readIf("{")) {
            do {
                int index = (int) readLong() - 1;
                if (index < 0 || index >= parameters.size()) {
                    throw getSyntaxError();
                }
                Parameter p = parameters.get(index);
                if (p == null) {
                    throw getSyntaxError();
                }
                read(":");
                Expression expr = readExpression();
                expr = expr.optimize(session);
                p.setValue(expr.getValue(session));
            } while (readIf(","));
            read("}");
            for (Parameter p : parameters) {
                p.checkSet();
            }
            parameters.clear();
        }
    }
    if (c == null) {
        throw getSyntaxError();
    }
    setSQL(c, null, start);
    return c;
}
Also used : NoOperation(org.h2.command.dml.NoOperation) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) Parameter(org.h2.expression.Parameter) ConditionInParameter(org.h2.expression.ConditionInParameter) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 2 with RunScript

use of org.h2.tools.RunScript in project h2database by h2database.

the class TestTools method testScriptRunscript.

private void testScriptRunscript() throws SQLException {
    org.h2.Driver.load();
    String url = getURL("jdbc:h2:" + getBaseDir() + "/testScriptRunscript", true);
    String user = "sa", password = "abc";
    String fileName = getBaseDir() + "/b2.sql";
    DeleteDbFiles.main("-dir", getBaseDir(), "-db", "testScriptRunscript", "-quiet");
    Connection conn = getConnection(url, user, password);
    conn.createStatement().execute("CREATE TABLE \u00f6()");
    conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
    conn.createStatement().execute("INSERT INTO TEST VALUES(1, 'Hello')");
    conn.close();
    Script.main("-url", url, "-user", user, "-password", password, "-script", fileName, "-options", "nodata", "compression", "lzf", "cipher", "aes", "password", "'123'", "charset", "'utf-8'");
    Script.main("-url", url, "-user", user, "-password", password, "-script", fileName + ".txt");
    DeleteDbFiles.main("-dir", getBaseDir(), "-db", "testScriptRunscript", "-quiet");
    RunScript.main("-url", url, "-user", user, "-password", password, "-script", fileName, "-options", "compression", "lzf", "cipher", "aes", "password", "'123'", "charset", "'utf-8'");
    conn = getConnection("jdbc:h2:" + getBaseDir() + "/testScriptRunscript", "sa", "abc");
    ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST");
    assertFalse(rs.next());
    rs = conn.createStatement().executeQuery("SELECT * FROM \u00f6");
    assertFalse(rs.next());
    conn.close();
    DeleteDbFiles.main("-dir", getBaseDir(), "-db", "testScriptRunscript", "-quiet");
    RunScript tool = new RunScript();
    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    tool.setOut(new PrintStream(buff));
    tool.runTool("-url", url, "-user", user, "-password", password, "-script", fileName + ".txt", "-showResults");
    assertContains(buff.toString(), "Hello");
    // test parsing of BLOCKSIZE option
    DeleteDbFiles.main("-dir", getBaseDir(), "-db", "testScriptRunscript", "-quiet");
    conn = getConnection(url, user, password);
    conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
    conn.close();
    Script.main("-url", url, "-user", user, "-password", password, "-script", fileName, "-options", "simple", "blocksize", "8192");
}
Also used : PrintStream(java.io.PrintStream) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) RunScript(org.h2.tools.RunScript) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with RunScript

use of org.h2.tools.RunScript in project h2database by h2database.

the class TestRecovery method testCompressedAndUncompressed.

private void testCompressedAndUncompressed() throws SQLException {
    DeleteDbFiles.execute(getBaseDir(), "recovery", true);
    DeleteDbFiles.execute(getBaseDir(), "recovery2", true);
    org.h2.Driver.load();
    Connection conn = getConnection("recovery");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, data clob)");
    stat.execute("insert into test values(1, space(10000))");
    stat.execute("set compress_lob lzf");
    stat.execute("insert into test values(2, space(10000))");
    conn.close();
    Recover rec = new Recover();
    rec.runTool("-dir", getBaseDir(), "-db", "recovery");
    Connection conn2 = getConnection("recovery2");
    Statement stat2 = conn2.createStatement();
    String name = "recovery.h2.sql";
    stat2.execute("runscript from '" + getBaseDir() + "/" + name + "'");
    stat2.execute("select * from test");
    conn2.close();
    conn = getConnection("recovery");
    stat = conn.createStatement();
    conn2 = getConnection("recovery2");
    stat2 = conn2.createStatement();
    assertEqualDatabases(stat, stat2);
    conn.close();
    conn2.close();
    DeleteDbFiles.execute(getBaseDir(), "recovery", true);
    DeleteDbFiles.execute(getBaseDir(), "recovery2", true);
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) Recover(org.h2.tools.Recover)

Example 4 with RunScript

use of org.h2.tools.RunScript in project h2database by h2database.

the class TestRecovery method testRunScript.

private void testRunScript() throws SQLException {
    DeleteDbFiles.execute(getBaseDir(), "recovery", true);
    DeleteDbFiles.execute(getBaseDir(), "recovery2", true);
    org.h2.Driver.load();
    Connection conn = getConnection("recovery");
    Statement stat = conn.createStatement();
    stat.execute("create table \"Joe\"\"s Table\" as " + "select 1");
    stat.execute("create table test as " + "select * from system_range(1, 100)");
    stat.execute("create view \"TEST VIEW OF TABLE TEST\" as " + "select * from test");
    stat.execute("create table a(id int primary key) as " + "select * from system_range(1, 100)");
    stat.execute("create table b(id int references a(id)) as " + "select * from system_range(1, 100)");
    stat.execute("create table lob(c clob, b blob) as " + "select space(10000) || 'end', SECURE_RAND(10000)");
    stat.execute("create table d(d varchar) as " + "select space(10000) || 'end'");
    stat.execute("alter table a add foreign key(id) references b(id)");
    // all rows have the same value - so that SCRIPT can't re-order the rows
    stat.execute("create table e(id varchar) as " + "select space(10) from system_range(1, 1000)");
    stat.execute("create index idx_e_id on e(id)");
    conn.close();
    Recover rec = new Recover();
    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    rec.setOut(new PrintStream(buff));
    rec.runTool("-dir", getBaseDir(), "-db", "recovery", "-trace");
    String out = new String(buff.toByteArray());
    assertContains(out, "Created file");
    Connection conn2 = getConnection("recovery2");
    Statement stat2 = conn2.createStatement();
    String name = "recovery.h2.sql";
    stat2.execute("runscript from '" + getBaseDir() + "/" + name + "'");
    stat2.execute("select * from test");
    conn2.close();
    conn = getConnection("recovery");
    stat = conn.createStatement();
    conn2 = getConnection("recovery2");
    stat2 = conn2.createStatement();
    assertEqualDatabases(stat, stat2);
    conn.close();
    conn2.close();
    Recover.execute(getBaseDir(), "recovery");
    deleteDb("recovery");
    deleteDb("recovery2");
    FileUtils.delete(getBaseDir() + "/recovery.h2.sql");
    String dir = getBaseDir() + "/recovery.lobs.db";
    FileUtils.deleteRecursive(dir, false);
}
Also used : PrintStream(java.io.PrintStream) Statement(java.sql.Statement) Connection(java.sql.Connection) Recover(org.h2.tools.Recover) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with RunScript

use of org.h2.tools.RunScript in project h2database by h2database.

the class TestWeb method testTools.

private void testTools() throws Exception {
    if (config.memory || config.cipher != null) {
        return;
    }
    deleteDb(getTestName());
    Connection conn = getConnection(getTestName());
    conn.createStatement().execute("create table test(id int) as select 1");
    conn.close();
    Server server = new Server();
    server.setOut(new PrintStream(new ByteArrayOutputStream()));
    server.runTool("-web", "-webPort", "8182", "-properties", "null", "-tcp", "-tcpPort", "9101");
    try {
        String url = "http://localhost:8182";
        WebClient client;
        String result;
        client = new WebClient();
        result = client.get(url);
        client.readSessionId(result);
        result = client.get(url, "tools.jsp");
        FileUtils.delete(getBaseDir() + "/backup.zip");
        result = client.get(url, "tools.do?tool=Backup&args=-dir," + getBaseDir() + ",-db," + getTestName() + ",-file," + getBaseDir() + "/backup.zip");
        deleteDb(getTestName());
        assertTrue(FileUtils.exists(getBaseDir() + "/backup.zip"));
        result = client.get(url, "tools.do?tool=DeleteDbFiles&args=-dir," + getBaseDir() + ",-db," + getTestName());
        String fn = getBaseDir() + "/" + getTestName();
        if (config.mvStore) {
            fn += Constants.SUFFIX_MV_FILE;
        } else {
            fn += Constants.SUFFIX_PAGE_FILE;
        }
        assertFalse(FileUtils.exists(fn));
        result = client.get(url, "tools.do?tool=Restore&args=-dir," + getBaseDir() + ",-db," + getTestName() + ",-file," + getBaseDir() + "/backup.zip");
        assertTrue(FileUtils.exists(fn));
        FileUtils.delete(getBaseDir() + "/web.h2.sql");
        FileUtils.delete(getBaseDir() + "/backup.zip");
        result = client.get(url, "tools.do?tool=Recover&args=-dir," + getBaseDir() + ",-db," + getTestName());
        assertTrue(FileUtils.exists(getBaseDir() + "/" + getTestName() + ".h2.sql"));
        FileUtils.delete(getBaseDir() + "/web.h2.sql");
        result = client.get(url, "tools.do?tool=RunScript&args=-script," + getBaseDir() + "/" + getTestName() + ".h2.sql,-url," + getURL(getTestName(), true) + ",-user," + getUser() + ",-password," + getPassword());
        FileUtils.delete(getBaseDir() + "/" + getTestName() + ".h2.sql");
        assertTrue(FileUtils.exists(fn));
        deleteDb(getTestName());
    } finally {
        server.shutdown();
    }
}
Also used : PrintStream(java.io.PrintStream) Server(org.h2.tools.Server) Connection(java.sql.Connection) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Connection (java.sql.Connection)8 Statement (java.sql.Statement)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 PrintStream (java.io.PrintStream)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 Recover (org.h2.tools.Recover)3 SimpleResultSet (org.h2.tools.SimpleResultSet)3 JdbcConnection (org.h2.jdbc.JdbcConnection)2 DbException (org.h2.message.DbException)2 RunScript (org.h2.tools.RunScript)2 ExtendedJdbcTemplate (com.serotonin.db.spring.ExtendedJdbcTemplate)1 DatabaseSchemaDefinition (com.serotonin.m2m2.module.DatabaseSchemaDefinition)1 User (com.serotonin.m2m2.vo.User)1 DefaultDataPointPropertiesTemplateFactory (com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 DbContents (org.h2.bnf.context.DbContents)1 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)1