|
1 | 1 | /*
|
2 |
| - * Copyright 2014 the original author or authors. |
| 2 | + * Copyright 2014-2015 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -57,7 +57,7 @@ public StoredProcedureAttributes createFrom(Method method, JpaEntityMetadata<?>
|
57 | 57 | procedure);
|
58 | 58 |
|
59 | 59 | if (namedStoredProc != null) {
|
60 |
| - return newProcedureAttributesFrom(method, namedStoredProc); |
| 60 | + return newProcedureAttributesFrom(method, namedStoredProc, procedure); |
61 | 61 | }
|
62 | 62 |
|
63 | 63 | String procedureName = deriveProcedureNameFrom(method, procedure);
|
@@ -90,43 +90,61 @@ private String deriveProcedureNameFrom(Method method, Procedure procedure) {
|
90 | 90 | /**
|
91 | 91 | * @param method
|
92 | 92 | * @param namedStoredProc
|
| 93 | + * @param procedure |
93 | 94 | * @return
|
94 | 95 | */
|
95 |
| - private StoredProcedureAttributes newProcedureAttributesFrom(Method method, NamedStoredProcedureQuery namedStoredProc) { |
| 96 | + private StoredProcedureAttributes newProcedureAttributesFrom(Method method, |
| 97 | + NamedStoredProcedureQuery namedStoredProc, Procedure procedure) { |
96 | 98 |
|
97 | 99 | String outputParameterName = null;
|
98 | 100 | Class<?> outputParameterType = null;
|
99 | 101 |
|
100 |
| - int outputParameterCount = 0; |
| 102 | + if (!procedure.outputParameterName().isEmpty()) { |
| 103 | + |
| 104 | + // we give the output parameter definition from the @Procedure annotation precedence |
| 105 | + outputParameterName = procedure.outputParameterName(); |
| 106 | + } else { |
| 107 | + |
| 108 | + // try to discover the output parameter |
| 109 | + List<StoredProcedureParameter> outputParameters = extractOutputParametersFrom(namedStoredProc); |
| 110 | + |
| 111 | + if (outputParameters.size() != 1) { |
| 112 | + throw new IllegalStateException(String.format( |
| 113 | + "Could not create ProcedureAttributes from %s. We currently support exactly one output parameter!", method)); |
| 114 | + } |
| 115 | + |
| 116 | + StoredProcedureParameter outputParameter = outputParameters.get(0); |
| 117 | + outputParameterName = outputParameter.name(); |
| 118 | + outputParameterType = outputParameter.type(); |
| 119 | + } |
| 120 | + |
| 121 | + if (outputParameterType == null || Object.class.equals(outputParameterType) |
| 122 | + || void.class.equals(outputParameterType)) { |
| 123 | + outputParameterType = method.getReturnType(); |
| 124 | + } |
| 125 | + |
| 126 | + return new StoredProcedureAttributes(namedStoredProc.name(), outputParameterName, outputParameterType, true); |
| 127 | + } |
| 128 | + |
| 129 | + private List<StoredProcedureParameter> extractOutputParametersFrom(NamedStoredProcedureQuery namedStoredProc) { |
| 130 | + |
| 131 | + List<StoredProcedureParameter> outputParameters = new ArrayList<StoredProcedureParameter>(); |
101 | 132 |
|
102 | 133 | for (StoredProcedureParameter param : namedStoredProc.parameters()) {
|
| 134 | + |
103 | 135 | switch (param.mode()) {
|
104 | 136 | case OUT:
|
105 | 137 | case INOUT:
|
106 |
| - |
107 |
| - if (outputParameterCount > 0) { |
108 |
| - throw new IllegalStateException( |
109 |
| - String.format( |
110 |
| - "Could not create ProcedureAttributes from %s. We currently support only one output parameter!", |
111 |
| - method)); |
112 |
| - } |
113 |
| - |
114 |
| - outputParameterName = param.name(); |
115 |
| - outputParameterType = param.type(); |
116 |
| - |
117 |
| - outputParameterCount++; |
| 138 | + case REF_CURSOR: |
| 139 | + outputParameters.add(param); |
118 | 140 | break;
|
119 | 141 | case IN:
|
120 | 142 | default:
|
121 | 143 | continue;
|
122 | 144 | }
|
123 | 145 | }
|
124 | 146 |
|
125 |
| - if (outputParameterType == null) { |
126 |
| - outputParameterType = method.getReturnType(); |
127 |
| - } |
128 |
| - |
129 |
| - return new StoredProcedureAttributes(namedStoredProc.name(), outputParameterName, outputParameterType, true); |
| 147 | + return outputParameters; |
130 | 148 | }
|
131 | 149 |
|
132 | 150 | /**
|
|
0 commit comments