1212
1313/**
1414 * This utility class provides facilities that enable you to define collections of Oracle database queries and
15- * execute them easily. Query collections are defined as Java enumeration that implement a {@link QueryAPI common
16- * interface} : <ul>
17- * <li>{@link QueryAPI#getQueryStr() getQueryStr} - Get the query string for this constant. This is the actual
18- * query that's sent to the database.</li>
15+ * execute them easily. Query collections are defined as Java enumeration that implement the {@link QueryAPI}
16+ * interface: <ul>
17+ * <li>{@link QueryAPI#getQueryStr() getQueryStr} - Get the query string for this constant. This is the actual query
18+ * that's sent to the database.</li>
1919 * <li>{@link QueryAPI#getArgNames() getArgNames} - Get the names of the arguments for this query. This provides
20- * diagnostic information if the incorrect number of arguments is specified</li>
21- * <li>{@link QueryAPI#getArgCount() getArgCount}</li>
22- * <li>{@link QueryAPI#getConnection() getConnection}</li>
23- * <li>{@link QueryAPI#getEnum() getEnum}</li>
20+ * diagnostic information if the incorrect number of arguments is specified by the client.</li>
21+ * <li>{@link QueryAPI#getArgCount() getArgCount} - Get the number of arguments required by this query. This enables
22+ * {@link #executeOracleQuery(Class, QueryAPI, Object[])} to verify that the correct number of arguments has been
23+ * specified by the client.</li>
24+ * <li>{@link QueryAPI#getConnection() getConnection} - Get the connection string associated with this query. This
25+ * eliminates the need for the client to provide this information.</li>
26+ * <li>{@link QueryAPI#getEnum() getEnum} - Get the enumeration to which this query belongs. This enables {@link
27+ * #executeOracleQuery(Class, QueryAPI, Object[])} to retrieve the name of the query's enumerated constant for
28+ * diagnostic messages.</li>
2429 * </ul>
30+ *
31+ * To maximize usability and configurability, we recommend the following implementation strategy for your query
32+ * collections: <ul>
33+ * <li>Define your query collection as an enumeration that implements {@link QueryAPI}.</li>
34+ * <li>Define each query constant with a property name and a name for each argument (if any).</li>
35+ * <li>To assist users of your queries, preface their names with a type indicator (GET or UPDATE).</li>
36+ * <li>Back the query collection with a configuration that implements the <b>{@code Settings API}</b>: <ul>
37+ * <li>groupId: com.nordstrom.test-automation.tools</li>
38+ * <li>artifactId: settings</li>
39+ * <li>className: com.nordstrom.automation.settings.SettingsCore</li>
40+ * </ul>
41+ * </li>
42+ * <li>To support execution on multiple endpoints, implement {@link QueryAPI#getConnection() getConnection} with
43+ * sub-configurations or other dynamic data sources (e.g. - web service).</li>
44+ * </ul>
45+ * <b>Query Collection Example</b>
46+ * <p>
47+ * <pre>
48+ * public class OpctConfig extends {@code SettingsCore<OpctConfig.OpctValues>} {
49+ *
50+ * private static final String SETTINGS_FILE = "OpctConfig.properties";
51+ *
52+ * private OpctConfig() throws ConfigurationException, IOException {
53+ * super(OpctValues.class);
54+ * }
55+ *
56+ * public enum OpctValues implements SettingsCore.SettingsAPI, QueryAPI {
57+ * /** args: [ ] */
58+ * GET_RULE_HEAD_DETAILS("opct.query.getRuleHeadDetails"),
59+ * /** args: [ name, zone_id, priority, rule_type ] */
60+ * GET_RULE_COUNT("opct.query.getRuleCount", "name", "zone_id", "priority", "rule_type"),
61+ * /** args: [ role_id, user_id ] */
62+ * UPDATE_USER_ROLE("opct.query.updateRsmUserRole", "role_id", "user_id"),
63+ * /** MST connection string */
64+ * MST_CONNECT("opct.connect.mst"),
65+ * /** RMS connection string */
66+ * RMS_CONNECT("opct.connect.rms");
67+ *
68+ * private String key;
69+ * private String[] args;
70+ * private String query;
71+ *
72+ * private static OpctConfig config;
73+ * private static String mstConnect;
74+ * private static String rmsConnect;
75+ *
76+ * private static {@code EnumSet<OpctValues>} rmsQueries = EnumSet.of(UPDATE_USER_ROLE);
77+ *
78+ * static {
79+ * try {
80+ * config = new OpctConfig();
81+ * } catch (ConfigurationException | IOException e) {
82+ * throw new RuntimeException("Unable to instantiate OPCT configuration object", e);
83+ * }
84+ * }
85+ *
86+ * OpctValues(String key, String... args) {
87+ * this.key = key;
88+ * this.args = args;
89+ * }
90+ *
91+ * {@code @Override}
92+ * public String key() {
93+ * return key;
94+ * }
95+ *
96+ * {@code @Override}
97+ * public String getQueryStr() {
98+ * if (query == null) {
99+ * query = config.getString(key);
100+ * }
101+ * return query;
102+ * }
103+ *
104+ * {@code @Override}
105+ * public String[] getArgNames() {
106+ * return args;
107+ * }
108+ *
109+ * {@code @Override}
110+ * public int getArgCount() {
111+ * return args.length;
112+ * }
113+ *
114+ * {@code @Override}
115+ * public String getConnection() {
116+ * if (rmsQueries.contains(this)) {
117+ * return getRmsConnect();
118+ * } else {
119+ * return getMstConnect();
120+ * }
121+ * }
122+ *
123+ * {@code @Override}
124+ * public {@code Enum<OpctValues>} getEnum() {
125+ * return this;
126+ * }
127+ *
128+ * /**
129+ * * Get MST connection string.
130+ * *
131+ * * @return MST connection string
132+ * */
133+ * public static String getMstConnect() {
134+ * if (mstConnect == null) {
135+ * mstConnect = config.getString(OpctValues.MST_CONNECT.key());
136+ * }
137+ * return mstConnect;
138+ * }
139+ *
140+ * /**
141+ * * Get RMS connection string.
142+ * *
143+ * * @return RMS connection string
144+ * */
145+ * public static String getRmsConnect() {
146+ * if (rmsConnect == null) {
147+ * rmsConnect = config.getString(OpctValues.RMS_CONNECT.key());
148+ * }
149+ * return rmsConnect;
150+ * }
151+ * }
152+ *
153+ * {@code @Override}
154+ * public String getSettingsPath() {
155+ * return SETTINGS_FILE;
156+ * }
157+ *
158+ * /**
159+ * * Get OPCT configuration object.
160+ * *
161+ * * @return OPCT configuration object
162+ * */
163+ * public static OpctConfig getConfig() {
164+ * return OpctValues.config;
165+ * }
166+ * }
167+ * </pre>
25168 */
26169public class DatabaseUtils {
27170
@@ -72,6 +215,17 @@ public static String getString(QueryAPI query, Object... queryArgs) {
72215 return (String ) executeOracleQuery (String .class , query , queryArgs );
73216 }
74217
218+ /**
219+ * Execute the specified query object with supplied arguments as a 'query' operation
220+ *
221+ * @param query query object to execute
222+ * @param queryArgs replacement values for query place-holders
223+ * @return {@link ResultPackage} object
224+ */
225+ public static ResultPackage getResultPackage (QueryAPI query , Object ... queryArgs ) {
226+ return (ResultPackage ) executeOracleQuery (ResultPackage .class , query , queryArgs );
227+ }
228+
75229 /**
76230 * Execute the specified query with the supplied arguments, returning a result of the indicated type.
77231 * <p>
@@ -323,4 +477,4 @@ public void close() {
323477 }
324478 }
325479 }
326- }
480+ }
0 commit comments