package org.postgresql.jdbc;
import org.postgresql.core.BaseConnection;
import org.postgresql.util.GT;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;
import java.sql.ParameterMetaData;
import java.sql.SQLException;
public class PgParameterMetaData implements ParameterMetaData {
private final BaseConnection connection;
private final int[] oids;
public PgParameterMetaData(BaseConnection connection, int[] oids) {
this.connection = connection;
this.oids = oids;
}
@Override
public String getParameterClassName(int param) throws SQLException {
checkParamIndex(param);
return connection.getTypeInfo().getJavaClass(oids[param - 1]);
}
@Override
public int getParameterCount() {
return oids.length;
}
public int getParameterMode(int param) throws SQLException {
checkParamIndex(param);
return ParameterMetaData.parameterModeIn;
}
@Override
public int getParameterType(int param) throws SQLException {
checkParamIndex(param);
return connection.getTypeInfo().getSQLType(oids[param - 1]);
}
@Override
public String getParameterTypeName(int param) throws SQLException {
checkParamIndex(param);
return connection.getTypeInfo().getPGType(oids[param - 1]);
}
public int getPrecision(int param) throws SQLException {
checkParamIndex(param);
return 0;
}
public int getScale(int param) throws SQLException {
checkParamIndex(param);
return 0;
}
public int isNullable(int param) throws SQLException {
checkParamIndex(param);
return ParameterMetaData.parameterNullableUnknown;
}
@Override
public boolean isSigned(int param) throws SQLException {
checkParamIndex(param);
return connection.getTypeInfo().isSigned(oids[param - 1]);
}
private void checkParamIndex(int param) throws PSQLException {
if (param < 1 || param > oids.length) {
throw new PSQLException(
GT.tr("The parameter index is out of range: {0}, number of parameters: {1}.",
param, oids.length),
PSQLState.INVALID_PARAMETER_VALUE);
}
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return iface.isAssignableFrom(getClass());
}
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface.isAssignableFrom(getClass())) {
return iface.cast(this);
}
throw new SQLException("Cannot unwrap to " + iface.getName());
}
}