|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigquery.jdbc; |
| 18 | + |
| 19 | +import com.google.cloud.bigquery.StandardSQLTypeName; |
| 20 | +import com.google.cloud.bigquery.exception.BigQueryJdbcException; |
| 21 | +import java.sql.ParameterMetaData; |
| 22 | +import java.sql.SQLException; |
| 23 | +import java.sql.Types; |
| 24 | + |
| 25 | +/** ParameterMetaData implementation for BigQuery JDBC PreparedStatement parameters. */ |
| 26 | +class BigQueryParameterMetaData implements ParameterMetaData { |
| 27 | + private final int parameterCount; |
| 28 | + private final BigQueryParameterHandler parameterHandler; |
| 29 | + |
| 30 | + BigQueryParameterMetaData(int parameterCount, BigQueryParameterHandler parameterHandler) { |
| 31 | + this.parameterCount = parameterCount; |
| 32 | + this.parameterHandler = parameterHandler; |
| 33 | + } |
| 34 | + |
| 35 | + private void checkValidIndex(int param) throws SQLException { |
| 36 | + if (param < 1 || param > this.parameterCount) { |
| 37 | + throw new BigQueryJdbcException("Invalid parameter index: " + param); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public int getParameterCount() { |
| 43 | + return this.parameterCount; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public int isNullable(int param) throws SQLException { |
| 48 | + checkValidIndex(param); |
| 49 | + return parameterNullable; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean isSigned(int param) throws SQLException { |
| 54 | + int type = getParameterType(param); |
| 55 | + return type == Types.SMALLINT |
| 56 | + || type == Types.TINYINT |
| 57 | + || type == Types.INTEGER |
| 58 | + || type == Types.BIGINT |
| 59 | + || type == Types.FLOAT |
| 60 | + || type == Types.DOUBLE |
| 61 | + || type == Types.DECIMAL |
| 62 | + || type == Types.NUMERIC; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public int getPrecision(int param) throws SQLException { |
| 67 | + checkValidIndex(param); |
| 68 | + StandardSQLTypeName sqlType = getStandardSQLTypeName(param); |
| 69 | + if (sqlType == null) { |
| 70 | + return 0; |
| 71 | + } |
| 72 | + BigQueryJdbcTypeMappings.ColumnTypeInfo typeInfo = |
| 73 | + BigQueryJdbcTypeMappings.STANDARD_TYPE_INFO.get(sqlType); |
| 74 | + if (typeInfo != null && typeInfo.columnSize != null) { |
| 75 | + return typeInfo.columnSize; |
| 76 | + } |
| 77 | + return 0; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public int getScale(int param) throws SQLException { |
| 82 | + checkValidIndex(param); |
| 83 | + StandardSQLTypeName sqlType = getStandardSQLTypeName(param); |
| 84 | + if (sqlType == null) { |
| 85 | + return 0; |
| 86 | + } |
| 87 | + BigQueryJdbcTypeMappings.ColumnTypeInfo typeInfo = |
| 88 | + BigQueryJdbcTypeMappings.STANDARD_TYPE_INFO.get(sqlType); |
| 89 | + if (typeInfo != null && typeInfo.decimalDigits != null) { |
| 90 | + return typeInfo.decimalDigits; |
| 91 | + } |
| 92 | + return 0; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public int getParameterType(int param) throws SQLException { |
| 97 | + checkValidIndex(param); |
| 98 | + StandardSQLTypeName sqlType = getStandardSQLTypeName(param); |
| 99 | + if (sqlType == null) { |
| 100 | + return Types.OTHER; |
| 101 | + } |
| 102 | + Integer jdbcType = BigQueryJdbcTypeMappings.standardSQLToJavaSqlTypesMapping.get(sqlType); |
| 103 | + if (jdbcType != null) { |
| 104 | + return jdbcType; |
| 105 | + } |
| 106 | + return Types.OTHER; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public String getParameterTypeName(int param) throws SQLException { |
| 111 | + checkValidIndex(param); |
| 112 | + StandardSQLTypeName sqlType = getStandardSQLTypeName(param); |
| 113 | + return sqlType != null ? sqlType.name() : "UNKNOWN"; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public String getParameterClassName(int param) throws SQLException { |
| 118 | + checkValidIndex(param); |
| 119 | + StandardSQLTypeName sqlType = getStandardSQLTypeName(param); |
| 120 | + if (sqlType != null) { |
| 121 | + Class<?> clazz = BigQueryJdbcTypeMappings.standardSQLToJavaTypeMapping.get(sqlType); |
| 122 | + if (clazz != null) { |
| 123 | + return clazz.getName(); |
| 124 | + } |
| 125 | + } |
| 126 | + if (this.parameterHandler == null) { |
| 127 | + return Object.class.getName(); |
| 128 | + } |
| 129 | + Class<?> boundType = this.parameterHandler.getType(param); |
| 130 | + if (boundType != null) { |
| 131 | + return boundType.getName(); |
| 132 | + } |
| 133 | + return Object.class.getName(); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public int getParameterMode(int param) throws SQLException { |
| 138 | + checkValidIndex(param); |
| 139 | + if (this.parameterHandler == null) { |
| 140 | + return parameterModeIn; |
| 141 | + } |
| 142 | + BigQueryParameterHandler.BigQueryStatementParameterType paramType = |
| 143 | + this.parameterHandler.getParameterType(param); |
| 144 | + if (paramType == BigQueryParameterHandler.BigQueryStatementParameterType.OUT) { |
| 145 | + return parameterModeOut; |
| 146 | + } |
| 147 | + if (paramType == BigQueryParameterHandler.BigQueryStatementParameterType.INOUT) { |
| 148 | + return parameterModeInOut; |
| 149 | + } |
| 150 | + return parameterModeIn; |
| 151 | + } |
| 152 | + |
| 153 | + private StandardSQLTypeName getStandardSQLTypeName(int param) { |
| 154 | + if (this.parameterHandler == null) { |
| 155 | + return null; |
| 156 | + } |
| 157 | + StandardSQLTypeName sqlType = this.parameterHandler.getSqlType(param); |
| 158 | + if (sqlType != null) { |
| 159 | + return sqlType; |
| 160 | + } |
| 161 | + Class<?> javaType = this.parameterHandler.getType(param); |
| 162 | + if (javaType == null) { |
| 163 | + return null; |
| 164 | + } |
| 165 | + try { |
| 166 | + return BigQueryJdbcTypeMappings.classToType(javaType); |
| 167 | + } catch (SQLException ignored) { |
| 168 | + // fall back to default |
| 169 | + return null; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public <T> T unwrap(Class<T> iface) throws SQLException { |
| 175 | + if (!isWrapperFor(iface)) { |
| 176 | + throw new BigQueryJdbcException( |
| 177 | + "Cannot unwrap to " + (iface != null ? iface.getName() : "null")); |
| 178 | + } |
| 179 | + return iface.cast(this); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public boolean isWrapperFor(Class<?> iface) throws SQLException { |
| 184 | + return iface != null && iface.isInstance(this); |
| 185 | + } |
| 186 | +} |
0 commit comments