| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.jdbc; |
| 7 |
|
| 8 |
import java.sql.ParameterMetaData; |
| 9 |
import java.sql.SQLException; |
| 10 |
import java.util.ArrayList; |
| 11 |
import org.h2.command.CommandInterface; |
| 12 |
import org.h2.expression.ParameterInterface; |
| 13 |
import org.h2.message.DbException; |
| 14 |
import org.h2.message.Trace; |
| 15 |
import org.h2.message.TraceObject; |
| 16 |
import org.h2.util.MathUtils; |
| 17 |
import org.h2.value.DataType; |
| 18 |
import org.h2.value.TypeInfo; |
| 19 |
import org.h2.value.Value; |
| 20 |
import org.h2.value.ValueToObjectConverter; |
| 21 |
|
| 22 |
/** |
| 23 |
* Information about the parameters of a prepared statement. |
| 24 |
*/ |
| 25 |
public final class JdbcParameterMetaData extends TraceObject implements ParameterMetaData { |
| 26 |
|
| 27 |
private final JdbcPreparedStatement prep; |
| 28 |
private final int paramCount; |
| 29 |
private final ArrayList<? extends ParameterInterface> parameters; |
| 30 |
|
| 31 |
JdbcParameterMetaData(Trace trace, JdbcPreparedStatement prep, |
| 32 |
CommandInterface command, int id) { |
| 33 |
setTrace(trace, TraceObject.PARAMETER_META_DATA, id); |
| 34 |
this.prep = prep; |
| 35 |
this.parameters = command.getParameters(); |
| 36 |
this.paramCount = parameters.size(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns the number of parameters. |
| 41 |
* |
| 42 |
* @return the number |
| 43 |
*/ |
| 44 |
@Override |
| 45 |
public int getParameterCount() throws SQLException { |
| 46 |
try { |
| 47 |
debugCodeCall("getParameterCount"); |
| 48 |
checkClosed(); |
| 49 |
return paramCount; |
| 50 |
} catch (Exception e) { |
| 51 |
throw logAndConvert(e); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns the parameter mode. |
| 57 |
* Always returns parameterModeIn. |
| 58 |
* |
| 59 |
* @param param the column index (1,2,...) |
| 60 |
* @return parameterModeIn |
| 61 |
*/ |
| 62 |
@Override |
| 63 |
public int getParameterMode(int param) throws SQLException { |
| 64 |
try { |
| 65 |
debugCodeCall("getParameterMode", param); |
| 66 |
getParameter(param); |
| 67 |
return parameterModeIn; |
| 68 |
} catch (Exception e) { |
| 69 |
throw logAndConvert(e); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Returns the parameter type. |
| 75 |
* java.sql.Types.VARCHAR is returned if the data type is not known. |
| 76 |
* |
| 77 |
* @param param the column index (1,2,...) |
| 78 |
* @return the data type |
| 79 |
*/ |
| 80 |
@Override |
| 81 |
public int getParameterType(int param) throws SQLException { |
| 82 |
try { |
| 83 |
debugCodeCall("getParameterType", param); |
| 84 |
TypeInfo type = getParameter(param).getType(); |
| 85 |
if (type.getValueType() == Value.UNKNOWN) { |
| 86 |
type = TypeInfo.TYPE_VARCHAR; |
| 87 |
} |
| 88 |
return DataType.convertTypeToSQLType(type); |
| 89 |
} catch (Exception e) { |
| 90 |
throw logAndConvert(e); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns the parameter precision. |
| 96 |
* The value 0 is returned if the precision is not known. |
| 97 |
* |
| 98 |
* @param param the column index (1,2,...) |
| 99 |
* @return the precision |
| 100 |
*/ |
| 101 |
@Override |
| 102 |
public int getPrecision(int param) throws SQLException { |
| 103 |
try { |
| 104 |
debugCodeCall("getPrecision", param); |
| 105 |
TypeInfo type = getParameter(param).getType(); |
| 106 |
return type.getValueType() == Value.UNKNOWN ? 0 : MathUtils.convertLongToInt(type.getPrecision()); |
| 107 |
} catch (Exception e) { |
| 108 |
throw logAndConvert(e); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Returns the parameter scale. |
| 114 |
* The value 0 is returned if the scale is not known. |
| 115 |
* |
| 116 |
* @param param the column index (1,2,...) |
| 117 |
* @return the scale |
| 118 |
*/ |
| 119 |
@Override |
| 120 |
public int getScale(int param) throws SQLException { |
| 121 |
try { |
| 122 |
debugCodeCall("getScale", param); |
| 123 |
TypeInfo type = getParameter(param).getType(); |
| 124 |
return type.getValueType() == Value.UNKNOWN ? 0 : type.getScale(); |
| 125 |
} catch (Exception e) { |
| 126 |
throw logAndConvert(e); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Checks if this is nullable parameter. |
| 132 |
* Returns ResultSetMetaData.columnNullableUnknown.. |
| 133 |
* |
| 134 |
* @param param the column index (1,2,...) |
| 135 |
* @return ResultSetMetaData.columnNullableUnknown |
| 136 |
*/ |
| 137 |
@Override |
| 138 |
public int isNullable(int param) throws SQLException { |
| 139 |
try { |
| 140 |
debugCodeCall("isNullable", param); |
| 141 |
return getParameter(param).getNullable(); |
| 142 |
} catch (Exception e) { |
| 143 |
throw logAndConvert(e); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Checks if this parameter is signed. |
| 149 |
* It always returns true. |
| 150 |
* |
| 151 |
* @param param the column index (1,2,...) |
| 152 |
* @return true |
| 153 |
*/ |
| 154 |
@Override |
| 155 |
public boolean isSigned(int param) throws SQLException { |
| 156 |
try { |
| 157 |
debugCodeCall("isSigned", param); |
| 158 |
getParameter(param); |
| 159 |
return true; |
| 160 |
} catch (Exception e) { |
| 161 |
throw logAndConvert(e); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Returns the Java class name of the parameter. |
| 167 |
* "java.lang.String" is returned if the type is not known. |
| 168 |
* |
| 169 |
* @param param the column index (1,2,...) |
| 170 |
* @return the Java class name |
| 171 |
*/ |
| 172 |
@Override |
| 173 |
public String getParameterClassName(int param) throws SQLException { |
| 174 |
try { |
| 175 |
debugCodeCall("getParameterClassName", param); |
| 176 |
int type = getParameter(param).getType().getValueType(); |
| 177 |
if (type == Value.UNKNOWN) { |
| 178 |
type = Value.VARCHAR; |
| 179 |
} |
| 180 |
return ValueToObjectConverter.getDefaultClass(type, true).getName(); |
| 181 |
} catch (Exception e) { |
| 182 |
throw logAndConvert(e); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Returns the parameter type name. |
| 188 |
* "VARCHAR" is returned if the type is not known. |
| 189 |
* |
| 190 |
* @param param the column index (1,2,...) |
| 191 |
* @return the type name |
| 192 |
*/ |
| 193 |
@Override |
| 194 |
public String getParameterTypeName(int param) throws SQLException { |
| 195 |
try { |
| 196 |
debugCodeCall("getParameterTypeName", param); |
| 197 |
TypeInfo type = getParameter(param).getType(); |
| 198 |
if (type.getValueType() == Value.UNKNOWN) { |
| 199 |
type = TypeInfo.TYPE_VARCHAR; |
| 200 |
} |
| 201 |
return type.getDeclaredTypeName(); |
| 202 |
} catch (Exception e) { |
| 203 |
throw logAndConvert(e); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
private ParameterInterface getParameter(int param) { |
| 208 |
checkClosed(); |
| 209 |
if (param < 1 || param > paramCount) { |
| 210 |
throw DbException.getInvalidValueException("param", param); |
| 211 |
} |
| 212 |
return parameters.get(param - 1); |
| 213 |
} |
| 214 |
|
| 215 |
private void checkClosed() { |
| 216 |
prep.checkClosed(); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Return an object of this class if possible. |
| 221 |
* |
| 222 |
* @param iface the class |
| 223 |
* @return this |
| 224 |
*/ |
| 225 |
@Override |
| 226 |
@SuppressWarnings("unchecked") |
| 227 |
public <T> T unwrap(Class<T> iface) throws SQLException { |
| 228 |
try { |
| 229 |
if (isWrapperFor(iface)) { |
| 230 |
return (T) this; |
| 231 |
} |
| 232 |
throw DbException.getInvalidValueException("iface", iface); |
| 233 |
} catch (Exception e) { |
| 234 |
throw logAndConvert(e); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Checks if unwrap can return an object of this class. |
| 240 |
* |
| 241 |
* @param iface the class |
| 242 |
* @return whether or not the interface is assignable from this class |
| 243 |
*/ |
| 244 |
@Override |
| 245 |
public boolean isWrapperFor(Class<?> iface) throws SQLException { |
| 246 |
return iface != null && iface.isAssignableFrom(getClass()); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* INTERNAL |
| 251 |
*/ |
| 252 |
@Override |
| 253 |
public String toString() { |
| 254 |
return getTraceObjectName() + ": parameterCount=" + paramCount; |
| 255 |
} |
| 256 |
|
| 257 |
} |