use of org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException in project rxjava2-jdbc by davidmoten.
the class DatabaseCreator method createDatabase.
private static void createDatabase(Connection c, boolean big) {
try {
c.setAutoCommit(true);
c.prepareStatement("create table person (name varchar(50) primary key, score int not null, date_of_birth date, registered timestamp)").execute();
if (big) {
List<String> lines = IOUtils.readLines(DatabaseCreator.class.getResourceAsStream("/big.txt"), StandardCharsets.UTF_8);
lines.stream().map(line -> line.split("\t")).forEach(items -> {
try {
c.prepareStatement("insert into person(name,score) values('" + items[0] + "'," + Integer.parseInt(items[1]) + ")").execute();
} catch (SQLException e) {
throw new SQLRuntimeException(e);
}
});
} else {
exec(c, "insert into person(name,score,registered) values('FRED',21, {ts '2015-09-17 18:47:52.69Z'})");
exec(c, "insert into person(name,score) values('JOSEPH',34)");
exec(c, "insert into person(name,score) values('MARMADUKE',25)");
}
exec(c, "create table person_clob (name varchar(50) not null, document clob)");
exec(c, "create table person_blob (name varchar(50) not null, document blob)");
exec(c, "create table address (address_id int primary key, full_address varchar(255) not null)");
exec(c, "insert into address(address_id, full_address) values(1,'57 Something St, El Barrio, Big Place')");
exec(c, "create table note(id bigint auto_increment primary key, text varchar(255))");
} catch (SQLException e) {
throw new SQLRuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException in project rxjava2-jdbc by davidmoten.
the class Database method testConnectionProvider.
private static ConnectionProvider testConnectionProvider(@Nonnull String url) {
return new ConnectionProvider() {
private final AtomicBoolean once = new AtomicBoolean();
private final CountDownLatch latch = new CountDownLatch(1);
@Override
public Connection get() {
try {
Connection c = DriverManager.getConnection(url);
if (once.compareAndSet(false, true)) {
createTestDatabase(c);
latch.countDown();
} else {
if (!latch.await(1, TimeUnit.MINUTES)) {
throw new SQLRuntimeException("waited 1 minute but test database was not created");
}
}
return c;
} catch (SQLException e) {
throw new SQLRuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public void close() {
//
}
};
}
use of org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException in project rxjava2-jdbc by davidmoten.
the class Database method createTestDatabase.
private static void createTestDatabase(@Nonnull Connection c) {
try {
//
Sql.statements(//
Database.class.getResourceAsStream("/database-test.sql")).stream().forEach(x -> {
try (PreparedStatement s = c.prepareStatement(x)) {
s.execute();
} catch (SQLException e) {
throw new SQLRuntimeException(e);
}
});
c.commit();
} catch (SQLException e) {
throw new SQLRuntimeException(e);
}
}
Aggregations