Search in sources :

Example 66 with Server

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

the class TestPgServer method testPrepareWithUnspecifiedType.

private void testPrepareWithUnspecifiedType() throws Exception {
    if (!getPgJdbcDriver()) {
        return;
    }
    Server server = createPgServer("-pgPort", "5535", "-pgDaemon", "-key", "pgserver", "mem:pgserver");
    try {
        Properties props = new Properties();
        props.setProperty("user", "sa");
        props.setProperty("password", "sa");
        // force server side prepare
        props.setProperty("prepareThreshold", "1");
        Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/pgserver", props);
        Statement stmt = conn.createStatement();
        stmt.executeUpdate("create table t1 (id integer, value timestamp)");
        stmt.close();
        PreparedStatement pstmt = conn.prepareStatement("insert into t1 values(100500, ?)");
        // assertTrue(((PGStatement) pstmt).isUseServerPrepare());
        assertEquals(Types.TIMESTAMP, pstmt.getParameterMetaData().getParameterType(1));
        Timestamp t = new Timestamp(System.currentTimeMillis());
        pstmt.setObject(1, t);
        assertEquals(1, pstmt.executeUpdate());
        pstmt.close();
        pstmt = conn.prepareStatement("SELECT * FROM t1 WHERE value = ?");
        assertEquals(Types.TIMESTAMP, pstmt.getParameterMetaData().getParameterType(1));
        pstmt.setObject(1, t);
        ResultSet rs = pstmt.executeQuery();
        assertTrue(rs.next());
        assertEquals(100500, rs.getInt(1));
        rs.close();
        pstmt.close();
        conn.close();
    } finally {
        server.stop();
    }
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) Timestamp(java.sql.Timestamp)

Example 67 with Server

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

the class TestPgServer method testKeyAlias.

private void testKeyAlias() throws SQLException {
    if (!getPgJdbcDriver()) {
        return;
    }
    Server server = createPgServer("-pgPort", "5535", "-pgDaemon", "-key", "pgserver", "mem:pgserver");
    try {
        Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/pgserver", "sa", "sa");
        Statement stat = conn.createStatement();
        // confirm that we've got the in memory implementation
        // by creating a table and checking flags
        stat.execute("create table test(id int primary key, name varchar)");
        ResultSet rs = stat.executeQuery("select storage_type from information_schema.tables " + "where table_name = 'TEST'");
        assertTrue(rs.next());
        assertEquals("MEMORY", rs.getString(1));
        conn.close();
    } finally {
        server.stop();
    }
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 68 with Server

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

the class TestPgServer method testCancelQuery.

private void testCancelQuery() throws Exception {
    if (!getPgJdbcDriver()) {
        return;
    }
    Server server = createPgServer("-pgPort", "5535", "-pgDaemon", "-key", "pgserver", "mem:pgserver");
    ExecutorService executor = Executors.newSingleThreadExecutor();
    try {
        Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/pgserver", "sa", "sa");
        final Statement stat = conn.createStatement();
        stat.execute("create alias sleep for \"java.lang.Thread.sleep\"");
        // create a table with 200 rows (cancel interval is 127)
        stat.execute("create table test(id int)");
        for (int i = 0; i < 200; i++) {
            stat.execute("insert into test (id) values (rand())");
        }
        Future<Boolean> future = executor.submit(new Callable<Boolean>() {

            @Override
            public Boolean call() throws SQLException {
                return stat.execute("select id, sleep(5) from test");
            }
        });
        // give it a little time to start and then cancel it
        Thread.sleep(100);
        stat.cancel();
        try {
            future.get();
            throw new IllegalStateException();
        } catch (ExecutionException e) {
            assertStartsWith(e.getCause().getMessage(), "ERROR: canceling statement due to user request");
        } finally {
            conn.close();
        }
    } finally {
        server.stop();
        executor.shutdown();
    }
    deleteDb("pgserver");
}
Also used : Server(org.h2.tools.Server) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ExecutorService(java.util.concurrent.ExecutorService) Connection(java.sql.Connection) ExecutionException(java.util.concurrent.ExecutionException)

Example 69 with Server

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

the class TestPgServer method testPgAdapter.

private void testPgAdapter() throws SQLException {
    deleteDb("pgserver");
    Server server = Server.createPgServer("-baseDir", getBaseDir(), "-pgPort", "5535", "-pgDaemon");
    assertEquals(5535, server.getPort());
    assertEquals("Not started", server.getStatus());
    server.start();
    assertStartsWith(server.getStatus(), "PG server running at pg://");
    try {
        if (getPgJdbcDriver()) {
            testPgClient();
        }
    } finally {
        server.stop();
    }
}
Also used : Server(org.h2.tools.Server)

Example 70 with Server

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

the class TestPgServer method testBinaryTypes.

private void testBinaryTypes() throws SQLException {
    if (!getPgJdbcDriver()) {
        return;
    }
    Server server = createPgServer("-pgPort", "5535", "-pgDaemon", "-key", "pgserver", "mem:pgserver");
    try {
        Properties props = new Properties();
        props.setProperty("user", "sa");
        props.setProperty("password", "sa");
        // force binary
        props.setProperty("prepareThreshold", "-1");
        Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5535/pgserver", props);
        Statement stat = conn.createStatement();
        stat.execute("create table test(x1 varchar, x2 int, " + "x3 smallint, x4 bigint, x5 double, x6 float, " + "x7 real, x8 boolean, x9 char, x10 bytea, " + "x11 date, x12 time, x13 timestamp, x14 numeric)");
        PreparedStatement ps = conn.prepareStatement("insert into test values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        ps.setString(1, "test");
        ps.setInt(2, 12345678);
        ps.setShort(3, (short) 12345);
        ps.setLong(4, 1234567890123L);
        ps.setDouble(5, 123.456);
        ps.setFloat(6, 123.456f);
        ps.setFloat(7, 123.456f);
        ps.setBoolean(8, true);
        ps.setByte(9, (byte) 0xfe);
        ps.setBytes(10, new byte[] { 'a', (byte) 0xfe, '\127' });
        ps.setDate(11, Date.valueOf("2015-01-31"));
        ps.setTime(12, Time.valueOf("20:11:15"));
        ps.setTimestamp(13, Timestamp.valueOf("2001-10-30 14:16:10.111"));
        ps.setBigDecimal(14, new BigDecimal("12345678901234567890.12345"));
        ps.execute();
        for (int i = 1; i <= 14; i++) {
            ps.setNull(i, Types.NULL);
        }
        ps.execute();
        ResultSet rs = stat.executeQuery("select * from test");
        assertTrue(rs.next());
        assertEquals("test", rs.getString(1));
        assertEquals(12345678, rs.getInt(2));
        assertEquals((short) 12345, rs.getShort(3));
        assertEquals(1234567890123L, rs.getLong(4));
        assertEquals(123.456, rs.getDouble(5));
        assertEquals(123.456f, rs.getFloat(6));
        assertEquals(123.456f, rs.getFloat(7));
        assertEquals(true, rs.getBoolean(8));
        assertEquals((byte) 0xfe, rs.getByte(9));
        assertEquals(new byte[] { 'a', (byte) 0xfe, '\127' }, rs.getBytes(10));
        assertEquals(Date.valueOf("2015-01-31"), rs.getDate(11));
        assertEquals(Time.valueOf("20:11:15"), rs.getTime(12));
        assertEquals(Timestamp.valueOf("2001-10-30 14:16:10.111"), rs.getTimestamp(13));
        assertEquals(new BigDecimal("12345678901234567890.12345"), rs.getBigDecimal(14));
        assertTrue(rs.next());
        for (int i = 1; i <= 14; i++) {
            assertNull(rs.getObject(i));
        }
        assertFalse(rs.next());
        conn.close();
    } finally {
        server.stop();
    }
}
Also used : Server(org.h2.tools.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) BigDecimal(java.math.BigDecimal)

Aggregations

Server (org.h2.tools.Server)47 Connection (java.sql.Connection)27 SQLException (java.sql.SQLException)22 Statement (java.sql.Statement)17 PreparedStatement (java.sql.PreparedStatement)15 IOException (java.io.IOException)13 ResultSet (java.sql.ResultSet)10 DbException (org.h2.message.DbException)9 Socket (java.net.Socket)8 Test (org.junit.Test)8 Properties (java.util.Properties)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 File (java.io.File)6 ServerSocket (java.net.ServerSocket)6 PrintStream (java.io.PrintStream)5 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)5 Task (org.h2.util.Task)5 TcpServer (org.h2.server.TcpServer)4 PgServer (org.h2.server.pg.PgServer)4 WebServer (org.h2.server.web.WebServer)4