-
Notifications
You must be signed in to change notification settings - Fork 0
ENG-5818vPersistOnly added optionals #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,12 @@ | |
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| import net.sf.persist.writer.Writer; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
|
|
@@ -91,6 +95,7 @@ public AnnotationTableMapping(final Class<?> objectClass) { | |
| if (annotation != null && annotation.autoGenerated()) { | ||
| autoGeneratedColumnsTemp.add(columnName); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| if (primaryKeysList.isEmpty()) { | ||
|
|
@@ -194,16 +199,40 @@ public Map<String, Method> getSettersMap() { | |
| return settersMap; | ||
| } | ||
|
|
||
| public Optional<String> getFieldName(final String columnName) { | ||
| Optional<String> fieldName = Optional.ofNullable(columnsMap.get(columnName.toLowerCase(Locale.ENGLISH))); | ||
| if (!fieldName.isPresent()) { | ||
| ENGINE_LOG.warn("Column name [" + columnName + "] has no corresponding field."); | ||
| } | ||
| return fieldName; | ||
| } | ||
|
|
||
| @Override | ||
| public Method getGetterForColumn(final String columnName) { | ||
| final String fieldName = columnsMap.get(columnName.toLowerCase(Locale.ENGLISH)); | ||
| return gettersMap.get(fieldName); | ||
| final Optional<String> fieldName = getFieldName(columnName); | ||
| if (fieldName.isPresent()) { | ||
| final Optional<Method> getterForFieldName = Optional.ofNullable(gettersMap.get(fieldName.get())); | ||
| if (!getterForFieldName.isPresent()) { | ||
| throw new NoSuchElementException( | ||
| "Could not find getter for columnn with field name [" + fieldName + "]"); | ||
| } | ||
| return getterForFieldName.get(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Method getSetterForColumn(final String columnName) { | ||
| final String fieldName = columnsMap.get(columnName.toLowerCase(Locale.ENGLISH)); | ||
| return settersMap.get(fieldName); | ||
| final Optional<String> fieldName = getFieldName(columnName); | ||
| if (fieldName.isPresent()) { | ||
| final Optional<Method> setterForFieldName = Optional.ofNullable(settersMap.get(fieldName.get())); | ||
| if (!setterForFieldName.isPresent()) { | ||
| throw new NoSuchElementException( | ||
| "Could not find setter for columnn with field name [" + fieldName + "]"); | ||
| } | ||
| return setterForFieldName.get(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public String getSelectSql() { | ||
|
|
@@ -226,6 +255,51 @@ public String getDeleteSql() { | |
| return deleteSql; | ||
| } | ||
|
|
||
| @Override | ||
| public Class<?> getOptionalSubType(final String columnName) { | ||
| final Optional<String> fieldName = getFieldName(columnName); | ||
| if (fieldName.isPresent()) { | ||
| final Optional<Class<?>> optSubTypeForFieldName = | ||
| Optional.ofNullable(annotationsMap.get(fieldName.get()).optionalSubType()); | ||
| if (!optSubTypeForFieldName.isPresent()) { | ||
| throw new NoSuchElementException( | ||
| "Could not find optional subtype for columnn with field name [" + fieldName + "]"); | ||
| } | ||
| return optSubTypeForFieldName.get(); | ||
| } | ||
| return null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if field name is not defined, we don't care, so return a null |
||
| } | ||
|
|
||
| @Override | ||
| public Class<?> getSerializationType(final String columnName) { | ||
| final Optional<String> fieldName = getFieldName(columnName); | ||
| if (fieldName.isPresent()) { | ||
| final Optional<Class<?>> serializationTypeForFieldName = | ||
| Optional.ofNullable(annotationsMap.get(fieldName.get()).serializeAs()); | ||
| if (!serializationTypeForFieldName.isPresent()) { | ||
| throw new NoSuchElementException( | ||
| "Could not find serialization type for columnn with field name [" + fieldName + "]"); | ||
| } | ||
| return serializationTypeForFieldName.get(); | ||
| } | ||
| return null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use case? |
||
| } | ||
|
|
||
| @Override | ||
| public Class<? extends Writer> getWriterClass(final String columnName) { | ||
| final Optional<String> fieldName = getFieldName(columnName); | ||
| if (fieldName.isPresent()) { | ||
| final Optional<Class<? extends Writer>> writerClassForFieldName = | ||
| Optional.ofNullable(annotationsMap.get(fieldName.get()).writerClass()); | ||
| if (!writerClassForFieldName.isPresent()) { | ||
| throw new NoSuchElementException( | ||
| "Could not find writer class for columnn with field name [" + fieldName + "]"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. columnn -> column (for this line and above exceptions) |
||
| } | ||
| return writerClassForFieldName.get(); | ||
| } | ||
| return null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use case? |
||
| } | ||
|
|
||
| // ---------- helpers ---------- | ||
|
|
||
| private static String[] toArray(final List<String> list) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,17 @@ | |
|
|
||
| import java.lang.reflect.Method; | ||
| import java.util.HashMap; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| import net.sf.persist.writer.Writer; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Represents the mapping of columns to getters and setters of a POJO. | ||
| * <p> | ||
|
|
@@ -17,6 +25,8 @@ | |
| */ | ||
| public class NoTableMapping extends Mapping { | ||
|
|
||
| private static final Logger ENGINE_LOG = LoggerFactory.getLogger("persist.engine"); | ||
|
|
||
| // POJO class | ||
| private final Class objectClass; | ||
|
|
||
|
|
@@ -29,6 +39,8 @@ public class NoTableMapping extends Mapping { | |
| // map possible column names to field names | ||
| private final Map<String, String> columnsMap; | ||
|
|
||
| private final Map<String, net.sf.persist.annotations.Column> annotationsMap; | ||
|
|
||
| public NoTableMapping(Class objectClass, NameGuesser nameGuesser) { | ||
|
|
||
| checkAnnotation(objectClass); | ||
|
|
@@ -37,7 +49,7 @@ public NoTableMapping(Class objectClass, NameGuesser nameGuesser) { | |
|
|
||
| // get the list of annotations, getters and setters | ||
| Map[] fieldsMaps = Mapping.getFieldsMaps(objectClass); | ||
| final Map<String, net.sf.persist.annotations.Column> annotationsMap = fieldsMaps[0]; | ||
| annotationsMap = fieldsMaps[0]; | ||
| gettersMap = fieldsMaps[1]; | ||
| settersMap = fieldsMaps[2]; | ||
|
|
||
|
|
@@ -91,16 +103,10 @@ public NoTableMapping(Class objectClass, NameGuesser nameGuesser) { | |
|
|
||
| } | ||
|
|
||
| /** | ||
| * Returns the field name associated with a given column. If a mapping can't | ||
| * be found, will throw a PersistException. | ||
| */ | ||
| public String getFieldNameForColumn(String columnName) { | ||
| String fieldName = columnsMap.get(columnName); | ||
| if (fieldName == null) { | ||
| throw new PersistException("Could map field for column [" + columnName + "] on class [" | ||
| + objectClass.getCanonicalName() | ||
| + "]. Please specify an explict @Column annotation for that column."); | ||
| public Optional<String> getFieldName(final String columnName) { | ||
| Optional<String> fieldName = Optional.ofNullable(columnsMap.get(columnName.toLowerCase(Locale.ENGLISH))); | ||
| if (!fieldName.isPresent()) { | ||
| ENGINE_LOG.warn("Column name [" + columnName + "] has no corresponding field."); | ||
| } | ||
| return fieldName; | ||
| } | ||
|
|
@@ -112,9 +118,17 @@ public String getFieldNameForColumn(String columnName) { | |
| * @see Mapping | ||
| */ | ||
| @Override | ||
| public Method getSetterForColumn(String columnName) { | ||
| String fieldName = getFieldNameForColumn(columnName); | ||
| return settersMap.get(fieldName); | ||
| public Method getSetterForColumn(final String columnName) { | ||
| final Optional<String> fieldName = getFieldName(columnName); | ||
| if (fieldName.isPresent()) { | ||
| final Optional<Method> setterForFieldName = Optional.ofNullable(settersMap.get(fieldName.get())); | ||
| if (!setterForFieldName.isPresent()) { | ||
| throw new NoSuchElementException( | ||
| "Could not find setter for columnn with field name [" + fieldName + "]"); | ||
| } | ||
| return setterForFieldName.get(); | ||
| } | ||
| return null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add warn (similar to getter) |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -124,11 +138,65 @@ public Method getSetterForColumn(String columnName) { | |
| * @see Mapping | ||
| */ | ||
| @Override | ||
| public Method getGetterForColumn(String columnName) { | ||
| String fieldName = getFieldNameForColumn(columnName); | ||
| return gettersMap.get(fieldName); | ||
| public Method getGetterForColumn(final String columnName) { | ||
| final Optional<String> fieldName = getFieldName(columnName); | ||
| if (fieldName.isPresent()) { | ||
| final Optional<Method> getterForFieldName = Optional.ofNullable(gettersMap.get(fieldName.get())); | ||
| if (!getterForFieldName.isPresent()) { | ||
| throw new NoSuchElementException( | ||
| "Could not find getter for columnn with field name [" + fieldName + "]"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comments as above (would love to understand the difference between "No Table Mapping" and "annotation table mapping " in persist library) |
||
| } | ||
| return getterForFieldName.get(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Class<?> getOptionalSubType(final String columnName) { | ||
| final Optional<String> fieldName = getFieldName(columnName); | ||
| if (fieldName.isPresent()) { | ||
| final Optional<Class<?>> optSubTypeForFieldName = | ||
| Optional.ofNullable(annotationsMap.get(fieldName.get()).optionalSubType()); | ||
| if (!optSubTypeForFieldName.isPresent()) { | ||
| throw new NoSuchElementException( | ||
| "Could not find optional subtype for columnn with field name [" + fieldName + "]"); | ||
| } | ||
| return optSubTypeForFieldName.get(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Class<?> getSerializationType(final String columnName) { | ||
| final Optional<String> fieldName = getFieldName(columnName); | ||
| if (fieldName.isPresent()) { | ||
| final Optional<Class<?>> serializationTypeForFieldName = | ||
| Optional.ofNullable(annotationsMap.get(fieldName.get()).serializeAs()); | ||
| if (!serializationTypeForFieldName.isPresent()) { | ||
| throw new NoSuchElementException( | ||
| "Could not find serialization type for columnn with field name [" + fieldName + "]"); | ||
| } | ||
| return serializationTypeForFieldName.get(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Class<? extends Writer> getWriterClass(final String columnName) { | ||
| final Optional<String> fieldName = getFieldName(columnName); | ||
| if (fieldName.isPresent()) { | ||
| final Optional<Class<? extends Writer>> writerClassForFieldName = | ||
| Optional.ofNullable(annotationsMap.get(fieldName.get()).writerClass()); | ||
| if (!writerClassForFieldName.isPresent()) { | ||
| throw new NoSuchElementException( | ||
| "Could not find writer class for columnn with field name [" + fieldName + "]"); | ||
| } | ||
| return writerClassForFieldName.get(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Checks if a given column name conflicts with an existing name in the | ||
| * columns map. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOG.warn - column exists in table that does not have a corresponding getter (or setter below) in the value class, ignoring column.