use of org.jdbi.v3.core.statement.StatementContext in project jdbi by jdbi.
the class SqlQueryHandler method configureReturner.
@Override
void configureReturner(Query q, SqlObjectStatementConfiguration cfg) {
UseRowMapper useRowMapper = getMethod().getAnnotation(UseRowMapper.class);
UseRowReducer useRowReducer = getMethod().getAnnotation(UseRowReducer.class);
if (useRowReducer != null && useRowMapper != null) {
throw new IllegalStateException("Cannot declare @UseRowMapper and @UseRowReducer on the same method.");
}
cfg.setReturner(() -> {
StatementContext ctx = q.getContext();
Type elementType = magic.elementType(ctx);
if (useRowReducer != null) {
return magic.reducedResult(q.reduceRows(rowReducerFor(useRowReducer)), ctx);
}
ResultIterable<?> iterable = useRowMapper == null ? q.mapTo(elementType) : q.map(rowMapperFor(useRowMapper));
return magic.mappedResult(iterable, ctx);
});
}
use of org.jdbi.v3.core.statement.StatementContext in project jdbi by jdbi.
the class JpaMapper method specialize.
@Override
public RowMapper<C> specialize(ResultSet rs, StatementContext ctx) throws SQLException {
Constructor<C> constructor;
try {
constructor = clazz.getDeclaredConstructor();
} catch (ReflectiveOperationException e) {
throw new EntityMemberAccessException("Unable to get constructor for " + clazz, e);
}
constructor.setAccessible(true);
List<MemberSetter<C>> setters = new ArrayList<>();
for (int colIndex = rs.getMetaData().getColumnCount(); colIndex >= 1; colIndex--) {
String columnLabel = rs.getMetaData().getColumnLabel(colIndex);
JpaMember member = jpaClass.lookupMember(columnLabel);
if (member != null) {
Type memberType = member.getType();
ColumnMapper<?> columnMapper = ctx.findColumnMapperFor(memberType).orElseThrow(() -> new NoSuchMapperException("No column mapper for " + memberType));
final int columnIndex = colIndex;
setters.add(obj -> member.write(obj, columnMapper.map(rs, columnIndex, ctx)));
}
}
return (r, c) -> {
C obj;
try {
obj = constructor.newInstance();
} catch (ReflectiveOperationException e) {
throw new EntityMemberAccessException("Unable to invoke " + constructor, e);
}
for (MemberSetter<C> setter : setters) {
setter.mapAndSetMember(obj);
}
return obj;
};
}
use of org.jdbi.v3.core.statement.StatementContext in project jdbi by jdbi.
the class StatementContextTest method testMapperForDelegatesToRegistry.
@Test
public void testMapperForDelegatesToRegistry() {
ColumnMapper<Foo> mapper = new FooMapper();
ConfigRegistry config = new ConfigRegistry();
config.get(ColumnMappers.class).register(mapper);
final StatementContext context = StatementContextAccess.createContext(config);
assertThat(context.findColumnMapperFor(Foo.class)).contains(mapper);
}
use of org.jdbi.v3.core.statement.StatementContext in project providence by morimekta.
the class MessageRowMapper method map.
@Override
public M map(ResultSet rs, StatementContext ctx) throws SQLException {
PMessageBuilder<M, F> builder = descriptor.builder();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); ++i) {
if (!tableName.isEmpty() && !tableName.equalsIgnoreCase(rs.getMetaData().getTableName(i))) {
continue;
}
String name = rs.getMetaData().getColumnLabel(i).toUpperCase(Locale.US);
F field = fieldNameMapping.get(name);
if (field != null) {
int columnType = rs.getMetaData().getColumnType(i);
switch(field.getType()) {
case BOOL:
{
if (columnType == Types.BOOLEAN || columnType == Types.BIT) {
boolean b = rs.getBoolean(i);
if (!rs.wasNull()) {
builder.set(field, b);
}
} else {
int b = rs.getInt(i);
if (!rs.wasNull()) {
builder.set(field, b != 0);
}
}
break;
}
case BYTE:
{
byte b = rs.getByte(i);
if (!rs.wasNull()) {
builder.set(field, b);
}
break;
}
case I16:
{
short b = rs.getShort(i);
if (!rs.wasNull()) {
builder.set(field, b);
}
break;
}
case I32:
{
if (columnType == Types.TIMESTAMP) {
Timestamp ts = rs.getTimestamp(i);
if (ts != null) {
builder.set(field, (int) (ts.getTime() / 1000L));
}
} else {
int b = rs.getInt(i);
if (!rs.wasNull()) {
builder.set(field, b);
}
}
break;
}
case I64:
{
if (columnType == Types.TIMESTAMP) {
Timestamp ts = rs.getTimestamp(i);
if (ts != null) {
builder.set(field, ts.getTime());
}
} else {
long b = rs.getLong(i);
if (!rs.wasNull()) {
builder.set(field, b);
}
}
break;
}
case DOUBLE:
{
double b = rs.getDouble(i);
if (!rs.wasNull()) {
builder.set(field, b);
}
break;
}
case STRING:
{
builder.set(field, rs.getString(i));
break;
}
case BINARY:
{
switch(columnType) {
case Types.BINARY:
case Types.VARBINARY:
byte[] ts = rs.getBytes(i);
if (ts != null) {
builder.set(field, Binary.copy(ts));
}
break;
case Types.BLOB:
Blob blob = rs.getBlob(i);
if (blob != null) {
try {
builder.set(field, Binary.read(blob.getBinaryStream(), (int) blob.length()));
} catch (IOException e) {
throw new UncheckedIOException(e.getMessage(), e);
}
}
break;
case Types.CHAR:
case Types.VARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
{
String tmp = rs.getString(i);
if (tmp != null) {
builder.set(field, Binary.fromBase64(tmp));
}
break;
}
case Types.NULL:
break;
default:
throw new ResultSetException("Unknown column type " + rs.getMetaData().getColumnTypeName(i) + " for " + descriptor.getType().toString() + " field " + name + " in " + descriptor.getQualifiedName(), null, ctx);
}
break;
}
case ENUM:
{
int val = rs.getInt(i);
if (!rs.wasNull()) {
PEnumDescriptor ed = (PEnumDescriptor) field.getDescriptor();
builder.set(field, ed.findById(val));
}
break;
}
case MESSAGE:
{
try {
PMessageDescriptor<?, ?> md = (PMessageDescriptor) field.getDescriptor();
switch(columnType) {
case Types.BINARY:
case Types.VARBINARY:
byte[] data = rs.getBytes(i);
if (data != null) {
ByteArrayInputStream in = new ByteArrayInputStream(data);
builder.set(field, BINARY.deserialize(in, md));
}
break;
case Types.BLOB:
{
Blob blob = rs.getBlob(i);
if (blob != null) {
builder.set(field, BINARY.deserialize(blob.getBinaryStream(), md));
}
break;
}
case Types.CHAR:
case Types.VARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
{
String tmp = rs.getString(i);
if (tmp != null) {
StringReader reader = new StringReader(tmp);
builder.set(field, JSON.deserialize(reader, md));
}
break;
}
case Types.CLOB:
{
Clob clob = rs.getClob(i);
if (clob != null) {
builder.set(field, JSON.deserialize(clob.getCharacterStream(), md));
}
break;
}
case Types.NULL:
break;
default:
throw new ResultSetException("Unknown column type " + rs.getMetaData().getColumnTypeName(i) + " for " + descriptor.getType().toString() + " field " + name + " in " + descriptor.getQualifiedName(), null, ctx);
}
} catch (IOException e) {
throw new UncheckedIOException(e.getMessage(), e);
}
break;
}
case LIST:
case SET:
case MAP:
{
// ... woot?
}
case VOID:
default:
{
throw new ResultSetException("Unhandled column of type " + rs.getMetaData().getColumnTypeName(i) + " for " + descriptor.getType().toString() + " field " + name + " in " + descriptor.getQualifiedName(), null, ctx);
}
}
}
}
return builder.build();
}
use of org.jdbi.v3.core.statement.StatementContext in project metrics by dropwizard.
the class TimedAnnotationNameStrategy method getStatementName.
@Override
public String getStatementName(StatementContext statementContext) {
final ExtensionMethod extensionMethod = statementContext.getExtensionMethod();
if (extensionMethod == null) {
return null;
}
final Class<?> clazz = extensionMethod.getType();
final Timed classTimed = clazz.getAnnotation(Timed.class);
final Method method = extensionMethod.getMethod();
final Timed methodTimed = method.getAnnotation(Timed.class);
// If the method is timed, figure out the name
if (methodTimed != null) {
String methodName = methodTimed.name().isEmpty() ? method.getName() : methodTimed.name();
if (methodTimed.absolute()) {
return methodName;
} else {
// We need to check if the class has a custom timer name
return classTimed == null || classTimed.name().isEmpty() ? MetricRegistry.name(clazz, methodName) : MetricRegistry.name(classTimed.name(), methodName);
}
} else if (classTimed != null) {
// Maybe the class is timed?
return classTimed.name().isEmpty() ? MetricRegistry.name(clazz, method.getName()) : MetricRegistry.name(classTimed.name(), method.getName());
} else {
// No timers neither on the method or the class
return null;
}
}
Aggregations