mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2024-11-22 00:52:16 -05:00
Add support for @SingleRow in JdbcMapper
This commit is contained in:
parent
d2353bd74b
commit
9829e98ae0
@ -125,6 +125,13 @@ public interface JdbcMapper extends Closeable {
|
|||||||
@Target({ElementType.PARAMETER})
|
@Target({ElementType.PARAMETER})
|
||||||
public @interface Clob {}
|
public @interface Clob {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Equivalent to QueryMapper.toObject for arrays, and .toSingleMap for maps
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
|
@Target({ElementType.METHOD})
|
||||||
|
public @interface SingleRow {}
|
||||||
|
|
||||||
public enum OptionalBool {
|
public enum OptionalBool {
|
||||||
DEFAULT,
|
DEFAULT,
|
||||||
TRUE,
|
TRUE,
|
||||||
|
@ -98,13 +98,13 @@ public class CompileTimeResultSetMapper {
|
|||||||
//final Class returnType = m.getReturnType();
|
//final Class returnType = m.getReturnType();
|
||||||
final TypeMirror returnTypeMirror = eeMethod.getReturnType();
|
final TypeMirror returnTypeMirror = eeMethod.getReturnType();
|
||||||
//final Class returnType = typeMirrorToClass(returnTypeMirror);
|
//final Class returnType = typeMirrorToClass(returnTypeMirror);
|
||||||
if (returnTypeMirror.getKind() == TypeKind.ARRAY && !types.isSameType(returnTypeMirror, byteArrayType)) {
|
if (returnTypeMirror.getKind() == TypeKind.ARRAY && !types.isSameType(returnTypeMirror, byteArrayType) && eeMethod.getAnnotation(JdbcMapper.SingleRow.class) == null) {
|
||||||
final TypeMirror componentType = ((ArrayType) returnTypeMirror).getComponentType();
|
final TypeMirror componentType = ((ArrayType) returnTypeMirror).getComponentType();
|
||||||
toArray(w, keys, componentType, maxRows, cal, cleaner, reflectionFields);
|
toArray(w, keys, componentType, maxRows, cal, cleaner, reflectionFields);
|
||||||
} else if (types.isAssignable(returnTypeMirror, collectionType)) {
|
} else if (types.isAssignable(returnTypeMirror, collectionType)) {
|
||||||
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
||||||
toCollection(w, keys, returnTypeMirror, typeArguments.get(0), maxRows, cal, cleaner, reflectionFields);
|
toCollection(w, keys, returnTypeMirror, typeArguments.get(0), maxRows, cal, cleaner, reflectionFields);
|
||||||
} else if (types.isAssignable(returnTypeMirror, mapType)) {
|
} else if (types.isAssignable(returnTypeMirror, mapType) && eeMethod.getAnnotation(JdbcMapper.SingleRow.class) == null) {
|
||||||
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeMirror).getTypeArguments();
|
||||||
//if (types[1] instanceof ParameterizedType) { // for collectionMaps
|
//if (types[1] instanceof ParameterizedType) { // for collectionMaps
|
||||||
if (types.isAssignable(returnTypeMirror, mapCollectionType)) { // for collectionMaps
|
if (types.isAssignable(returnTypeMirror, mapCollectionType)) { // for collectionMaps
|
||||||
|
@ -16,6 +16,7 @@ import java.util.regex.Matcher;
|
|||||||
|
|
||||||
import static com.moparisthebest.jdbc.CompilingRowToObjectMapper.escapeMapKeyString;
|
import static com.moparisthebest.jdbc.CompilingRowToObjectMapper.escapeMapKeyString;
|
||||||
import static com.moparisthebest.jdbc.RowToObjectMapper._setterRegex;
|
import static com.moparisthebest.jdbc.RowToObjectMapper._setterRegex;
|
||||||
|
import static com.moparisthebest.jdbc.codegen.CompileTimeResultSetMapper.getConcreteClassCanonicalName;
|
||||||
import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.typeMirrorToClass;
|
import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.typeMirrorToClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,9 +70,9 @@ public class CompileTimeRowToObjectMapper {
|
|||||||
|
|
||||||
returnMap = rsm.types.isAssignable(returnTypeClass, rsm.mapType);
|
returnMap = rsm.types.isAssignable(returnTypeClass, rsm.mapType);
|
||||||
if (returnMap) {
|
if (returnMap) {
|
||||||
// todo: need this? _returnTypeClass = ResultSetMapper.getConcreteClass(returnTypeClass, HashMap.class);
|
_returnTypeClass = returnTypeClass;
|
||||||
_returnTypeClass = null;
|
final List<? extends TypeMirror> typeArguments = ((DeclaredType) returnTypeClass).getTypeArguments();
|
||||||
componentType = mapValType;
|
componentType = typeArguments.size() > 1 ? typeArguments.get(1) : mapValType;
|
||||||
resultSetConstructor = false;
|
resultSetConstructor = false;
|
||||||
} else {
|
} else {
|
||||||
_returnTypeClass = returnTypeClass;
|
_returnTypeClass = returnTypeClass;
|
||||||
@ -325,11 +326,11 @@ public class CompileTimeRowToObjectMapper {
|
|||||||
|
|
||||||
if (returnMap) // we want a map
|
if (returnMap) // we want a map
|
||||||
try {
|
try {
|
||||||
java.append("final ").append(tType).append("<String, Object> ret = new ").append(tType).append("<String, Object>();\n");
|
java.append("final ").append(tType).append(" ret = new ").append(getConcreteClassCanonicalName(_returnTypeClass, HashMap.class)).append(tType.substring(tType.indexOf('<'))).append("();\n");
|
||||||
final int columnLength = _columnCount + 1;
|
final int columnLength = _columnCount + 1;
|
||||||
int typeId = getTypeId(componentType);
|
int typeId = getTypeId(componentType);
|
||||||
final String enumName = componentType.toString();
|
|
||||||
if (componentType != null && typeId != TypeMappingsFactory.TYPE_UNKNOWN) { // we want a specific value type
|
if (componentType != null && typeId != TypeMappingsFactory.TYPE_UNKNOWN) { // we want a specific value type
|
||||||
|
final String enumName = componentType.toString();
|
||||||
for (int x = 1; x < columnLength; ++x) {
|
for (int x = 1; x < columnLength; ++x) {
|
||||||
java.append("ret.put(").append(escapeMapKeyString(keys[x]).toLowerCase()).append(", ");
|
java.append("ret.put(").append(escapeMapKeyString(keys[x]).toLowerCase()).append(", ");
|
||||||
extractColumnValueString(java, x, typeId, enumName);
|
extractColumnValueString(java, x, typeId, enumName);
|
||||||
|
@ -11,8 +11,10 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
//IFJAVA8_START
|
//IFJAVA8_START
|
||||||
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import java.time.*;
|
import java.time.*;
|
||||||
@ -194,4 +196,17 @@ public class JdbcMapperTest {
|
|||||||
public void testPerson() throws SQLException {
|
public void testPerson() throws SQLException {
|
||||||
assertEquals(fieldPerson1, dao.getPerson(fieldPerson1.getPersonNo()));
|
assertEquals(fieldPerson1, dao.getPerson(fieldPerson1.getPersonNo()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSinglePersonNameArray() throws SQLException {
|
||||||
|
assertArrayEquals(new String[]{fieldPerson1.getFirstName(), fieldPerson1.getLastName()}, dao.getSinglePersonNameArray(fieldPerson1.getPersonNo()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSinglePersonNameMap() throws SQLException {
|
||||||
|
final Map<String, String> expected = new HashMap<String, String>();
|
||||||
|
expected.put("first_name", fieldPerson1.getFirstName());
|
||||||
|
expected.put("last_name", fieldPerson1.getLastName());
|
||||||
|
assertEquals(expected, dao.getSinglePersonNameMap(fieldPerson1.getPersonNo()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -242,4 +242,13 @@ public interface PersonDAO extends JdbcMapper {
|
|||||||
// test blob
|
// test blob
|
||||||
@JdbcMapper.SQL("SELECT some_blob FROM val WHERE val_no = {valNo}")
|
@JdbcMapper.SQL("SELECT some_blob FROM val WHERE val_no = {valNo}")
|
||||||
byte[] getBlob(long valNo);
|
byte[] getBlob(long valNo);
|
||||||
|
|
||||||
|
// test Single rows
|
||||||
|
@JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE person_no = {personNo}")
|
||||||
|
@SingleRow
|
||||||
|
String[] getSinglePersonNameArray(long personNo) throws SQLException;
|
||||||
|
|
||||||
|
@JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE person_no = {personNo}")
|
||||||
|
@SingleRow
|
||||||
|
Map<String,String> getSinglePersonNameMap(long personNo) throws SQLException;
|
||||||
}
|
}
|
||||||
|
@ -242,4 +242,13 @@ public interface PrestoPersonDAO extends PersonDAO {
|
|||||||
// test blob
|
// test blob
|
||||||
@JdbcMapper.SQL("SELECT some_blob FROM val WHERE val_no = {valNo}")
|
@JdbcMapper.SQL("SELECT some_blob FROM val WHERE val_no = {valNo}")
|
||||||
byte[] getBlob(long valNo);
|
byte[] getBlob(long valNo);
|
||||||
|
|
||||||
|
// test Single rows
|
||||||
|
@JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE person_no = {personNo}")
|
||||||
|
@SingleRow
|
||||||
|
String[] getSinglePersonNameArray(long personNo) throws SQLException;
|
||||||
|
|
||||||
|
@JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE person_no = {personNo}")
|
||||||
|
@SingleRow
|
||||||
|
Map<String,String> getSinglePersonNameMap(long personNo) throws SQLException;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user