Search in sources :

Example 66 with ClientConfiguration

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

the class SystemTablesTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(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)");
                statement.execute("CREATE INDEX mytableindex ON mytable(name)");
                statement.execute("CREATE UNIQUE INDEX mytableindex2 ON mytable(name, key)");
                statement.execute("CREATE TABLE mytable2 (n2 int primary key auto_increment, name string not null, ts timestamp)");
                statement.execute("CREATE TABLE mytable3 (n2 int primary key auto_increment, age int not null default 99, phone long not null, salary double, married bool)");
                try (ResultSet rs = statement.executeQuery("SELECT * FROM SYSTABLES")) {
                    Set<String> tables = new HashSet<>();
                    while (rs.next()) {
                        String name = rs.getString("table_name");
                        tables.add(name);
                    }
                    assertTrue(tables.contains("mytable"));
                    assertTrue(tables.contains("mytable2"));
                    assertTrue(tables.contains("mytable3"));
                }
                DatabaseMetaData metaData = con.getMetaData();
                try (ResultSet rs = metaData.getTables(null, null, null, null)) {
                    List<List<String>> records = new ArrayList<>();
                    while (rs.next()) {
                        List<String> record = new ArrayList<>();
                        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                            String value = rs.getString(i + 1);
                            record.add(value);
                        }
                        records.add(record);
                    }
                    assertTrue(records.stream().filter(s -> s.contains("mytable")).findAny().isPresent());
                    assertTrue(records.stream().filter(s -> s.contains("mytable2")).findAny().isPresent());
                    assertTrue(records.stream().filter(s -> s.contains("mytable3")).findAny().isPresent());
                    assertTrue(records.stream().filter(s -> s.contains("systables")).findAny().isPresent());
                    assertTrue(records.stream().filter(s -> s.contains("syscolumns")).findAny().isPresent());
                    assertTrue(records.stream().filter(s -> s.contains("sysstatements")).findAny().isPresent());
                }
                try (ResultSet rs = metaData.getTables(null, null, "m%table", null)) {
                    List<List<String>> records = new ArrayList<>();
                    while (rs.next()) {
                        List<String> record = new ArrayList<>();
                        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                            String value = rs.getString(i + 1);
                            record.add(value);
                        }
                        records.add(record);
                    }
                    assertTrue(records.stream().filter(s -> s.contains("mytable")).findAny().isPresent());
                    assertEquals(1, records.size());
                }
                try (ResultSet rs = metaData.getTables(null, null, "m_table%", null)) {
                    List<List<String>> records = new ArrayList<>();
                    while (rs.next()) {
                        List<String> record = new ArrayList<>();
                        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                            String value = rs.getString(i + 1);
                            record.add(value);
                        }
                        records.add(record);
                    }
                    assertTrue(records.stream().filter(s -> s.contains("mytable")).findAny().isPresent());
                    assertTrue(records.stream().filter(s -> s.contains("mytable2")).findAny().isPresent());
                    assertTrue(records.stream().filter(s -> s.contains("mytable3")).findAny().isPresent());
                    assertEquals(3, records.size());
                }
                List<List<String>> records1 = new ArrayList<>();
                validateColumnsOfTable2(metaData, "mytable2", records1);
                List<List<String>> records2 = new ArrayList<>();
                validateColumnsOfTable2(metaData, "MyTable2", records2);
                // getColumns must be non case sensitive
                assertEquals(records1, records2);
                try (ResultSet rs = metaData.getColumns(null, null, "mytable3", null)) {
                    List<List<String>> records = new ArrayList<>();
                    while (rs.next()) {
                        List<String> record = new ArrayList<>();
                        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                            String value = rs.getString(i + 1);
                            record.add(value);
                        }
                        records.add(record);
                    }
                    assertEquals(5, records.size());
                    assertTrue(records.stream().filter(s -> s.contains("n2") && s.contains("integer")).findAny().isPresent());
                    assertTrue(records.stream().filter(s -> s.contains("age") && s.contains(Types.INTEGER + "") && s.contains("integer not null") && s.contains("99")).findAny().isPresent());
                    assertTrue(records.stream().filter(s -> s.contains("phone") && s.contains(Types.BIGINT + "") && s.contains("long not null")).findAny().isPresent());
                    assertTrue(records.stream().filter(s -> s.contains("salary") && s.contains(Types.DOUBLE + "") && s.contains("double")).findAny().isPresent());
                    assertTrue(records.stream().filter(s -> s.contains("married") && s.contains(Types.BOOLEAN + "") && s.contains("boolean")).findAny().isPresent());
                }
                try (ResultSet rs = metaData.getIndexInfo(null, null, "mytable", false, false)) {
                    List<List<String>> records = new ArrayList<>();
                    while (rs.next()) {
                        List<String> record = new ArrayList<>();
                        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                            String value = rs.getString(i + 1);
                            record.add(value);
                        }
                        if (rs.getString("INDEX_NAME").equals("mytableindex2")) {
                            assertFalse(rs.getBoolean("NON_UNIQUE"));
                        }
                        if (rs.getString("INDEX_NAME").equals("mytableindex")) {
                            assertTrue(rs.getBoolean("NON_UNIQUE"));
                        }
                        records.add(record);
                    }
                    // pk + secondary indexed
                    assertEquals(4, records.size());
                }
                try (ResultSet rs = metaData.getPrimaryKeys(null, null, "mytable")) {
                    List<List<String>> records = new ArrayList<>();
                    while (rs.next()) {
                        List<String> record = new ArrayList<>();
                        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                            String value = rs.getString(i + 1);
                            record.add(value);
                        }
                        records.add(record);
                    }
                    assertEquals(1, records.size());
                }
                try (ResultSet rs = metaData.getIndexInfo(null, null, "mytable", true, false)) {
                    List<List<String>> records = new ArrayList<>();
                    while (rs.next()) {
                        List<String> record = new ArrayList<>();
                        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                            String value = rs.getString(i + 1);
                            record.add(value);
                        }
                        records.add(record);
                    }
                    // only pk + 1 unique index with two columns
                    assertEquals(3, records.size());
                }
                try (ResultSet rs = metaData.getIndexInfo(null, null, null, true, false)) {
                    List<List<String>> records = new ArrayList<>();
                    while (rs.next()) {
                        List<String> record = new ArrayList<>();
                        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                            String value = rs.getString(i + 1);
                            record.add(value);
                        }
                        records.add(record);
                    }
                    // this is to be incremented at every new systable
                    assertEquals(28, records.size());
                }
                try (ResultSet rs = metaData.getSchemas()) {
                    List<List<String>> records = new ArrayList<>();
                    while (rs.next()) {
                        List<String> record = new ArrayList<>();
                        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                            String value = rs.getString(i + 1);
                            record.add(value);
                        }
                        records.add(record);
                    }
                    assertTrue(records.stream().filter(s -> s.contains(TableSpace.DEFAULT)).findAny().isPresent());
                    assertEquals(1, records.size());
                }
                try (ResultSet rs = metaData.getSchemas(null, "%" + TableSpace.DEFAULT.substring(2))) {
                    List<List<String>> records = new ArrayList<>();
                    while (rs.next()) {
                        List<String> record = new ArrayList<>();
                        for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                            String value = rs.getString(i + 1);
                            record.add(value);
                        }
                        records.add(record);
                    }
                    assertTrue(records.stream().filter(s -> s.contains(TableSpace.DEFAULT)).findAny().isPresent());
                    assertEquals(1, records.size());
                }
                try (ResultSet rs = metaData.getSchemas(null, "no")) {
                    List<List<String>> records = new ArrayList<>();
                    assertFalse(rs.next());
                }
            }
        }
    }
}
Also used : Server(herddb.server.Server) Connection(java.sql.Connection) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) List(java.util.List) Rule(org.junit.Rule) Assert.assertFalse(org.junit.Assert.assertFalse) ResultSet(java.sql.ResultSet) HDBClient(herddb.client.HDBClient) Statement(java.sql.Statement) TableSpace(herddb.model.TableSpace) Assert.assertEquals(org.junit.Assert.assertEquals) TemporaryFolder(org.junit.rules.TemporaryFolder) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Types(java.sql.Types) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DatabaseMetaData(java.sql.DatabaseMetaData) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) ClientConfiguration(herddb.client.ClientConfiguration) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 67 with ClientConfiguration

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

the class MysqlCompatilityTest method test2.

@Test
public void test2() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(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();
                Connection con2 = dataSource.getConnection();
                Statement statement = con.createStatement()) {
                con.setAutoCommit(false);
                statement.execute("CREATE TABLE `queuebouncecategory_history` (\n" + "  `queueid` int(11) NOT NULL,\n" + "  `idbouncecategory` int(6) NOT NULL,\n" + "  `refdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,\n" + "  `messagecount` bigint(20) NOT NULL,\n" + "  PRIMARY KEY (`queueid`,`refdate`,`idbouncecategory`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
                statement.executeUpdate("INSERT INTO `queuebouncecategory_history` VALUES (1,3,'2015-03-29 01:00:00',1)");
                statement.executeUpdate("INSERT INTO `queuebouncecategory_history` VALUES (1,3,'2015-03-29 02:00:00',1)");
                statement.executeUpdate("INSERT INTO `queuebouncecategory_history` VALUES (1,3,'2015-03-29 03:00:00',1)");
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 68 with ClientConfiguration

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

the class MysqlCompatilityTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        // assumeTrue(server.getManager().getPlanner() instanceof SQLPlanner);
        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();
                Connection con2 = dataSource.getConnection();
                Statement statement = con.createStatement()) {
                statement.execute("DROP TABLE IF EXISTS `sm_machine`;");
                statement.execute("CREATE TABLE `sm_machine` (\n" + "  `ip` varchar(20) NOT NULL DEFAULT '',\n" + "  `firefox_version` int(50) DEFAULT NULL,\n" + "  `chrome_version` int(50) DEFAULT NULL,\n" + "  `ie_version` int(50) DEFAULT NULL,\n" + "  `log` varchar(2000) DEFAULT NULL,\n" + "  `offset` int(50) DEFAULT NULL,\n" + "  PRIMARY KEY (`ip`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
                int multiInsertCount = statement.executeUpdate("INSERT INTO `sm_machine` VALUES" + "('10.168.10.106',26,36,9,NULL,1)," + "('10.168.10.107',26,31,10,NULL,1)," + "('10.168.10.108',26,36,11,NULL,2)," + "('10.168.10.109',33,38,10,NULL,3)," + "('10.168.10.110',33,38,10,NULL,4)");
                assertEquals(5, multiInsertCount);
                try (PreparedStatement ps = statement.getConnection().prepareStatement("INSERT INTO `sm_machine` VALUES" + "(?,?,?,?,?,?)," + "(?,?,?,?,?,?)," + "(?,?,?,?,?,?)," + "(?,?,?,?,?,?)," + "(?,?,?,?,?,?)")) {
                    int i = 1;
                    ps.setString(i++, "11.168.10.106");
                    ps.setInt(i++, 1);
                    ps.setInt(i++, 1);
                    ps.setString(i++, null);
                    ps.setInt(i++, 1);
                    ps.setInt(i++, 1);
                    ps.setString(i++, "11.168.10.107");
                    ps.setInt(i++, 1);
                    ps.setInt(i++, 1);
                    ps.setString(i++, null);
                    ps.setInt(i++, 1);
                    ps.setInt(i++, 1);
                    ps.setString(i++, "11.168.10.108");
                    ps.setInt(i++, 1);
                    ps.setInt(i++, 1);
                    ps.setString(i++, null);
                    ps.setInt(i++, 1);
                    ps.setInt(i++, 1);
                    ps.setString(i++, "11.168.10.109");
                    ps.setInt(i++, 1);
                    ps.setInt(i++, 1);
                    ps.setString(i++, null);
                    ps.setInt(i++, 1);
                    ps.setInt(i++, 1);
                    ps.setString(i++, "11.168.10.110");
                    ps.setInt(i++, 1);
                    ps.setInt(i++, 1);
                    ps.setString(i++, null);
                    ps.setInt(i++, 1);
                    ps.setInt(i++, 1);
                    ps.execute();
                    int multiInsertCount2 = ps.getUpdateCount();
                    assertEquals(5, multiInsertCount2);
                }
                statement.executeQuery("SELECT ip, `offset` FROM sm_machine WHERE `offset` = 1").close();
                statement.execute("DROP TABLE sm_machine;");
                try {
                    statement.executeQuery("SELECT COUNT(*) FROM sm_machine").close();
                    fail();
                } catch (SQLException err) {
                    assertTrue(err.getMessage().contains(herddb.model.TableDoesNotExistException.class.getName()) || err.getMessage().contains("SM_MACHINE"));
                }
                statement.execute("CREATE TABLE test_odd_names (\n" + "  value varchar(20) NOT NULL DEFAULT '',\n" + "  PRIMARY KEY (`value`)\n" + ")");
                statement.executeUpdate("INSERT INTO test_odd_names VALUES" + "('10.168.10.106')");
                try (ResultSet rs = statement.executeQuery("SELECT `value` FROM test_odd_names WHERE `value` = '10.168.10.106'")) {
                    assertTrue(rs.next());
                    assertEquals("10.168.10.106", rs.getString(1));
                    assertEquals("10.168.10.106", rs.getString("value"));
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 69 with ClientConfiguration

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

the class GeneratedKeysTest method testPreparedStatementLong.

@Test
public void testPreparedStatementLong() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(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);
                }
                // async
                try (PreparedStatementAsync delete = con.prepareStatement("DELETE FROM mytable").unwrap(PreparedStatementAsync.class);
                    PreparedStatementAsync prepared = con.prepareStatement("INSERT INTO mytable (name) values('name1')", Statement.RETURN_GENERATED_KEYS).unwrap(PreparedStatementAsync.class)) {
                    assertEquals(1, delete.executeUpdateAsync().get().intValue());
                    assertEquals(1, prepared.executeUpdateAsync().get().intValue());
                    Object key = null;
                    // getGeneratedKeys makes sense only if the Future has completed with success
                    try (ResultSet generatedKeys = prepared.getGeneratedKeys()) {
                        if (generatedKeys.next()) {
                            key = generatedKeys.getObject(1);
                        }
                    }
                    assertNotNull(key);
                    assertEquals(Long.valueOf(2), 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) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 70 with ClientConfiguration

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

the class GeneratedKeysTest method testStatementLong.

@Test
public void testStatementLong() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(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) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Aggregations

ClientConfiguration (herddb.client.ClientConfiguration)89 HDBClient (herddb.client.HDBClient)83 Test (org.junit.Test)79 Server (herddb.server.Server)56 HDBConnection (herddb.client.HDBConnection)45 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)41 Connection (java.sql.Connection)39 Statement (java.sql.Statement)34 PreparedStatement (java.sql.PreparedStatement)30 ResultSet (java.sql.ResultSet)29 Table (herddb.model.Table)23 CreateTableStatement (herddb.model.commands.CreateTableStatement)23 InsertStatement (herddb.model.commands.InsertStatement)22 ServerConfiguration (herddb.server.ServerConfiguration)17 ScanResultSet (herddb.client.ScanResultSet)13 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ProgressListener (herddb.backup.ProgressListener)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 HashSet (java.util.HashSet)12 Index (herddb.model.Index)9