use of org.jdbi.v3.core.internal.exceptions.Unchecked in project jdbi by jdbi.
the class AnnotationFactory method create.
public static <T extends Annotation> T create(Class<T> annotationType, Map<String, ?> values) {
Arrays.stream(annotationType.getDeclaredMethods()).filter(m -> m.getDefaultValue() == null).filter(m -> !values.containsKey(m.getName())).findAny().ifPresent(m -> {
throw new IllegalArgumentException(String.format("Cannot synthesize annotation @%s from %s.class because it has attribute " + m.getName() + " without a default or specified value", annotationType.getSimpleName(), annotationType.getSimpleName()));
});
Class<?>[] interfaces = { annotationType };
InvocationHandler invocationHandler = getInvocationHandler(annotationType, values);
@SuppressWarnings("unchecked") T annotation = (T) Proxy.newProxyInstance(Optional.ofNullable(annotationType.getClassLoader()).orElseGet(Thread.currentThread()::getContextClassLoader), interfaces, invocationHandler);
return annotation;
}
use of org.jdbi.v3.core.internal.exceptions.Unchecked in project jdbi by jdbi.
the class SqlArrayMapperFactory method build.
@Override
@SuppressWarnings("unchecked")
public Optional<ColumnMapper<?>> build(Type type, ConfigRegistry config) {
final Class<?> erasedType = GenericTypes.getErasedType(type);
if (erasedType.isArray()) {
Class<?> elementType = erasedType.getComponentType();
return elementTypeMapper(elementType, config).map(elementMapper -> new ArrayColumnMapper(elementMapper, elementType));
}
JdbiCollectors collectorRegistry = config.get(JdbiCollectors.class);
return (Optional) collectorRegistry.findFor(type).flatMap(collector -> collectorRegistry.findElementTypeFor(type).flatMap(elementType -> elementTypeMapper(elementType, config)).map(elementMapper -> new CollectorColumnMapper(elementMapper, collector)));
}
use of org.jdbi.v3.core.internal.exceptions.Unchecked in project providence by morimekta.
the class MessageFieldArgument method apply.
@Override
@SuppressWarnings("unchecked")
public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException {
if (message.has(field)) {
switch(field.getType()) {
case BOOL:
{
boolean value = message.get(field);
if (type == Types.BOOLEAN || type == Types.BIT) {
statement.setBoolean(position, value);
} else {
statement.setInt(position, value ? 1 : 0);
}
break;
}
case BYTE:
{
statement.setByte(position, message.get(field));
break;
}
case I16:
{
statement.setShort(position, message.get(field));
break;
}
case I32:
{
if (type == Types.TIMESTAMP) {
Timestamp timestamp = new Timestamp(1000L * (int) message.get(field));
statement.setTimestamp(position, timestamp);
} else {
statement.setInt(position, message.get(field));
}
break;
}
case I64:
{
if (type == Types.TIMESTAMP) {
Timestamp timestamp = new Timestamp(message.get(field));
statement.setTimestamp(position, timestamp);
} else {
statement.setLong(position, message.get(field));
}
break;
}
case DOUBLE:
{
statement.setDouble(position, message.get(field));
break;
}
case STRING:
{
statement.setString(position, message.get(field));
break;
}
case BINARY:
{
Binary binary = message.get(field);
switch(type) {
case Types.BINARY:
case Types.VARBINARY:
{
statement.setBytes(position, binary.get());
break;
}
case Types.BLOB:
{
statement.setBlob(position, binary.getInputStream());
break;
}
case Types.CHAR:
case Types.VARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
{
statement.setString(position, binary.toBase64());
break;
}
default:
throw new ResultSetException("Unknown binary field type: " + type + " for " + field, null, ctx);
}
break;
}
case ENUM:
{
PEnumValue value = message.get(field);
statement.setInt(position, value.asInteger());
break;
}
case MESSAGE:
{
PMessage value = message.get(field);
switch(type) {
case Types.BINARY:
case Types.VARBINARY:
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
BINARY.serialize(out, value);
statement.setBytes(position, out.toByteArray());
} catch (IOException e) {
throw new ResultSetException(e.getMessage(), e, ctx);
}
break;
}
case Types.BLOB:
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
BINARY.serialize(out, value);
statement.setBlob(position, new ByteArrayInputStream(out.toByteArray()));
} catch (IOException e) {
throw new ResultSetException(e.getMessage(), e, ctx);
}
break;
}
case Types.CHAR:
case Types.VARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
{
StringWriter writer = new StringWriter();
try {
JSON.serialize(new PrintWriter(writer), value);
statement.setString(position, writer.getBuffer().toString());
} catch (IOException e) {
throw new ResultSetException(e.getMessage(), e, ctx);
}
break;
}
case Types.CLOB:
{
StringWriter writer = new StringWriter();
try {
JSON.serialize(new PrintWriter(writer), value);
statement.setClob(position, new StringReader(writer.getBuffer().toString()));
} catch (IOException e) {
throw new ResultSetException(e.getMessage(), e, ctx);
}
break;
}
default:
throw new ResultSetException("Unknown message field type: " + type + " for " + field, null, ctx);
}
break;
}
default:
throw new ResultSetException("Unhandled field type in SQL: " + field, null, ctx);
}
} else {
statement.setNull(position, type);
}
}
use of org.jdbi.v3.core.internal.exceptions.Unchecked in project providence by morimekta.
the class MessageNamedArgumentFinder method find.
@Nonnull
@Override
@SuppressWarnings("unchecked")
public Optional<Argument> find(String name, StatementContext ctx) {
if (!prefix.isEmpty()) {
if (name.startsWith(prefix)) {
name = name.substring(prefix.length());
} else {
return Optional.empty();
}
}
String[] parts = name.split("[.]");
PMessage leaf = message;
PMessageDescriptor leafDescriptor = message.descriptor();
for (int i = 0; i < parts.length - 1; ++i) {
String part = parts[i];
PField field = leafDescriptor.findFieldByName(part);
if (field == null)
return Optional.empty();
if (field.getType() != PType.MESSAGE) {
throw new IllegalArgumentException("");
}
leafDescriptor = (PMessageDescriptor) field.getDescriptor();
if (leaf != null) {
leaf = (PMessage) leaf.get(field.getId());
}
}
String leafName = parts[parts.length - 1];
PField field = leafDescriptor.findFieldByName(leafName);
if (field != null) {
if (leaf != null) {
return Optional.of(new MessageFieldArgument(leaf, field, getColumnType(field)));
}
return Optional.of(new NullArgument(getColumnType(field)));
}
return Optional.empty();
}
use of org.jdbi.v3.core.internal.exceptions.Unchecked in project jdbi by jdbi.
the class FieldMapper method specialize0.
private Optional<RowMapper<T>> specialize0(StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) {
final List<FieldData> fields = new ArrayList<>();
for (Class<?> aType = type; aType != null; aType = aType.getSuperclass()) {
for (Field field : aType.getDeclaredFields()) {
Nested anno = field.getAnnotation(Nested.class);
if (anno == null) {
String paramName = prefix + paramName(field);
findColumnIndex(paramName, columnNames, columnNameMatchers, () -> debugName(field)).ifPresent(index -> {
QualifiedType<?> fieldType = QualifiedType.of(field.getGenericType()).withAnnotations(ctx.getConfig(Qualifiers.class).findFor(field));
@SuppressWarnings("unchecked") ColumnMapper<?> mapper = ctx.findColumnMapperFor(fieldType).orElse((ColumnMapper) (r, n, c) -> r.getObject(n));
fields.add(new FieldData(field, new SingleColumnMapper<>(mapper, index + 1)));
unmatchedColumns.remove(columnNames.get(index));
});
} else {
String nestedPrefix = prefix + anno.value().toLowerCase();
if (anyColumnsStartWithPrefix(columnNames, nestedPrefix, columnNameMatchers)) {
nestedMappers.computeIfAbsent(field, f -> new FieldMapper<>(field.getType(), nestedPrefix)).specialize0(ctx, columnNames, columnNameMatchers, unmatchedColumns).ifPresent(mapper -> fields.add(new FieldData(field, mapper)));
}
}
}
}
if (fields.isEmpty() && !columnNames.isEmpty()) {
return Optional.empty();
}
Collections.sort(fields, Comparator.comparing(f -> f.propagateNull ? 1 : 0));
final Optional<String> nullMarkerColumn = Optional.ofNullable(type.getAnnotation(PropagateNull.class)).map(PropagateNull::value);
return Optional.of((r, c) -> {
if (PojoMapper.propagateNull(r, nullMarkerColumn)) {
return null;
}
T obj = construct();
for (FieldData f : fields) {
Object value = f.mapper.map(r, ctx);
if (f.propagateNull && (value == null || (f.isPrimitive && r.wasNull()))) {
return null;
}
writeField(obj, f.field, value);
}
return obj;
});
}
Aggregations