2017-05-27 23:54:22 -04:00
package com.moparisthebest.jdbc.codegen ;
2017-06-07 19:15:00 -04:00
import com.moparisthebest.jdbc.Finishable ;
2017-11-08 23:31:28 -05:00
import com.moparisthebest.jdbc.MapperException ;
2017-05-27 23:54:22 -04:00
import com.moparisthebest.jdbc.ResultSetMapper ;
2017-06-28 16:00:32 -04:00
import com.moparisthebest.jdbc.TypeMappingsFactory ;
2017-06-09 00:36:41 -04:00
import com.moparisthebest.jdbc.util.ResultSetIterable ;
2017-05-27 23:54:22 -04:00
2017-06-06 22:33:51 -04:00
import javax.annotation.processing.ProcessingEnvironment ;
2017-06-12 19:24:04 -04:00
import javax.lang.model.SourceVersion ;
2017-05-27 23:54:22 -04:00
import javax.lang.model.element.ExecutableElement ;
2017-06-07 02:43:46 -04:00
import javax.lang.model.element.Modifier ;
2017-05-27 23:54:22 -04:00
import javax.lang.model.type.ArrayType ;
import javax.lang.model.type.DeclaredType ;
2017-06-06 22:33:51 -04:00
import javax.lang.model.type.TypeKind ;
2017-05-27 23:54:22 -04:00
import javax.lang.model.type.TypeMirror ;
2017-06-06 22:33:51 -04:00
import javax.lang.model.util.Elements ;
import javax.lang.model.util.Types ;
2017-11-08 23:31:28 -05:00
import javax.tools.Diagnostic ;
2017-05-27 23:54:22 -04:00
import java.io.IOException ;
import java.io.Writer ;
2017-06-07 19:15:00 -04:00
import java.sql.ResultSet ;
2017-05-27 23:54:22 -04:00
import java.util.* ;
2017-06-12 23:48:54 -04:00
//IFJAVA8_START
import java.util.stream.Stream ;
//IFJAVA8_END
2017-05-27 23:54:22 -04:00
2017-11-29 23:51:27 -05:00
import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.java8 ;
2017-06-07 02:43:46 -04:00
import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.typeMirrorStringNoGenerics ;
2017-05-27 23:54:22 -04:00
import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.typeMirrorToClass ;
/ * *
* Created by mopar on 5 / 26 / 17 .
* /
public class CompileTimeResultSetMapper {
2017-06-07 19:15:00 -04:00
public final Types types ;
2018-01-15 14:12:41 -05:00
public final TypeMirror collectionType , mapType , mapCollectionType , iteratorType , listIteratorType , finishableType , resultSetType , resultSetIterableType , byteArrayType , enumType ;
2017-06-12 23:48:54 -04:00
//IFJAVA8_START
public final TypeMirror streamType ;
//IFJAVA8_END
2017-06-06 22:33:51 -04:00
public CompileTimeResultSetMapper ( final ProcessingEnvironment processingEnv ) {
types = processingEnv . getTypeUtils ( ) ;
final Elements elements = processingEnv . getElementUtils ( ) ;
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 ) ) ;
iteratorType = types . getDeclaredType ( elements . getTypeElement ( Iterator . class . getCanonicalName ( ) ) , types . getWildcardType ( null , null ) ) ;
listIteratorType = types . getDeclaredType ( elements . getTypeElement ( ListIterator . class . getCanonicalName ( ) ) , types . getWildcardType ( null , null ) ) ;
2017-06-07 19:15:00 -04:00
finishableType = elements . getTypeElement ( Finishable . class . getCanonicalName ( ) ) . asType ( ) ;
resultSetType = elements . getTypeElement ( ResultSet . class . getCanonicalName ( ) ) . asType ( ) ;
2017-06-09 00:36:41 -04:00
resultSetIterableType = types . getDeclaredType ( elements . getTypeElement ( ResultSetIterable . class . getCanonicalName ( ) ) , types . getWildcardType ( null , null ) ) ;
2017-06-12 23:48:54 -04:00
2017-07-12 22:34:51 -04:00
byteArrayType = types . getArrayType ( types . getPrimitiveType ( TypeKind . BYTE ) ) ;
2018-01-15 14:12:41 -05:00
enumType = types . getDeclaredType ( elements . getTypeElement ( Enum . class . getCanonicalName ( ) ) , types . getWildcardType ( null , null ) ) ;
2017-06-12 23:48:54 -04:00
//IFJAVA8_START
streamType = types . getDeclaredType ( elements . getTypeElement ( Stream . class . getCanonicalName ( ) ) , types . getWildcardType ( null , null ) ) ;
//IFJAVA8_END
2017-06-06 22:33:51 -04:00
}
2017-06-07 03:00:29 -04:00
public static String getConcreteClassCanonicalName ( final TypeMirror returnType , final Class defaultConcreteClass ) {
2017-06-07 02:43:46 -04:00
final Set < Modifier > modifiers = ( ( DeclaredType ) returnType ) . asElement ( ) . getModifiers ( ) ;
if ( modifiers . contains ( Modifier . ABSTRACT ) ) { // todo: no interface?
try {
2017-06-07 03:00:29 -04:00
final Class concrete = ResultSetMapper . interfaceToConcrete . get ( typeMirrorToClass ( returnType ) ) ;
return ( concrete = = null ? defaultConcreteClass : concrete ) . getCanonicalName ( ) ;
2017-06-07 02:43:46 -04:00
} catch ( ClassNotFoundException e ) {
// ignore?
}
}
return typeMirrorStringNoGenerics ( returnType ) ;
}
2017-06-09 00:36:41 -04:00
/ * *
*
* @return true if calling code should close rs ( ResultSet ) and ps ( PreparedStatement ) if closePs is false , false otherwise
* /
2017-06-19 00:34:23 -04:00
public boolean mapToResultType ( final Writer w , final String [ ] keys , final ExecutableElement eeMethod , final MaxRows maxRows , final String cal , final String cleaner , final boolean closePs , final ReflectionFields reflectionFields ) throws IOException , NoSuchMethodException , ClassNotFoundException {
2017-05-29 18:26:11 -04:00
//final Method m = fromExecutableElement(eeMethod);
//final Class returnType = m.getReturnType();
2017-05-27 23:54:22 -04:00
final TypeMirror returnTypeMirror = eeMethod . getReturnType ( ) ;
2017-06-06 22:33:51 -04:00
//final Class returnType = typeMirrorToClass(returnTypeMirror);
2017-11-08 23:31:28 -05:00
try {
if ( returnTypeMirror . getKind ( ) = = TypeKind . ARRAY & & ! types . isSameType ( returnTypeMirror , byteArrayType ) & & eeMethod . getAnnotation ( JdbcMapper . SingleRow . class ) = = null ) {
final TypeMirror componentType = ( ( ArrayType ) returnTypeMirror ) . getComponentType ( ) ;
toArray ( w , keys , componentType , maxRows , cal , cleaner , reflectionFields ) ;
} else if ( types . isAssignable ( returnTypeMirror , collectionType ) ) {
final List < ? extends TypeMirror > typeArguments = ( ( DeclaredType ) returnTypeMirror ) . getTypeArguments ( ) ;
2018-03-09 00:09:01 -05:00
if ( typeArguments . isEmpty ( ) ) {
JdbcMapperProcessor . getMessager ( ) . printMessage ( Diagnostic . Kind . ERROR , " Collection type must have a generic type argument " , eeMethod ) ;
return true ;
}
2017-11-08 23:31:28 -05:00
toCollection ( w , keys , returnTypeMirror , typeArguments . get ( 0 ) , maxRows , cal , cleaner , reflectionFields ) ;
} else if ( types . isAssignable ( returnTypeMirror , mapType ) & & eeMethod . getAnnotation ( JdbcMapper . SingleRow . class ) = = null ) {
final List < ? extends TypeMirror > typeArguments = ( ( DeclaredType ) returnTypeMirror ) . getTypeArguments ( ) ;
//if (types[1] instanceof ParameterizedType) { // for collectionMaps
if ( types . isAssignable ( returnTypeMirror , mapCollectionType ) ) { // for collectionMaps
final TypeMirror collectionTypeMirror = typeArguments . get ( 1 ) ;
final TypeMirror componentTypeMirror = ( ( DeclaredType ) collectionTypeMirror ) . getTypeArguments ( ) . get ( 0 ) ;
toMapCollection ( w , keys ,
returnTypeMirror ,
typeArguments . get ( 0 ) ,
collectionTypeMirror ,
componentTypeMirror ,
maxRows , cal , cleaner , reflectionFields ) ;
return true ;
}
toMap ( w , keys , returnTypeMirror , typeArguments . get ( 0 ) , typeArguments . get ( 1 ) , maxRows , cal , cleaner , reflectionFields ) ;
} else if ( types . isAssignable ( returnTypeMirror , iteratorType ) ) {
final List < ? extends TypeMirror > typeArguments = ( ( DeclaredType ) returnTypeMirror ) . getTypeArguments ( ) ;
if ( types . isAssignable ( returnTypeMirror , resultSetIterableType ) ) {
toResultSetIterable ( w , keys , typeArguments . get ( 0 ) , cal , cleaner , closePs , reflectionFields ) ;
return false ;
} else if ( types . isAssignable ( returnTypeMirror , listIteratorType ) )
toListIterator ( w , keys , typeArguments . get ( 0 ) , maxRows , cal , cleaner , reflectionFields ) ;
else
toIterator ( w , keys , typeArguments . get ( 0 ) , maxRows , cal , cleaner , reflectionFields ) ;
2017-05-27 23:54:22 -04:00
}
2017-11-08 23:31:28 -05:00
//IFJAVA8_START
else if ( types . isAssignable ( returnTypeMirror , streamType ) ) {
toStream ( w , keys , ( ( DeclaredType ) returnTypeMirror ) . getTypeArguments ( ) . get ( 0 ) , cal , cleaner , closePs , reflectionFields ) ;
2017-06-09 00:36:41 -04:00
return false ;
2017-11-08 23:31:28 -05:00
}
//IFJAVA8_END
else if ( types . isAssignable ( returnTypeMirror , resultSetType ) ) {
toResultSet ( w , closePs ) ;
return false ;
} else {
toObject ( w , keys , returnTypeMirror , cal , cleaner , reflectionFields ) ;
}
} catch ( MapperException e ) {
JdbcMapperProcessor . getMessager ( ) . printMessage ( Diagnostic . Kind . ERROR , e . getMessage ( ) , eeMethod ) ;
2017-05-27 23:54:22 -04:00
}
2017-06-09 00:36:41 -04:00
return true ;
2017-05-27 23:54:22 -04:00
}
2017-06-19 00:34:23 -04:00
public CompileTimeRowToObjectMapper getRowMapper ( final String [ ] keys , TypeMirror returnTypeClass , String cal , TypeMirror mapValType , TypeMirror mapKeyType , final ReflectionFields reflectionFields ) {
return getRowMapper ( keys , returnTypeClass , " rs " , cal , mapValType , mapKeyType , reflectionFields ) ;
2017-06-12 19:24:04 -04:00
}
2017-06-19 00:34:23 -04:00
public CompileTimeRowToObjectMapper getRowMapper ( final String [ ] keys , TypeMirror returnTypeClass , final String resultSetName , String cal , TypeMirror mapValType , TypeMirror mapKeyType , final ReflectionFields reflectionFields ) {
return new CompileTimeRowToObjectMapper ( this , keys , returnTypeClass , resultSetName , cal , mapValType , mapKeyType , reflectionFields ) ;
2017-05-27 23:54:22 -04:00
}
2017-06-19 00:34:23 -04:00
public void writeObject ( final Writer w , final String [ ] keys , final TypeMirror returnTypeMirror , final String cal , final ReflectionFields reflectionFields ) throws IOException , ClassNotFoundException {
writeObject ( w , keys , returnTypeMirror , " rs " , cal , reflectionFields ) ;
2017-06-12 19:24:04 -04:00
}
2017-06-19 00:34:23 -04:00
public void writeObject ( final Writer w , final String [ ] keys , final TypeMirror returnTypeMirror , final String resultSetName , final String cal , final ReflectionFields reflectionFields ) throws IOException , ClassNotFoundException {
getRowMapper ( keys , returnTypeMirror , resultSetName , cal , null , null , reflectionFields ) . gen ( w , returnTypeMirror . toString ( ) ) ;
2017-05-27 23:54:22 -04:00
}
2017-06-19 23:50:59 -04:00
private void toResultSet ( final Writer w , final boolean closePs ) throws IOException {
w . append ( " \ t \ t \ treturn " ) ;
if ( closePs )
w . append ( " new com.moparisthebest.jdbc.StatementClosingResultSet(rs, ps) " ) ;
else
w . append ( " rs " ) ;
w . append ( " ; \ n " ) ;
}
2017-06-19 00:34:23 -04:00
public void toObject ( final Writer w , final String [ ] keys , final TypeMirror returnTypeMirror , final String cal , final String cleaner , final ReflectionFields reflectionFields ) throws IOException , ClassNotFoundException {
2017-05-27 23:54:22 -04:00
w . write ( " \ t \ t \ tif(rs.next()) { \ n " ) ;
2017-06-19 00:34:23 -04:00
writeObject ( w , keys , returnTypeMirror , cal , reflectionFields ) ;
2017-06-07 21:29:28 -04:00
w . write ( " \ t \ t \ t \ treturn " ) ;
// this does not clean null on purpose, neither does CleaningResultSetMapper
2017-06-28 16:00:32 -04:00
clean ( w , cleaner ) . write ( " ; \ n \ t \ t \ t} else { \ n \ t \ t \ t \ treturn " ) ;
if ( returnTypeMirror . getKind ( ) . isPrimitive ( ) )
w . append ( TypeMappingsFactory . getInstance ( ) . fixNull ( typeMirrorToClass ( returnTypeMirror ) ) . toString ( ) ) ; // todo: ok, but could be better
else
w . append ( " null " ) ;
w . append ( " ; \ n \ t \ t \ t} \ n " ) ;
2017-05-27 23:54:22 -04:00
}
2017-06-19 00:34:23 -04:00
private void toResultSetIterable ( final Writer w , final String [ ] keys , final TypeMirror returnTypeMirror , final String cal , final String cleaner , final boolean closePs , final ReflectionFields reflectionFields ) throws IOException , ClassNotFoundException {
2017-06-09 00:36:41 -04:00
w . write ( " \ t \ t \ treturn com.moparisthebest.jdbc.util.ResultSetIterable.getResultSetIterable(rs, \ n \ t \ t \ t \ t \ trs.next() ? " ) ;
if ( java8 ) {
2017-06-12 19:24:04 -04:00
w . append ( " (_rs, _cal) -> { \ n " ) ;
2017-06-09 00:36:41 -04:00
} else {
final String returnTypeString = returnTypeMirror . toString ( ) ;
w . append ( " new com.moparisthebest.jdbc.util.ResultSetToObject< " )
. append ( returnTypeString ) . append ( " >() { \ n \ t \ t \ t \ t \ tpublic " )
2017-06-12 19:24:04 -04:00
. append ( returnTypeString ) . append ( " toObject(final ResultSet _rs, final java.util.Calendar _cal) throws SQLException { \ n " ) ;
2017-06-09 00:36:41 -04:00
}
// com.moparisthebest.jdbc.util.ResultSetToObject implementation
2017-06-19 00:34:23 -04:00
writeObject ( w , keys , returnTypeMirror , " _rs " , cal = = null ? null : " _cal " , reflectionFields ) ;
2017-06-09 00:36:41 -04:00
w . write ( " \ t \ t \ t \ t \ t \ treturn " ) ;
clean ( w , cleaner ) . write ( " ; \ n " ) ;
// end ResultSetToObject implementation
if ( ! java8 )
w . write ( " \ t \ t \ t \ t \ t} \ n " ) ;
w . append ( " \ t \ t \ t \ t \ t} \ n \ t \ t \ t \ t: null, " ) . append ( cal = = null ? " null " : cal ) . append ( " ) " ) ;
if ( closePs )
w . append ( " .setPreparedStatementToClose(ps) " ) ;
w . append ( " ; \ n " ) ;
}
2017-06-12 23:48:54 -04:00
//IFJAVA8_START
// being in this method implies java8 is true already, how else could you be compiling code using Stream? so this won't have the checks for lambdas toResultSetIterable does...
2017-06-19 00:34:23 -04:00
private void toStream ( final Writer w , final String [ ] keys , final TypeMirror returnTypeMirror , final String cal , final String cleaner , final boolean closePs , final ReflectionFields reflectionFields ) throws IOException , ClassNotFoundException {
2017-06-12 23:48:54 -04:00
if ( closePs )
w . write ( " \ t \ t \ tfinal PreparedStatement finalPs = ps; \ n " ) ;
w . write ( " \ t \ t \ treturn com.moparisthebest.jdbc.util.ResultSetIterable.getStream(rs, \ n \ t \ t \ t \ t \ trs.next() ? " ) ;
w . append ( " (_rs, _cal) -> { \ n " ) ;
// com.moparisthebest.jdbc.util.ResultSetToObject implementation
2017-06-19 00:34:23 -04:00
writeObject ( w , keys , returnTypeMirror , " _rs " , cal = = null ? null : " _cal " , reflectionFields ) ;
2017-06-12 23:48:54 -04:00
w . write ( " \ t \ t \ t \ t \ t \ treturn " ) ;
clean ( w , cleaner ) . write ( " ; \ n " ) ;
// end ResultSetToObject implementation
w . append ( " \ t \ t \ t \ t \ t} \ n \ t \ t \ t \ t: null, " ) . append ( cal = = null ? " null " : cal ) . append ( " ) " ) ;
if ( closePs )
w . append ( " .onClose(() -> tryClose(finalPs)) " ) ;
w . append ( " ; \ n " ) ;
}
//IFJAVA8_END
2017-06-19 00:34:23 -04:00
public void writeCollection ( final Writer w , final String [ ] keys , final String returnTypeString , final String concreteTypeString , final TypeMirror componentTypeMirror , MaxRows maxRows , String cal , final String cleaner , final ReflectionFields reflectionFields ) throws IOException , ClassNotFoundException {
2017-06-08 00:09:00 -04:00
maxRowInit ( w , maxRows ) . write ( " \ t \ t \ tfinal " ) ;
2017-05-27 23:54:22 -04:00
w . write ( returnTypeString ) ;
w . write ( " _colret = new " ) ;
2017-06-07 02:43:46 -04:00
w . write ( concreteTypeString ) ;
2017-05-27 23:54:22 -04:00
w . write ( returnTypeString . substring ( returnTypeString . indexOf ( '<' ) ) ) ;
w . write ( " (); \ n \ t \ t \ twhile(rs.next()) { \ n " ) ;
2017-06-19 00:34:23 -04:00
writeObject ( w , keys , componentTypeMirror , cal , reflectionFields ) ;
2017-06-07 21:29:28 -04:00
w . write ( " \ t \ t \ t \ t_colret.add( " ) ;
2017-06-08 00:09:00 -04:00
clean ( w , cleaner ) . write ( " ); \ n " ) ;
maxRowBreak ( w , maxRows ) . write ( " \ t \ t \ t} \ n " ) ;
2017-05-27 23:54:22 -04:00
}
2017-06-19 00:34:23 -04:00
public void toCollection ( final Writer w , final String [ ] keys , final TypeMirror collectionTypeMirror , final TypeMirror componentTypeMirror , MaxRows maxRows , String cal , final String cleaner , final ReflectionFields reflectionFields ) throws IOException , ClassNotFoundException {
2017-06-07 02:43:46 -04:00
final String collectionType = getConcreteClassCanonicalName ( collectionTypeMirror , ArrayList . class ) ;
2017-06-19 00:34:23 -04:00
writeCollection ( w , keys , collectionTypeMirror . toString ( ) , collectionType , componentTypeMirror , maxRows , cal , cleaner , reflectionFields ) ;
2017-05-27 23:54:22 -04:00
w . write ( " \ t \ t \ treturn _colret; \ n " ) ;
}
2017-06-19 00:34:23 -04:00
public void toArray ( final Writer w , final String [ ] keys , final TypeMirror componentTypeMirror , MaxRows maxRows , String cal , final String cleaner , final ReflectionFields reflectionFields ) throws IOException , ClassNotFoundException {
2017-05-27 23:54:22 -04:00
final String returnTypeString = componentTypeMirror . toString ( ) ;
2017-06-19 00:34:23 -04:00
writeCollection ( w , keys , " java.util.List< " + returnTypeString + " > " , " java.util.ArrayList " , componentTypeMirror , maxRows , cal , cleaner , reflectionFields ) ;
2018-05-11 00:14:58 -04:00
// have to strip generics to avoid "generic array creation" compilation failure
2018-05-08 00:09:56 -04:00
final int indexOfGeneric = returnTypeString . indexOf ( '<' ) ;
2018-05-11 00:14:58 -04:00
if ( indexOfGeneric < 0 ) {
w . write ( " \ t \ t \ treturn _colret.toArray(new " ) ;
w . write ( returnTypeString ) ;
w . write ( " [_colret.size()]); \ n " ) ;
} else {
w . write ( " \ t \ t \ t@SuppressWarnings( \" unchecked \" ) \ n " ) ;
w . append ( " \ t \ t \ tfinal " ) . append ( returnTypeString ) . append ( " [] _warnret = _colret.toArray(new " ) ;
w . write ( returnTypeString . substring ( 0 , indexOfGeneric ) ) ;
w . write ( " [_colret.size()]); \ n " ) ;
w . write ( " \ t \ t \ treturn _warnret; \ n " ) ;
}
2017-05-27 23:54:22 -04:00
}
2017-06-19 00:34:23 -04:00
public void toListIterator ( final Writer w , final String [ ] keys , final TypeMirror componentTypeMirror , MaxRows maxRows , String cal , final String cleaner , final ReflectionFields reflectionFields ) throws IOException , ClassNotFoundException {
2017-05-27 23:54:22 -04:00
final String returnTypeString = componentTypeMirror . toString ( ) ;
2017-06-19 00:34:23 -04:00
writeCollection ( w , keys , " java.util.List< " + returnTypeString + " > " , " java.util.ArrayList " , componentTypeMirror , maxRows , cal , cleaner , reflectionFields ) ;
2017-05-27 23:54:22 -04:00
w . write ( " \ t \ t \ treturn _colret.listIterator(); \ n " ) ;
}
2017-06-19 00:34:23 -04:00
public void toIterator ( final Writer w , final String [ ] keys , final TypeMirror componentTypeMirror , MaxRows maxRows , String cal , final String cleaner , final ReflectionFields reflectionFields ) throws IOException , ClassNotFoundException {
2017-05-27 23:54:22 -04:00
final String returnTypeString = componentTypeMirror . toString ( ) ;
2017-06-19 00:34:23 -04:00
writeCollection ( w , keys , " java.util.List< " + returnTypeString + " > " , " java.util.ArrayList " , componentTypeMirror , maxRows , cal , cleaner , reflectionFields ) ;
2017-05-27 23:54:22 -04:00
w . write ( " \ t \ t \ treturn _colret.iterator(); \ n " ) ;
}
2017-06-19 00:34:23 -04:00
public void toMap ( final Writer w , final String [ ] keys , final TypeMirror mapTypeMirror , final TypeMirror mapKeyTypeMirror , final TypeMirror componentTypeMirror , MaxRows maxRows , String cal , final String cleaner , final ReflectionFields reflectionFields ) throws IOException , ClassNotFoundException {
2017-06-07 02:43:46 -04:00
final String mapType = getConcreteClassCanonicalName ( mapTypeMirror , HashMap . class ) ;
2017-05-27 23:54:22 -04:00
final String returnTypeString = mapTypeMirror . toString ( ) ;
2017-06-08 00:09:00 -04:00
maxRowInit ( w , maxRows ) . write ( " \ t \ t \ tfinal " ) ;
2017-05-27 23:54:22 -04:00
w . write ( returnTypeString ) ;
w . write ( " _colret = new " ) ;
2017-06-07 02:43:46 -04:00
w . write ( mapType ) ;
2017-05-27 23:54:22 -04:00
w . write ( returnTypeString . substring ( returnTypeString . indexOf ( '<' ) ) ) ;
w . write ( " (); \ n \ t \ t \ twhile(rs.next()) { \ n " ) ;
//writeObject(w, keys, componentTypeMirror, componentType, cal);
2017-06-19 00:34:23 -04:00
final CompileTimeRowToObjectMapper rm = getRowMapper ( keys , componentTypeMirror , cal , null , mapKeyTypeMirror , reflectionFields ) ;
2017-05-27 23:54:22 -04:00
rm . gen ( w , componentTypeMirror . toString ( ) ) ;
w . write ( " \ t \ t \ t \ t_colret.put( " ) ;
2017-06-07 02:43:46 -04:00
rm . extractColumnValueString ( w , 1 , mapKeyTypeMirror ) ;
2017-06-07 21:29:28 -04:00
w . write ( " , " ) ;
2017-06-08 00:09:00 -04:00
clean ( w , cleaner ) . write ( " ); \ n " ) ;
maxRowBreak ( w , maxRows ) . write ( " \ t \ t \ t} \ n " ) ;
2017-05-27 23:54:22 -04:00
w . write ( " \ t \ t \ treturn _colret; \ n " ) ;
}
2017-06-07 03:00:29 -04:00
public void toMapCollection ( final Writer w , final String [ ] keys ,
2017-06-07 21:29:28 -04:00
final TypeMirror mapTypeMirror ,
final TypeMirror mapKeyTypeMirror ,
final TypeMirror collectionTypeMirror ,
final TypeMirror componentTypeMirror ,
2017-06-19 00:34:23 -04:00
MaxRows maxRows , String cal , final String cleaner , final ReflectionFields reflectionFields ) throws IOException , ClassNotFoundException {
2017-06-07 02:43:46 -04:00
final String mapType = getConcreteClassCanonicalName ( mapTypeMirror , HashMap . class ) ;
final String collectionType = getConcreteClassCanonicalName ( collectionTypeMirror , ArrayList . class ) ;
2017-05-27 23:54:22 -04:00
final String returnTypeString = mapTypeMirror . toString ( ) ;
final String collectionTypeString = collectionTypeMirror . toString ( ) ;
2017-06-08 00:09:00 -04:00
maxRowInit ( w , maxRows ) . write ( " \ t \ t \ tfinal " ) ;
2017-05-27 23:54:22 -04:00
w . write ( returnTypeString ) ;
w . write ( " _colret = new " ) ;
2017-06-07 02:43:46 -04:00
w . write ( mapType ) ;
2017-05-27 23:54:22 -04:00
w . write ( returnTypeString . substring ( returnTypeString . indexOf ( '<' ) ) ) ;
w . write ( " (); \ n \ t \ t \ twhile(rs.next()) { \ n " ) ;
//writeObject(w, keys, componentTypeMirror, componentType, cal);
2017-06-19 00:34:23 -04:00
final CompileTimeRowToObjectMapper rm = getRowMapper ( keys , componentTypeMirror , cal , null , mapKeyTypeMirror , reflectionFields ) ;
2017-05-27 23:54:22 -04:00
rm . gen ( w , componentTypeMirror . toString ( ) ) ;
w . write ( " \ t \ t \ t \ tfinal " ) ;
w . write ( mapKeyTypeMirror . toString ( ) ) ;
w . write ( " _colkey = " ) ;
2017-06-07 02:43:46 -04:00
rm . extractColumnValueString ( w , 1 , mapKeyTypeMirror ) ;
2017-05-27 23:54:22 -04:00
w . write ( " ; \ n \ t \ t \ t \ t " ) ;
w . write ( collectionTypeString ) ;
w . write ( " _collist = _colret.get(_colkey); \ n \ t \ t \ t \ tif(_collist == null) { \ n \ t \ t \ t \ t \ t_collist = new " ) ;
2017-06-07 02:43:46 -04:00
w . write ( collectionType ) ;
2017-05-27 23:54:22 -04:00
w . write ( collectionTypeString . substring ( collectionTypeString . indexOf ( '<' ) ) ) ;
2017-06-07 21:29:28 -04:00
w . write ( " (); \ n \ t \ t \ t \ t \ t_colret.put(_colkey, _collist); \ n \ t \ t \ t \ t} \ n \ t \ t \ t \ t_collist.add( " ) ;
2017-06-08 00:09:00 -04:00
clean ( w , cleaner ) . write ( " ); \ n " ) ;
maxRowBreak ( w , maxRows ) . write ( " \ t \ t \ t} \ n \ t \ t \ treturn _colret; \ n " ) ;
}
2017-06-08 01:27:56 -04:00
private Writer maxRowInit ( final Writer w , final MaxRows maxRows ) throws IOException {
2017-06-08 00:09:00 -04:00
if ( maxRows ! = null )
2017-06-08 01:27:56 -04:00
w . append ( " \ t \ t \ t " ) . append ( maxRows . type ) . append ( " _rowCount = 0; \ n " ) ;
2017-06-08 00:09:00 -04:00
return w ;
}
2017-06-08 01:27:56 -04:00
private Writer maxRowBreak ( final Writer w , final MaxRows maxRows ) throws IOException {
2017-06-08 00:09:00 -04:00
if ( maxRows ! = null ) {
w . append ( " \ t \ t \ t \ tif( " ) ;
2017-06-08 01:27:56 -04:00
if ( maxRows . dynamic )
w . append ( maxRows . value ) . append ( " > 0 && " ) ;
w . append ( " ++_rowCount == " ) . append ( maxRows . value ) . append ( " ) \ n \ t \ t \ t \ t \ tbreak; \ n " ) ;
2017-06-08 00:09:00 -04:00
}
return w ;
2017-06-07 21:29:28 -04:00
}
private Writer clean ( final Writer w , final String cleaner ) throws IOException {
if ( cleaner = = null )
w . write ( " ret " ) ;
else {
2018-04-18 00:16:16 -04:00
w . append ( cleaner ) . append ( " == null ? ret : " ) . append ( cleaner ) . append ( " .clean(ret) " ) ;
2017-06-07 21:29:28 -04:00
}
return w ;
2017-05-27 23:54:22 -04:00
}
2017-06-08 01:27:56 -04:00
static class MaxRows {
final String value , type ;
final boolean dynamic ;
static MaxRows getMaxRows ( final long value ) {
return value < 1 ? null : new MaxRows ( value ) ;
}
static MaxRows getMaxRows ( final String value , final String type ) {
return new MaxRows ( value , type ) ;
}
private MaxRows ( final long value ) {
String valueString = Long . toString ( value ) ;
this . dynamic = false ;
if ( value < = Byte . MAX_VALUE )
this . type = " byte " ;
else if ( value < = Short . MAX_VALUE )
this . type = " short " ;
else if ( value < = Integer . MAX_VALUE )
this . type = " int " ;
else {
this . type = " long " ;
valueString + = " L " ; // fun!
}
this . value = valueString ;
}
private MaxRows ( final String value , final String type ) {
this . value = value ;
this . type = type ;
this . dynamic = true ;
}
}
2017-05-27 23:54:22 -04:00
}