Search in sources :

Example 16 with ClientConfiguration

use of herddb.client.ClientConfiguration in project herddb by diennea.

the class GeneratedKeysTest method testPreparedStatementInt.

@Test
public void testPreparedStatementInt() 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)");
                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(Integer.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 17 with ClientConfiguration

use of herddb.client.ClientConfiguration in project herddb by diennea.

the class ScanHugeTableTest method testBatch.

@Test
public void testBatch() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.getManager().setMaxDataUsedMemory(750 * 1024 * 1024);
        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 s = con.createStatement();
                PreparedStatement ps = con.prepareStatement("INSERT INTO mytable (n1, name) values(?,?)")) {
                s.execute("CREATE TABLE mytable (n1 int primary key, name string)");
                String bigPrefix = StringUtils.repeat("Test", 300);
                // int size = 1_000_000;
                int size = 100_000;
                {
                    long _start = System.currentTimeMillis();
                    con.setAutoCommit(false);
                    for (int i = 0; i < size; i++) {
                        ps.setInt(1, i);
                        ps.setString(2, bigPrefix + i);
                        ps.addBatch();
                        if (i % 60000 == 0) {
                            ps.executeBatch();
                            con.commit();
                            long _stop = System.currentTimeMillis();
                            System.out.println("written " + i + " records_ " + (_stop - _start) + " ms");
                        }
                    }
                    ps.executeBatch();
                    con.commit();
                    long _stop = System.currentTimeMillis();
                    System.out.println("insert: " + (_stop - _start) + " ms");
                }
                server.getManager().checkpoint();
                con.setAutoCommit(true);
                {
                    long _start = System.currentTimeMillis();
                    try (ResultSet rs = s.executeQuery("SELECT COUNT(*) from mytable")) {
                        assertTrue(rs.next());
                        long res = rs.getLong(1);
                        assertEquals(size, res);
                    }
                    long _stop = System.currentTimeMillis();
                    System.out.println("count: " + (_stop - _start) + " ms");
                }
                {
                    long _start = System.currentTimeMillis();
                    try (ResultSet rs = s.executeQuery("SELECT * from mytable")) {
                        int i = 0;
                        while (rs.next()) {
                            if (i % 100000 == 0) {
                                System.out.println("read " + i + " records");
                            }
                            i++;
                        }
                        assertEquals(size, i);
                    }
                    long _stop = System.currentTimeMillis();
                    System.out.println("read: " + (_stop - _start) + " ms");
                }
            }
        }
    }
}
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 18 with ClientConfiguration

use of herddb.client.ClientConfiguration in project herddb by diennea.

the class SimpleDataSourceTest method test.

@Test
public void test() 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 (key string primary key, name string)");
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,NamE) values('k1','name1')"));
                try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
                    boolean found = false;
                    while (rs.next()) {
                        String key = rs.getString("key");
                        String name = rs.getString("name");
                        assertEquals("k1", key);
                        assertEquals("name1", name);
                        found = true;
                    }
                    assertTrue(found);
                }
                assertEquals(1, statement.executeUpdate("UPDATE MYTaBLE set name='name2' where key='k1'"));
                try (ResultSet rs = statement.executeQuery("SELECT KeY,nAme FROM mytable")) {
                    boolean found = false;
                    while (rs.next()) {
                        String key = rs.getString("key");
                        String name = rs.getString("naMe");
                        assertEquals("k1", key);
                        assertEquals("name2", name);
                        found = true;
                    }
                    assertTrue(found);
                }
                try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
                    boolean found = false;
                    while (rs.next()) {
                        String key = rs.getString("key");
                        String name = rs.getString("name");
                        assertEquals("k1", key);
                        assertEquals("name2", name);
                        found = true;
                    }
                    assertTrue(found);
                }
                assertEquals(1, statement.executeUpdate("DELETE FROM mytable where name='name2' and key='k1'"));
                assertEquals(0, statement.executeUpdate("UPDATE mytable set name='name2' where key='k1'"));
                try (PreparedStatement ps = con.prepareStatement("INSERT INTO mytable (key,name) values(?,?)")) {
                    for (int i = 0; i < 10; i++) {
                        ps.setString(1, "kp_" + i);
                        ps.setString(2, "n2_" + i);
                        assertEquals(1, ps.executeUpdate());
                    }
                }
                try (PreparedStatement ps = con.prepareStatement("SELECT * FROM mytable");
                    ResultSet rs = ps.executeQuery()) {
                    Set<String> foundKeys = new HashSet<>();
                    while (rs.next()) {
                        foundKeys.add(rs.getString("key"));
                    }
                    for (int i = 0; i < 10; i++) {
                        assertTrue(foundKeys.contains("kp_" + i));
                    }
                }
                try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) as total FROM mytable");
                    ResultSet rs = ps.executeQuery()) {
                    assertTrue(rs.next());
                    assertEquals(10, rs.getLong("total"));
                }
                try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) as cc FROM mytable");
                    ResultSet rs = ps.executeQuery()) {
                    assertTrue(rs.next());
                    assertEquals(10, rs.getLong("cc"));
                    assertEquals(10, rs.getLong(1));
                    assertEquals(Long.valueOf(10), rs.getObject(1));
                    assertEquals(Long.valueOf(10), rs.getObject("cc"));
                }
            }
        }
    }
}
Also used : Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with ClientConfiguration

use of herddb.client.ClientConfiguration in project herddb by diennea.

the class SimpleExpressionsTest method test.

@Test
public void test() 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)) {
                try (Connection con = dataSource.getConnection();
                    Statement statement = con.createStatement()) {
                    statement.execute("CREATE TABLE mytable (k1 string primary key, n1 int, l1 long, t1 timestamp, nu string, b1 bool, d1 double)");
                }
                try (Connection con = dataSource.getConnection();
                    PreparedStatement statement = con.prepareStatement("INSERT INTO mytable(k1,n1,l1,t1,nu,b1,d1) values(?,?,?,?,?,?,?)")) {
                    int i = 1;
                    statement.setString(i++, "mykey");
                    statement.setInt(i++, 1);
                    statement.setLong(i++, 2);
                    statement.setTimestamp(i++, new java.sql.Timestamp(System.currentTimeMillis()));
                    statement.setString(i++, null);
                    statement.setBoolean(i++, true);
                    statement.setDouble(i++, 1.5);
                    int rows = statement.executeUpdate();
                    Assert.assertEquals(1, rows);
                }
                // Types
                try (Connection con = dataSource.getConnection()) {
                    // String
                    try (PreparedStatement statement = con.prepareStatement("SELECT k1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals("mykey", rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT upper(k1) FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals("MYKEY", rs.getObject(1));
                        }
                    }
                    // Number
                    try (PreparedStatement statement = con.prepareStatement("SELECT n1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(1, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT n1*5 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(5.0, rs.getObject(1));
                            assertEquals(5, rs.getLong(1));
                        }
                    }
                    // Long
                    try (PreparedStatement statement = con.prepareStatement("SELECT l1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(2L, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT l1*5 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(10.0, rs.getObject(1));
                            assertEquals(10, rs.getLong(1));
                        }
                    }
                    // Null
                    try (PreparedStatement statement = con.prepareStatement("SELECT nu FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(null, rs.getObject(1));
                        }
                    }
                    // Double
                    try (PreparedStatement statement = con.prepareStatement("SELECT 3/2 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(1.5, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT d1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(1.5, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT d1*5/2 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(3.75, rs.getObject(1));
                            assertEquals(3.75, rs.getDouble(1), 0);
                            assertEquals(3, rs.getLong(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT -n1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(-1, rs.getObject(1));
                            assertEquals(-1, rs.getDouble(1), 0);
                            assertEquals(-1, rs.getInt(1), 0);
                            assertEquals(-1, rs.getLong(1), 0);
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT -n1-n1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(-2L, rs.getObject(1));
                            assertEquals(-2, rs.getDouble(1), 0);
                            assertEquals(-2, rs.getInt(1), 0);
                            assertEquals(-2, rs.getLong(1), 0);
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT +n1+n1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(2L, rs.getObject(1));
                            assertEquals(2, rs.getDouble(1), 0);
                            assertEquals(2, rs.getInt(1), 0);
                            assertEquals(2, rs.getLong(1), 0);
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT n1*n1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(1L, rs.getObject(1));
                            assertEquals(1, rs.getDouble(1), 0);
                            assertEquals(1, rs.getInt(1), 0);
                            assertEquals(1, rs.getLong(1), 0);
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT -d1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(-1.5, rs.getObject(1));
                            assertEquals(-1.5, rs.getDouble(1), 0);
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT +d1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(1.5, rs.getObject(1));
                            assertEquals(1.5, rs.getDouble(1), 0);
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT +d1+d1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(3, rs.getDouble(1), 0);
                            assertEquals(3.0, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT +d1*d1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(2.25, rs.getDouble(1), 0);
                            assertEquals(2.25, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT -d1+d1 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(0, rs.getDouble(1), 0);
                            assertEquals(0.0, rs.getObject(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT +d1+4 FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(5.5, rs.getDouble(1), 0);
                            assertEquals(5.5, rs.getObject(1));
                        }
                    }
                    // Boolean
                    try (PreparedStatement statement = con.prepareStatement("SELECT true FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(true, rs.getObject(1));
                            assertEquals(true, rs.getBoolean(1));
                        }
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT false FROM mytable")) {
                        try (ResultSet rs = statement.executeQuery()) {
                            assertTrue(rs.next());
                            assertEquals(false, rs.getObject(1));
                            assertEquals(false, rs.getBoolean(1));
                        }
                    }
                }
            }
        }
    }
}
Also used : Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 20 with ClientConfiguration

use of herddb.client.ClientConfiguration in project herddb by diennea.

the class SimpleJoinTest method test.

@Test
public void test() 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 (key string primary key, name string)");
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k1','name1')"));
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k2','name2')"));
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k3','name3')"));
                try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable a" + " INNER JOIN mytable b ON 1=1")) {
                    int count = 0;
                    while (rs.next()) {
                        count++;
                    }
                    assertEquals(9, 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) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Aggregations

ClientConfiguration (herddb.client.ClientConfiguration)53 HDBClient (herddb.client.HDBClient)53 Test (org.junit.Test)49 HDBConnection (herddb.client.HDBConnection)26 Server (herddb.server.Server)26 ServerConfiguration (herddb.server.ServerConfiguration)26 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)26 Connection (java.sql.Connection)25 Statement (java.sql.Statement)24 PreparedStatement (java.sql.PreparedStatement)22 ResultSet (java.sql.ResultSet)19 Table (herddb.model.Table)11 CreateTableStatement (herddb.model.commands.CreateTableStatement)11 InsertStatement (herddb.model.commands.InsertStatement)10 ScanResultSet (herddb.client.ScanResultSet)8 HashSet (java.util.HashSet)7 Map (java.util.Map)7 ProgressListener (herddb.backup.ProgressListener)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6