Search in sources :

Example 6 with PgServer

use of org.h2.server.pg.PgServer 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 7 with PgServer

use of org.h2.server.pg.PgServer 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)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 Statement (java.sql.Statement)6 ResultSet (java.sql.ResultSet)4 Properties (java.util.Properties)3 Timestamp (java.sql.Timestamp)2 BigDecimal (java.math.BigDecimal)1 Date (java.sql.Date)1 SQLException (java.sql.SQLException)1 Time (java.sql.Time)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1