use of herddb.client.HDBClient in project herddb by diennea.
the class GeneratedKeysTest method testPreparedStatementLong.
@Test
public void testPreparedStatementLong() 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 long primary key auto_increment, name string)");
try (PreparedStatement prepared = con.prepareStatement("INSERT INTO mytable (name) values('name1')", Statement.RETURN_GENERATED_KEYS)) {
assertEquals(1, prepared.executeUpdate());
Object key = null;
try (ResultSet generatedKeys = prepared.getGeneratedKeys()) {
if (generatedKeys.next()) {
key = generatedKeys.getObject(1);
}
}
assertNotNull(key);
assertEquals(Long.valueOf(1), key);
}
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(1, count);
}
// async
try (PreparedStatementAsync delete = con.prepareStatement("DELETE FROM mytable").unwrap(PreparedStatementAsync.class);
PreparedStatementAsync prepared = con.prepareStatement("INSERT INTO mytable (name) values('name1')", Statement.RETURN_GENERATED_KEYS).unwrap(PreparedStatementAsync.class)) {
assertEquals(1, delete.executeUpdateAsync().get().intValue());
assertEquals(1, prepared.executeUpdateAsync().get().intValue());
Object key = null;
// getGeneratedKeys makes sense only if the Future has completed with success
try (ResultSet generatedKeys = prepared.getGeneratedKeys()) {
if (generatedKeys.next()) {
key = generatedKeys.getObject(1);
}
}
assertNotNull(key);
assertEquals(Long.valueOf(2), key);
}
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(1, count);
}
}
}
}
}
use of herddb.client.HDBClient in project herddb by diennea.
the class GeneratedKeysTest method testStatementLong.
@Test
public void testStatementLong() 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 long 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(Long.valueOf(1), key);
}
}
}
}
use of herddb.client.HDBClient 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);
}
}
}
}
use of herddb.client.HDBClient 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);
}
}
}
}
use of herddb.client.HDBClient 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));
}
}
}
}
}
}
Aggregations