use of herddb.server.Server in project herddb by diennea.
the class BatchTest method testBatch.
@Test
public void testBatch() throws Exception {
try (Server server = new Server(new ServerConfiguration(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)");
{
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);
}
}
int next_id;
try (ResultSet rs = statement.executeQuery("SELECT MAX(n1) FROM mytable")) {
assertTrue(rs.next());
next_id = rs.getInt(1) + 1;
assertEquals(101, next_id);
}
statement.executeUpdate("DELETE FROM mytable");
try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM mytable")) {
assertTrue(rs.next());
assertEquals(0, rs.getInt(1));
}
// transactions
con.setAutoCommit(false);
{
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 + next_id, rs.getInt("n1"));
count++;
}
assertEquals(100, count);
}
}
con.commit();
}
}
}
}
use of herddb.server.Server in project herddb by diennea.
the class GeneratedKeysTest method testStatementInt.
@Test
public void testStatementInt() throws Exception {
try (Server server = new Server(new ServerConfiguration(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);
}
}
}
}
use of herddb.server.Server in project herddb by diennea.
the class GeneratedKeysTest method testPreparedStatementLong.
@Test
public void testPreparedStatementLong() throws Exception {
try (Server server = new Server(new ServerConfiguration(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);
}
}
}
}
}
use of herddb.server.Server in project herddb by diennea.
the class GeneratedKeysTest method testStatementLong.
@Test
public void testStatementLong() throws Exception {
try (Server server = new Server(new ServerConfiguration(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.server.Server in project herddb by diennea.
the class PrettySQLExceptionTest method test.
@Test
public void test() throws Exception {
ServerConfiguration serverConfiguration = new ServerConfiguration(folder.newFolder().toPath());
try (Server server = new Server(serverConfiguration)) {
server.start();
server.waitForStandaloneBoot();
try (Connection connection = DriverManager.getConnection("jdbc:herddb:server:localhost:7000?");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM bad_table")) {
fail();
} catch (SQLException errorExpected) {
System.out.println("EXCEPTION MESSAGE: " + errorExpected.getMessage());
assertTrue("herddb.model.TableDoesNotExistException: no such table bad_table in tablespace default".equals(errorExpected.getMessage()) || errorExpected.getMessage().contains("Object 'BAD_TABLE' not found"));
}
}
}
Aggregations