001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: VirtualParameters.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.database;
009:
010: import com.uwyn.rife.database.queries.Query;
011: import com.uwyn.rife.database.queries.QueryParameters;
012: import com.uwyn.rife.pcj.map.IntKeyIntMap;
013: import com.uwyn.rife.pcj.map.IntKeyMap;
014: import com.uwyn.rife.pcj.map.IntKeyOpenHashMap;
015:
016: /**
017: * Internal class to handle virtual parameters of a
018: * <code>DbPreparedStatement</code>.
019: *
020: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
021: * @version $Revision: 3634 $
022: * @since 1.0
023: */
024: public class VirtualParameters {
025: private QueryParameters mParameters = null;
026: private IntKeyIntMap mIndexMapping = null;
027: private IntKeyMap mValues = null;
028: private VirtualParametersHandler mHandler = null;
029:
030: /**
031: * Creates a new <code>VirtualParameters</code> instance.
032: *
033: * @param parameters the actual parameters that are virtual.
034: * @param handler the <code>VirtualParametersHandler</code> that will
035: * be used by the {@link #callHandler(DbPreparedStatement)} method.
036: * @since 1.0
037: */
038: public VirtualParameters(QueryParameters parameters,
039: VirtualParametersHandler handler) {
040: if (null == parameters)
041: throw new IllegalArgumentException(
042: "parameters can't be null.");
043: if (null == handler)
044: throw new IllegalArgumentException("handler can't be null.");
045:
046: mParameters = parameters;
047: mHandler = handler;
048: }
049:
050: /**
051: * Calls the registered <code>VirtualParametersHandler</code>. This is
052: * typically called when all virtual parameters have been defined in a
053: * prepared statement and the statement is ready to be executed.
054: *
055: * @param statement the prepared statement that has all the virtual
056: * parameters defined.
057: * @since 1.0
058: */
059: public void callHandler(DbPreparedStatement statement) {
060: mHandler.handleValues(statement);
061: }
062:
063: void setup(Query query) {
064: mIndexMapping = query.getParameters().getVirtualIndexMapping(
065: mParameters);
066: }
067:
068: Object getValue(int index) {
069: if (null == mValues) {
070: return null;
071: }
072:
073: return mValues.get(index);
074: }
075:
076: boolean hasValue(int index) {
077: if (null == mValues) {
078: return false;
079: }
080:
081: return mValues.containsKey(index);
082: }
083:
084: boolean hasParameter(int index) {
085: if (null == mIndexMapping) {
086: return false;
087: }
088:
089: return mIndexMapping.containsKey(index);
090: }
091:
092: int getRealIndex(int index) {
093: if (null == mIndexMapping) {
094: return -1;
095: }
096:
097: return mIndexMapping.get(index);
098: }
099:
100: void putValue(int index, Object value) {
101: if (null == mValues) {
102: mValues = new IntKeyOpenHashMap();
103: }
104:
105: mValues.put(index, value);
106: }
107: }
|