001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.jdbc;
007:
008: import java.sql.ParameterMetaData;
009: import java.sql.ResultSetMetaData;
010: import java.sql.SQLException;
011: import java.sql.Types;
012:
013: import org.h2.command.CommandInterface;
014: import org.h2.engine.SessionInterface;
015: import org.h2.message.Message;
016: import org.h2.message.TraceObject;
017:
018: /**
019: * Information about the parameters of a prepared statement.
020: */
021: public class JdbcParameterMetaData extends TraceObject
022: //#ifdef JDK14
023: implements ParameterMetaData
024: //#endif
025: {
026:
027: private JdbcPreparedStatement prep;
028: private int paramCount;
029:
030: /**
031: * Returns the number of parameters.
032: *
033: * @return the number
034: */
035: public int getParameterCount() throws SQLException {
036: try {
037: debugCodeCall("getParameterCount");
038: checkClosed();
039: return paramCount;
040: } catch (Throwable e) {
041: throw logAndConvert(e);
042: }
043: }
044:
045: /**
046: * Returns the parameter mode.
047: * Always returns parameterModeIn
048: *
049: * @return parameterModeIn
050: */
051: //#ifdef JDK14
052: public int getParameterMode(int param) throws SQLException {
053: try {
054: debugCodeCall("getParameterMode", param);
055: checkParameterIndex(param);
056: return parameterModeIn;
057: } catch (Throwable e) {
058: throw logAndConvert(e);
059: }
060: }
061:
062: //#endif
063:
064: /**
065: * Returns the parameter type.
066: * Always returns Types.VARCHAR as everything can be passed as a VARCHAR.
067: *
068: * @return Types.VARCHAR
069: */
070: public int getParameterType(int param) throws SQLException {
071: try {
072: debugCodeCall("getParameterType", param);
073: checkParameterIndex(param);
074: return Types.VARCHAR;
075: } catch (Throwable e) {
076: throw logAndConvert(e);
077: }
078: }
079:
080: /**
081: * Returns the parameter precision.
082: * Always returns 0.
083: *
084: * @return 0
085: */
086: public int getPrecision(int param) throws SQLException {
087: try {
088: debugCodeCall("getPrecision", param);
089: checkParameterIndex(param);
090: return 0;
091: } catch (Throwable e) {
092: throw logAndConvert(e);
093: }
094: }
095:
096: /**
097: * Returns the parameter precision.
098: * Always returns 0.
099: *
100: * @return 0
101: */
102: public int getScale(int param) throws SQLException {
103: try {
104: debugCodeCall("getScale", param);
105: checkParameterIndex(param);
106: return 0;
107: } catch (Throwable e) {
108: throw logAndConvert(e);
109: }
110: }
111:
112: /**
113: * Checks if this is nullable parameter.
114: * Returns ResultSetMetaData.columnNullableUnknown..
115: *
116: * @return ResultSetMetaData.columnNullableUnknown
117: */
118: public int isNullable(int param) throws SQLException {
119: try {
120: debugCodeCall("isNullable", param);
121: checkParameterIndex(param);
122: return ResultSetMetaData.columnNullableUnknown;
123: } catch (Throwable e) {
124: throw logAndConvert(e);
125: }
126: }
127:
128: /**
129: * Checks if this parameter is signed.
130: * It always returns true.
131: *
132: * @return true
133: */
134: public boolean isSigned(int param) throws SQLException {
135: try {
136: debugCodeCall("isSigned", param);
137: checkParameterIndex(param);
138: return true;
139: } catch (Throwable e) {
140: throw logAndConvert(e);
141: }
142: }
143:
144: /**
145: * Returns the parameter class name.
146: * Always returns java.lang.String.
147: *
148: * @return "java.lang.String"
149: */
150: public String getParameterClassName(int param) throws SQLException {
151: try {
152: debugCodeCall("getParameterClassName", param);
153: checkParameterIndex(param);
154: return String.class.getName();
155: } catch (Throwable e) {
156: throw logAndConvert(e);
157: }
158: }
159:
160: /**
161: * Returns the parameter type name.
162: * Always returns VARCHAR.
163: *
164: * @return "VARCHAR"
165: */
166: public String getParameterTypeName(int param) throws SQLException {
167: try {
168: debugCodeCall("getParameterTypeName", param);
169: checkParameterIndex(param);
170: return "VARCHAR";
171: } catch (Throwable e) {
172: throw logAndConvert(e);
173: }
174: }
175:
176: JdbcParameterMetaData(SessionInterface session,
177: JdbcPreparedStatement prep, CommandInterface command, int id) {
178: setTrace(session.getTrace(), TraceObject.PARAMETER_META_DATA,
179: id);
180: this .prep = prep;
181: this .paramCount = command.getParameters().size();
182: }
183:
184: void checkParameterIndex(int param) throws SQLException {
185: checkClosed();
186: if (param < 1 || param > paramCount) {
187: throw Message.getInvalidValueException("" + param, "param");
188: }
189: }
190:
191: void checkClosed() throws SQLException {
192: prep.checkClosed();
193: }
194:
195: /**
196: * [Not supported] Return an object of this class if possible.
197: */
198: //#ifdef JDK16
199: /*
200: public <T> T unwrap(Class<T> iface) throws SQLException {
201: debugCodeCall("unwrap");
202: throw Message.getUnsupportedException();
203: }
204: */
205: //#endif
206: /**
207: * [Not supported] Checks if unwrap can return an object of this class.
208: */
209: //#ifdef JDK16
210: /*
211: public boolean isWrapperFor(Class< ? > iface) throws SQLException {
212: debugCodeCall("isWrapperFor");
213: throw Message.getUnsupportedException();
214: }
215: */
216: //#endif
217: /**
218: * INTERNAL
219: */
220: public String toString() {
221: return getTraceObjectName() + ": parameterCount=" + paramCount;
222: }
223:
224: }
|