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(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.ClientConfiguration 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.ClientConfiguration 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));
}
}
}
}
}
}
use of herddb.client.ClientConfiguration in project herddb by diennea.
the class JdbcForeignKeyMetadataTest 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 ptable (pkey string primary key, p1 string, p2 string)");
statement.execute("CREATE TABLE ctable (ckey string primary key, c1 string, c2 string," + " CONSTRAINT `fk1` FOREIGN KEY (`c1`,`c2`) REFERENCES ptable(`p1`,`p2`) ON DELETE CASCADE)");
DatabaseMetaData metaData = con.getMetaData();
try (ResultSet rs = metaData.getImportedKeys(null, null, "CTABLE")) {
verifyForeignKeyResultSet(rs);
}
try (ResultSet rs = metaData.getImportedKeys(null, TableSpace.DEFAULT, "CTABLE")) {
verifyForeignKeyResultSet(rs);
}
try (ResultSet rs = metaData.getImportedKeys(null, TableSpace.DEFAULT, "PTABLE")) {
assertFalse(rs.next());
}
try (ResultSet rs = metaData.getExportedKeys(null, null, "ptable")) {
verifyForeignKeyResultSet(rs);
}
try (ResultSet rs = metaData.getExportedKeys(null, TableSpace.DEFAULT, "ptable")) {
verifyForeignKeyResultSet(rs);
}
try (ResultSet rs = metaData.getExportedKeys(null, null, "CTABLE")) {
assertFalse(rs.next());
}
try (ResultSet rs = metaData.getCrossReference(null, TableSpace.DEFAULT, "ptable", null, TableSpace.DEFAULT, "ctable")) {
verifyForeignKeyResultSet(rs);
}
try (ResultSet rs = metaData.getCrossReference(null, TableSpace.DEFAULT, "ptable", null, null, "ctable")) {
verifyForeignKeyResultSet(rs);
}
try (ResultSet rs = metaData.getCrossReference(null, null, "ptable", null, null, "ctable")) {
verifyForeignKeyResultSet(rs);
}
}
}
}
}
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(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 = 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"));
}
}
}
}
}
Aggregations