001: package com.mockrunner.jdbc;
002:
003: import java.util.ArrayList;
004: import java.util.Arrays;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008: import java.util.TreeMap;
009:
010: /**
011: * Abstract base class for all statement types
012: * that support out parameters, i.e. <code>CallableStatement</code>.
013: */
014: public abstract class AbstractOutParameterResultSetHandler extends
015: AbstractParameterResultSetHandler {
016: private boolean mustRegisterOutParameters = false;
017: private Map globalOutParameter = null;
018: private Map outParameterForStatement = new TreeMap();
019: private Map outParameterForStatementParameters = new TreeMap();
020:
021: /**
022: * Set if out parameters must be registered to be returned.
023: * The default is <code>false</code>, i.e. if there are matching
024: * out parameters prepared, they are returned even if the
025: * <code>registerOutParameter</code> methods of <code>CallableStatement</code>
026: * have not been called. If set to <code>true</code>, <code>registerOutParameter</code>
027: * must be called.
028: * @param mustOutParameterBeRegistered must out parameter be registered
029: */
030: public void setMustRegisterOutParameters(
031: boolean mustOutParameterBeRegistered) {
032: this .mustRegisterOutParameters = mustOutParameterBeRegistered;
033: }
034:
035: /**
036: * Get if out parameter must be registered to be returned.
037: * @return must out parameter be registered
038: */
039: public boolean getMustRegisterOutParameters() {
040: return mustRegisterOutParameters;
041: }
042:
043: /**
044: * Returns the first out parameter <code>Map</code> that matches
045: * the specified SQL string.
046: * Please note that you can modify the match parameters with
047: * {@link #setCaseSensitive}, {@link #setExactMatch} and
048: * {@link #setUseRegularExpressions}.
049: * @param sql the SQL string
050: * @return the corresponding out parameter <code>Map</code>
051: */
052: public Map getOutParameter(String sql) {
053: SQLStatementMatcher matcher = new SQLStatementMatcher(
054: getCaseSensitive(), getExactMatch(),
055: getUseRegularExpressions());
056: List list = matcher.getMatchingObjects(
057: outParameterForStatement, sql, true, true);
058: if (null != list && list.size() > 0) {
059: return (Map) list.get(0);
060: }
061: return null;
062: }
063:
064: /**
065: * Returns the first out parameter <code>Map</code> that matches
066: * the specified SQL string and the specified parameters.
067: * Please note that you can modify the match parameters with
068: * {@link #setCaseSensitive}, {@link #setExactMatch} and
069: * {@link #setUseRegularExpressions} and the match parameters for the
070: * specified parameter list with {@link #setExactMatchParameter}.
071: * @param sql the SQL string
072: * @param parameters the parameters
073: * @return the corresponding out parameter <code>Map</code>
074: */
075: public Map getOutParameter(String sql, Map parameters) {
076: MockOutParameterWrapper wrapper = (MockOutParameterWrapper) getMatchingParameterWrapper(
077: sql, parameters, outParameterForStatementParameters);
078: if (null != wrapper) {
079: return wrapper.getOutParameter();
080: }
081: return null;
082: }
083:
084: /**
085: * Clears the out parameters.
086: */
087: public void clearOutParameter() {
088: outParameterForStatement.clear();
089: outParameterForStatementParameters.clear();
090: }
091:
092: /**
093: * Returns the global out parameter <code>Map</code>.
094: * @return the global out parameter <code>Map</code>
095: */
096: public Map getGlobalOutParameter() {
097: return globalOutParameter;
098: }
099:
100: /**
101: * Prepares the global out parameter <code>Map</code>.
102: * @param outParameters the global out parameter <code>Map</code>
103: */
104: public void prepareGlobalOutParameter(Map outParameters) {
105: globalOutParameter = new HashMap(outParameters);
106: }
107:
108: /**
109: * Prepare an out parameter <code>Map</code> for a specified
110: * SQL string.
111: * Please note that you can modify the match parameters with
112: * {@link #setCaseSensitive}, {@link #setExactMatch} and
113: * {@link #setUseRegularExpressions}.
114: * @param sql the SQL string
115: * @param outParameters the out parameter <code>Map</code>
116: */
117: public void prepareOutParameter(String sql, Map outParameters) {
118: outParameterForStatement.put(sql, new HashMap(outParameters));
119: }
120:
121: /**
122: * Prepare an out parameter <code>Map</code> for a specified SQL string and
123: * the specified parameters. The specified parameters array
124: * must contain the parameters in the correct order starting with index 0 for
125: * the first parameter. Please keep in mind that parameters in
126: * <code>CallableStatement</code> objects start with 1 as the first
127: * parameter. So <code>parameters[0]</code> maps to the
128: * parameter with index 1.
129: * Please note that you can modify the match parameters with
130: * {@link #setCaseSensitive}, {@link #setExactMatch} and
131: * {@link #setUseRegularExpressions} and the match parameters for the
132: * specified parameter list with {@link #setExactMatchParameter}.
133: * @param sql the SQL string
134: * @param outParameters the corresponding out parameter <code>Map</code>
135: * @param parameters the parameters
136: */
137: public void prepareOutParameter(String sql, Map outParameters,
138: Object[] parameters) {
139: prepareOutParameter(sql, outParameters, Arrays
140: .asList(parameters));
141: }
142:
143: /**
144: * Prepare an out parameter <code>Map</code> for a specified SQL string and
145: * the specified parameters. The specified parameters array
146: * must contain the parameters in the correct order starting with index 0 for
147: * the first parameter. Please keep in mind that parameters in
148: * <code>CallableStatement</code> objects start with 1 as the first
149: * parameter. So <code>parameters.get(0)</code> maps to the
150: * parameter with index 1.
151: * Please note that you can modify the match parameters with
152: * {@link #setCaseSensitive}, {@link #setExactMatch} and
153: * {@link #setUseRegularExpressions} and the match parameters for the
154: * specified parameter list with {@link #setExactMatchParameter}.
155: * @param sql the SQL string
156: * @param outParameters the corresponding out parameter <code>Map</code>
157: * @param parameters the parameters
158: */
159: public void prepareOutParameter(String sql, Map outParameters,
160: List parameters) {
161: Map params = new HashMap();
162: for (int ii = 0; ii < parameters.size(); ii++) {
163: params.put(new Integer(ii + 1), parameters.get(ii));
164: }
165: prepareOutParameter(sql, outParameters, params);
166: }
167:
168: /**
169: * Prepare an out parameter <code>Map</code> for a specified SQL string
170: * and the specified parameters. The specified parameters <code>Map</code>
171: * must contain the parameters by mapping <code>Integer</code> or
172: * <code>String</code> objects to the corresponding parameter.
173: * An <code>Integer</code> object is the index of the parameter.
174: * A <code>String</code> is the name of the parameter.
175: * Please note that you can modify the match parameters with
176: * {@link #setCaseSensitive}, {@link #setExactMatch} and
177: * {@link #setUseRegularExpressions} and the match parameters for the
178: * specified parameter list with {@link #setExactMatchParameter}.
179: * @param sql the SQL string
180: * @param outParameters the corresponding out parameter <code>Map</code>
181: * @param parameters the parameters
182: */
183: public void prepareOutParameter(String sql, Map outParameters,
184: Map parameters) {
185: List list = (List) outParameterForStatementParameters.get(sql);
186: if (null == list) {
187: list = new ArrayList();
188: outParameterForStatementParameters.put(sql, list);
189: }
190: list.add(new MockOutParameterWrapper(
191: new HashMap(outParameters), new HashMap(parameters)));
192: }
193:
194: private class MockOutParameterWrapper extends ParameterWrapper {
195: private Map outParameter;
196:
197: public MockOutParameterWrapper(Map outParameter, Map parameters) {
198: super (parameters);
199: this .outParameter = outParameter;
200: }
201:
202: public Map getOutParameter() {
203: return outParameter;
204: }
205: }
206: }
|