use of herddb.client.HDBClient in project herddb by diennea.
the class AdvancedInsertSyntaxTest method testInsertFromSelect.
@Test
public void testInsertFromSelect() 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(?)")) {
create.execute("CREATE TABLE mytable (n1 int primary key auto_increment, name string)");
create.execute("CREATE TABLE mytable2 (n1 int primary key auto_increment, name string)");
{
for (int i = 0; i < 100; i++) {
statement.setString(1, "v" + i);
statement.addBatch();
}
int[] results = statement.executeBatch();
for (int i = 0; i < 100; i++) {
assertEquals(1, results[i]);
}
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable ORDER BY n1")) {
int count = 0;
while (rs.next()) {
assertEquals("v" + count, rs.getString("name"));
assertEquals(count + 1, rs.getInt("n1"));
count++;
}
assertEquals(100, count);
}
}
statement.executeUpdate("INSERT INTO mytable2(n1,name) SELECT n1, name from mytable order by name desc");
try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM mytable2")) {
assertTrue(rs.next());
assertEquals(100, rs.getInt(1));
}
// leverage auto_increment
statement.executeUpdate("INSERT INTO mytable2(name) SELECT name from mytable order by name desc");
try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM mytable2")) {
assertTrue(rs.next());
assertEquals(200, rs.getInt(1));
}
try (ResultSet rs = statement.executeQuery("SELECT n1 FROM mytable2 order by n1")) {
Set<Integer> ids = new HashSet<>();
while (rs.next()) {
int id = rs.getInt(1);
assertTrue(ids.add(id));
}
assertEquals(200, ids.size());
}
}
}
}
}
use of herddb.client.HDBClient in project herddb by diennea.
the class GeneratedKeysTest method testPreparedStatementInt.
@Test
public void testPreparedStatementInt() 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)");
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(Integer.valueOf(1), 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 MaxRowsTest 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 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")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(3, count);
}
statement.setMaxRows(2);
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(2, count);
}
statement.setMaxRows(0);
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(3, count);
}
statement.setMaxRows(Integer.MAX_VALUE);
try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable")) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(3, count);
}
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM mytable")) {
try (ResultSet rs = ps.executeQuery()) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(3, count);
}
ps.setMaxRows(2);
try (ResultSet rs = ps.executeQuery()) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(2, count);
}
ps.setMaxRows(0);
try (ResultSet rs = ps.executeQuery()) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(3, count);
}
ps.setMaxRows(Integer.MAX_VALUE);
try (ResultSet rs = ps.executeQuery()) {
int count = 0;
while (rs.next()) {
count++;
}
assertEquals(3, count);
}
}
}
}
}
}
use of herddb.client.HDBClient in project herddb by diennea.
the class SimpleJoinTest 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 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);
}
}
}
}
}
use of herddb.client.HDBClient 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(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();
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();
}
}
}
}
Aggregations