mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2024-12-21 23:08:52 -05:00
Enable java 8 lambda for ResultSetIterable when compiling for 8+
This commit is contained in:
parent
12ba416181
commit
099d88c26d
@ -5,6 +5,7 @@ import com.moparisthebest.jdbc.ResultSetMapper;
|
||||
import com.moparisthebest.jdbc.util.ResultSetIterable;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.type.ArrayType;
|
||||
@ -26,14 +27,29 @@ import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.typeMirrorToCl
|
||||
*/
|
||||
public class CompileTimeResultSetMapper {
|
||||
|
||||
public static final SourceVersion RELEASE_8;
|
||||
|
||||
static {
|
||||
SourceVersion rl8 = null;
|
||||
try {
|
||||
rl8 = SourceVersion.valueOf("RELEASE_8");
|
||||
} catch(Throwable e) {
|
||||
// ignore
|
||||
}
|
||||
RELEASE_8 = rl8;
|
||||
}
|
||||
|
||||
public final Types types;
|
||||
public final TypeMirror collectionType, mapType, mapCollectionType, iteratorType, listIteratorType, finishableType, resultSetType, resultSetIterableType;
|
||||
private final boolean java8 = false;
|
||||
private final boolean java8;
|
||||
|
||||
public CompileTimeResultSetMapper(final ProcessingEnvironment processingEnv) {
|
||||
types = processingEnv.getTypeUtils();
|
||||
final Elements elements = processingEnv.getElementUtils();
|
||||
|
||||
// is this the proper way to do this?
|
||||
java8 = RELEASE_8 != null && processingEnv.getSourceVersion().ordinal() >= RELEASE_8.ordinal();
|
||||
|
||||
collectionType = types.getDeclaredType(elements.getTypeElement(Collection.class.getCanonicalName()), types.getWildcardType(null, null));
|
||||
mapType = types.getDeclaredType(elements.getTypeElement(Map.class.getCanonicalName()), types.getWildcardType(null, null), types.getWildcardType(null, null));
|
||||
mapCollectionType = types.getDeclaredType(elements.getTypeElement(Map.class.getCanonicalName()), types.getWildcardType(null, null), types.getWildcardType(collectionType, null));
|
||||
@ -106,11 +122,19 @@ public class CompileTimeResultSetMapper {
|
||||
}
|
||||
|
||||
public CompileTimeRowToObjectMapper getRowMapper(final String[] keys, TypeMirror returnTypeClass, String cal, TypeMirror mapValType, TypeMirror mapKeyType) {
|
||||
return new CompileTimeRowToObjectMapper(this, keys, returnTypeClass, cal, mapValType, mapKeyType);
|
||||
return getRowMapper(keys, returnTypeClass, "rs", cal, mapValType, mapKeyType);
|
||||
}
|
||||
|
||||
public CompileTimeRowToObjectMapper getRowMapper(final String[] keys, TypeMirror returnTypeClass, final String resultSetName, String cal, TypeMirror mapValType, TypeMirror mapKeyType) {
|
||||
return new CompileTimeRowToObjectMapper(this, keys, returnTypeClass, resultSetName, cal, mapValType, mapKeyType);
|
||||
}
|
||||
|
||||
public void writeObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal) throws IOException, ClassNotFoundException {
|
||||
getRowMapper(keys, returnTypeMirror, cal, null, null).gen(w, returnTypeMirror.toString());
|
||||
writeObject(w, keys, returnTypeMirror, "rs", cal);
|
||||
}
|
||||
|
||||
public void writeObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String resultSetName, final String cal) throws IOException, ClassNotFoundException {
|
||||
getRowMapper(keys, returnTypeMirror, resultSetName, cal, null, null).gen(w, returnTypeMirror.toString());
|
||||
}
|
||||
|
||||
public void toObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal, final String cleaner) throws IOException, ClassNotFoundException {
|
||||
@ -125,18 +149,16 @@ public class CompileTimeResultSetMapper {
|
||||
w.write("\t\t\treturn com.moparisthebest.jdbc.util.ResultSetIterable.getResultSetIterable(rs,\n\t\t\t\t\trs.next() ? ");
|
||||
|
||||
if(java8) {
|
||||
w.append("(rs, ").append(cal == null ? "_cal" : cal).append(") -> {\n");
|
||||
w.append("(_rs, _cal) -> {\n");
|
||||
} else {
|
||||
final String returnTypeString = returnTypeMirror.toString();
|
||||
w.append("new com.moparisthebest.jdbc.util.ResultSetToObject<")
|
||||
.append(returnTypeString).append(">() {\n\t\t\t\t\tpublic ")
|
||||
.append(returnTypeString).append(" toObject(final ResultSet rs, final java.util.Calendar ")
|
||||
.append(cal == null ? "_cal" : cal)
|
||||
.append(") throws SQLException {\n");
|
||||
.append(returnTypeString).append(" toObject(final ResultSet _rs, final java.util.Calendar _cal) throws SQLException {\n");
|
||||
}
|
||||
|
||||
// com.moparisthebest.jdbc.util.ResultSetToObject implementation
|
||||
writeObject(w, keys, returnTypeMirror, cal);
|
||||
writeObject(w, keys, returnTypeMirror, "_rs", cal == null ? null : "_cal");
|
||||
w.write("\t\t\t\t\t\treturn ");
|
||||
clean(w, cleaner).write(";\n");
|
||||
// end ResultSetToObject implementation
|
||||
|
@ -31,7 +31,7 @@ public class CompileTimeRowToObjectMapper {
|
||||
/**
|
||||
* Calendar instance for date/time mappings.
|
||||
*/
|
||||
protected final String _calendarName;
|
||||
protected final String _calendarName, _resultSetName;
|
||||
|
||||
/**
|
||||
* Class to map ResultSet Rows to.
|
||||
@ -51,11 +51,12 @@ public class CompileTimeRowToObjectMapper {
|
||||
protected Element[] _fields = null;
|
||||
protected int[] _fieldTypes;
|
||||
|
||||
public CompileTimeRowToObjectMapper(final CompileTimeResultSetMapper rsm, final String[] keys, final TypeMirror returnTypeClass, final String calendarName, final TypeMirror mapValType, final TypeMirror mapKeyType) {
|
||||
public CompileTimeRowToObjectMapper(final CompileTimeResultSetMapper rsm, final String[] keys, final TypeMirror returnTypeClass, final String resultSetName, final String calendarName, final TypeMirror mapValType, final TypeMirror mapKeyType) {
|
||||
this.rsm = rsm;
|
||||
this.keys = keys;
|
||||
|
||||
_calendarName = calendarName;
|
||||
_resultSetName = resultSetName;
|
||||
_mapKeyType = mapKeyType;
|
||||
|
||||
_columnCount = keys.length - 1;
|
||||
@ -389,10 +390,10 @@ public class CompileTimeRowToObjectMapper {
|
||||
}
|
||||
|
||||
public void extractColumnValueString(final Appendable java, final int index, final int resultType) throws IOException, ClassNotFoundException {
|
||||
CompilingRowToObjectMapper.extractColumnValueString(java, index, resultType, _calendarName);
|
||||
CompilingRowToObjectMapper.extractColumnValueString(java, index, resultType, _resultSetName, _calendarName);
|
||||
}
|
||||
|
||||
public void extractColumnValueString(final Appendable java, final int index, final TypeMirror resultType) throws IOException, ClassNotFoundException {
|
||||
CompilingRowToObjectMapper.extractColumnValueString(java, index, typeMirrorToClass(resultType), _calendarName);
|
||||
CompilingRowToObjectMapper.extractColumnValueString(java, index, typeMirrorToClass(resultType), _resultSetName, _calendarName);
|
||||
}
|
||||
}
|
||||
|
@ -354,7 +354,16 @@ public class CompilingRowToObjectMapper<K, T> extends RowToObjectMapper<K, T> {
|
||||
}
|
||||
|
||||
public static void extractColumnValueString(final Appendable java, final int index, final Class resultType, final String calendarName) throws IOException {
|
||||
extractColumnValueString(java, index, _tmf.getTypeId(resultType), calendarName);
|
||||
extractColumnValueString(java, index, _tmf.getTypeId(resultType), "rs", calendarName);
|
||||
}
|
||||
|
||||
public static void extractColumnValueString(final Appendable java, final int index, final Class resultType, final String resultSetName, final String calendarName) throws IOException {
|
||||
extractColumnValueString(java, index, _tmf.getTypeId(resultType), resultSetName, calendarName);
|
||||
}
|
||||
|
||||
|
||||
public static void extractColumnValueString(final Appendable java, final int index, final int resultType, final String calendarName) throws IOException {
|
||||
extractColumnValueString(java, index, resultType, "rs", calendarName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -365,101 +374,101 @@ public class CompilingRowToObjectMapper<K, T> extends RowToObjectMapper<K, T> {
|
||||
* @return The extracted value
|
||||
* @throws java.sql.SQLException on error.
|
||||
*/
|
||||
public static void extractColumnValueString(final Appendable java, final int index, final int resultType, final String calendarName) throws IOException {
|
||||
public static void extractColumnValueString(final Appendable java, final int index, final int resultType, final String resultSetName, final String calendarName) throws IOException {
|
||||
switch (resultType) {
|
||||
case TypeMappingsFactory.TYPE_INT:
|
||||
java.append("rs.getInt(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getInt(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_LONG:
|
||||
java.append("rs.getLong(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getLong(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_FLOAT:
|
||||
java.append("rs.getFloat(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getFloat(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_DOUBLE:
|
||||
java.append("rs.getDouble(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getDouble(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_BYTE:
|
||||
java.append("rs.getByte(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getByte(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_SHORT:
|
||||
java.append("rs.getInt(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getInt(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_BOOLEAN:
|
||||
java.append("getBooleanYN(rs, ").append(String.valueOf(index)).append(")");
|
||||
java.append("getBooleanYN(").append(resultSetName).append(", ").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_INT_OBJ:
|
||||
java.append("getObjectInt(rs, ").append(String.valueOf(index)).append(")");
|
||||
java.append("getObjectInt(").append(resultSetName).append(", ").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_LONG_OBJ:
|
||||
java.append("getObjectLong(rs, ").append(String.valueOf(index)).append(")");
|
||||
java.append("getObjectLong(").append(resultSetName).append(", ").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_FLOAT_OBJ:
|
||||
java.append("getObjectFloat(rs, ").append(String.valueOf(index)).append(")");
|
||||
java.append("getObjectFloat(").append(resultSetName).append(", ").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_DOUBLE_OBJ:
|
||||
java.append("getObjectDouble(rs, ").append(String.valueOf(index)).append(")");
|
||||
java.append("getObjectDouble(").append(resultSetName).append(", ").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_BYTE_OBJ:
|
||||
java.append("getObjectByte(rs, ").append(String.valueOf(index)).append(")");
|
||||
java.append("getObjectByte(").append(resultSetName).append(", ").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_SHORT_OBJ:
|
||||
java.append("getObjectShort(rs, ").append(String.valueOf(index)).append(")");
|
||||
java.append("getObjectShort(").append(resultSetName).append(", ").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_BOOLEAN_OBJ:
|
||||
java.append("getObjectBooleanYN(rs, ").append(String.valueOf(index)).append(")");
|
||||
java.append("getObjectBooleanYN(").append(resultSetName).append(", ").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_STRING:
|
||||
case TypeMappingsFactory.TYPE_XMLBEAN_ENUM:
|
||||
java.append("rs.getString(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getString(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_BIG_DECIMAL:
|
||||
java.append("rs.getBigDecimal(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getBigDecimal(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_BYTES:
|
||||
java.append("rs.getBytes(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getBytes(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_TIMESTAMP:
|
||||
java.append("rs.getTimestamp(").append(String.valueOf(index));
|
||||
java.append(resultSetName).append(".getTimestamp(").append(String.valueOf(index));
|
||||
if(calendarName != null)
|
||||
java.append(", ").append(calendarName);
|
||||
java.append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_TIME:
|
||||
java.append("rs.getTime(").append(String.valueOf(index));
|
||||
java.append(resultSetName).append(".getTime(").append(String.valueOf(index));
|
||||
if(calendarName != null)
|
||||
java.append(", ").append(calendarName);
|
||||
java.append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_SQLDATE:
|
||||
java.append("rs.getDate(").append(String.valueOf(index));
|
||||
java.append(resultSetName).append(".getDate(").append(String.valueOf(index));
|
||||
if(calendarName != null)
|
||||
java.append(", ").append(calendarName);
|
||||
java.append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_DATE:
|
||||
java.append("getUtilDate(rs, ").append(String.valueOf(index));
|
||||
java.append("getUtilDate(").append(resultSetName).append(", ").append(String.valueOf(index));
|
||||
if(calendarName != null)
|
||||
java.append(", ").append(calendarName);
|
||||
java.append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_CALENDAR:
|
||||
java.append("getCalendar(rs, ").append(String.valueOf(index));
|
||||
java.append("getCalendar(").append(resultSetName).append(", ").append(String.valueOf(index));
|
||||
if(calendarName != null)
|
||||
java.append(", ").append(calendarName);
|
||||
java.append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_REF:
|
||||
java.append("rs.getRef(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getRef(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_BLOB:
|
||||
java.append("rs.getBlob(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getBlob(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_CLOB:
|
||||
java.append("rs.getClob(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getClob(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_ARRAY:
|
||||
java.append("rs.getArray(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getArray(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
case TypeMappingsFactory.TYPE_READER:
|
||||
case TypeMappingsFactory.TYPE_STREAM:
|
||||
@ -467,7 +476,7 @@ public class CompilingRowToObjectMapper<K, T> extends RowToObjectMapper<K, T> {
|
||||
case TypeMappingsFactory.TYPE_STRUCT:
|
||||
case TypeMappingsFactory.TYPE_UNKNOWN:
|
||||
// JAVA_TYPE (could be any), or REF
|
||||
java.append("rs.getObject(").append(String.valueOf(index)).append(")");
|
||||
java.append(resultSetName).append(".getObject(").append(String.valueOf(index)).append(")");
|
||||
return;
|
||||
default:
|
||||
throw new MapperException("internal error: unknown type ID: " + Integer.toString(resultType));
|
||||
|
Loading…
Reference in New Issue
Block a user