use of herddb.client.HDBClient 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);
}
}
}
}
}
use of herddb.client.HDBClient 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);
}
}
}
}
use of herddb.client.HDBClient 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);
}
}
}
}
}
}
}
}
use of herddb.client.HDBClient in project herddb by diennea.
the class SimpleClientScanTest method test.
@Test
public void test() throws Exception {
try (Server server = new Server(newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
for (int i = 0; i < 99; i++) {
Assert.assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", 0, false, true, Arrays.asList("test_" + i, 1, 2)).updateCount);
}
assertEquals(99, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
// maxRows
assertEquals(17, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable", true, Collections.emptyList(), 0, 17, 10, true).consume().size());
// empty result set
assertEquals(0, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='none'", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
// single fetch result test
assertEquals(1, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test_1'", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
// agregation in transaction, this is trickier than what you can think
long tx = connection.beginTransaction(TableSpace.DEFAULT);
assertEquals(1, connection.executeScan(TableSpace.DEFAULT, "SELECT count(*) FROM mytable WHERE id='test_1'", true, Collections.emptyList(), tx, 0, 10, true).consume().size());
connection.rollbackTransaction(TableSpace.DEFAULT, tx);
assertEquals(0, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id=?", true, Arrays.<Object>asList((Object) null), 0, 0, 10, true).consume().size());
}
}
}
use of herddb.client.HDBClient in project herddb by diennea.
the class SimpleClientScanTest method scanMultiChunk.
@Test
public void scanMultiChunk() throws Exception {
try (Server server = new Server(newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
server.start();
server.waitForStandaloneBoot();
ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
// more than one socket
clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 10);
try (HDBClient client = new HDBClient(clientConfiguration);
HDBConnection connection = client.openConnection()) {
client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, true, Collections.emptyList()).updateCount;
Assert.assertEquals(1, resultCreateTable);
for (int i = 0; i < 99; i++) {
Assert.assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", 0, false, true, Arrays.asList("test_" + i, 1, 2)).updateCount);
}
checkUseOnlyOneSocket(connection, server);
checkNoScannersOnTheServer(server);
checkCloseScannerOnConnectionClose(client, connection, server, true);
checkNoScannersOnTheServer(server);
checkCloseScannerOnConnectionClose(client, connection, server, false);
checkNoScannersOnTheServer(server);
checkCloseResultSetNotFullyScanned(connection, server, true);
checkNoScannersOnTheServer(server);
checkCloseResultSetNotFullyScanned(connection, server, false);
checkNoScannersOnTheServer(server);
checkCloseResultSetNotFullyScannedWithTransactionAndAggregation(connection, server, true);
checkNoScannersOnTheServer(server);
checkCloseResultSetNotFullyScannedWithTransactionAndAggregation(connection, server, false);
checkNoScannersOnTheServer(server);
}
checkNoScannersOnTheServer(server);
}
}
Aggregations