Skip to content

Commit 68cefbb

Browse files
committed
Add support for varargs
1 parent cb3178c commit 68cefbb

File tree

2 files changed

+56
-39
lines changed

2 files changed

+56
-39
lines changed

src/main/java/com/nordstrom/common/jdbc/DatabaseUtils.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -325,22 +325,6 @@ public static Object executeQuery(Class<?> resultType, String connectionStr, Str
325325
public static Object executeStoredProcedure(Class<?> resultType, SProcAPI sproc, Object... parms) {
326326
Objects.requireNonNull(resultType, "[resultType] argument must be non-null");
327327

328-
int typesCount = sproc.getArgCount();
329-
int parmsCount = parms.length;
330-
331-
if (parmsCount != typesCount) {
332-
String message;
333-
334-
if (typesCount == 0) {
335-
message = "No arguments expected for " + sproc.getEnum().name();
336-
} else {
337-
message = String.format("Incorrect argument count for %s%s: expect: %d; actual: %d",
338-
sproc.getEnum().name(), Arrays.toString(sproc.getArgTypes()), typesCount, parmsCount);
339-
}
340-
341-
throw new IllegalArgumentException(message);
342-
}
343-
344328
String sprocName;
345329
String[] args = {};
346330
String signature = sproc.getSignature();
@@ -358,31 +342,47 @@ public static Object executeStoredProcedure(Class<?> resultType, SProcAPI sproc,
358342
}
359343

360344
int argsCount = args.length;
345+
int typesCount = sproc.getArgCount();
346+
347+
// if unbalanced args/types
361348
if (argsCount != typesCount) {
362349
String message = String.format("Signature argument count differs from declared type count for %s%s: "
363350
+ "signature: %d; declared: %d",
364351
sproc.getEnum().name(), Arrays.toString(sproc.getArgTypes()), argsCount, typesCount);
365352
throw new IllegalArgumentException(message);
366353
}
367354

355+
int parmsCount = parms.length;
356+
357+
// if parms specified with no matching types
358+
if ((parmsCount > 0) && (typesCount == 0)) {
359+
throw new IllegalArgumentException("No arguments expected for " + sproc.getEnum().name());
360+
}
361+
362+
// if fewer parms than types
363+
if (parmsCount < typesCount) {
364+
String message = String.format("Insufficient arguments count for %s%s: minimum: %d; actual: %d",
365+
sproc.getEnum().name(), Arrays.toString(sproc.getArgTypes()), typesCount, parmsCount);
366+
throw new IllegalArgumentException(message);
367+
}
368+
368369
int[] argTypes = sproc.getArgTypes();
369-
Param[] parmArray = Param.array(typesCount);
370-
for (int i = 0; i < typesCount; i++) {
371-
int type = argTypes[i];
372-
Mode mode = Mode.fromChar(args[i].charAt(0));
373-
switch (mode) {
374-
case IN:
375-
parmArray[i] = Param.in(parms[i], type);
376-
break;
377-
378-
case OUT:
379-
parmArray[i] = Param.out(type);
380-
break;
381-
382-
case INOUT:
383-
parmArray[i] = Param.inOut(parms[i], type);
384-
break;
385-
}
370+
Param[] parmArray = Param.array(parmsCount);
371+
372+
int i = 0;
373+
int type = 0;
374+
Mode mode = Mode.IN;
375+
376+
// process declared parameters
377+
for (i = 0; i < typesCount; i++) {
378+
type = argTypes[i];
379+
mode = Mode.fromChar(args[i].charAt(0));
380+
parmArray[i] = Param.create(mode, type, parms[i]);
381+
}
382+
383+
// handle varargs parameters
384+
for (; i < parmsCount; i++) {
385+
parmArray[i] = Param.create(mode, type, parms[i]);
386386
}
387387

388388
return executeStoredProcedure(resultType, sproc.getConnection(), sprocName, parmArray);

src/main/java/com/nordstrom/common/jdbc/Param.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
public class Param {
1313

1414
private Mode mode = Mode.IN;
15-
private Object inputValue;
1615
private int paramType;
16+
private Object inputValue;
1717

1818
/**
1919
* Constructor: Private, to discourage direct instantiation.
@@ -22,17 +22,34 @@ private Param() {
2222
}
2323

2424
/**
25-
* Instantiate an IN parameter of the indicated type with the specified value.
25+
* Instantiate a parameter of the indicated mode and type with the specified value.
2626
*
27+
* @param mode parameter {@link Mode mode}
28+
* @param paramType parameter {@link Types type}
2729
* @param inputValue parameter value
30+
* @return new {@link Param} object
31+
*/
32+
public static Param create(Mode mode, int paramType, Object inputValue) {
33+
if (mode == Mode.OUT) {
34+
return Param.out(paramType);
35+
} else if (mode == Mode.INOUT) {
36+
return Param.inOut(paramType, inputValue);
37+
}
38+
return Param.in(paramType, inputValue);
39+
}
40+
41+
/**
42+
* Instantiate an IN parameter of the indicated type with the specified value.
43+
*
2844
* @param paramType parameter {@link Types type}
45+
* @param inputValue parameter value
2946
* @return new {@link Param} object
3047
*/
31-
public static Param in(Object inputValue, int paramType) {
48+
public static Param in(int paramType, Object inputValue) {
3249
Param parameter = new Param();
3350
parameter.mode = Mode.IN;
34-
parameter.inputValue = inputValue;
3551
parameter.paramType = paramType;
52+
parameter.inputValue = inputValue;
3653
return parameter;
3754
}
3855

@@ -52,11 +69,11 @@ public static Param out(int paramType) {
5269
/**
5370
* Instantiate an INOUT parameter of the indicated type with the specified value.
5471
*
55-
* @param inputValue parameter value
5672
* @param paramType parameter {@link Types type}
73+
* @param inputValue parameter value
5774
* @return new {@link Param} object
5875
*/
59-
public static Param inOut(Object inputValue, int paramType) {
76+
public static Param inOut(int paramType, Object inputValue) {
6077
Param parameter = new Param();
6178
parameter.mode = Mode.INOUT;
6279
parameter.inputValue = inputValue;

0 commit comments

Comments
 (0)