Search in sources :

Example 1 with MultiOperation

use of jdk.incubator.sql2.MultiOperation in project oracle-db-examples by oracle.

the class MultiOperationTest method multiRowCountOperation.

/**
 * Do something that approximates real work. Do a transaction. Uses
 * TransactionCompletion, CompletionStage args, and catch Operation.
 */
@Test
public void multiRowCountOperation() throws Exception {
    fail("TODO: Fix this test");
    DataSourceFactory factory = DataSourceFactory.newFactory(FACTORY_NAME);
    try (DataSource ds = factory.builder().url(URL).username(USER).password(PASSWORD).build();
        Session session = ds.getSession(t -> fail("ERROR: " + t.getMessage()))) {
        assertNotNull(session);
        TransactionCompletion trans = session.transactionCompletion();
        MultiOperation multiOp = session.multiOperation("update forum_user set city_id = ?" + " where id = ?").set("1", 40, AdbaType.INTEGER).set("2", 7782, AdbaType.INTEGER).onError(t -> {
            System.out.println(t.toString());
        });
        RowCountOperation<Long> cntOp = multiOp.<Long>rowCountOperation();
        multiOp.submit();
        cntOp.apply(c -> {
            long count = c.getCount();
            if (count != 1L)
                throw new SqlException("updated wrong number of rows", null, null, -1, null, -1);
            return count;
        }).onError(t -> t.printStackTrace()).submit().getCompletionStage().thenAccept(c -> {
            assertTrue((long) c > 0);
        });
        session.catchErrors();
        session.rollback().toCompletableFuture().get(TestConfig.getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    }
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) BeforeClass(org.junit.BeforeClass) RowOperation(jdk.incubator.sql2.RowOperation) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) SqlException(jdk.incubator.sql2.SqlException) OperationGroup(jdk.incubator.sql2.OperationGroup) Flow(java.util.concurrent.Flow) BiConsumer(java.util.function.BiConsumer) Collector(java.util.stream.Collector) Session(jdk.incubator.sql2.Session) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) JDBC_CONNECTION_PROPERTIES(com.oracle.adbaoverjdbc.JdbcConnectionProperties.JDBC_CONNECTION_PROPERTIES) AdbaType(jdk.incubator.sql2.AdbaType) Test(org.junit.Test) RowPublisherOperation(jdk.incubator.sql2.RowPublisherOperation) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Result(jdk.incubator.sql2.Result) RowCountOperation(jdk.incubator.sql2.RowCountOperation) ForkJoinPool(java.util.concurrent.ForkJoinPool) MultiOperation(jdk.incubator.sql2.MultiOperation) Assert(org.junit.Assert) Submission(jdk.incubator.sql2.Submission) DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) MultiOperation(jdk.incubator.sql2.MultiOperation) SqlException(jdk.incubator.sql2.SqlException) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 2 with MultiOperation

use of jdk.incubator.sql2.MultiOperation in project oracle-db-examples by oracle.

the class MultiOperationTest method multiRowCountOperationWithCountHandler.

@Test
public void multiRowCountOperationWithCountHandler() throws Exception {
    fail("TODO: Fix this test");
    DataSourceFactory factory = DataSourceFactory.newFactory(FACTORY_NAME);
    try (DataSource ds = factory.builder().url(URL).username(USER).password(PASSWORD).build();
        Session session = ds.getSession(t -> fail("ERROR: " + t.getMessage()))) {
        assertNotNull(session);
        BiConsumer<Integer, RowCountOperation<Long>> countHandler = (resNum, cntOp) -> {
            assertTrue(resNum > 0);
            cntOp.apply(c -> {
                long count = c.getCount();
                if (count != 1L)
                    fail("updated wrong number of rows");
                return count;
            }).onError(t -> fail(t.toString())).submit().getCompletionStage().thenAccept(c -> {
                assertTrue(c > 0);
            });
        };
        MultiOperation multiOp = session.multiOperation("update forum_user" + " set city_id = ?" + " where id = ?").set("1", 40, AdbaType.INTEGER).set("2", 7782, AdbaType.INTEGER).onError(t -> {
            fail(t.toString());
        });
        multiOp.onCount(countHandler).submit();
        session.catchErrors();
        session.rollback().toCompletableFuture().get(TestConfig.getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    }
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) BeforeClass(org.junit.BeforeClass) RowOperation(jdk.incubator.sql2.RowOperation) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) SqlException(jdk.incubator.sql2.SqlException) OperationGroup(jdk.incubator.sql2.OperationGroup) Flow(java.util.concurrent.Flow) BiConsumer(java.util.function.BiConsumer) Collector(java.util.stream.Collector) Session(jdk.incubator.sql2.Session) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) JDBC_CONNECTION_PROPERTIES(com.oracle.adbaoverjdbc.JdbcConnectionProperties.JDBC_CONNECTION_PROPERTIES) AdbaType(jdk.incubator.sql2.AdbaType) Test(org.junit.Test) RowPublisherOperation(jdk.incubator.sql2.RowPublisherOperation) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Result(jdk.incubator.sql2.Result) RowCountOperation(jdk.incubator.sql2.RowCountOperation) ForkJoinPool(java.util.concurrent.ForkJoinPool) MultiOperation(jdk.incubator.sql2.MultiOperation) Assert(org.junit.Assert) Submission(jdk.incubator.sql2.Submission) DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) MultiOperation(jdk.incubator.sql2.MultiOperation) RowCountOperation(jdk.incubator.sql2.RowCountOperation) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 3 with MultiOperation

use of jdk.incubator.sql2.MultiOperation in project oracle-db-examples by oracle.

the class MultiOperationTest method multiRowOperationWithRowHandler.

/**
 * Execute a query returning a single result.
 */
@Test
public void multiRowOperationWithRowHandler() {
    fail("TODO: Fix this test");
    Properties props = new Properties();
    DataSourceFactory factory = DataSourceFactory.newFactory(FACTORY_NAME);
    try (DataSource ds = factory.builder().url(URL).username(USER).password(PASSWORD).sessionProperty(JDBC_CONNECTION_PROPERTIES, props).build();
        Session session = ds.getSession(t -> fail("ERROR: " + t.getMessage()))) {
        assertNotNull(session);
        MultiOperation multiOp = session.multiOperation("select * from forum_user").onError(t -> {
            fail(t.toString());
        });
        BiConsumer<Integer, RowOperation<Integer>> rowHandler = (resNum, rowOp) -> {
            assertTrue(resNum > 0);
            rowOp.collect(Collector.<Result.RowColumn, int[], Integer>of(() -> new int[1], (int[] a, Result.RowColumn r) -> {
                a[0] = a[0] + r.at("sal").get(Integer.class);
            }, (l, r) -> l, a -> (Integer) a[0])).onError(t -> {
                fail(t.toString());
            }).submit().getCompletionStage().thenAccept(n -> {
                assertTrue((long) n > 0);
            }).toCompletableFuture();
        };
        multiOp.onRows(rowHandler).submit().getCompletionStage().toCompletableFuture().get(TestConfig.getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) BeforeClass(org.junit.BeforeClass) RowOperation(jdk.incubator.sql2.RowOperation) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) SqlException(jdk.incubator.sql2.SqlException) OperationGroup(jdk.incubator.sql2.OperationGroup) Flow(java.util.concurrent.Flow) BiConsumer(java.util.function.BiConsumer) Collector(java.util.stream.Collector) Session(jdk.incubator.sql2.Session) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) JDBC_CONNECTION_PROPERTIES(com.oracle.adbaoverjdbc.JdbcConnectionProperties.JDBC_CONNECTION_PROPERTIES) AdbaType(jdk.incubator.sql2.AdbaType) Test(org.junit.Test) RowPublisherOperation(jdk.incubator.sql2.RowPublisherOperation) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Result(jdk.incubator.sql2.Result) RowCountOperation(jdk.incubator.sql2.RowCountOperation) ForkJoinPool(java.util.concurrent.ForkJoinPool) MultiOperation(jdk.incubator.sql2.MultiOperation) Assert(org.junit.Assert) Submission(jdk.incubator.sql2.Submission) DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) Properties(java.util.Properties) RowOperation(jdk.incubator.sql2.RowOperation) SqlException(jdk.incubator.sql2.SqlException) DataSource(jdk.incubator.sql2.DataSource) Result(jdk.incubator.sql2.Result) MultiOperation(jdk.incubator.sql2.MultiOperation) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 4 with MultiOperation

use of jdk.incubator.sql2.MultiOperation in project oracle-db-examples by oracle-samples.

the class MultiOperationTest method multiRowOperationWithRowHandler.

/**
 * Execute a query returning a single result.
 */
@Test
public void multiRowOperationWithRowHandler() {
    fail("TODO: Fix this test");
    Properties props = new Properties();
    DataSourceFactory factory = DataSourceFactory.newFactory(FACTORY_NAME);
    try (DataSource ds = factory.builder().url(URL).username(USER).password(PASSWORD).sessionProperty(JDBC_CONNECTION_PROPERTIES, props).build();
        Session session = ds.getSession(t -> fail("ERROR: " + t.getMessage()))) {
        assertNotNull(session);
        MultiOperation multiOp = session.multiOperation("select * from forum_user").onError(t -> {
            fail(t.toString());
        });
        BiConsumer<Integer, RowOperation<Integer>> rowHandler = (resNum, rowOp) -> {
            assertTrue(resNum > 0);
            rowOp.collect(Collector.<Result.RowColumn, int[], Integer>of(() -> new int[1], (int[] a, Result.RowColumn r) -> {
                a[0] = a[0] + r.at("sal").get(Integer.class);
            }, (l, r) -> l, a -> (Integer) a[0])).onError(t -> {
                fail(t.toString());
            }).submit().getCompletionStage().thenAccept(n -> {
                assertTrue((long) n > 0);
            }).toCompletableFuture();
        };
        multiOp.onRows(rowHandler).submit().getCompletionStage().toCompletableFuture().get(TestConfig.getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) BeforeClass(org.junit.BeforeClass) RowOperation(jdk.incubator.sql2.RowOperation) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) SqlException(jdk.incubator.sql2.SqlException) OperationGroup(jdk.incubator.sql2.OperationGroup) Flow(java.util.concurrent.Flow) BiConsumer(java.util.function.BiConsumer) Collector(java.util.stream.Collector) Session(jdk.incubator.sql2.Session) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) JDBC_CONNECTION_PROPERTIES(com.oracle.adbaoverjdbc.JdbcConnectionProperties.JDBC_CONNECTION_PROPERTIES) AdbaType(jdk.incubator.sql2.AdbaType) Test(org.junit.Test) RowPublisherOperation(jdk.incubator.sql2.RowPublisherOperation) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) Date(java.sql.Date) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Result(jdk.incubator.sql2.Result) RowCountOperation(jdk.incubator.sql2.RowCountOperation) ForkJoinPool(java.util.concurrent.ForkJoinPool) MultiOperation(jdk.incubator.sql2.MultiOperation) Assert(org.junit.Assert) Submission(jdk.incubator.sql2.Submission) DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) Properties(java.util.Properties) RowOperation(jdk.incubator.sql2.RowOperation) SqlException(jdk.incubator.sql2.SqlException) DataSource(jdk.incubator.sql2.DataSource) Result(jdk.incubator.sql2.Result) MultiOperation(jdk.incubator.sql2.MultiOperation) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 5 with MultiOperation

use of jdk.incubator.sql2.MultiOperation in project oracle-db-examples by oracle-samples.

the class MultiOperationTest method multiRowPublisherOperation.

@Test
public void multiRowPublisherOperation() throws Exception {
    fail("TODO: Fix this test");
    String sql = "select id, name from forum_user";
    CompletableFuture<List<String>> result = new CompletableFuture<>();
    CompletionStage<List<String>> cs = null;
    DataSourceFactory factory = DataSourceFactory.newFactory(FACTORY_NAME);
    try (DataSource ds = factory.builder().url(URL).username(USER).password(PASSWORD).build();
        Session session = ds.getSession(t -> fail("ERROR: " + t.getMessage()))) {
        assertNotNull(session);
        MultiOperation multiOp = session.multiOperation(sql).onError(t -> {
            fail(t.toString());
        });
        RowPublisherOperation rowPublisherOp = multiOp.rowPublisherOperation();
        multiOp.submit();
        Flow.Subscriber<Result.RowColumn> subscriber = getSubscriber(result, false);
        cs = rowPublisherOp.subscribe(subscriber, result).onError(e -> fail(e.toString())).timeout(TestConfig.getTimeout()).submit().getCompletionStage();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    List<String> names = result.get(TestConfig.getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    assertTrue(names != null && !names.isEmpty());
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) SqlException(jdk.incubator.sql2.SqlException) DataSource(jdk.incubator.sql2.DataSource) Flow(java.util.concurrent.Flow) CompletableFuture(java.util.concurrent.CompletableFuture) MultiOperation(jdk.incubator.sql2.MultiOperation) ArrayList(java.util.ArrayList) List(java.util.List) RowPublisherOperation(jdk.incubator.sql2.RowPublisherOperation) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)10 List (java.util.List)10 CompletableFuture (java.util.concurrent.CompletableFuture)10 Flow (java.util.concurrent.Flow)10 DataSource (jdk.incubator.sql2.DataSource)10 DataSourceFactory (jdk.incubator.sql2.DataSourceFactory)10 MultiOperation (jdk.incubator.sql2.MultiOperation)10 RowPublisherOperation (jdk.incubator.sql2.RowPublisherOperation)10 Session (jdk.incubator.sql2.Session)10 SqlException (jdk.incubator.sql2.SqlException)10 Test (org.junit.Test)10 JDBC_CONNECTION_PROPERTIES (com.oracle.adbaoverjdbc.JdbcConnectionProperties.JDBC_CONNECTION_PROPERTIES)8 Date (java.sql.Date)8 Properties (java.util.Properties)8 CompletionStage (java.util.concurrent.CompletionStage)8 ForkJoinPool (java.util.concurrent.ForkJoinPool)8 TimeUnit (java.util.concurrent.TimeUnit)8 BiConsumer (java.util.function.BiConsumer)8 Collector (java.util.stream.Collector)8 AdbaType (jdk.incubator.sql2.AdbaType)8