Search in sources :

Example 56 with Set

use of org.h2.command.dml.Set in project h2database by h2database.

the class FunctionMultiReturn method polar2Cartesian.

/**
 * Convert polar coordinates to cartesian coordinates. The function may be
 * called twice, once to retrieve the result columns (with null parameters),
 * and the second time to return the data.
 *
 * @param r the distance from the point 0/0
 * @param alpha the angle
 * @return a result set with two columns: x and y
 */
public static ResultSet polar2Cartesian(Double r, Double alpha) {
    SimpleResultSet rs = new SimpleResultSet();
    rs.addColumn("X", Types.DOUBLE, 0, 0);
    rs.addColumn("Y", Types.DOUBLE, 0, 0);
    if (r != null && alpha != null) {
        double x = r.doubleValue() * Math.cos(alpha.doubleValue());
        double y = r.doubleValue() * Math.sin(alpha.doubleValue());
        rs.addRow(x, y);
    }
    return rs;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet)

Example 57 with Set

use of org.h2.command.dml.Set in project h2database by h2database.

the class FunctionMultiReturn method polar2CartesianSet.

/**
 * Convert a set of polar coordinates to cartesian coordinates. The function
 * may be called twice, once to retrieve the result columns (with null
 * parameters), and the second time to return the data.
 *
 * @param conn the connection
 * @param query the query
 * @return a result set with the coordinates
 */
public static ResultSet polar2CartesianSet(Connection conn, String query) throws SQLException {
    SimpleResultSet result = new SimpleResultSet();
    result.addColumn("R", Types.DOUBLE, 0, 0);
    result.addColumn("A", Types.DOUBLE, 0, 0);
    result.addColumn("X", Types.DOUBLE, 0, 0);
    result.addColumn("Y", Types.DOUBLE, 0, 0);
    if (query != null) {
        ResultSet rs = conn.createStatement().executeQuery(query);
        while (rs.next()) {
            double r = rs.getDouble("R");
            double alpha = rs.getDouble("A");
            double x = r * Math.cos(alpha);
            double y = r * Math.sin(alpha);
            result.addRow(r, alpha, x, y);
        }
    }
    return result;
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet)

Example 58 with Set

use of org.h2.command.dml.Set in project h2database by h2database.

the class TestMVTableEngine method testShrinkDatabaseFile.

private void testShrinkDatabaseFile() throws Exception {
    if (config.memory) {
        return;
    }
    deleteDb(getTestName());
    String dbName = getTestName() + ";MV_STORE=TRUE";
    Connection conn;
    Statement stat;
    long maxSize = 0;
    // by default, the database does not shrink for 45 seconds
    int retentionTime = 45000;
    for (int i = 0; i < 20; i++) {
        // the first 10 times, keep the default retention time
        // then switch to 0, at which point the database file
        // should stop to grow
        conn = getConnection(dbName);
        stat = conn.createStatement();
        if (i == 10) {
            stat.execute("set retention_time 0");
            retentionTime = 0;
        }
        ResultSet rs = stat.executeQuery("select value from information_schema.settings " + "where name='RETENTION_TIME'");
        assertTrue(rs.next());
        assertEquals(retentionTime, rs.getInt(1));
        stat.execute("create table test(id int primary key, data varchar)");
        stat.execute("insert into test select x, space(100) " + "from system_range(1, 1000)");
        // this table is kept
        if (i < 10) {
            stat.execute("create table test" + i + "(id int primary key, data varchar) " + "as select x, space(10) from system_range(1, 100)");
        }
        // force writing the chunk
        stat.execute("checkpoint");
        // drop the table - but the chunk is still used
        stat.execute("drop table test");
        stat.execute("checkpoint");
        stat.execute("shutdown immediately");
        try {
            conn.close();
        } catch (Exception e) {
        // ignore
        }
        String fileName = getBaseDir() + "/" + getTestName() + Constants.SUFFIX_MV_FILE;
        long size = FileUtils.size(fileName);
        if (i < 10) {
            maxSize = (int) (Math.max(size, maxSize) * 1.2);
        } else if (size > maxSize) {
            fail(i + " size: " + size + " max: " + maxSize);
        }
    }
    long sizeOld = FileUtils.size(getBaseDir() + "/" + getTestName() + Constants.SUFFIX_MV_FILE);
    conn = getConnection(dbName);
    stat = conn.createStatement();
    stat.execute("shutdown compact");
    conn.close();
    long sizeNew = FileUtils.size(getBaseDir() + "/" + getTestName() + Constants.SUFFIX_MV_FILE);
    assertTrue("new: " + sizeNew + " old: " + sizeOld, sizeNew < sizeOld);
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) Savepoint(java.sql.Savepoint) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 59 with Set

use of org.h2.command.dml.Set in project h2database by h2database.

the class TestMVTableEngine method testWriteDelay.

private void testWriteDelay() throws Exception {
    if (config.memory) {
        return;
    }
    Connection conn;
    Statement stat;
    ResultSet rs;
    deleteDb(getTestName());
    conn = getConnection(getTestName() + ";MV_STORE=TRUE");
    stat = conn.createStatement();
    stat.execute("create table test(id int)");
    stat.execute("set write_delay 0");
    stat.execute("insert into test values(1)");
    stat.execute("shutdown immediately");
    try {
        conn.close();
    } catch (Exception e) {
    // ignore
    }
    conn = getConnection(getTestName() + ";MV_STORE=TRUE");
    stat = conn.createStatement();
    rs = stat.executeQuery("select * from test");
    assertTrue(rs.next());
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 60 with Set

use of org.h2.command.dml.Set in project h2database by h2database.

the class TestMVTableEngine method testTwoPhaseCommit.

private void testTwoPhaseCommit() throws Exception {
    if (config.memory) {
        return;
    }
    Connection conn;
    Statement stat;
    deleteDb(getTestName());
    String url = getTestName() + ";MV_STORE=TRUE";
    url = getURL(url, true);
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar)");
    stat.execute("set write_delay 0");
    conn.setAutoCommit(false);
    stat.execute("insert into test values(1, 'Hello')");
    stat.execute("prepare commit test_tx");
    stat.execute("shutdown immediately");
    JdbcUtils.closeSilently(conn);
    conn = getConnection(url);
    stat = conn.createStatement();
    ResultSet rs;
    rs = stat.executeQuery("select * from information_schema.in_doubt");
    assertTrue(rs.next());
    stat.execute("commit transaction test_tx");
    rs = stat.executeQuery("select * from test");
    assertTrue(rs.next());
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

Aggregations

SQLException (java.sql.SQLException)107 DbException (org.h2.message.DbException)80 PreparedStatement (java.sql.PreparedStatement)78 Connection (java.sql.Connection)70 Statement (java.sql.Statement)63 ResultSet (java.sql.ResultSet)62 Value (org.h2.value.Value)51 JdbcConnection (org.h2.jdbc.JdbcConnection)48 SimpleResultSet (org.h2.tools.SimpleResultSet)36 HashSet (java.util.HashSet)28 Column (org.h2.table.Column)24 Savepoint (java.sql.Savepoint)23 ValueString (org.h2.value.ValueString)21 IOException (java.io.IOException)20 Random (java.util.Random)17 Expression (org.h2.expression.Expression)16 Task (org.h2.util.Task)16 ExpressionColumn (org.h2.expression.ExpressionColumn)14 ValueExpression (org.h2.expression.ValueExpression)13 IndexColumn (org.h2.table.IndexColumn)13