Skip to content

Commit f41f65a

Browse files
committed
Documentation and interface updates
1 parent 178d5b8 commit f41f65a

File tree

2 files changed

+77
-63
lines changed

2 files changed

+77
-63
lines changed

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

Lines changed: 77 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,51 +23,51 @@
2323
* procedures in an easy-to-execute format.
2424
* <p>
2525
* Query collections are defined as Java enumerations that implement the {@link QueryAPI}
26-
* interface: <ul>
26+
* interface:
27+
* <ul>
2728
* <li>{@link QueryAPI#getQueryStr() getQueryStr} - Get the query string for this constant. This is the actual query
2829
* that's sent to the database.</li>
2930
* <li>{@link QueryAPI#getArgNames() getArgNames} - Get the names of the arguments for this query. This provides
3031
* diagnostic information if the incorrect number of arguments is specified by the client.</li>
31-
* <li>{@link QueryAPI#getArgCount() getArgCount} - Get the number of arguments required by this query. This enables
32-
* {@link #executeQuery(Class, QueryAPI, Object[])} to verify that the correct number of arguments has been
33-
* specified by the client.</li>
3432
* <li>{@link QueryAPI#getConnection() getConnection} - Get the connection string associated with this query. This
3533
* eliminates the need for the client to provide this information.</li>
3634
* <li>{@link QueryAPI#getEnum() getEnum} - Get the enumeration to which this query belongs. This enables {@link
3735
* #executeQuery(Class, QueryAPI, Object[])} to retrieve the name of the query's enumerated constant for
3836
* diagnostic messages.</li>
3937
* </ul>
40-
*
38+
* <p>
4139
* Store procedure collections are defined as Java enumerations that implement the {@link SProcAPI}
42-
* interface: <ul>
40+
* interface:
41+
* <ul>
4342
* <li>{@link SProcAPI#getSignature() getSignature} - Get the signature for this stored procedure object. This defines
44-
* the name of the stored procedure and
45-
* that's sent to the database.</li>
46-
* <li>{@link SProcAPI#getArgNames() getArgNames} - Get the names of the arguments for this query. This provides
47-
* diagnostic information if the incorrect number of arguments is specified by the client.</li>
48-
* <li>{@link SProcAPI#getArgCount() getArgCount} - Get the number of arguments required by this query. This enables
49-
* {@link #executeQuery(Class, SProcAPI, Object[])} to verify that the correct number of arguments has been
50-
* specified by the client.</li>
51-
* <li>{@link SProcAPI#getConnection() getConnection} - Get the connection string associated with this query. This
52-
* eliminates the need for the client to provide this information.</li>
53-
* <li>{@link SProcAPI#getEnum() getEnum} - Get the enumeration to which this query belongs. This enables {@link
54-
* #executeQuery(Class, SProcAPI, Object[])} to retrieve the name of the query's enumerated constant for
55-
* diagnostic messages.</li>
43+
* the name of the stored procedure and the modes of its arguments. If the stored procedure accepts varargs, this
44+
* will also be indicated.</li>
45+
* <li>{@link SProcAPI#getArgTypes() getArgTypes} - Get the argument types for this stored procedure object. </li>
46+
* <li>{@link SProcAPI#getConnection() getConnection} - Get the connection string associated with this stored
47+
* procedure. This eliminates the need for the client to provide this information.</li>
48+
* <li>{@link SProcAPI#getEnum() getEnum} - Get the enumeration to which this stored procedure belongs. This enables
49+
* {@link #executeStoredProcedure(Class, SProcAPI, Object[])} to retrieve the name of the stored procedured's
50+
* enumerated constant for diagnostic messages.</li>
5651
* </ul>
57-
*
58-
* To maximize usability and configurability, we recommend the following implementation strategy for your query
59-
* collections: <ul>
60-
* <li>Define your query collection as an enumeration that implements {@link QueryAPI}.</li>
61-
* <li>Define each query constant with a property name and a name for each argument (if any).</li>
52+
* <p>
53+
* To maximize usability and configurability, we recommend the following implementation strategy: <ul>
54+
* <li>Define your collection as an enumeration: <ul>
55+
* <li>Query collections implement {@link QueryAPI}.</li>
56+
* <li>Stored procedure collections implement {@link SProcAPI}.</li>
57+
* </ul></li>
58+
* <li>Define each constant: <ul>
59+
* <li>(query) Specify a property name and a name for each argument (if any).</li>
60+
* <li>(sproc) Declare the signature and the type for each argument (if any).</li>
61+
* </ul></li>
6262
* <li>To assist users of your queries, preface their names with a type indicator (<b>GET</b> or <b>UPDATE</b>).</li>
63-
* <li>Back the query collection with a configuration that implements the <b>{@code Settings API}</b>: <ul>
63+
* <li>Back query collections with configurations that implement the <b>{@code Settings API}</b>: <ul>
6464
* <li>groupId: com.nordstrom.test-automation.tools</li>
6565
* <li>artifactId: settings</li>
6666
* <li>className: com.nordstrom.automation.settings.SettingsCore</li>
67-
* </ul>
68-
* </li>
69-
* <li>To support execution on multiple endpoints, implement {@link QueryAPI#getConnection() getConnection} with
70-
* sub-configurations or other dynamic data sources (e.g. - web service).</li>
67+
* </ul></li>
68+
* <li>To support execution on multiple endpoints, implement {@link QueryAPI#getConnection()} or {@link
69+
* SProcAPI#getConnection()} with sub-configurations or other dynamic data sources (e.g.
70+
* - web service).</li>
7171
* </ul>
7272
* <b>Query Collection Example</b>
7373
* <p>
@@ -134,11 +134,6 @@
134134
* }
135135
*
136136
* {@code @Override}
137-
* public int getArgCount() {
138-
* return args.length;
139-
* }
140-
*
141-
* {@code @Override}
142137
* public String getConnection() {
143138
* if (rmsQueries.contains(this)) {
144139
* return getRmsConnect();
@@ -190,6 +185,47 @@
190185
* public static OpctConfig getConfig() {
191186
* return OpctValues.config;
192187
* }
188+
*
189+
* public enum SProcValues implements SProcAPI {
190+
* /** args: [ ] *&#47;
191+
* SHOW_SUPPLIERS("SHOW_SUPPLIERS()"),
192+
* /** args: [ coffee_name, supplier_name ] *&#47;
193+
* GET_SUPPLIER_OF_COFFEE("GET_SUPPLIER_OF_COFFEE(>, <)", Types.VARCHAR, Types.VARCHAR),
194+
* /** args: [ coffee_name, max_percent, new_price ] *&#47;
195+
* RAISE_PRICE("RAISE_PRICE(>, >, =)", Types.VARCHAR, Types.REAL, Types.NUMERIC),
196+
* /** args: [ str, val... ] *&#47;
197+
* IN_VARARGS("IN_VARARGS(<, >...)", Types.VARCHAR, Types.INTEGER),
198+
* /** args: [ val, str... ] *&#47;
199+
* OUT_VARARGS("OUT_VARARGS(>, <...)", Types.INTEGER, Types.VARCHAR);
200+
*
201+
* private int[] argTypes;
202+
* private String signature;
203+
*
204+
* SProcValues(String signature, int... argTypes) {
205+
* this.signature = signature;
206+
* this.argTypes = argTypes;
207+
* }
208+
*
209+
* {@code @Override}
210+
* public String getSignature() {
211+
* return signature;
212+
* }
213+
*
214+
* {@code @Override}
215+
* public int[] getArgTypes () {
216+
* return argTypes;
217+
* }
218+
*
219+
* {@code @Override}
220+
* public String getConnection() {
221+
return OpctValues.getRmsConnect();
222+
* }
223+
*
224+
* {@code @Override}
225+
* public {@code Enum<SProcValues>} getEnum() {
226+
* return this;
227+
* }
228+
* }
193229
* }
194230
* </pre>
195231
*/
@@ -273,7 +309,7 @@ public static ResultPackage getResultPackage(QueryAPI query, Object... queryArgs
273309
* when you're done with it to free up database and JDBC resources that were allocated for it.
274310
*/
275311
private static Object executeQuery(Class<?> resultType, QueryAPI query, Object... queryArgs) {
276-
int expectCount = query.getArgCount();
312+
int expectCount = query.getArgNames().length;
277313
int actualCount = queryArgs.length;
278314

279315
if (actualCount != expectCount) {
@@ -347,6 +383,7 @@ public static Object executeStoredProcedure(Class<?> resultType, SProcAPI sproc,
347383
String[] args = {};
348384
String sprocName = null;
349385
boolean hasVarArgs = false;
386+
int[] argTypes = sproc.getArgTypes();
350387
String signature = sproc.getSignature();
351388
Matcher matcher = SPROC_PATTERN.matcher(signature);
352389

@@ -372,7 +409,7 @@ public static Object executeStoredProcedure(Class<?> resultType, SProcAPI sproc,
372409
}
373410

374411
int argsCount = args.length;
375-
int typesCount = sproc.getArgCount();
412+
int typesCount = argTypes.length;
376413
int parmsCount = parms.length;
377414

378415
int minCount = typesCount;
@@ -382,32 +419,28 @@ public static Object executeStoredProcedure(Class<?> resultType, SProcAPI sproc,
382419
message = String.format(
383420
"Signature argument count differs from declared type count for %s%s: "
384421
+ "signature: %d; declared: %d",
385-
sproc.getEnum().name(), Arrays.toString(sproc.getArgTypes()), argsCount,
386-
typesCount);
422+
sproc.getEnum().name(), Arrays.toString(argTypes), argsCount, typesCount);
387423
} else if (hasVarArgs) {
388424
minCount -= 1;
389425
if (parmsCount < minCount) {
390426
message = String.format(
391427
"Insufficient arguments count for %s%s: minimum: %d; actual: %d",
392-
sproc.getEnum().name(), Arrays.toString(sproc.getArgTypes()),
393-
minCount, parmsCount);
428+
sproc.getEnum().name(), Arrays.toString(argTypes), minCount, parmsCount);
394429
}
395430
} else if (parmsCount != typesCount) {
396431
if (typesCount == 0) {
397432
message = "No arguments expected for " + sproc.getEnum().name();
398433
} else {
399434
message = String.format(
400435
"Incorrect arguments count for %s%s: expect: %d; actual: %d",
401-
sproc.getEnum().name(), Arrays.toString(sproc.getArgTypes()),
402-
typesCount, parmsCount);
436+
sproc.getEnum().name(), Arrays.toString(argTypes), typesCount, parmsCount);
403437
}
404438
}
405439

406440
if (message != null) {
407441
throw new IllegalArgumentException(message);
408442
}
409443

410-
int[] argTypes = sproc.getArgTypes();
411444
Param[] parmArray = Param.array(parmsCount);
412445

413446
int i;
@@ -574,19 +607,12 @@ public interface QueryAPI {
574607
String getQueryStr();
575608

576609
/**
577-
* Get the argument names for this query object
610+
* Get the argument names for this query object.
578611
*
579612
* @return query object argument names
580613
*/
581614
String[] getArgNames();
582615

583-
/**
584-
* Get the count of arguments for this query object.
585-
*
586-
* @return query object argument count
587-
*/
588-
int getArgCount();
589-
590616
/**
591617
* Get the database connection string for this query object.
592618
*
@@ -630,19 +656,12 @@ public interface SProcAPI {
630656
String getSignature();
631657

632658
/**
633-
* Get the argument types for this stored procedure object
659+
* Get the argument types for this stored procedure object.
634660
*
635661
* @return stored procedure argument types
636662
*/
637663
int[] getArgTypes();
638664

639-
/**
640-
* Get the count of argument types for this stored procedure object.
641-
*
642-
* @return stored procedure argument count
643-
*/
644-
int getArgCount();
645-
646665
/**
647666
* Get the database connection string for this stored procedure object.
648667
*

src/test/java/com/nordstrom/common/jdbc/DatabaseUtilsTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,6 @@ public String[] getArgNames() {
123123
return args;
124124
}
125125

126-
@Override
127-
public int getArgCount() {
128-
return args.length;
129-
}
130-
131126
@Override
132127
public String getConnection() {
133128
return connection();

0 commit comments

Comments
 (0)