Search in sources :

Example 41 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(new ServerConfiguration(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()) {
                con.setAutoCommit(false);
                statement.execute("CREATE TABLE `queuebouncecategory_history` (\n" + "  `queueid` int(11) NOT NULL,\n" + "  `idbouncecategory` smallint(6) NOT NULL,\n" + "  `refdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE 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) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) SQLPlanner(herddb.sql.SQLPlanner) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 42 with ClientConfiguration

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

the class PreparedStatemetParametersTest method missingParameter.

/**
 * Execute a prepared statement without a needed parameter
 */
@Test(expected = SQLException.class)
public void missingParameter() 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 (c1 int primary key)");
                }
                try (Connection con = dataSource.getConnection();
                    PreparedStatement statement = con.prepareStatement("INSERT INTO mytable values (?)")) {
                    int rows = statement.executeUpdate();
                    Assert.assertEquals(1, rows);
                }
            }
        }
    }
}
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) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 43 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(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 (?),(?)");
                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) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 44 with ClientConfiguration

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

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