Skip to content

Commit 6f59b64

Browse files
committed
Code reformat
Changed: * Made some code design changes
1 parent c973d9f commit 6f59b64

3 files changed

Lines changed: 25 additions & 23 deletions

File tree

core/src/main/java/me/zort/sqllib/mapping/DefaultResultAdapter.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,15 @@ public class DefaultResultAdapter implements StatementMappingResultAdapter {
1414
@Override
1515
public Object adaptResult(Method method, QueryResult result) {
1616
Class<?> returnType = method.getReturnType();
17-
1817
if(returnType.equals(QueryResult.class)) {
1918
return result;
2019
} else if (isVoid(returnType) || !(result instanceof QueryRowsResult)) {
2120
return null;
2221
}
23-
2422
QueryRowsResult<?> rows = (QueryRowsResult<?>) result;
25-
26-
if (List.class.isAssignableFrom(returnType)) {
27-
return rows;
28-
}
29-
23+
if (List.class.isAssignableFrom(returnType)) return rows;
3024
Object obj = rows.isEmpty() ? null : rows.get(0);
31-
32-
if (Optional.class.isAssignableFrom(returnType))
33-
return Optional.ofNullable(obj);
34-
25+
if (Optional.class.isAssignableFrom(returnType)) return Optional.ofNullable(obj);
3526
return obj;
3627
}
3728

core/src/main/java/me/zort/sqllib/mapping/DefaultStatementMapping.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,8 @@ public QueryResult executeQuery(StatementMappingOptions options, Method method,
4040
i++;
4141
}
4242

43-
Annotation queryAnnotation = null;
44-
QueryAnnotation wrappedAnnotation = null;
45-
for (Annotation annotation : method.getAnnotations()) {
46-
if (QueryAnnotation.isQueryAnnotation(annotation)) {
47-
queryAnnotation = annotation;
48-
wrappedAnnotation = QueryAnnotation.wrap(annotation);
49-
}
50-
}
43+
Annotation queryAnnotation = filterQueryAnnotation(method, args);
44+
QueryAnnotation wrappedAnnotation = QueryAnnotation.wrap(queryAnnotation);
5145

5246
if (wrappedAnnotation == null) {
5347
throw new SQLMappingException("No query builder found for method " + method.getName() + "! Is query annotation present?", method, args);
@@ -56,10 +50,8 @@ public QueryResult executeQuery(StatementMappingOptions options, Method method,
5650
}
5751

5852
QueryNode<?> node = wrappedAnnotation.getQueryBuilder().build(
59-
new QueryAnnotation.DefaultMappingDetails(connection, options),
60-
queryAnnotation,
61-
method,
62-
parameters);
53+
new QueryAnnotation.DefaultMappingDetails(connection, options), queryAnnotation,
54+
method, parameters);
6355
if (method.isAnnotationPresent(Append.class)) {
6456
Append append = method.getAnnotation(Append.class);
6557
node.then(new PlaceholderMapper(parameters).assignValues(append.value()));
@@ -78,6 +70,20 @@ public QueryResult executeQuery(StatementMappingOptions options, Method method,
7870
}
7971
}
8072

73+
private static Annotation filterQueryAnnotation(Method method, Object[] args) {
74+
Annotation queryAnnotation = null;
75+
for (Annotation annotation : method.getAnnotations()) {
76+
boolean isQueryAnnot = QueryAnnotation.isQueryAnnotation(annotation);
77+
if (isQueryAnnot && queryAnnotation == null) {
78+
queryAnnotation = annotation;
79+
} else if (isQueryAnnot) {
80+
String errMessage = String.format("Multiple query annotations (Select/Insert/...) found on method %s!", method.getName());
81+
throw new SQLMappingException(errMessage, method, args);
82+
}
83+
}
84+
return queryAnnotation;
85+
}
86+
8187
@Override
8288
public boolean isMappingMethod(Method method) {
8389
for (Annotation annot : method.getAnnotations()) {

core/src/main/java/me/zort/sqllib/mapping/QueryAnnotation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ public class QueryAnnotation {
4545
QUERY_ANNOT.put(Exec.class, new QueryAnnotation(false, new NativeQueryBuilder()));
4646
}
4747

48+
public static <T extends Annotation> void register(Class<T> annotation, boolean producesResult, QueryBuilder<T> builder) {
49+
QUERY_ANNOT.put(annotation, new QueryAnnotation(producesResult, builder));
50+
}
51+
4852
@Nullable
4953
public static QueryAnnotation wrap(Annotation annotation) {
54+
if (annotation == null) return null;
5055
return isQueryAnnotation(annotation) ? QUERY_ANNOT.get(annotation.annotationType()) : null;
5156
}
5257

0 commit comments

Comments
 (0)