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));
}
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();
}
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());
}
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);
}
Aggregations