001: /*
002:
003: Derby - Class org.apache.derby.impl.jdbc.EmbedParameterSetMetaData
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.jdbc;
023:
024: import org.apache.derby.iapi.sql.Activation;
025: import org.apache.derby.iapi.sql.ParameterValueSet;
026: import org.apache.derby.iapi.types.DataTypeDescriptor;
027: import org.apache.derby.iapi.types.DataTypeUtilities;
028: import org.apache.derby.iapi.reference.JDBC30Translation;
029: import org.apache.derby.iapi.reference.SQLState;
030: import org.apache.derby.iapi.jdbc.EngineParameterMetaData;
031:
032: import java.sql.SQLException;
033: import java.sql.Types;
034:
035: /**
036: * This class immitates to implement the ParameterMetaData interface from JDBC3.0
037: * We want to provide the functionality to JDKs before JDBC3.0. We put it here
038: * instead of in Local20 because we want to make it available for CallableStatement.
039: * It provides the parameter meta data for callable & prepared statements.
040: * The subclass in Local30 actually implements ParameterMetaData interface.
041: *
042: * For use of ParameterMetaData functionality in network server, please do not use
043: * this class directly. Instead use the method available on EnginePreparedStatement
044: * @see org.apache.derby.iapi.jdbc.EngineParameterMetaData
045: * @see org.apache.derby.iapi.jdbc.EnginePreparedStatement
046: */
047: public class EmbedParameterSetMetaData implements
048: EngineParameterMetaData {
049:
050: private final ParameterValueSet pvs;
051: private final DataTypeDescriptor[] types;
052: private final int paramCount;
053:
054: //////////////////////////////////////////////////////////////
055: //
056: // CONSTRUCTORS
057: //
058: //////////////////////////////////////////////////////////////
059: protected EmbedParameterSetMetaData(ParameterValueSet pvs,
060: DataTypeDescriptor[] types) {
061: int paramCount;
062: paramCount = pvs.getParameterCount();
063: this .pvs = pvs;
064: this .paramCount = paramCount;
065: this .types = types;
066: }
067:
068: /**
069: *
070: * Retrieves the number of parameters in the PreparedStatement object for which
071: * this ParameterMetaData object contains information.
072: *
073: * @return the number of parameters
074: */
075: public int getParameterCount() {
076: return paramCount;
077: }
078:
079: /**
080: *
081: * Retrieves whether null values are allowed in the designated parameter.
082: *
083: * @param param - the first parameter is 1, the second is 2, ...
084: * @return the nullability status of the given parameter; one of
085: * ParameterMetaData.parameterNoNulls, ParameterMetaData.parameterNullable, or
086: * ParameterMetaData.parameterNullableUnknown
087: * @exception SQLException if a database access error occurs
088: */
089: public int isNullable(int param) throws SQLException {
090: checkPosition(param);
091:
092: if (types[param - 1].isNullable())
093: return JDBC30Translation.PARAMETER_NULLABLE;
094: else
095: return JDBC30Translation.PARAMETER_NO_NULLS;
096: }
097:
098: /**
099: *
100: * Retrieves whether values for the designated parameter can be signed numbers.
101: *
102: * @param param - the first parameter is 1, the second is 2, ...
103: * @return true if it can be signed numbers
104: * @exception SQLException if a database access error occurs
105: */
106: public boolean isSigned(int param) throws SQLException {
107: checkPosition(param);
108:
109: return types[param - 1].getTypeId().isNumericTypeId();
110: }
111:
112: /**
113: *
114: * Retrieves the designated parameter's number of decimal digits.
115: *
116: * @param param - the first parameter is 1, the second is 2, ...
117: * @return precision
118: * @exception SQLException if a database access error occurs
119: */
120: public int getPrecision(int param) throws SQLException {
121: checkPosition(param);
122:
123: int outparamPrecision = -1;
124: int precision = DataTypeUtilities
125: .getPrecision(types[param - 1]);
126:
127: if (((param == 1) && pvs.hasReturnOutputParameter())) {
128: outparamPrecision = pvs.getPrecision(param);
129: }
130:
131: return (outparamPrecision == -1) ? precision
132: : outparamPrecision;
133:
134: }
135:
136: /**
137: *
138: * Retrieves the designated parameter's number of digits to right of the decimal point.
139: *
140: * @param param - the first parameter is 1, the second is 2, ...
141: * @return scale
142: * @exception SQLException if a database access error occurs
143: */
144: public int getScale(int param) throws SQLException {
145: checkPosition(param);
146:
147: if (((param == 1) && pvs.hasReturnOutputParameter()))
148: return pvs.getScale(param);
149: return types[param - 1].getScale();
150:
151: }
152:
153: /**
154: *
155: * Retrieves the designated parameter's SQL type.
156: *
157: * @param param - the first parameter is 1, the second is 2, ...
158: * @return SQL type from java.sql.Types
159: * @exception SQLException if a database access error occurs
160: */
161: public int getParameterType(int param) throws SQLException {
162: checkPosition(param);
163:
164: return types[param - 1].getTypeId().getJDBCTypeId();
165: }
166:
167: /**
168: *
169: * Retrieves the designated parameter's database-specific type name.
170: *
171: * @param param - the first parameter is 1, the second is 2, ...
172: * @return type the name used by the database. If the parameter
173: * type is a user-defined type, then a fully-qualified type name is returned.
174: * @exception SQLException if a database access error occurs
175: */
176: public String getParameterTypeName(int param) throws SQLException {
177: checkPosition(param);
178:
179: return types[param - 1].getTypeId().getSQLTypeName();
180: }
181:
182: /**
183: *
184: * Retrieves the fully-qualified name of the Java class whose instances should be
185: * passed to the method PreparedStatement.setObject.
186: *
187: * @param param - the first parameter is 1, the second is 2, ...
188: * @return the fully-qualified name of the class in the Java
189: * programming language that would be used by the method
190: * PreparedStatement.setObject to set the value in the specified parameter.
191: * This is the class name used for custom mapping.
192: * @exception SQLException if a database access error occurs
193: */
194: public String getParameterClassName(int param) throws SQLException {
195: checkPosition(param);
196:
197: return types[param - 1].getTypeId()
198: .getResultSetMetaDataTypeName();
199: }
200:
201: /**
202: *
203: * Retrieves the designated parameter's mode.
204: *
205: * @param param - the first parameter is 1, the second is 2, ...
206: * @return mode of the parameter; one of ParameterMetaData.parameterModeIn,
207: * ParameterMetaData.parameterModeOut, or ParameterMetaData.parameterModeInOut
208: * ParameterMetaData.parameterModeUnknown.
209: * @exception SQLException if a database access error occurs
210: */
211: public int getParameterMode(int param) throws SQLException {
212: checkPosition(param);
213:
214: //bug 4857 - only the return parameter is of type OUT. All the other output
215: //parameter are IN_OUT (it doesn't matter if their value is set or not).
216: if ((param == 1) && pvs.hasReturnOutputParameter())//only the first parameter can be of return type
217: return JDBC30Translation.PARAMETER_MODE_OUT;
218: return pvs.getParameterMode(param);
219: }
220:
221: // Check the position number for a parameter and throw an exception if
222: // it is out of range.
223: private void checkPosition(int parameterIndex) throws SQLException {
224: /* Check that the parameterIndex is in range. */
225: if (parameterIndex < 1 || parameterIndex > paramCount) {
226:
227: /* This message matches the one used by the DBMS */
228: throw Util.generateCsSQLException(
229: SQLState.LANG_INVALID_PARAM_POSITION, new Integer(
230: parameterIndex), new Integer(paramCount));
231: }
232: }
233: }
|