001: // jTDS JDBC Driver for Microsoft SQL Server and Sybase
002: // Copyright (C) 2004 The jTDS Project
003: //
004: // This library is free software; you can redistribute it and/or
005: // modify it under the terms of the GNU Lesser General Public
006: // License as published by the Free Software Foundation; either
007: // version 2.1 of the License, or (at your option) any later version.
008: //
009: // This library is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: // Lesser General Public License for more details.
013: //
014: // You should have received a copy of the GNU Lesser General Public
015: // License along with this library; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: //
018: package net.sourceforge.jtds.jdbc;
019:
020: import java.sql.*;
021:
022: /**
023: * jTDS implementation of <code>ParameterMetaData</code>.
024: * <p/>
025: * For Sybase it is usually possible to obtain true parameter data for prepared
026: * statements. For Microsoft just use information determined from the actual
027: * parameters if set or return some reasonable defaults otherwise.
028: *
029: * @author Brian Heineman
030: * @author Mike Hutchinson
031: * @version $Id: ParameterMetaDataImpl.java,v 1.7 2005/09/21 21:50:34 ddkilzer Exp $
032: */
033: public class ParameterMetaDataImpl implements ParameterMetaData {
034: private final ParamInfo[] parameterList;
035: private final int maxPrecision;
036: private final boolean useLOBs;
037:
038: public ParameterMetaDataImpl(ParamInfo[] parameterList,
039: ConnectionJDBC2 connection) {
040: if (parameterList == null) {
041: parameterList = new ParamInfo[0];
042: }
043:
044: this .parameterList = parameterList;
045: this .maxPrecision = connection.getMaxPrecision();
046: this .useLOBs = connection.getUseLOBs();
047: }
048:
049: public int getParameterCount() throws SQLException {
050: return parameterList.length;
051: }
052:
053: public int isNullable(int param) throws SQLException {
054: return ParameterMetaData.parameterNullableUnknown;
055: }
056:
057: public int getParameterType(int param) throws SQLException {
058: if (useLOBs) {
059: return getParameter(param).jdbcType;
060: } else {
061: return Support.convertLOBType(getParameter(param).jdbcType);
062: }
063: }
064:
065: public int getScale(int param) throws SQLException {
066: ParamInfo pi = getParameter(param);
067:
068: return (pi.scale >= 0) ? pi.scale : 0;
069: }
070:
071: public boolean isSigned(int param) throws SQLException {
072: ParamInfo pi = getParameter(param);
073:
074: switch (pi.jdbcType) {
075: case java.sql.Types.BIGINT:
076: case java.sql.Types.DECIMAL:
077: case java.sql.Types.DOUBLE:
078: case java.sql.Types.FLOAT:
079: case java.sql.Types.INTEGER:
080: case java.sql.Types.SMALLINT:
081: case java.sql.Types.REAL:
082: case java.sql.Types.NUMERIC:
083: return true;
084: default:
085: return false;
086: }
087: }
088:
089: public int getPrecision(int param) throws SQLException {
090: ParamInfo pi = getParameter(param);
091:
092: return (pi.precision >= 0) ? pi.precision : maxPrecision;
093: }
094:
095: public String getParameterTypeName(int param) throws SQLException {
096: return getParameter(param).sqlType;
097: }
098:
099: public String getParameterClassName(int param) throws SQLException {
100: return Support.getClassName(getParameterType(param));
101: }
102:
103: public int getParameterMode(int param) throws SQLException {
104: ParamInfo pi = getParameter(param);
105:
106: if (pi.isOutput) {
107: return pi.isSet ? parameterModeInOut : parameterModeOut;
108: }
109:
110: return pi.isSet ? parameterModeIn : parameterModeUnknown;
111: }
112:
113: private ParamInfo getParameter(int param) throws SQLException {
114: if (param < 1 || param > parameterList.length) {
115: throw new SQLException(Messages
116: .get("error.prepare.paramindex", Integer
117: .toString(param)), "07009");
118: }
119:
120: return parameterList[param - 1];
121: }
122: }
|