Search in sources :

Example 1 with HandleSupplier

use of org.jdbi.v3.core.extension.HandleSupplier in project jdbi by jdbi.

the class SqlObjectFactory method attach.

/**
 * Create a sql object of the specified type bound to this handle. Any state changes to the handle, or the sql
 * object, such as transaction status, closing it, etc, will apply to both the object and the handle.
 *
 * @param extensionType the type of sql object to create
 * @param handle the Handle instance to attach ths sql object to
 * @return the new sql object bound to this handle
 */
@Override
public <E> E attach(Class<E> extensionType, HandleSupplier handle) {
    Map<Method, Handler> handlers = methodHandlersFor(extensionType, handle.getConfig(Handlers.class), handle.getConfig(HandlerDecorators.class));
    ConfigRegistry instanceConfig = handle.getConfig().createCopy();
    for (Class<?> iface : extensionType.getInterfaces()) {
        forEachConfigurer(iface, (configurer, annotation) -> configurer.configureForType(instanceConfig, annotation, extensionType));
    }
    forEachConfigurer(extensionType, (configurer, annotation) -> configurer.configureForType(instanceConfig, annotation, extensionType));
    InvocationHandler invocationHandler = createInvocationHandler(extensionType, instanceConfig, handlers, handle);
    return extensionType.cast(Proxy.newProxyInstance(extensionType.getClassLoader(), new Class[] { extensionType }, invocationHandler));
}
Also used : ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) InvocationHandler(java.lang.reflect.InvocationHandler) ExtensionMethod(org.jdbi.v3.core.extension.ExtensionMethod) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 2 with HandleSupplier

use of org.jdbi.v3.core.extension.HandleSupplier in project jdbi by jdbi.

the class CustomizingStatementHandler method invoke.

@Override
public Object invoke(Object target, Object[] args, HandleSupplier hs) throws Exception {
    final Handle h = hs.getHandle();
    final String locatedSql = locateSql(h);
    final StatementType stmt = createStatement(h, locatedSql);
    final SqlObjectStatementConfiguration cfg = stmt.getConfig(SqlObjectStatementConfiguration.class);
    cfg.setArgs(args);
    configureReturner(stmt, cfg);
    applyCustomizers(stmt, args);
    return cfg.getReturner().get();
}
Also used : Handle(org.jdbi.v3.core.Handle)

Example 3 with HandleSupplier

use of org.jdbi.v3.core.extension.HandleSupplier in project jdbi by jdbi.

the class SqlBatchHandler method invoke.

@Override
public Object invoke(Object target, Object[] args, HandleSupplier h) {
    final Handle handle = h.getHandle();
    final String sql = locateSql(handle);
    final int chunkSize = batchChunkSize.call(args);
    final Iterator<Object[]> batchArgs = zipArgs(getMethod(), args);
    ResultIterator<Object> result;
    if (batchArgs.hasNext()) {
        result = new ResultIterator<Object>() {

            ResultIterator<?> batchResult;

            boolean closed = false;

            {
                // Ensure our batchResult is prepared, so we can get its context
                hasNext();
            }

            @Override
            public boolean hasNext() {
                if (closed) {
                    throw new IllegalStateException("closed");
                }
                // first, any elements already buffered?
                if (batchResult != null) {
                    if (batchResult.hasNext()) {
                        return true;
                    }
                    // no more in this chunk, release resources
                    batchResult.close();
                }
                // more chunks?
                if (!batchArgs.hasNext()) {
                    return false;
                }
                // execute a single chunk and buffer
                PreparedBatch batch = handle.prepareBatch(sql);
                for (int i = 0; i < chunkSize && batchArgs.hasNext(); i++) {
                    applyCustomizers(batch, batchArgs.next());
                    batch.add();
                }
                batchResult = executeBatch(handle, batch);
                // recurse to ensure we actually got elements
                return hasNext();
            }

            @Override
            public Object next() {
                if (closed) {
                    throw new IllegalStateException("closed");
                }
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                return batchResult.next();
            }

            @Override
            public StatementContext getContext() {
                return batchResult.getContext();
            }

            @Override
            public void close() {
                closed = true;
                batchResult.close();
            }
        };
    } else {
        PreparedBatch dummy = handle.prepareBatch(sql);
        result = new ResultIterator<Object>() {

            @Override
            public void close() {
            // no op
            }

            @Override
            public StatementContext getContext() {
                return dummy.getContext();
            }

            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public Object next() {
                throw new NoSuchElementException();
            }
        };
    }
    ResultIterable<Object> iterable = ResultIterable.of(result);
    return magic.mappedResult(iterable, result.getContext());
}
Also used : Handle(org.jdbi.v3.core.Handle) StatementContext(org.jdbi.v3.core.statement.StatementContext) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with HandleSupplier

use of org.jdbi.v3.core.extension.HandleSupplier in project jdbi by jdbi.

the class TestSqlObjectMethodBehavior method setUp.

@Before
public void setUp() throws Exception {
    HandleSupplier handleSupplier = new HandleSupplier() {

        @Override
        public ConfigRegistry getConfig() {
            return new ConfigRegistry();
        }

        @Override
        public Handle getHandle() {
            throw new UnsupportedOperationException();
        }

        @Override
        public <V> V invokeInContext(ExtensionMethod extensionMethod, ConfigRegistry config, Callable<V> task) throws Exception {
            return task.call();
        }
    };
    SqlObjectFactory factory = new SqlObjectFactory();
    dao = factory.attach(UselessDao.class, handleSupplier);
    anotherDao = factory.attach(UselessDao.class, handleSupplier);
}
Also used : ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) HandleSupplier(org.jdbi.v3.core.extension.HandleSupplier) ExtensionMethod(org.jdbi.v3.core.extension.ExtensionMethod) Callable(java.util.concurrent.Callable) Before(org.junit.Before)

Aggregations

Handle (org.jdbi.v3.core.Handle)2 ConfigRegistry (org.jdbi.v3.core.config.ConfigRegistry)2 ExtensionMethod (org.jdbi.v3.core.extension.ExtensionMethod)2 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 NoSuchElementException (java.util.NoSuchElementException)1 Callable (java.util.concurrent.Callable)1 HandleSupplier (org.jdbi.v3.core.extension.HandleSupplier)1 PreparedBatch (org.jdbi.v3.core.statement.PreparedBatch)1 StatementContext (org.jdbi.v3.core.statement.StatementContext)1 Before (org.junit.Before)1