001: /*-------------------------------------------------------------------------
002: *
003: * Copyright (c) 2004-2005, PostgreSQL Global Development Group
004: * Copyright (c) 2004, Open Cloud Limited.
005: *
006: * IDENTIFICATION
007: * $PostgreSQL: pgjdbc/org/postgresql/core/v3/CompositeParameterList.java,v 1.9 2006/05/22 09:52:37 jurka Exp $
008: *
009: *-------------------------------------------------------------------------
010: */
011: package org.postgresql.core.v3;
012:
013: import org.postgresql.core.*;
014: import org.postgresql.util.PSQLException;
015: import org.postgresql.util.PSQLState;
016: import org.postgresql.util.GT;
017:
018: import java.sql.SQLException;
019: import java.io.InputStream;
020:
021: /**
022: * Parameter list for V3 query strings that contain multiple statements.
023: * We delegate to one SimpleParameterList per statement, and translate
024: * parameter indexes as needed.
025: *
026: * @author Oliver Jowett (oliver@opencloud.com)
027: */
028: class CompositeParameterList implements V3ParameterList {
029: CompositeParameterList(SimpleParameterList[] subparams,
030: int[] offsets) {
031: this .subparams = subparams;
032: this .offsets = offsets;
033: this .total = offsets[offsets.length - 1]
034: + subparams[offsets.length - 1].getInParameterCount();
035: }
036:
037: private final int findSubParam(int index) throws SQLException {
038: if (index < 1 || index > total)
039: throw new PSQLException(
040: GT
041: .tr(
042: "The column index is out of range: {0}, number of columns: {1}.",
043: new Object[] { new Integer(index),
044: new Integer(total) }),
045: PSQLState.INVALID_PARAMETER_VALUE);
046:
047: for (int i = offsets.length - 1; i >= 0; --i)
048: if (offsets[i] < index)
049: return i;
050:
051: throw new IllegalArgumentException(
052: "I am confused; can't find a subparam for index "
053: + index);
054: }
055:
056: public void registerOutParameter(int index, int sqlType) {
057:
058: }
059:
060: public int getDirection(int i) {
061: return 0;
062: }
063:
064: public int getParameterCount() {
065: return total;
066: }
067:
068: public int getInParameterCount() {
069: return total;
070: }
071:
072: public int getOutParameterCount() {
073: return 0;
074: }
075:
076: public int[] getTypeOIDs() {
077: int oids[] = new int[total];
078: for (int i = 0; i < offsets.length; i++) {
079: int subOids[] = subparams[i].getTypeOIDs();
080: System.arraycopy(subOids, 0, oids, offsets[i],
081: subOids.length);
082: }
083: return oids;
084: }
085:
086: public void setIntParameter(int index, int value)
087: throws SQLException {
088: int sub = findSubParam(index);
089: subparams[sub].setIntParameter(index - offsets[sub], value);
090: }
091:
092: public void setLiteralParameter(int index, String value, int oid)
093: throws SQLException {
094: int sub = findSubParam(index);
095: subparams[sub].setStringParameter(index - offsets[sub], value,
096: oid);
097: }
098:
099: public void setStringParameter(int index, String value, int oid)
100: throws SQLException {
101: int sub = findSubParam(index);
102: subparams[sub].setStringParameter(index - offsets[sub], value,
103: oid);
104: }
105:
106: public void setBytea(int index, byte[] data, int offset, int length)
107: throws SQLException {
108: int sub = findSubParam(index);
109: subparams[sub].setBytea(index - offsets[sub], data, offset,
110: length);
111: }
112:
113: public void setBytea(int index, InputStream stream, int length)
114: throws SQLException {
115: int sub = findSubParam(index);
116: subparams[sub].setBytea(index - offsets[sub], stream, length);
117: }
118:
119: public void setNull(int index, int oid) throws SQLException {
120: int sub = findSubParam(index);
121: subparams[sub].setNull(index - offsets[sub], oid);
122: }
123:
124: public String toString(int index) {
125: try {
126: int sub = findSubParam(index);
127: return subparams[sub].toString(index - offsets[sub]);
128: } catch (SQLException e) {
129: throw new IllegalStateException(e.getMessage());
130: }
131: }
132:
133: public ParameterList copy() {
134: SimpleParameterList[] copySub = new SimpleParameterList[subparams.length];
135: for (int sub = 0; sub < subparams.length; ++sub)
136: copySub[sub] = (SimpleParameterList) subparams[sub].copy();
137:
138: return new CompositeParameterList(copySub, offsets);
139: }
140:
141: public void clear() {
142: for (int sub = 0; sub < subparams.length; ++sub) {
143: subparams[sub].clear();
144: }
145: }
146:
147: public SimpleParameterList[] getSubparams() {
148: return subparams;
149: }
150:
151: public void checkAllParametersSet() throws SQLException {
152: for (int sub = 0; sub < subparams.length; ++sub)
153: subparams[sub].checkAllParametersSet();
154: }
155:
156: private final int total;
157: private final SimpleParameterList[] subparams;
158: private final int[] offsets;
159: }
|