Search in sources :

Example 71 with ClientConfiguration

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

the class GeneratedKeysTest method testStatementInt.

@Test
public void testStatementInt() 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 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);
                // test with UPSERT
                assertEquals(1, statement.executeUpdate("UPSERT INTO mytable (name) values('name1')", Statement.RETURN_GENERATED_KEYS));
                key = null;
                try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
                    if (generatedKeys.next()) {
                        key = generatedKeys.getObject(1);
                    }
                }
                assertNotNull(key);
                assertEquals(Integer.valueOf(2), 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)

Example 72 with ClientConfiguration

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

the class HerdDbSqlDataIntegrityTest method basic_test.

@Test()
public void basic_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));
            BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
            Connection con = dataSource.getConnection();
            Statement create = con.createStatement();
            create.execute("CREATE TABLE test_t1 (n1 int primary key, name string)");
            PreparedStatement statement = con.prepareStatement("INSERT INTO test_t1(n1,name) values(?,?)");
            statement.setInt(1, 100);
            statement.setString(2, "test1");
            statement.executeUpdate();
            try {
                con.prepareStatement("INSERT INTO test_t1 (n1,name) values(?,?)");
                statement.setInt(1, 100);
                statement.setString(2, "test1");
                statement.executeUpdate();
            } catch (SQLIntegrityConstraintViolationException ex) {
                Assert.assertTrue(ex instanceof SQLIntegrityConstraintViolationException);
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 73 with ClientConfiguration

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

the class HerdDbSqlDataIntegrityTest method basic_test_with_transactions.

@Test()
public void basic_test_with_transactions() 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));
            BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
            Connection con = dataSource.getConnection();
            con.setAutoCommit(false);
            Statement create = con.createStatement();
            create.execute("CREATE TABLE test_t1 (n1 int primary key, name string)");
            Statement statement = con.createStatement();
            statement.executeUpdate("INSERT INTO test_t1(n1,name) values(100,'test100')");
            try {
                statement.executeUpdate("INSERT INTO test_t1(n1,name) values(100,'test10220')");
            } catch (SQLIntegrityConstraintViolationException ex) {
                Assert.assertTrue(ex instanceof SQLIntegrityConstraintViolationException);
            }
            statement.executeUpdate("INSERT INTO test_t1(n1,name) values(101,'test101')");
            con.commit();
            try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM test_t1")) {
                assertTrue(rs.next());
                assertEquals(2, rs.getLong(1));
            }
            try (ResultSet rs = statement.executeQuery("SELECT * FROM test_t1")) {
                int i = 0;
                while (rs.next()) {
                    if (i == 0) {
                        i++;
                        Assert.assertEquals(100, rs.getLong(1));
                        Assert.assertEquals("test100", rs.getString(2));
                    } else {
                        Assert.assertEquals(101, rs.getLong(1));
                        Assert.assertEquals("test101", rs.getString(2));
                    }
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 74 with ClientConfiguration

use of herddb.client.ClientConfiguration 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 75 with ClientConfiguration

use of herddb.client.ClientConfiguration 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)

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