Search in sources :

Example 26 with Server

use of herddb.server.Server in project herddb by diennea.

the class BatchTest method testBatch.

@Test
public void testBatch() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection();
                Statement create = con.createStatement();
                PreparedStatement statement = con.prepareStatement("INSERT INTO mytable (name) values(?)")) {
                create.execute("CREATE TABLE mytable (n1 int primary key auto_increment, name string)");
                {
                    for (int i = 0; i < 100; i++) {
                        statement.setString(1, "v" + i);
                        statement.addBatch();
                    }
                    int[] results = statement.executeBatch();
                    for (int i = 0; i < 100; i++) {
                        assertEquals(1, results[i]);
                    }
                    try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable ORDER BY n1")) {
                        int count = 0;
                        while (rs.next()) {
                            assertEquals("v" + count, rs.getString("name"));
                            assertEquals(count + 1, rs.getInt("n1"));
                            count++;
                        }
                        assertEquals(100, count);
                    }
                }
                int next_id;
                try (ResultSet rs = statement.executeQuery("SELECT MAX(n1) FROM mytable")) {
                    assertTrue(rs.next());
                    next_id = rs.getInt(1) + 1;
                    assertEquals(101, next_id);
                }
                statement.executeUpdate("DELETE FROM mytable");
                try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM mytable")) {
                    assertTrue(rs.next());
                    assertEquals(0, rs.getInt(1));
                }
                // transactions
                con.setAutoCommit(false);
                {
                    for (int i = 0; i < 100; i++) {
                        statement.setString(1, "v" + i);
                        statement.addBatch();
                    }
                    int[] results = statement.executeBatch();
                    for (int i = 0; i < 100; i++) {
                        assertEquals(1, results[i]);
                    }
                    try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable ORDER BY n1")) {
                        int count = 0;
                        while (rs.next()) {
                            assertEquals("v" + count, rs.getString("name"));
                            assertEquals(count + next_id, rs.getInt("n1"));
                            count++;
                        }
                        assertEquals(100, count);
                    }
                }
                con.commit();
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 27 with Server

use of herddb.server.Server in project herddb by diennea.

the class GeneratedKeysTest method testStatementInt.

@Test
public void testStatementInt() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection();
                Statement statement = con.createStatement()) {
                statement.execute("CREATE TABLE mytable (n1 int primary key auto_increment, name string)");
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (name) values('name1')", Statement.RETURN_GENERATED_KEYS));
                try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
                    int count = 0;
                    while (rs.next()) {
                        count++;
                    }
                    assertEquals(1, count);
                }
                Object key = null;
                try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
                    if (generatedKeys.next()) {
                        key = generatedKeys.getObject(1);
                    }
                }
                assertNotNull(key);
                assertEquals(Integer.valueOf(1), key);
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 28 with Server

use of herddb.server.Server in project herddb by diennea.

the class GeneratedKeysTest method testPreparedStatementLong.

@Test
public void testPreparedStatementLong() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection();
                Statement statement = con.createStatement()) {
                statement.execute("CREATE TABLE mytable (n1 long primary key auto_increment, name string)");
                try (PreparedStatement prepared = con.prepareStatement("INSERT INTO mytable (name) values('name1')", Statement.RETURN_GENERATED_KEYS)) {
                    assertEquals(1, prepared.executeUpdate());
                    Object key = null;
                    try (ResultSet generatedKeys = prepared.getGeneratedKeys()) {
                        if (generatedKeys.next()) {
                            key = generatedKeys.getObject(1);
                        }
                    }
                    assertNotNull(key);
                    assertEquals(Long.valueOf(1), key);
                }
                try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
                    int count = 0;
                    while (rs.next()) {
                        count++;
                    }
                    assertEquals(1, count);
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 29 with Server

use of herddb.server.Server in project herddb by diennea.

the class GeneratedKeysTest method testStatementLong.

@Test
public void testStatementLong() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection();
                Statement statement = con.createStatement()) {
                statement.execute("CREATE TABLE mytable (n1 long primary key auto_increment, name string)");
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (name) values('name1')", Statement.RETURN_GENERATED_KEYS));
                try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
                    int count = 0;
                    while (rs.next()) {
                        count++;
                    }
                    assertEquals(1, count);
                }
                Object key = null;
                try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
                    if (generatedKeys.next()) {
                        key = generatedKeys.getObject(1);
                    }
                }
                assertNotNull(key);
                assertEquals(Long.valueOf(1), key);
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 30 with Server

use of herddb.server.Server in project herddb by diennea.

the class PrettySQLExceptionTest method test.

@Test
public void test() throws Exception {
    ServerConfiguration serverConfiguration = new ServerConfiguration(folder.newFolder().toPath());
    try (Server server = new Server(serverConfiguration)) {
        server.start();
        server.waitForStandaloneBoot();
        try (Connection connection = DriverManager.getConnection("jdbc:herddb:server:localhost:7000?");
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM bad_table")) {
            fail();
        } catch (SQLException errorExpected) {
            System.out.println("EXCEPTION MESSAGE: " + errorExpected.getMessage());
            assertTrue("herddb.model.TableDoesNotExistException: no such table bad_table in tablespace default".equals(errorExpected.getMessage()) || errorExpected.getMessage().contains("Object 'BAD_TABLE' not found"));
        }
    }
}
Also used : Server(herddb.server.Server) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Aggregations

Server (herddb.server.Server)36 Connection (java.sql.Connection)33 Test (org.junit.Test)33 ServerConfiguration (herddb.server.ServerConfiguration)32 Statement (java.sql.Statement)32 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)27 ClientConfiguration (herddb.client.ClientConfiguration)26 HDBClient (herddb.client.HDBClient)26 ResultSet (java.sql.ResultSet)25 PreparedStatement (java.sql.PreparedStatement)22 SQLException (java.sql.SQLException)6 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)2 HDBException (herddb.client.HDBException)1 BasicHerdDBDataSource (herddb.jdbc.BasicHerdDBDataSource)1 TableSpace (herddb.model.TableSpace)1 SQLPlanner (herddb.sql.SQLPlanner)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 List (java.util.List)1 Set (java.util.Set)1