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.ResultSetMetaData;
021: import java.sql.SQLException;
022:
023: /**
024: * jTDS implementation of the java.sql.ResultSetMetaData interface.
025: * <p>
026: * Implementation notes:
027: * <ol>
028: * <li>New simple implementation required by the new column info structure.
029: * <li>Unlike the equivalent in the older jTDS, this version is generic and does
030: * not need to know details of the TDS protocol.
031: * </ol>
032: *
033: * @author Mike Hutchinson
034: * @version $Id: JtdsResultSetMetaData.java,v 1.9 2007/07/08 18:53:30 bheineman Exp $
035: */
036: public class JtdsResultSetMetaData implements ResultSetMetaData {
037: private final ColInfo[] columns;
038: private final int columnCount;
039: private final boolean useLOBs;
040:
041: /**
042: * Construct ResultSetMetaData object over the current ColInfo array.
043: *
044: * @param columns The current ColInfo row descriptor array.
045: * @param columnCount The number of visible columns.
046: */
047: JtdsResultSetMetaData(ColInfo[] columns, int columnCount,
048: boolean useLOBs) {
049: this .columns = columns;
050: this .columnCount = columnCount;
051: this .useLOBs = useLOBs;
052: }
053:
054: /**
055: * Return the column descriptor given a column index.
056: *
057: * @param column The column index (from 1 .. n).
058: * @return The column descriptor as a <code>ColInfo<code>.
059: * @throws SQLException
060: */
061: ColInfo getColumn(int column) throws SQLException {
062: if (column < 1 || column > columnCount) {
063: throw new SQLException(Messages.get(
064: "error.resultset.colindex", Integer
065: .toString(column)), "07009");
066: }
067:
068: return columns[column - 1];
069: }
070:
071: // ------ java.sql.ResultSetMetaData methods follow -------
072:
073: public int getColumnCount() throws SQLException {
074: return this .columnCount;
075: }
076:
077: public int getColumnDisplaySize(int column) throws SQLException {
078: return getColumn(column).displaySize;
079: }
080:
081: public int getColumnType(int column) throws SQLException {
082: if (useLOBs) {
083: return getColumn(column).jdbcType;
084: } else {
085: return Support.convertLOBType(getColumn(column).jdbcType);
086: }
087: }
088:
089: public int getPrecision(int column) throws SQLException {
090: return getColumn(column).precision;
091: }
092:
093: public int getScale(int column) throws SQLException {
094: return getColumn(column).scale;
095: }
096:
097: public int isNullable(int column) throws SQLException {
098: return getColumn(column).nullable;
099: }
100:
101: public boolean isAutoIncrement(int column) throws SQLException {
102: return getColumn(column).isIdentity;
103: }
104:
105: public boolean isCaseSensitive(int column) throws SQLException {
106: return getColumn(column).isCaseSensitive;
107: }
108:
109: public boolean isCurrency(int column) throws SQLException {
110: return TdsData.isCurrency(getColumn(column));
111: }
112:
113: public boolean isDefinitelyWritable(int column) throws SQLException {
114: getColumn(column);
115:
116: return false;
117: }
118:
119: public boolean isReadOnly(int column) throws SQLException {
120: return !getColumn(column).isWriteable;
121: }
122:
123: public boolean isSearchable(int column) throws SQLException {
124: return TdsData.isSearchable(getColumn(column));
125: }
126:
127: public boolean isSigned(int column) throws SQLException {
128: return TdsData.isSigned(getColumn(column));
129: }
130:
131: public boolean isWritable(int column) throws SQLException {
132: return getColumn(column).isWriteable;
133: }
134:
135: public String getCatalogName(int column) throws SQLException {
136: ColInfo col = getColumn(column);
137:
138: return (col.catalog == null) ? "" : col.catalog;
139: }
140:
141: public String getColumnClassName(int column) throws SQLException {
142: String c = Support.getClassName(getColumnType(column));
143:
144: if (!useLOBs) {
145: if ("java.sql.Clob".equals(c)) {
146: return "java.lang.String";
147: }
148:
149: if ("java.sql.Blob".equals(c)) {
150: return "[B";
151: }
152: }
153:
154: return c;
155: }
156:
157: public String getColumnLabel(int column) throws SQLException {
158: return getColumn(column).name;
159: }
160:
161: public String getColumnName(int column) throws SQLException {
162: return getColumn(column).name;
163: }
164:
165: public String getColumnTypeName(int column) throws SQLException {
166: return getColumn(column).sqlType;
167: }
168:
169: public String getSchemaName(int column) throws SQLException {
170: ColInfo col = getColumn(column);
171:
172: return (col.schema == null) ? "" : col.schema;
173: }
174:
175: public String getTableName(int column) throws SQLException {
176: ColInfo col = getColumn(column);
177:
178: return (col.tableName == null) ? "" : col.tableName;
179: }
180: }
|