net.morimekta.providence.jdbi.v3.ProvidenceJdbi Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of providence-jdbi-v3 Show documentation
Show all versions of providence-jdbi-v3 Show documentation
Utilities for handling providence messages using jdbi v3.
/*
* Copyright 2018-2019 Providence Authors
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package net.morimekta.providence.jdbi.v3;
import net.morimekta.providence.PMessage;
import net.morimekta.providence.PMessageOrBuilder;
import net.morimekta.providence.descriptor.PField;
import net.morimekta.providence.descriptor.PMessageDescriptor;
import net.morimekta.providence.jdbi.v3.util.FieldType;
import net.morimekta.util.collect.UnmodifiableMap;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import static net.morimekta.providence.jdbi.v3.MessageRowMapper.ALL_FIELDS;
/**
* Utility class and helper to make mappers and argument helpers for
* JDBI queries and updates.
*/
public class ProvidenceJdbi {
/**
* Bind to the given field for the message.
*
* @param message The message tp bind value from.
* @param field The field to bind to.
* @param The message type.
* @return The message field argument.
*/
public static >
MessageFieldArgument toField(PMessageOrBuilder message, PField field) {
return new MessageFieldArgument<>(message, field);
}
/**
* Bind to the given field for the message.
*
* @param message The message tp bind value from.
* @param field The field to bind to.
* @param type The SQL type.
* @param The message type.
* @return The message field argument.
*/
public static >
MessageFieldArgument toField(PMessageOrBuilder message, PField field, int type) {
return new MessageFieldArgument<>(message, field, type);
}
/**
* With all column with default types.
*
* @param The message type.
* @return The mapped field.
*/
public static >
MappedField columnsFromAllFields() {
return new MappedField<>(ALL_FIELDS, null);
}
/**
* With column mapped to field using the field name.
*
* @param field Field it is mapped to.
* @param The message type.
* @return The mapped field.
*/
public static >
MappedField withColumn(PField field) {
return new MappedField<>(field.getName(), field);
}
/**
* With column mapped to field.
*
* @param name Name of column.
* @param field Field it is mapped to.
* @param The message type.
* @return The mapped field.
*/
public static >
MappedField withColumn(String name, PField field) {
return new MappedField<>(name, field);
}
/**
* Bind to message using row mapper.
*
* @param descriptor The message descriptor.
* @param fieldMapping Extra field mapping.
* @param The message type.
* @return The row mapper.
*/
@SafeVarargs
public static >
MessageRowMapper toMessage(@Nonnull PMessageDescriptor descriptor,
@Nonnull MappedField... fieldMapping) {
return new MessageRowMapper<>(descriptor, makeMapping(fieldMapping));
}
/**
* Bind to message using row mapper.
*
* @param tableName Table name to restrict field lookup to.
* @param descriptor The message descriptor.
* @param fieldMapping Extra field mapping.
* @param The message type.
* @return The row mapper.
*/
@SafeVarargs
public static >
MessageRowMapper toMessage(@Nonnull String tableName,
@Nonnull PMessageDescriptor descriptor,
@Nonnull MappedField... fieldMapping) {
return new MessageRowMapper<>(tableName, descriptor, makeMapping(fieldMapping));
}
/**
* With field mapped to SQL type.
*
* @param field The field to be mapped.
* @param type The SQL type. See {@link java.sql.Types}.
* @param The message type.
* @return The field type mapping.
*/
public static >
FieldType withType(PField field, int type) {
return new FieldType<>(field, type);
}
/**
* Get named argument finder for message.
*
* @param message The message to map fields from.
* @param fieldTypes Field type mappings.
* @param The message type.
* @return The named argument finder.
*/
@SafeVarargs
public static >
MessageNamedArgumentFinder forMessage(@Nonnull PMessageOrBuilder message,
@Nonnull FieldType... fieldTypes) {
return new MessageNamedArgumentFinder<>(null, message, makeFieldTypes(fieldTypes));
}
/**
* Get named argument finder for message.
*
* @param prefix Name prefix for naming distinction.
* @param message The message to map fields from.
* @param fieldTypes Field type mappings.
* @param The message type.
* @return The named argument finder.
*/
@SafeVarargs
public static >
MessageNamedArgumentFinder forMessage(@Nonnull String prefix,
@Nonnull PMessageOrBuilder message,
@Nonnull FieldType... fieldTypes) {
return new MessageNamedArgumentFinder<>(prefix, message, makeFieldTypes(fieldTypes));
}
public static class MappedField > {
private final String name;
private final PField field;
MappedField(String name, PField field) {
this.name = name;
this.field = field;
}
}
@SafeVarargs
private static >
Map, Integer> makeFieldTypes(FieldType... mappings) {
UnmodifiableMap.Builder, Integer> builder = UnmodifiableMap.builder();
for (FieldType mapping : mappings) {
builder.put(mapping.field, mapping.type);
}
return builder.build();
}
@SafeVarargs
private static >
Map> makeMapping(@Nonnull MappedField... mappedFields) {
HashMap> out = new HashMap<>();
for (MappedField mappedField : mappedFields) {
out.put(mappedField.name.toUpperCase(Locale.US), mappedField.field);
}
return out;
}
}