|
6 | 6 | import me.zort.sqllib.internal.query.QueryDetails; |
7 | 7 | import me.zort.sqllib.internal.query.QueryNode; |
8 | 8 | import me.zort.sqllib.internal.query.QueryPriority; |
| 9 | +import me.zort.sqllib.internal.query.ResultSetAware; |
9 | 10 | import me.zort.sqllib.mapping.PlaceholderMapper; |
10 | 11 | import me.zort.sqllib.mapping.QueryAnnotation; |
11 | 12 | import me.zort.sqllib.mapping.annotation.Exec; |
|
17 | 18 | import java.util.ArrayList; |
18 | 19 | import java.util.HashMap; |
19 | 20 | import java.util.Map; |
| 21 | +import java.util.concurrent.atomic.AtomicInteger; |
| 22 | +import java.util.function.Supplier; |
20 | 23 |
|
21 | 24 | public class NativeQueryBuilder implements QueryAnnotation.QueryBuilder<Annotation> { |
22 | 25 | @Override |
23 | 26 | public QueryNode<?> build(QueryAnnotation.DefaultMappingDetails details, Annotation queryAnnotation, Method method, ParameterPair[] parameters) { |
24 | 27 | SQLConnection connection = details.getConnection(); |
25 | 28 | PlaceholderMapper mapper = new PlaceholderMapper(parameters); |
26 | | - return new LocalQueryNodeExecutive(connection) { |
27 | | - private int currPhIndex = 0; |
28 | | - @Override |
29 | | - public QueryDetails buildQueryDetails() { |
30 | | - String annotValue; |
31 | | - String[] annotParams; |
| 29 | + AtomicInteger currPhIndex = new AtomicInteger(0); |
| 30 | + Supplier<String> nextPlaceholder = () -> "nq_" + currPhIndex.getAndIncrement(); |
| 31 | + return buildNodeImpl(queryAnnotation, connection, () -> { |
| 32 | + String annotValue; |
| 33 | + String[] annotParams; |
32 | 34 |
|
33 | | - if (queryAnnotation instanceof Query) { |
34 | | - annotValue = ((Query) queryAnnotation).value(); |
35 | | - annotParams = ((Query) queryAnnotation).params(); |
36 | | - } else if(queryAnnotation instanceof Exec) { |
37 | | - annotValue = ((Exec) queryAnnotation).value(); |
38 | | - annotParams = ((Exec) queryAnnotation).params(); |
39 | | - } else { |
40 | | - throw new IllegalArgumentException(String.format("NativeQueryBuilder does not support %s annotation!", queryAnnotation.getClass().getName())); |
41 | | - } |
| 35 | + if (queryAnnotation instanceof Query) { |
| 36 | + annotValue = ((Query) queryAnnotation).value(); |
| 37 | + annotParams = ((Query) queryAnnotation).params(); |
| 38 | + } else if(queryAnnotation instanceof Exec) { |
| 39 | + annotValue = ((Exec) queryAnnotation).value(); |
| 40 | + annotParams = ((Exec) queryAnnotation).params(); |
| 41 | + } else { |
| 42 | + throw new IllegalArgumentException(String.format("NativeQueryBuilder does not support %s annotation!", queryAnnotation.getClass().getName())); |
| 43 | + } |
42 | 44 |
|
43 | | - annotValue = mapper.assignValues(annotValue); |
| 45 | + annotValue = mapper.assignValues(annotValue); |
44 | 46 |
|
45 | | - Map<String, Object> params = new HashMap<>(); |
46 | | - for (String param : annotParams) { |
47 | | - param = mapper.assignValues(param); |
48 | | - int index = annotValue.indexOf("?"); |
49 | | - if (index == -1) |
50 | | - break; |
| 47 | + Map<String, Object> params = new HashMap<>(); |
| 48 | + for (String param : annotParams) { |
| 49 | + param = mapper.assignValues(param); |
| 50 | + int index = annotValue.indexOf("?"); |
| 51 | + if (index == -1) |
| 52 | + break; |
51 | 53 |
|
52 | | - String placeholder = nextPlaceholder(); |
53 | | - annotValue = annotValue.replaceFirst("\\?", "<" + placeholder + ">"); |
54 | | - params.put(placeholder, param); |
55 | | - } |
56 | | - return new QueryDetails(annotValue, params); |
| 54 | + String placeholder = nextPlaceholder.get(); |
| 55 | + annotValue = annotValue.replaceFirst("\\?", "<" + placeholder + ">"); |
| 56 | + params.put(placeholder, param); |
57 | 57 | } |
| 58 | + return new QueryDetails(annotValue, params); |
| 59 | + }); |
| 60 | + } |
58 | 61 |
|
59 | | - private String nextPlaceholder() { |
60 | | - return "nq_" + currPhIndex++; |
61 | | - } |
| 62 | + private static QueryNode<?> buildNodeImpl(Annotation queryAnnotation, SQLConnection connection, Supplier<QueryDetails> detailsSupplier) { |
| 63 | + if (queryAnnotation instanceof Exec) return new NodeExecutiveImpl(connection) { |
| 64 | + @Override |
| 65 | + public QueryDetails buildQueryDetails() {return detailsSupplier.get();} |
62 | 66 | }; |
| 67 | + else if (queryAnnotation instanceof Query) return new NodeExecutiveImplRS(connection) { |
| 68 | + @Override |
| 69 | + public QueryDetails buildQueryDetails() {return detailsSupplier.get();} |
| 70 | + }; |
| 71 | + throw new IllegalArgumentException(); |
63 | 72 | } |
64 | 73 |
|
65 | | - private static abstract class LocalQueryNodeExecutive extends QueryNode<QueryNode<?>> implements Executive { |
66 | | - |
| 74 | + private static abstract class NodeExecutiveImpl extends QueryNode<QueryNode<?>> implements Executive { |
67 | 75 | @Getter |
68 | 76 | private final SQLConnection connection; |
69 | 77 |
|
70 | | - public LocalQueryNodeExecutive(SQLConnection connection) { |
| 78 | + public NodeExecutiveImpl(SQLConnection connection) { |
71 | 79 | super(null, new ArrayList<>(), QueryPriority.GENERAL); |
72 | 80 | this.connection = connection; |
73 | 81 | } |
74 | 82 | } |
75 | 83 |
|
| 84 | + private static abstract class NodeExecutiveImplRS extends NodeExecutiveImpl implements ResultSetAware { |
| 85 | + public NodeExecutiveImplRS(SQLConnection connection) { |
| 86 | + super(connection); |
| 87 | + } |
| 88 | + } |
| 89 | + |
76 | 90 | } |
0 commit comments