Search in sources :

Example 56 with StaticClientSideMetadataProvider

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

the class MixedCaseIdentifiersTest method testUpdate.

@Test
public void testUpdate() 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_insert = con.prepareStatement("INSERT INTO q1_MESSAGE(msg_id,STATUS,recipient) values(?,?,?)");
                PreparedStatement statement_update = con.prepareStatement("UPDATE q1_MESSAGE SET STATUS = 2,lastbouncecategory=null WHERE MSG_ID = ? and (status=1 or status=5)")) {
                create.execute(CREATE_TABLE);
                long msg_id = 213;
                statement_insert.setLong(1, msg_id);
                statement_insert.setInt(2, 1);
                statement_insert.setString(3, "test@localhost");
                assertEquals(1, statement_insert.executeUpdate());
                statement_update.setLong(1, msg_id);
                assertEquals(1, statement_update.executeUpdate());
                try (ResultSet rs = create.executeQuery("SELECT M.MSG_ID FROM q1_MESSAGE M WHERE 1=1 AND (M.RECIPIENT LIKE '%@localhost%')")) {
                    long _msg_id = -1;
                    int record_count = 0;
                    while (rs.next()) {
                        _msg_id = rs.getLong(1);
                        record_count++;
                    }
                    assertEquals(1, record_count);
                    assertTrue(_msg_id > 0);
                }
                try (PreparedStatement ps = con.prepareStatement("UPDATE q1_MESSAGE SET STATUS= 6,SID=?,LAST_MODIFY_TIME=?,NEXT_SEND_TIME = null, LASTBOUNCECATEGORY=? WHERE MSG_ID = ?")) {
                    ps.setInt(1, 1);
                    ps.setTimestamp(2, new java.sql.Timestamp(System.currentTimeMillis()));
                    ps.setInt(3, 2);
                    ps.setLong(4, msg_id);
                    assertEquals(1, ps.executeUpdate());
                }
                try (ResultSet rs = create.executeQuery("SELECT M.MSG_ID FROM q1_MESSAGE M WHERE 1=1 AND status=6 and (M.RECIPIENT LIKE '%@localhost%')")) {
                    long _msg_id = -1;
                    int record_count = 0;
                    while (rs.next()) {
                        _msg_id = rs.getLong(1);
                        record_count++;
                    }
                    assertEquals(1, record_count);
                    assertTrue(_msg_id > 0);
                }
            }
        }
    }
}
Also used : Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) 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 57 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider 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(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)) {
                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) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 58 with StaticClientSideMetadataProvider

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

the class SimpleScanTest method testCloseResultSetsOnCloseConnection.

@Test
public void testCloseResultSetsOnCloseConnection() throws Exception {
    try (Server server = new Server(herddb.jdbc.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)) {
                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()) {
                    con.setAutoCommit(false);
                    Transaction tx;
                    try (Statement s = con.createStatement()) {
                        // force the creation of the transaction, by issuing a DML command
                        s.executeUpdate("UPDATE mytable set n1=1 where k1 ='aaa'");
                    }
                    try (PreparedStatement statement = con.prepareStatement("SELECT n1, k1 FROM mytable")) {
                        statement.setFetchSize(1);
                        ResultSet rs = statement.executeQuery();
                        assertTrue(rs.next());
                        List<Transaction> transactions = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransactions();
                        assertEquals(1, transactions.size());
                        tx = transactions.get(0);
                        assertEquals(1, tx.getRefCount());
                    }
                    // close statement -> close result set -> transaction refcount = 0
                    // closing of scanners is non blocking, we have to wait
                    TestUtils.waitForCondition(() -> tx.getRefCount() == 0, NOOP, 100);
                }
                Transaction tx;
                try (Connection con = dataSource.getConnection()) {
                    con.setAutoCommit(false);
                    try (Statement s = con.createStatement()) {
                        // force the creation of the transaction, by issuing a DML command
                        s.executeUpdate("UPDATE mytable set n1=1 where k1 ='aaa'");
                    }
                    PreparedStatement statement = con.prepareStatement("SELECT n1, k1 FROM mytable");
                    statement.setFetchSize(1);
                    ResultSet rs = statement.executeQuery();
                    assertTrue(rs.next());
                    List<Transaction> transactions = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransactions();
                    assertEquals(1, transactions.size());
                    tx = transactions.get(0);
                    assertEquals(1, tx.getRefCount());
                }
                // close connection -> close statement -> close result set -> transaction refcount = 0 (and rolled back)
                TestUtils.waitForCondition(() -> tx.getRefCount() == 0, NOOP, 100);
            }
        }
    }
}
Also used : Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Transaction(herddb.model.Transaction) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 59 with StaticClientSideMetadataProvider

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

the class CheckBigIntConversionTest 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 BIGINT primary key)");
                /* With high enough longs the value was converted to double loosing "precision" */
                long value = 72057598332895235L;
                try (PreparedStatement insert = con.prepareStatement("INSERT INTO mytable (key) values(" + value + ")")) {
                    insert.executeUpdate();
                }
                try (PreparedStatement selectKey = con.prepareStatement("SELECT key FROM mytable")) {
                    try (ResultSet rs = selectKey.executeQuery()) {
                        while (rs.next()) {
                            Long result = (Long) rs.getObject(1);
                            if (result.longValue() != value) {
                                throw new IllegalStateException(value + " != " + result);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) 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