BindEnumValue.java

  1. package net.morimekta.providence.jdbi.v2.annotations;

  2. import net.morimekta.providence.PEnumValue;
  3. import net.morimekta.providence.jdbi.v2.EnumValueArgument;
  4. import net.morimekta.providence.jdbi.v2.util.NullArgument;
  5. import org.skife.jdbi.v2.sqlobject.Binder;
  6. import org.skife.jdbi.v2.sqlobject.BinderFactory;
  7. import org.skife.jdbi.v2.sqlobject.BindingAnnotation;

  8. import java.lang.annotation.ElementType;
  9. import java.lang.annotation.Retention;
  10. import java.lang.annotation.RetentionPolicy;
  11. import java.lang.annotation.Target;
  12. import java.sql.Types;

  13. @Target(value = ElementType.PARAMETER)
  14. @Retention(value = RetentionPolicy.RUNTIME)
  15. @BindingAnnotation(BindEnumValue.Factory.class)
  16. public @interface BindEnumValue {
  17.     String value();

  18.     class Factory implements BinderFactory<BindEnumValue> {
  19.         @Override
  20.         public Binder<BindEnumValue, Object> build(BindEnumValue bind) {
  21.             return (sqlStatement, annotation, arg) -> {
  22.                 if (arg instanceof PEnumValue) {
  23.                     PEnumValue value = (PEnumValue) arg;
  24.                     sqlStatement.bind(annotation.value(), new EnumValueArgument(value));
  25.                 } else if (arg == null){
  26.                     sqlStatement.bind(annotation.value(), new NullArgument(Types.INTEGER));
  27.                 } else {
  28.                     throw new IllegalArgumentException("Not a providence enum value: " + arg.getClass().toString());
  29.                 }
  30.             };
  31.         }
  32.     }

  33. }