use of org.jdbi.v3.core.argument.Argument in project jdbi by jdbi.
the class Binding method toString.
@Override
public String toString() {
boolean wrote = false;
StringBuilder b = new StringBuilder();
b.append("{ positional:{");
for (Map.Entry<Integer, Argument> entry : positionals.entrySet()) {
wrote = true;
b.append(entry.getKey()).append(":").append(entry.getValue()).append(",");
}
if (wrote) {
wrote = false;
b.deleteCharAt(b.length() - 1);
}
b.append("}");
b.append(", named:{");
for (Map.Entry<String, Argument> entry : named.entrySet()) {
wrote = true;
b.append(entry.getKey()).append(":").append(entry.getValue()).append(",");
}
if (wrote) {
wrote = false;
b.deleteCharAt(b.length() - 1);
}
b.append("}");
b.append(", finder:[");
for (NamedArgumentFinder argument : namedArgumentFinder) {
wrote = true;
b.append(argument).append(",");
}
if (wrote) {
b.deleteCharAt(b.length() - 1);
}
b.append("]");
b.append("}");
return b.toString();
}
use of org.jdbi.v3.core.argument.Argument in project jdbi by jdbi.
the class DurationArgumentFactory method build.
@Override
public Argument build(Duration duration, ConfigRegistry config) {
final boolean isNegative = duration.isNegative();
if (isNegative) {
duration = duration.negated();
}
final long days = duration.toDays();
if (days > Integer.MAX_VALUE) {
throw new IllegalArgumentException(String.format("duration %s too large to be represented unambiguously as postgres interval", duration));
}
duration = duration.minusDays(days);
final int hours = (int) duration.toHours();
duration = duration.minusHours(hours);
final int minutes = (int) duration.toMinutes();
duration = duration.minusMinutes(minutes);
if (duration.getNano() % 1000 != 0) {
throw new IllegalArgumentException(String.format("duration %s too precise to represented as postgres interval", duration));
}
double seconds = duration.getSeconds() + duration.getNano() / 1e9;
final PGInterval interval = new PGInterval(0, 0, (int) days, hours, minutes, seconds);
if (isNegative) {
interval.scale(-1);
}
return (i, p, cx) -> p.setObject(i, interval, Types.OTHER);
}
use of org.jdbi.v3.core.argument.Argument in project jdbi by jdbi.
the class RegisterArgumentFactoryImpl method configureForType.
@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
RegisterArgumentFactory raf = (RegisterArgumentFactory) annotation;
Arguments arguments = registry.get(Arguments.class);
try {
arguments.register(raf.value().newInstance());
} catch (Exception e) {
throw new IllegalStateException("unable to instantiate specified argument factory", e);
}
}
use of org.jdbi.v3.core.argument.Argument in project jdbi by jdbi.
the class BindListFactory method createForParameter.
@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
final BindList bindList = (BindList) annotation;
final String name = ParameterUtil.findParameterName(bindList.value(), param).orElseThrow(() -> new UnsupportedOperationException("A @BindList parameter was not given a name, " + "and parameter name data is not present in the class file, for: " + param.getDeclaringExecutable() + "::" + param));
return (stmt, arg) -> {
if (arg == null || IterableLike.isEmpty(arg)) {
switch(bindList.onEmpty()) {
case VOID:
stmt.define(name, "");
return;
case NULL:
stmt.define(name, "null");
return;
case THROW:
throw new IllegalArgumentException(arg == null ? "argument is null; null was explicitly forbidden on this instance of BindList" : "argument is empty; emptiness was explicitly forbidden on this instance of BindList");
default:
throw new IllegalStateException(valueNotHandledMessage);
}
}
stmt.bindList(name, IterableLike.toList(arg));
};
}
use of org.jdbi.v3.core.argument.Argument 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();
}
Aggregations