Search in sources :

Example 51 with StaticClientSideMetadataProvider

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

the class JdbcForeignKeyMetadataTest 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 ptable (pkey string primary key, p1 string, p2 string)");
                statement.execute("CREATE TABLE ctable (ckey string primary key, c1 string, c2 string," + "        CONSTRAINT `fk1` FOREIGN KEY (`c1`,`c2`) REFERENCES ptable(`p1`,`p2`) ON DELETE CASCADE)");
                DatabaseMetaData metaData = con.getMetaData();
                try (ResultSet rs = metaData.getImportedKeys(null, null, "CTABLE")) {
                    verifyForeignKeyResultSet(rs);
                }
                try (ResultSet rs = metaData.getImportedKeys(null, TableSpace.DEFAULT, "CTABLE")) {
                    verifyForeignKeyResultSet(rs);
                }
                try (ResultSet rs = metaData.getImportedKeys(null, TableSpace.DEFAULT, "PTABLE")) {
                    assertFalse(rs.next());
                }
                try (ResultSet rs = metaData.getExportedKeys(null, null, "ptable")) {
                    verifyForeignKeyResultSet(rs);
                }
                try (ResultSet rs = metaData.getExportedKeys(null, TableSpace.DEFAULT, "ptable")) {
                    verifyForeignKeyResultSet(rs);
                }
                try (ResultSet rs = metaData.getExportedKeys(null, null, "CTABLE")) {
                    assertFalse(rs.next());
                }
                try (ResultSet rs = metaData.getCrossReference(null, TableSpace.DEFAULT, "ptable", null, TableSpace.DEFAULT, "ctable")) {
                    verifyForeignKeyResultSet(rs);
                }
                try (ResultSet rs = metaData.getCrossReference(null, TableSpace.DEFAULT, "ptable", null, null, "ctable")) {
                    verifyForeignKeyResultSet(rs);
                }
                try (ResultSet rs = metaData.getCrossReference(null, null, "ptable", null, null, "ctable")) {
                    verifyForeignKeyResultSet(rs);
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 52 with StaticClientSideMetadataProvider

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

the class HerdDBEmbeddedDataSource method startEmbeddedServer.

private void startEmbeddedServer() throws SQLException {
    if (!serverInitialized) {
        ServerConfiguration serverConfiguration = new ServerConfiguration(properties);
        try {
            serverConfiguration.readJdbcUrl(url);
        } catch (RuntimeException err) {
            throw new SQLException(err);
        }
        startServer = serverConfiguration.getBoolean("server.start", startServer);
        String mode = serverConfiguration.getString(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_LOCAL);
        if (ServerConfiguration.PROPERTY_MODE_LOCAL.equals(mode) || (ServerConfiguration.PROPERTY_MODE_STANDALONE.equals(mode) && startServer) || (ServerConfiguration.PROPERTY_MODE_CLUSTER.equals(mode) && startServer) || (ServerConfiguration.PROPERTY_MODE_DISKLESSCLUSTER.equals(mode) && startServer)) {
            LOGGER.log(Level.INFO, "Booting Local Embedded HerdDB mode, url:" + url + ", properties:" + serverConfiguration);
            server = new Server(serverConfiguration, statsLogger);
            try {
                server.start();
                int waitForTableSpaceTimeout = getWaitForTableSpaceTimeout();
                if (waitForTableSpaceTimeout > 0) {
                    server.waitForBootOfLocalTablespaces(waitForTableSpaceTimeout);
                }
                if (ServerConfiguration.PROPERTY_MODE_LOCAL.equals(mode) || ServerConfiguration.PROPERTY_MODE_STANDALONE.equals(mode)) {
                    client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
                }
            } catch (Exception ex) {
                throw new SQLException("Cannot boot embedded server " + ex, ex);
            }
        }
        serverInitialized = true;
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Server(herddb.server.Server) SQLException(java.sql.SQLException) ServerConfiguration(herddb.server.ServerConfiguration) SQLException(java.sql.SQLException)

Example 53 with StaticClientSideMetadataProvider

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

the class AdvancedInsertSyntaxTest method testInsertMultiValues.

@Test
public void testInsertMultiValues() 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 create = con.createStatement();
                PreparedStatement statement = con.prepareStatement("INSERT INTO mytable (name) values (?),(?)");
                PreparedStatement statement_return_keys = con.prepareStatement("INSERT INTO mytable (name) values (?),(?)", Statement.RETURN_GENERATED_KEYS)) {
                create.execute("CREATE TABLE mytable (n1 int primary key auto_increment, name string)");
                statement.setString(1, "v1");
                statement.setString(2, "v2");
                statement.executeUpdate();
                try {
                    statement_return_keys.setString(1, "v1");
                    statement_return_keys.setString(2, "v2");
                    statement_return_keys.executeUpdate();
                    fail();
                } catch (SQLException err) {
                    assertTrue(err.getMessage().contains("cannot 'return values' on multi-values insert"));
                }
            }
        }
    }
}
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) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 54 with StaticClientSideMetadataProvider

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

the class BatchTest method testBatch.

@Test
public void testBatch() 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();
                PreparedStatement statementCreatePage = con.prepareStatement("CREATE TABLE mytable (n1 int primary key auto_increment, name string)");
                PreparedStatement statement = con.prepareStatement("INSERT INTO mytable (name) values(?)")) {
                statementCreatePage.addBatch();
                // use executeBatch for DDL, like in MySQL import scripts
                int[] resultsCreate = statementCreatePage.executeBatch();
                assertEquals(1, resultsCreate[0]);
                {
                    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) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 55 with StaticClientSideMetadataProvider

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

the class BatchTest method testBatchAsync.

@Test
public void testBatchAsync() 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();
                PreparedStatementAsync statementCreatePage = con.prepareStatement("CREATE TABLE mytable (n1 int primary key auto_increment, name string)").unwrap(PreparedStatementAsync.class);
                PreparedStatementAsync statement = con.prepareStatement("INSERT INTO mytable (name) values(?)").unwrap(PreparedStatementAsync.class)) {
                statementCreatePage.addBatch();
                // use executeBatch for DDL, like in MySQL import scripts
                int[] resultsCreate = statementCreatePage.executeBatchAsync().get();
                assertEquals(1, resultsCreate[0]);
                {
                    for (int i = 0; i < 100; i++) {
                        statement.setString(1, "v" + i);
                        statement.addBatch();
                    }
                    int[] results = statement.executeBatchAsync().get();
                    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.executeBatchAsync().get();
                    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) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Aggregations

StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)59 Server (herddb.server.Server)56 Test (org.junit.Test)54 ClientConfiguration (herddb.client.ClientConfiguration)41 HDBClient (herddb.client.HDBClient)41 Connection (java.sql.Connection)39 Statement (java.sql.Statement)36 PreparedStatement (java.sql.PreparedStatement)30 ResultSet (java.sql.ResultSet)29 Path (java.nio.file.Path)15 RawString (herddb.utils.RawString)8 ServerConfiguration (herddb.server.ServerConfiguration)7 Map (java.util.Map)7 SQLException (java.sql.SQLException)6 HashSet (java.util.HashSet)4 HDBConnection (herddb.client.HDBConnection)3 MissingJDBCParameterException (herddb.model.MissingJDBCParameterException)3 List (java.util.List)3 HDBOperationTimeoutException (herddb.client.impl.HDBOperationTimeoutException)2 Transaction (herddb.model.Transaction)2