Compile differently based on Calendar object existence

This commit is contained in:
Travis Burtrum 2017-06-07 20:30:58 -04:00
parent 8e125d8e68
commit 85e0d868fe
9 changed files with 155 additions and 77 deletions

View File

@ -69,41 +69,38 @@ public class ResultSetUtil {
return ret; return ret;
} }
public static Timestamp getTimestamp(final ResultSet _resultSet, final Calendar _cal, final int index) throws SQLException { public static java.util.Date getUtilDate(final ResultSet _resultSet, final int index) throws SQLException {
if (null == _cal)
return _resultSet.getTimestamp(index);
else
return _resultSet.getTimestamp(index, _cal);
}
public static Time getTime(final ResultSet _resultSet, final Calendar _cal, final int index) throws SQLException {
if (null == _cal)
return _resultSet.getTime(index);
else
return _resultSet.getTime(index, _cal);
}
public static java.sql.Date getSqlDate(final ResultSet _resultSet, final Calendar _cal, final int index) throws SQLException {
if (null == _cal)
return _resultSet.getDate(index);
else
return _resultSet.getDate(index, _cal);
}
public static java.util.Date getUtilDate(final ResultSet _resultSet, final Calendar _cal, final int index) throws SQLException {
// convert explicity to java.util.Date // convert explicity to java.util.Date
// 12918 | knex does not return java.sql.Date properly from web service // 12918 | knex does not return java.sql.Date properly from web service
java.sql.Timestamp ts = (null == _cal) ? _resultSet.getTimestamp(index) : _resultSet.getTimestamp(index, _cal); java.sql.Timestamp ts = _resultSet.getTimestamp(index);
if (null == ts) if (null == ts)
return null; return null;
return new java.util.Date(ts.getTime()); return new java.util.Date(ts.getTime());
} }
public static Calendar getCalendar(final ResultSet _resultSet, final Calendar _cal, final int index) throws SQLException { public static java.util.Date getUtilDate(final ResultSet _resultSet, final int index, final Calendar _cal) throws SQLException {
java.sql.Timestamp ts = (null == _cal) ? _resultSet.getTimestamp(index) : _resultSet.getTimestamp(index, _cal); // convert explicity to java.util.Date
// 12918 | knex does not return java.sql.Date properly from web service
java.sql.Timestamp ts = _resultSet.getTimestamp(index, _cal);
if (null == ts) if (null == ts)
return null; return null;
Calendar c = (null == _cal) ? Calendar.getInstance() : (Calendar) _cal.clone(); return new java.util.Date(ts.getTime());
}
public static Calendar getCalendar(final ResultSet _resultSet, final int index) throws SQLException {
java.sql.Timestamp ts = _resultSet.getTimestamp(index);
if (null == ts)
return null;
Calendar c = Calendar.getInstance();
c.setTimeInMillis(ts.getTime());
return c;
}
public static Calendar getCalendar(final ResultSet _resultSet, final int index, final Calendar _cal) throws SQLException {
java.sql.Timestamp ts = _resultSet.getTimestamp(index, _cal);
if (null == ts)
return null;
Calendar c = (Calendar) _cal.clone();
c.setTimeInMillis(ts.getTime()); c.setTimeInMillis(ts.getTime());
return c; return c;
} }

View File

@ -56,7 +56,7 @@ public class CompileTimeResultSetMapper {
return typeMirrorStringNoGenerics(returnType); return typeMirrorStringNoGenerics(returnType);
} }
public void mapToResultType(final Writer w, final String[] keys, final ExecutableElement eeMethod, final int arrayMaxLength, final Calendar cal) throws IOException, NoSuchMethodException, ClassNotFoundException { public void mapToResultType(final Writer w, final String[] keys, final ExecutableElement eeMethod, final int arrayMaxLength, final String cal) throws IOException, NoSuchMethodException, ClassNotFoundException {
//final Method m = fromExecutableElement(eeMethod); //final Method m = fromExecutableElement(eeMethod);
//final Class returnType = m.getReturnType(); //final Class returnType = m.getReturnType();
final TypeMirror returnTypeMirror = eeMethod.getReturnType(); final TypeMirror returnTypeMirror = eeMethod.getReturnType();
@ -93,21 +93,21 @@ public class CompileTimeResultSetMapper {
} }
} }
public CompileTimeRowToObjectMapper getRowMapper(final String[] keys, TypeMirror returnTypeClass, Calendar cal, TypeMirror mapValType, TypeMirror mapKeyType) { public CompileTimeRowToObjectMapper getRowMapper(final String[] keys, TypeMirror returnTypeClass, String cal, TypeMirror mapValType, TypeMirror mapKeyType) {
return new CompileTimeRowToObjectMapper(this, keys, returnTypeClass, cal, mapValType, mapKeyType); return new CompileTimeRowToObjectMapper(this, keys, returnTypeClass, cal, mapValType, mapKeyType);
} }
public void writeObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final Calendar cal) throws IOException, ClassNotFoundException { 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()); getRowMapper(keys, returnTypeMirror, cal, null, null).gen(w, returnTypeMirror.toString());
} }
public void toObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final Calendar cal) throws IOException, ClassNotFoundException { public void toObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal) throws IOException, ClassNotFoundException {
w.write("\t\t\tif(rs.next()) {\n"); w.write("\t\t\tif(rs.next()) {\n");
writeObject(w, keys, returnTypeMirror, cal); writeObject(w, keys, returnTypeMirror, cal);
w.write("\t\t\t\treturn ret;\n\t\t\t} else {\n\t\t\t\treturn null;\n\t\t\t}\n"); w.write("\t\t\t\treturn ret;\n\t\t\t} else {\n\t\t\t\treturn null;\n\t\t\t}\n");
} }
public void writeCollection(final Writer w, final String[] keys, final String returnTypeString, final String concreteTypeString, final TypeMirror componentTypeMirror, int arrayMaxLength, Calendar cal) throws IOException, ClassNotFoundException { public void writeCollection(final Writer w, final String[] keys, final String returnTypeString, final String concreteTypeString, final TypeMirror componentTypeMirror, int arrayMaxLength, String cal) throws IOException, ClassNotFoundException {
w.write("\t\t\tfinal "); w.write("\t\t\tfinal ");
w.write(returnTypeString); w.write(returnTypeString);
w.write(" _colret = new "); w.write(" _colret = new ");
@ -118,13 +118,13 @@ public class CompileTimeResultSetMapper {
w.write("\t\t\t\t_colret.add(ret);\n\t\t\t}\n"); w.write("\t\t\t\t_colret.add(ret);\n\t\t\t}\n");
} }
public void toCollection(final Writer w, final String[] keys, final TypeMirror collectionTypeMirror, final TypeMirror componentTypeMirror, int arrayMaxLength, Calendar cal) throws IOException, ClassNotFoundException { public void toCollection(final Writer w, final String[] keys, final TypeMirror collectionTypeMirror, final TypeMirror componentTypeMirror, int arrayMaxLength, String cal) throws IOException, ClassNotFoundException {
final String collectionType = getConcreteClassCanonicalName(collectionTypeMirror, ArrayList.class); final String collectionType = getConcreteClassCanonicalName(collectionTypeMirror, ArrayList.class);
writeCollection(w, keys, collectionTypeMirror.toString(), collectionType, componentTypeMirror, arrayMaxLength, cal); writeCollection(w, keys, collectionTypeMirror.toString(), collectionType, componentTypeMirror, arrayMaxLength, cal);
w.write("\t\t\treturn _colret;\n"); w.write("\t\t\treturn _colret;\n");
} }
public void toArray(final Writer w, final String[] keys, final TypeMirror componentTypeMirror, int arrayMaxLength, Calendar cal) throws IOException, ClassNotFoundException { public void toArray(final Writer w, final String[] keys, final TypeMirror componentTypeMirror, int arrayMaxLength, String cal) throws IOException, ClassNotFoundException {
final String returnTypeString = componentTypeMirror.toString(); final String returnTypeString = componentTypeMirror.toString();
writeCollection(w, keys, "java.util.List<" + returnTypeString + ">", "java.util.ArrayList", componentTypeMirror, arrayMaxLength, cal); writeCollection(w, keys, "java.util.List<" + returnTypeString + ">", "java.util.ArrayList", componentTypeMirror, arrayMaxLength, cal);
w.write("\t\t\treturn _colret.toArray(new "); w.write("\t\t\treturn _colret.toArray(new ");
@ -132,19 +132,19 @@ public class CompileTimeResultSetMapper {
w.write("[_colret.size()]);\n"); w.write("[_colret.size()]);\n");
} }
public void toListIterator(final Writer w, final String[] keys, final TypeMirror componentTypeMirror, int arrayMaxLength, Calendar cal) throws IOException, ClassNotFoundException { public void toListIterator(final Writer w, final String[] keys, final TypeMirror componentTypeMirror, int arrayMaxLength, String cal) throws IOException, ClassNotFoundException {
final String returnTypeString = componentTypeMirror.toString(); final String returnTypeString = componentTypeMirror.toString();
writeCollection(w, keys, "java.util.List<" + returnTypeString + ">", "java.util.ArrayList", componentTypeMirror, arrayMaxLength, cal); writeCollection(w, keys, "java.util.List<" + returnTypeString + ">", "java.util.ArrayList", componentTypeMirror, arrayMaxLength, cal);
w.write("\t\t\treturn _colret.listIterator();\n"); w.write("\t\t\treturn _colret.listIterator();\n");
} }
public void toIterator(final Writer w, final String[] keys, final TypeMirror componentTypeMirror, int arrayMaxLength, Calendar cal) throws IOException, ClassNotFoundException { public void toIterator(final Writer w, final String[] keys, final TypeMirror componentTypeMirror, int arrayMaxLength, String cal) throws IOException, ClassNotFoundException {
final String returnTypeString = componentTypeMirror.toString(); final String returnTypeString = componentTypeMirror.toString();
writeCollection(w, keys, "java.util.List<" + returnTypeString + ">", "java.util.ArrayList", componentTypeMirror, arrayMaxLength, cal); writeCollection(w, keys, "java.util.List<" + returnTypeString + ">", "java.util.ArrayList", componentTypeMirror, arrayMaxLength, cal);
w.write("\t\t\treturn _colret.iterator();\n"); w.write("\t\t\treturn _colret.iterator();\n");
} }
public void toMap(final Writer w, final String[] keys, final TypeMirror mapTypeMirror, final TypeMirror mapKeyTypeMirror, final TypeMirror componentTypeMirror, int arrayMaxLength, Calendar cal) throws IOException, ClassNotFoundException { public void toMap(final Writer w, final String[] keys, final TypeMirror mapTypeMirror, final TypeMirror mapKeyTypeMirror, final TypeMirror componentTypeMirror, int arrayMaxLength, String cal) throws IOException, ClassNotFoundException {
final String mapType = getConcreteClassCanonicalName(mapTypeMirror, HashMap.class); final String mapType = getConcreteClassCanonicalName(mapTypeMirror, HashMap.class);
final String returnTypeString = mapTypeMirror.toString(); final String returnTypeString = mapTypeMirror.toString();
w.write("\t\t\tfinal "); w.write("\t\t\tfinal ");
@ -168,7 +168,7 @@ public class CompileTimeResultSetMapper {
final TypeMirror mapKeyTypeMirror, final TypeMirror mapKeyTypeMirror,
final TypeMirror collectionTypeMirror, final TypeMirror collectionTypeMirror,
final TypeMirror componentTypeMirror, final TypeMirror componentTypeMirror,
int arrayMaxLength, Calendar cal) throws IOException, ClassNotFoundException { int arrayMaxLength, String cal) throws IOException, ClassNotFoundException {
final String mapType = getConcreteClassCanonicalName(mapTypeMirror, HashMap.class); final String mapType = getConcreteClassCanonicalName(mapTypeMirror, HashMap.class);
final String collectionType = getConcreteClassCanonicalName(collectionTypeMirror, ArrayList.class); final String collectionType = getConcreteClassCanonicalName(collectionTypeMirror, ArrayList.class);
final String returnTypeString = mapTypeMirror.toString(); final String returnTypeString = mapTypeMirror.toString();

View File

@ -1,7 +1,6 @@
package com.moparisthebest.jdbc.codegen; package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.CompilingRowToObjectMapper; import com.moparisthebest.jdbc.CompilingRowToObjectMapper;
import com.moparisthebest.jdbc.Finishable;
import com.moparisthebest.jdbc.MapperException; import com.moparisthebest.jdbc.MapperException;
import com.moparisthebest.jdbc.TypeMappingsFactory; import com.moparisthebest.jdbc.TypeMappingsFactory;
@ -11,8 +10,6 @@ import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import java.io.IOException; import java.io.IOException;
import java.io.Writer;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -34,7 +31,7 @@ public class CompileTimeRowToObjectMapper {
/** /**
* Calendar instance for date/time mappings. * Calendar instance for date/time mappings.
*/ */
protected final Calendar _cal; protected final String _calendarName;
/** /**
* Class to map ResultSet Rows to. * Class to map ResultSet Rows to.
@ -54,11 +51,11 @@ public class CompileTimeRowToObjectMapper {
protected Element[] _fields = null; protected Element[] _fields = null;
protected int[] _fieldTypes; protected int[] _fieldTypes;
public CompileTimeRowToObjectMapper(final CompileTimeResultSetMapper rsm, final String[] keys, final TypeMirror returnTypeClass, final Calendar cal, final TypeMirror mapValType, final TypeMirror mapKeyType) { public CompileTimeRowToObjectMapper(final CompileTimeResultSetMapper rsm, final String[] keys, final TypeMirror returnTypeClass, final String calendarName, final TypeMirror mapValType, final TypeMirror mapKeyType) {
this.rsm = rsm; this.rsm = rsm;
this.keys = keys; this.keys = keys;
_cal = cal; _calendarName = calendarName;
_mapKeyType = mapKeyType; _mapKeyType = mapKeyType;
_columnCount = keys.length - 1; _columnCount = keys.length - 1;
@ -392,10 +389,10 @@ public class CompileTimeRowToObjectMapper {
} }
public void extractColumnValueString(final Appendable java, final int index, final int resultType) throws IOException, ClassNotFoundException { public void extractColumnValueString(final Appendable java, final int index, final int resultType) throws IOException, ClassNotFoundException {
CompilingRowToObjectMapper.extractColumnValueString(java, index, resultType); CompilingRowToObjectMapper.extractColumnValueString(java, index, resultType, _calendarName);
} }
public void extractColumnValueString(final Appendable java, final int index, final TypeMirror resultType) throws IOException, ClassNotFoundException { public void extractColumnValueString(final Appendable java, final int index, final TypeMirror resultType) throws IOException, ClassNotFoundException {
CompilingRowToObjectMapper.extractColumnValueString(java, index, typeMirrorToClass(resultType)); CompilingRowToObjectMapper.extractColumnValueString(java, index, typeMirrorToClass(resultType), _calendarName);
} }
} }

View File

@ -29,7 +29,7 @@ public class JdbcMapperProcessor extends AbstractProcessor {
private Types types; private Types types;
private TypeMirror sqlExceptionType, stringType, numberType, utilDateType, readerType, clobType, private TypeMirror sqlExceptionType, stringType, numberType, utilDateType, readerType, clobType,
byteArrayType, inputStreamType, fileType, blobType, sqlArrayType, collectionType; byteArrayType, inputStreamType, fileType, blobType, sqlArrayType, collectionType, calendarType;
private JdbcMapper.DatabaseType defaultDatabaseType; private JdbcMapper.DatabaseType defaultDatabaseType;
private String defaultArrayNumberTypeName, defaultArrayStringTypeName; private String defaultArrayNumberTypeName, defaultArrayStringTypeName;
private CompileTimeResultSetMapper rsm; private CompileTimeResultSetMapper rsm;
@ -52,6 +52,7 @@ public class JdbcMapperProcessor extends AbstractProcessor {
inputStreamType = elements.getTypeElement(InputStream.class.getCanonicalName()).asType(); inputStreamType = elements.getTypeElement(InputStream.class.getCanonicalName()).asType();
fileType = elements.getTypeElement(File.class.getCanonicalName()).asType(); fileType = elements.getTypeElement(File.class.getCanonicalName()).asType();
blobType = elements.getTypeElement(Blob.class.getCanonicalName()).asType(); blobType = elements.getTypeElement(Blob.class.getCanonicalName()).asType();
calendarType = elements.getTypeElement(Calendar.class.getCanonicalName()).asType();
// throws NPE: // throws NPE:
//byteArrayType = elements.getTypeElement(byte[].class.getCanonicalName()).asType(); //byteArrayType = elements.getTypeElement(byte[].class.getCanonicalName()).asType();
//byteArrayType = this.types.getArrayType(elements.getTypeElement(byte.class.getCanonicalName()).asType()); //byteArrayType = this.types.getArrayType(elements.getTypeElement(byte.class.getCanonicalName()).asType());
@ -211,6 +212,7 @@ public class JdbcMapperProcessor extends AbstractProcessor {
// build query and bind param order // build query and bind param order
final List<VariableElement> bindParams = new ArrayList<VariableElement>(); final List<VariableElement> bindParams = new ArrayList<VariableElement>();
final String sqlStatement; final String sqlStatement;
String calendarName = null;
boolean sqlExceptionThrown = false; boolean sqlExceptionThrown = false;
{ {
// now parameters // now parameters
@ -286,7 +288,11 @@ public class JdbcMapperProcessor extends AbstractProcessor {
sqlStatement = sb.toString(); sqlStatement = sb.toString();
for (final Map.Entry<String, VariableElement> unusedParam : unusedParams.entrySet()) { for (final Map.Entry<String, VariableElement> unusedParam : unusedParams.entrySet()) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format("@JdbcMapper.SQL method has unused parameter '%s'", unusedParam.getKey()), unusedParam.getValue()); // look for lone calendar object
if(types.isAssignable(unusedParam.getValue().asType(), calendarType) && calendarName == null)
calendarName = unusedParam.getKey();
else
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format("@JdbcMapper.SQL method has unused parameter '%s'", unusedParam.getKey()), unusedParam.getValue());
} }
} }
@ -335,7 +341,7 @@ public class JdbcMapperProcessor extends AbstractProcessor {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@JdbcMapper.SQL sql parsed a wildcard column name which is not supported", methodElement); processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@JdbcMapper.SQL sql parsed a wildcard column name which is not supported", methodElement);
return false; return false;
} }
rsm.mapToResultType(w, keys, eeMethod, sql.arrayMaxLength(), null); rsm.mapToResultType(w, keys, eeMethod, sql.arrayMaxLength(), calendarName);
} }
// if no SQLException is thrown, we have to catch it here and wrap it with RuntimeException... // if no SQLException is thrown, we have to catch it here and wrap it with RuntimeException...

View File

@ -3,10 +3,9 @@ package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.dto.FieldPerson; import com.moparisthebest.jdbc.dto.FieldPerson;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Iterator; import java.sql.Time;
import java.util.List; import java.sql.Timestamp;
import java.util.ListIterator; import java.util.*;
import java.util.Map;
/** /**
* Created by mopar on 5/24/17. * Created by mopar on 5/24/17.
@ -34,7 +33,10 @@ public interface PersonDAO {
@JdbcMapper.SQL("SELECT first_name FROM person WHERE person_no = {personNo}") @JdbcMapper.SQL("SELECT first_name FROM person WHERE person_no = {personNo}")
String getFirstName(long personNo) throws SQLException; String getFirstName(long personNo) throws SQLException;
@JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE person_no = {personNo}") @JdbcMapper.SQL("SELECT first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
FieldPerson getPerson(long personNo, Calendar cal) throws SQLException;
@JdbcMapper.SQL("SELECT first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
FieldPerson getPerson(long personNo) throws SQLException; FieldPerson getPerson(long personNo) throws SQLException;
@JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE last_name = {lastName}") @JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE last_name = {lastName}")
@ -63,4 +65,30 @@ public interface PersonDAO {
@JdbcMapper.SQL("SELECT first_name FROM person WHERE person_no = {personNo} and last_name = {lastName}") @JdbcMapper.SQL("SELECT first_name FROM person WHERE person_no = {personNo} and last_name = {lastName}")
String getFirstName(long personNo, String lastName) throws SQLException; String getFirstName(long personNo, String lastName) throws SQLException;
// various date checks here
@JdbcMapper.SQL("SELECT birth_date FROM person WHERE person_no = {personNo}")
Calendar getBirthDateCalendar(long personNo) throws SQLException;
@JdbcMapper.SQL("SELECT birth_date FROM person WHERE person_no = {personNo}")
Calendar getBirthDateCalendar(long personNo, Calendar mycal) throws SQLException;
@JdbcMapper.SQL("SELECT birth_date FROM person WHERE person_no = {personNo}")
Timestamp getBirthDateTimestamp(long personNo) throws SQLException;
@JdbcMapper.SQL("SELECT birth_date FROM person WHERE person_no = {personNo}")
Timestamp getBirthDateTimestamp(long personNo, Calendar mycal) throws SQLException;
@JdbcMapper.SQL("SELECT birth_date FROM person WHERE person_no = {personNo}")
Time getBirthDateTime(long personNo) throws SQLException;
@JdbcMapper.SQL("SELECT birth_date FROM person WHERE person_no = {personNo}")
Time getBirthDateTime(long personNo, Calendar mycal) throws SQLException;
@JdbcMapper.SQL("SELECT birth_date FROM person WHERE person_no = {personNo}")
java.sql.Date getBirthDateSqlDate(long personNo) throws SQLException;
@JdbcMapper.SQL("SELECT birth_date FROM person WHERE person_no = {personNo}")
java.sql.Date getBirthDateSqlDate(long personNo, Calendar mycal) throws SQLException;
@JdbcMapper.SQL("SELECT birth_date FROM person WHERE person_no = {personNo}")
java.util.Date getBirthDateUtilDate(long personNo) throws SQLException;
@JdbcMapper.SQL("SELECT birth_date FROM person WHERE person_no = {personNo}")
java.util.Date getBirthDateUtilDate(long personNo, Calendar mycal) throws SQLException;
} }

View File

@ -30,17 +30,17 @@ public class CleaningCompilingResultSetMapper<E> extends CompilingResultSetMappe
this.cleaner = cleaner; this.cleaner = cleaner;
} }
public CleaningCompilingResultSetMapper(final Cleaner<E> cleaner, final Calendar cal, final int arrayMaxLength, final Map<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?, ?>> cache) { public CleaningCompilingResultSetMapper(final Cleaner<E> cleaner, final Calendar cal, final int arrayMaxLength, final Map<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?, ?>> cache) {
super(cal, arrayMaxLength, cache); super(cal, arrayMaxLength, cache);
this.cleaner = cleaner; this.cleaner = cleaner;
} }
public CleaningCompilingResultSetMapper(final Cleaner<E> cleaner, final int arrayMaxLength, final Map<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?, ?>> cache) { public CleaningCompilingResultSetMapper(final Cleaner<E> cleaner, final int arrayMaxLength, final Map<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?, ?>> cache) {
super(arrayMaxLength, cache); super(arrayMaxLength, cache);
this.cleaner = cleaner; this.cleaner = cleaner;
} }
public CleaningCompilingResultSetMapper(final Cleaner<E> cleaner, final Map<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?, ?>> cache) { public CleaningCompilingResultSetMapper(final Cleaner<E> cleaner, final Map<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?, ?>> cache) {
super(cache); super(cache);
this.cleaner = cleaner; this.cleaner = cleaner;
} }

View File

@ -25,7 +25,7 @@ import java.util.concurrent.ConcurrentHashMap;
public class CompilingResultSetMapper extends ResultSetMapper { public class CompilingResultSetMapper extends ResultSetMapper {
protected final Compiler compiler = new Compiler(); protected final Compiler compiler = new Compiler();
protected final Map<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>> cache; protected final Map<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>> cache;
/** /**
* CompilingResultSetMapper with optional maxEntries, expiring old ones in LRU fashion * CompilingResultSetMapper with optional maxEntries, expiring old ones in LRU fashion
@ -40,14 +40,14 @@ public class CompilingResultSetMapper extends ResultSetMapper {
final float loadFactor = 0.75f; // default for HashMaps final float loadFactor = 0.75f; // default for HashMaps
// if we set the initialCapacity this way, nothing should ever need re-sized // if we set the initialCapacity this way, nothing should ever need re-sized
final int initialCapacity = ((int) Math.ceil(maxEntries / loadFactor)) + 1; final int initialCapacity = ((int) Math.ceil(maxEntries / loadFactor)) + 1;
cache = new LinkedHashMap<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>>(initialCapacity, loadFactor, true) { cache = new LinkedHashMap<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>>(initialCapacity, loadFactor, true) {
@Override @Override
protected boolean removeEldestEntry(final Map.Entry<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>> eldest) { protected boolean removeEldestEntry(final Map.Entry<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>> eldest) {
return size() > maxEntries; return size() > maxEntries;
} }
}; };
} else } else
cache = new HashMap<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>>(); cache = new HashMap<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>>();
} }
/** /**
@ -83,7 +83,7 @@ public class CompilingResultSetMapper extends ResultSetMapper {
* @param arrayMaxLength max array/list/map length, a value of less than 1 indicates that all rows from the ResultSet should be included * @param arrayMaxLength max array/list/map length, a value of less than 1 indicates that all rows from the ResultSet should be included
* @param cache any Map implementation for cache you wish, does not need to handle null keys or values * @param cache any Map implementation for cache you wish, does not need to handle null keys or values
*/ */
public CompilingResultSetMapper(final Calendar cal, final int arrayMaxLength, final Map<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>> cache) { public CompilingResultSetMapper(final Calendar cal, final int arrayMaxLength, final Map<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>> cache) {
super(cal, arrayMaxLength); super(cal, arrayMaxLength);
if (cache == null) if (cache == null)
throw new IllegalArgumentException("cache cannot be null"); throw new IllegalArgumentException("cache cannot be null");
@ -96,7 +96,7 @@ public class CompilingResultSetMapper extends ResultSetMapper {
* @param arrayMaxLength max array/list/map length, a value of less than 1 indicates that all rows from the ResultSet should be included * @param arrayMaxLength max array/list/map length, a value of less than 1 indicates that all rows from the ResultSet should be included
* @param cache any Map implementation for cache you wish, does not need to handle null keys or values * @param cache any Map implementation for cache you wish, does not need to handle null keys or values
*/ */
public CompilingResultSetMapper(final int arrayMaxLength, final Map<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>> cache) { public CompilingResultSetMapper(final int arrayMaxLength, final Map<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>> cache) {
this(null, arrayMaxLength, cache); this(null, arrayMaxLength, cache);
} }
@ -105,7 +105,7 @@ public class CompilingResultSetMapper extends ResultSetMapper {
* *
* @param cache any Map implementation for cache you wish, does not need to handle null keys or values * @param cache any Map implementation for cache you wish, does not need to handle null keys or values
*/ */
public CompilingResultSetMapper(final Map<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>> cache) { public CompilingResultSetMapper(final Map<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>> cache) {
this(-1, cache); this(-1, cache);
} }
@ -118,9 +118,9 @@ public class CompilingResultSetMapper extends ResultSetMapper {
*/ */
public CompilingResultSetMapper(final Calendar cal, final int arrayMaxLength, final boolean threadSafe) { public CompilingResultSetMapper(final Calendar cal, final int arrayMaxLength, final boolean threadSafe) {
this(cal, arrayMaxLength, threadSafe ? this(cal, arrayMaxLength, threadSafe ?
new ConcurrentHashMap<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>>() new ConcurrentHashMap<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>>()
: :
new HashMap<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>>() new HashMap<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>>()
); );
} }

View File

@ -31,21 +31,25 @@ public class CompilingRowToObjectMapper<K, T> extends RowToObjectMapper<K, T> {
protected final Compiler compiler; protected final Compiler compiler;
protected final ResultSetToObject<K, T> resultSetToObject; protected final ResultSetToObject<K, T> resultSetToObject;
public CompilingRowToObjectMapper(final Compiler compiler, final Map<CachingRowToObjectMapper.ResultSetKey, ResultSetToObject<?,?>> cache, ResultSet resultSet, Class<T> returnTypeClass, Calendar cal, Class<?> mapValType, Class<K> mapKeyType) { protected String _calendarName = null;
public CompilingRowToObjectMapper(final Compiler compiler, final Map<CompilingRowToObjectMapper.ResultSetKey, ResultSetToObject<?,?>> cache, ResultSet resultSet, Class<T> returnTypeClass, Calendar cal, Class<?> mapValType, Class<K> mapKeyType) {
this(compiler, cache, resultSet, returnTypeClass, cal, mapValType, mapKeyType, false); this(compiler, cache, resultSet, returnTypeClass, cal, mapValType, mapKeyType, false);
} }
public CompilingRowToObjectMapper(final Compiler compiler, final Map<CachingRowToObjectMapper.ResultSetKey, ResultSetToObject<?,?>> cache, ResultSet resultSet, Class<T> returnTypeClass, Calendar cal, Class<?> mapValType, Class<K> mapKeyType, final boolean caseInsensitiveMap) { public CompilingRowToObjectMapper(final Compiler compiler, final Map<CompilingRowToObjectMapper.ResultSetKey, ResultSetToObject<?,?>> cache, ResultSet resultSet, Class<T> returnTypeClass, Calendar cal, Class<?> mapValType, Class<K> mapKeyType, final boolean caseInsensitiveMap) {
super(resultSet, returnTypeClass, cal, mapValType, mapKeyType, caseInsensitiveMap); super(resultSet, returnTypeClass, cal, mapValType, mapKeyType, caseInsensitiveMap);
this.compiler = compiler; this.compiler = compiler;
try { try {
final CachingRowToObjectMapper.ResultSetKey keys = new CachingRowToObjectMapper.ResultSetKey(super.getKeysFromResultSet(), _returnTypeClass, _mapKeyType); final CompilingRowToObjectMapper.ResultSetKey keys = new CompilingRowToObjectMapper.ResultSetKey(super.getKeysFromResultSet(), _returnTypeClass, _mapKeyType, cal != null);
//System.out.printf("keys: %s\n", keys); //System.out.printf("keys: %s\n", keys);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final ResultSetToObject<K,T> resultSetToObject = (ResultSetToObject<K,T>) cache.get(keys); final ResultSetToObject<K,T> resultSetToObject = (ResultSetToObject<K,T>) cache.get(keys);
if (resultSetToObject == null) { if (resultSetToObject == null) {
//System.out.printf("cache miss, keys: %s\n", keys); //System.out.printf("cache miss, keys: %s\n", keys);
// generate and put into cache // generate and put into cache
if(keys.hasCalendar)
_calendarName = "cal";
cache.put(keys, this.resultSetToObject = genClass()); cache.put(keys, this.resultSetToObject = genClass());
this.keys = null; this.keys = null;
this._fields = null; this._fields = null;
@ -308,9 +312,40 @@ public class CompilingRowToObjectMapper<K, T> extends RowToObjectMapper<K, T> {
java.append("ret.finish(rs);\n"); java.append("ret.finish(rs);\n");
} }
public void extractColumnValueString(final Appendable java, final int index, final int resultType) throws IOException {
extractColumnValueString(java, index, resultType, _calendarName);
}
public static void extractColumnValueString(final Appendable java, final int index, final Class resultType) throws IOException { public void extractColumnValueString(final Appendable java, final int index, final Class resultType) throws IOException {
extractColumnValueString(java, index, _tmf.getTypeId(resultType)); extractColumnValueString(java, index, resultType, _calendarName);
}
public static class ResultSetKey extends CachingRowToObjectMapper.ResultSetKey {
protected final boolean hasCalendar;
public ResultSetKey(final String[] keys, final Class<?> returnTypeClass, final Class<?> mapKeyType, final boolean hasCalendar) {
super(keys, returnTypeClass, mapKeyType);
this.hasCalendar = hasCalendar;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof ResultSetKey)) return false;
final ResultSetKey that = (ResultSetKey) o;
return hasCalendar == that.hasCalendar && super.equals(o);
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (hasCalendar ? 1 : 0);
return result;
}
}
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);
} }
/** /**
@ -321,7 +356,7 @@ public class CompilingRowToObjectMapper<K, T> extends RowToObjectMapper<K, T> {
* @return The extracted value * @return The extracted value
* @throws java.sql.SQLException on error. * @throws java.sql.SQLException on error.
*/ */
public static void extractColumnValueString(final Appendable java, final int index, final int resultType) throws IOException { public static void extractColumnValueString(final Appendable java, final int index, final int resultType, final String calendarName) throws IOException {
switch (resultType) { switch (resultType) {
case TypeMappingsFactory.TYPE_INT: case TypeMappingsFactory.TYPE_INT:
java.append("rs.getInt(").append(String.valueOf(index)).append(")"); java.append("rs.getInt(").append(String.valueOf(index)).append(")");
@ -376,19 +411,34 @@ public class CompilingRowToObjectMapper<K, T> extends RowToObjectMapper<K, T> {
java.append("rs.getBytes(").append(String.valueOf(index)).append(")"); java.append("rs.getBytes(").append(String.valueOf(index)).append(")");
return; return;
case TypeMappingsFactory.TYPE_TIMESTAMP: case TypeMappingsFactory.TYPE_TIMESTAMP:
java.append("getTimestamp(rs, cal, ").append(String.valueOf(index)).append(")"); java.append("rs.getTimestamp(").append(String.valueOf(index));
if(calendarName != null)
java.append(", ").append(calendarName);
java.append(")");
return; return;
case TypeMappingsFactory.TYPE_TIME: case TypeMappingsFactory.TYPE_TIME:
java.append("getTime(rs, cal, ").append(String.valueOf(index)).append(")"); java.append("rs.getTime(").append(String.valueOf(index));
if(calendarName != null)
java.append(", ").append(calendarName);
java.append(")");
return; return;
case TypeMappingsFactory.TYPE_SQLDATE: case TypeMappingsFactory.TYPE_SQLDATE:
java.append("getSqlDate(rs, cal, ").append(String.valueOf(index)).append(")"); java.append("rs.getDate(").append(String.valueOf(index));
if(calendarName != null)
java.append(", ").append(calendarName);
java.append(")");
return; return;
case TypeMappingsFactory.TYPE_DATE: case TypeMappingsFactory.TYPE_DATE:
java.append("getUtilDate(rs, cal, ").append(String.valueOf(index)).append(")"); java.append("getUtilDate(rs, ").append(String.valueOf(index));
if(calendarName != null)
java.append(", ").append(calendarName);
java.append(")");
return; return;
case TypeMappingsFactory.TYPE_CALENDAR: case TypeMappingsFactory.TYPE_CALENDAR:
java.append("getCalendar(rs, cal, ").append(String.valueOf(index)).append(")"); java.append("getCalendar(rs, ").append(String.valueOf(index));
if(calendarName != null)
java.append(", ").append(calendarName);
java.append(")");
return; return;
case TypeMappingsFactory.TYPE_REF: case TypeMappingsFactory.TYPE_REF:
java.append("rs.getRef(").append(String.valueOf(index)).append(")"); java.append("rs.getRef(").append(String.valueOf(index)).append(")");

View File

@ -9,7 +9,7 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
public class StaticCompilingResultSetMapper extends CompilingResultSetMapper { public class StaticCompilingResultSetMapper extends CompilingResultSetMapper {
private static final Map<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>> cache = new ConcurrentHashMap<CachingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>>(); private static final Map<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>> cache = new ConcurrentHashMap<CompilingRowToObjectMapper.ResultSetKey, CompilingRowToObjectMapper.ResultSetToObject<?,?>>();
public static final StaticCompilingResultSetMapper instance = new StaticCompilingResultSetMapper(); public static final StaticCompilingResultSetMapper instance = new StaticCompilingResultSetMapper();