Search in sources :

Example 11 with Server

use of herddb.server.Server 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)

Example 12 with Server

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

the class SimpleScanTest 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(?,?,?,?,?,?,?)")) {
                    for (int n = 0; n < 10; ++n) {
                        int i = 1;
                        statement.setString(i++, "mykey_" + n);
                        statement.setInt(i++, n);
                        statement.setLong(i++, n);
                        statement.setTimestamp(i++, new java.sql.Timestamp(System.currentTimeMillis()));
                        statement.setString(i++, null);
                        statement.setBoolean(i++, true);
                        statement.setDouble(i++, n + 0.5);
                        statement.addBatch();
                    }
                    int[] batches = statement.executeBatch();
                    Assert.assertEquals(10, batches.length);
                    for (int batch : batches) {
                        Assert.assertEquals(1, batch);
                    }
                }
                try (Connection con = dataSource.getConnection()) {
                    // Get and projection
                    try (PreparedStatement statement = con.prepareStatement("SELECT n1, k1 FROM mytable WHERE k1 = 'mykey_1'")) {
                        int count = 0;
                        try (ResultSet rs = statement.executeQuery()) {
                            while (rs.next()) {
                                assertEquals(1, rs.getInt(1));
                                assertNotNull(rs.getString(2));
                                ++count;
                            }
                        }
                        assertEquals(1, count);
                    }
                    // Scan and projection
                    try (PreparedStatement statement = con.prepareStatement("SELECT n1, k1 FROM mytable WHERE n1 > 1")) {
                        int count = 0;
                        try (ResultSet rs = statement.executeQuery()) {
                            while (rs.next()) {
                                assertTrue(rs.getInt(1) > 1);
                                assertNotNull(rs.getString(2));
                                ++count;
                            }
                        }
                        assertEquals(8, count);
                    }
                    // Scan, sort and projection
                    try (PreparedStatement statement = con.prepareStatement("SELECT n1, k1 FROM mytable WHERE n1 > 1 ORDER BY n1")) {
                        int count = 0;
                        try (ResultSet rs = statement.executeQuery()) {
                            while (rs.next()) {
                                assertTrue(rs.getInt(1) > 1);
                                assertNotNull(rs.getString(2));
                                ++count;
                            }
                        }
                        assertEquals(8, count);
                    }
                }
            }
        }
    }
}
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 13 with Server

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

the class TransactionsTest method noAutoSteartTransaction.

/**
 * Commit on a connection with just instructions that do not auto create a transaction
 */
@Test
public void noAutoSteartTransaction() 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();
                Connection con2 = dataSource.getConnection();
                Statement statement = con.createStatement();
                Statement statement2 = con2.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')"));
                con2.setAutoCommit(false);
                int update = statement2.executeUpdate("ALTER TABLE mytable ADD COLUMN other string");
                assertEquals(1, update);
                con2.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) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 14 with Server

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

the class GenericClientDataSourceTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.start();
        HerdDBDataSource dataSource = new HerdDBDataSource();
        try (Connection connection = dataSource.getConnection();
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM SYSTABLES")) {
            int count = 0;
            while (rs.next()) {
                System.out.println("table: " + rs.getString(1));
                count++;
            }
            assertTrue(count > 0);
        }
        dataSource.close();
    }
}
Also used : Server(herddb.server.Server) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 15 with Server

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

the class JdbcDriverTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (Connection connection = DriverManager.getConnection("jdbc:herddb:server:localhost:7000?");
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM SYSTABLES")) {
            int count = 0;
            while (rs.next()) {
                System.out.println("table: " + rs.getString(1));
                count++;
            }
            assertTrue(count > 0);
        }
        try (Connection connection = DriverManager.getConnection("jdbc:herddb:server:localhost");
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM SYSTABLES")) {
            int count = 0;
            while (rs.next()) {
                System.out.println("table: " + rs.getString(1));
                count++;
            }
            assertTrue(count > 0);
        }
    }
}
Also used : Server(herddb.server.Server) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Aggregations

Server (herddb.server.Server)36 Connection (java.sql.Connection)33 Test (org.junit.Test)33 ServerConfiguration (herddb.server.ServerConfiguration)32 Statement (java.sql.Statement)32 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)27 ClientConfiguration (herddb.client.ClientConfiguration)26 HDBClient (herddb.client.HDBClient)26 ResultSet (java.sql.ResultSet)25 PreparedStatement (java.sql.PreparedStatement)22 SQLException (java.sql.SQLException)6 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)2 HDBException (herddb.client.HDBException)1 BasicHerdDBDataSource (herddb.jdbc.BasicHerdDBDataSource)1 TableSpace (herddb.model.TableSpace)1 SQLPlanner (herddb.sql.SQLPlanner)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 List (java.util.List)1 Set (java.util.Set)1