Search in sources :

Example 6 with SQLRuntimeException

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);
    }
}
Also used : ConnectionProvider(org.davidmoten.rx.jdbc.ConnectionProvider) Connection(java.sql.Connection) Database(org.davidmoten.rx.jdbc.Database) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) IOUtils(org.apache.commons.io.IOUtils) SQLException(java.sql.SQLException) List(java.util.List) Scheduler(io.reactivex.Scheduler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) DriverManager(java.sql.DriverManager) ExecutorService(java.util.concurrent.ExecutorService) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException) SQLException(java.sql.SQLException) IOException(java.io.IOException) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException)

Example 7 with SQLRuntimeException

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() {
        // 
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException)

Example 8 with SQLRuntimeException

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);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) SQLRuntimeException(org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException)

Aggregations

SQLException (java.sql.SQLException)8 SQLRuntimeException (org.davidmoten.rx.jdbc.exceptions.SQLRuntimeException)8 Connection (java.sql.Connection)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ConnectionProvider (org.davidmoten.rx.jdbc.ConnectionProvider)3 TimeoutException (java.util.concurrent.TimeoutException)2 Scheduler (io.reactivex.Scheduler)1 Schedulers (io.reactivex.schedulers.Schedulers)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 DriverManager (java.sql.DriverManager)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IOUtils (org.apache.commons.io.IOUtils)1