| 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.meta; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.util.ArrayList; |
| 10 |
|
| 11 |
import org.h2.api.ErrorCode; |
| 12 |
import org.h2.engine.SessionRemote; |
| 13 |
import org.h2.message.DbException; |
| 14 |
import org.h2.mode.DefaultNullOrdering; |
| 15 |
import org.h2.result.ResultInterface; |
| 16 |
import org.h2.result.ResultRemote; |
| 17 |
import org.h2.value.Transfer; |
| 18 |
import org.h2.value.TypeInfo; |
| 19 |
import org.h2.value.Value; |
| 20 |
import org.h2.value.ValueArray; |
| 21 |
import org.h2.value.ValueBoolean; |
| 22 |
import org.h2.value.ValueInteger; |
| 23 |
import org.h2.value.ValueNull; |
| 24 |
import org.h2.value.ValueVarchar; |
| 25 |
|
| 26 |
/** |
| 27 |
* Remote implementation of database meta information. |
| 28 |
*/ |
| 29 |
public class DatabaseMetaRemote extends DatabaseMeta { |
| 30 |
|
| 31 |
static final int DEFAULT_NULL_ORDERING = 0; |
| 32 |
|
| 33 |
static final int GET_DATABASE_PRODUCT_VERSION = 1; |
| 34 |
|
| 35 |
static final int GET_SQL_KEYWORDS = 2; |
| 36 |
|
| 37 |
static final int GET_NUMERIC_FUNCTIONS = 3; |
| 38 |
|
| 39 |
static final int GET_STRING_FUNCTIONS = 4; |
| 40 |
|
| 41 |
static final int GET_SYSTEM_FUNCTIONS = 5; |
| 42 |
|
| 43 |
static final int GET_TIME_DATE_FUNCTIONS = 6; |
| 44 |
|
| 45 |
static final int GET_SEARCH_STRING_ESCAPE = 7; |
| 46 |
|
| 47 |
static final int GET_PROCEDURES_3 = 8; |
| 48 |
|
| 49 |
static final int GET_PROCEDURE_COLUMNS_4 = 9; |
| 50 |
|
| 51 |
static final int GET_TABLES_4 = 10; |
| 52 |
|
| 53 |
static final int GET_SCHEMAS = 11; |
| 54 |
|
| 55 |
static final int GET_CATALOGS = 12; |
| 56 |
|
| 57 |
static final int GET_TABLE_TYPES = 13; |
| 58 |
|
| 59 |
static final int GET_COLUMNS_4 = 14; |
| 60 |
|
| 61 |
static final int GET_COLUMN_PRIVILEGES_4 = 15; |
| 62 |
|
| 63 |
static final int GET_TABLE_PRIVILEGES_3 = 16; |
| 64 |
|
| 65 |
static final int GET_BEST_ROW_IDENTIFIER_5 = 17; |
| 66 |
|
| 67 |
static final int GET_VERSION_COLUMNS_3 = 18; |
| 68 |
|
| 69 |
static final int GET_PRIMARY_KEYS_3 = 19; |
| 70 |
|
| 71 |
static final int GET_IMPORTED_KEYS_3 = 20; |
| 72 |
|
| 73 |
static final int GET_EXPORTED_KEYS_3 = 21; |
| 74 |
|
| 75 |
static final int GET_CROSS_REFERENCE_6 = 22; |
| 76 |
|
| 77 |
static final int GET_TYPE_INFO = 23; |
| 78 |
|
| 79 |
static final int GET_INDEX_INFO_5 = 24; |
| 80 |
|
| 81 |
static final int GET_UDTS_4 = 25; |
| 82 |
|
| 83 |
static final int GET_SUPER_TYPES_3 = 26; |
| 84 |
|
| 85 |
static final int GET_SUPER_TABLES_3 = 27; |
| 86 |
|
| 87 |
static final int GET_ATTRIBUTES_4 = 28; |
| 88 |
|
| 89 |
static final int GET_DATABASE_MAJOR_VERSION = 29; |
| 90 |
|
| 91 |
static final int GET_DATABASE_MINOR_VERSION = 30; |
| 92 |
|
| 93 |
static final int GET_SCHEMAS_2 = 31; |
| 94 |
|
| 95 |
static final int GET_FUNCTIONS_3 = 32; |
| 96 |
|
| 97 |
static final int GET_FUNCTION_COLUMNS_4 = 33; |
| 98 |
|
| 99 |
static final int GET_PSEUDO_COLUMNS_4 = 34; |
| 100 |
|
| 101 |
private final SessionRemote session; |
| 102 |
|
| 103 |
private final ArrayList<Transfer> transferList; |
| 104 |
|
| 105 |
public DatabaseMetaRemote(SessionRemote session, ArrayList<Transfer> transferList) { |
| 106 |
this.session = session; |
| 107 |
this.transferList = transferList; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public DefaultNullOrdering defaultNullOrdering() { |
| 112 |
ResultInterface result = executeQuery(DEFAULT_NULL_ORDERING); |
| 113 |
result.next(); |
| 114 |
return DefaultNullOrdering.valueOf(result.currentRow()[0].getInt()); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public String getDatabaseProductVersion() { |
| 119 |
ResultInterface result = executeQuery(GET_DATABASE_PRODUCT_VERSION); |
| 120 |
result.next(); |
| 121 |
return result.currentRow()[0].getString(); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public String getSQLKeywords() { |
| 126 |
ResultInterface result = executeQuery(GET_SQL_KEYWORDS); |
| 127 |
result.next(); |
| 128 |
return result.currentRow()[0].getString(); |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public String getNumericFunctions() { |
| 133 |
ResultInterface result = executeQuery(GET_NUMERIC_FUNCTIONS); |
| 134 |
result.next(); |
| 135 |
return result.currentRow()[0].getString(); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public String getStringFunctions() { |
| 140 |
ResultInterface result = executeQuery(GET_STRING_FUNCTIONS); |
| 141 |
result.next(); |
| 142 |
return result.currentRow()[0].getString(); |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public String getSystemFunctions() { |
| 147 |
ResultInterface result = executeQuery(GET_SYSTEM_FUNCTIONS); |
| 148 |
result.next(); |
| 149 |
return result.currentRow()[0].getString(); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public String getTimeDateFunctions() { |
| 154 |
ResultInterface result = executeQuery(GET_TIME_DATE_FUNCTIONS); |
| 155 |
result.next(); |
| 156 |
return result.currentRow()[0].getString(); |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public String getSearchStringEscape() { |
| 161 |
ResultInterface result = executeQuery(GET_SEARCH_STRING_ESCAPE); |
| 162 |
result.next(); |
| 163 |
return result.currentRow()[0].getString(); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public ResultInterface getProcedures(String catalog, String schemaPattern, String procedureNamePattern) { |
| 168 |
return executeQuery(GET_PROCEDURES_3, getString(catalog), getString(schemaPattern), |
| 169 |
getString(procedureNamePattern)); |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public ResultInterface getProcedureColumns(String catalog, String schemaPattern, String procedureNamePattern, |
| 174 |
String columnNamePattern) { |
| 175 |
return executeQuery(GET_PROCEDURE_COLUMNS_4, getString(catalog), getString(schemaPattern), |
| 176 |
getString(procedureNamePattern), getString(columnNamePattern)); |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public ResultInterface getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) { |
| 181 |
return executeQuery(GET_TABLES_4, getString(catalog), getString(schemaPattern), getString(tableNamePattern), |
| 182 |
getStringArray(types)); |
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
public ResultInterface getSchemas() { |
| 187 |
return executeQuery(GET_SCHEMAS); |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public ResultInterface getCatalogs() { |
| 192 |
return executeQuery(GET_CATALOGS); |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public ResultInterface getTableTypes() { |
| 197 |
return executeQuery(GET_TABLE_TYPES); |
| 198 |
} |
| 199 |
|
| 200 |
@Override |
| 201 |
public ResultInterface getColumns(String catalog, String schemaPattern, String tableNamePattern, |
| 202 |
String columnNamePattern) { |
| 203 |
return executeQuery(GET_COLUMNS_4, getString(catalog), getString(schemaPattern), getString(tableNamePattern), |
| 204 |
getString(columnNamePattern)); |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
public ResultInterface getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern) { |
| 209 |
return executeQuery(GET_COLUMN_PRIVILEGES_4, getString(catalog), getString(schema), getString(table), |
| 210 |
getString(columnNamePattern)); |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public ResultInterface getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) { |
| 215 |
return executeQuery(GET_TABLE_PRIVILEGES_3, getString(catalog), getString(schemaPattern), // |
| 216 |
getString(tableNamePattern)); |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public ResultInterface getBestRowIdentifier(String catalog, String schema, String table, int scope, |
| 221 |
boolean nullable) { |
| 222 |
return executeQuery(GET_BEST_ROW_IDENTIFIER_5, getString(catalog), getString(schema), getString(table), |
| 223 |
ValueInteger.get(scope), ValueBoolean.get(nullable)); |
| 224 |
} |
| 225 |
|
| 226 |
@Override |
| 227 |
public ResultInterface getVersionColumns(String catalog, String schema, String table) { |
| 228 |
return executeQuery(GET_VERSION_COLUMNS_3, getString(catalog), getString(schema), getString(table)); |
| 229 |
} |
| 230 |
|
| 231 |
@Override |
| 232 |
public ResultInterface getPrimaryKeys(String catalog, String schema, String table) { |
| 233 |
return executeQuery(GET_PRIMARY_KEYS_3, getString(catalog), getString(schema), getString(table)); |
| 234 |
} |
| 235 |
|
| 236 |
@Override |
| 237 |
public ResultInterface getImportedKeys(String catalog, String schema, String table) { |
| 238 |
return executeQuery(GET_IMPORTED_KEYS_3, getString(catalog), getString(schema), getString(table)); |
| 239 |
} |
| 240 |
|
| 241 |
@Override |
| 242 |
public ResultInterface getExportedKeys(String catalog, String schema, String table) { |
| 243 |
return executeQuery(GET_EXPORTED_KEYS_3, getString(catalog), getString(schema), getString(table)); |
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public ResultInterface getCrossReference(String primaryCatalog, String primarySchema, String primaryTable, |
| 248 |
String foreignCatalog, String foreignSchema, String foreignTable) { |
| 249 |
return executeQuery(GET_CROSS_REFERENCE_6, getString(primaryCatalog), getString(primarySchema), |
| 250 |
getString(primaryTable), getString(foreignCatalog), getString(foreignSchema), getString(foreignTable)); |
| 251 |
} |
| 252 |
|
| 253 |
@Override |
| 254 |
public ResultInterface getTypeInfo() { |
| 255 |
return executeQuery(GET_TYPE_INFO); |
| 256 |
} |
| 257 |
|
| 258 |
@Override |
| 259 |
public ResultInterface getIndexInfo(String catalog, String schema, String table, boolean unique, |
| 260 |
boolean approximate) { |
| 261 |
return executeQuery(GET_INDEX_INFO_5, getString(catalog), getString(schema), // |
| 262 |
getString(table), ValueBoolean.get(unique), ValueBoolean.get(approximate)); |
| 263 |
} |
| 264 |
|
| 265 |
@Override |
| 266 |
public ResultInterface getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) { |
| 267 |
return executeQuery(GET_UDTS_4, getString(catalog), getString(schemaPattern), getString(typeNamePattern), |
| 268 |
getIntArray(types)); |
| 269 |
} |
| 270 |
|
| 271 |
@Override |
| 272 |
public ResultInterface getSuperTypes(String catalog, String schemaPattern, String typeNamePattern) { |
| 273 |
return executeQuery(GET_SUPER_TYPES_3, getString(catalog), getString(schemaPattern), |
| 274 |
getString(typeNamePattern)); |
| 275 |
} |
| 276 |
|
| 277 |
@Override |
| 278 |
public ResultInterface getSuperTables(String catalog, String schemaPattern, String tableNamePattern) { |
| 279 |
return executeQuery(GET_SUPER_TABLES_3, getString(catalog), getString(schemaPattern), |
| 280 |
getString(tableNamePattern)); |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
public ResultInterface getAttributes(String catalog, String schemaPattern, String typeNamePattern, |
| 285 |
String attributeNamePattern) { |
| 286 |
return executeQuery(GET_ATTRIBUTES_4, getString(catalog), getString(schemaPattern), getString(typeNamePattern), |
| 287 |
getString(attributeNamePattern)); |
| 288 |
} |
| 289 |
|
| 290 |
@Override |
| 291 |
public int getDatabaseMajorVersion() { |
| 292 |
ResultInterface result = executeQuery(GET_DATABASE_MAJOR_VERSION); |
| 293 |
result.next(); |
| 294 |
return result.currentRow()[0].getInt(); |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public int getDatabaseMinorVersion() { |
| 299 |
ResultInterface result = executeQuery(GET_DATABASE_MINOR_VERSION); |
| 300 |
result.next(); |
| 301 |
return result.currentRow()[0].getInt(); |
| 302 |
} |
| 303 |
|
| 304 |
@Override |
| 305 |
public ResultInterface getSchemas(String catalog, String schemaPattern) { |
| 306 |
return executeQuery(GET_SCHEMAS_2, getString(catalog), getString(schemaPattern)); |
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
public ResultInterface getFunctions(String catalog, String schemaPattern, String functionNamePattern) { |
| 311 |
return executeQuery(GET_FUNCTIONS_3, getString(catalog), getString(schemaPattern), |
| 312 |
getString(functionNamePattern)); |
| 313 |
} |
| 314 |
|
| 315 |
@Override |
| 316 |
public ResultInterface getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern, |
| 317 |
String columnNamePattern) { |
| 318 |
return executeQuery(GET_FUNCTION_COLUMNS_4, getString(catalog), getString(schemaPattern), |
| 319 |
getString(functionNamePattern), getString(columnNamePattern)); |
| 320 |
} |
| 321 |
|
| 322 |
@Override |
| 323 |
public ResultInterface getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern, |
| 324 |
String columnNamePattern) { |
| 325 |
return executeQuery(GET_PSEUDO_COLUMNS_4, getString(catalog), getString(schemaPattern), |
| 326 |
getString(tableNamePattern), getString(columnNamePattern)); |
| 327 |
} |
| 328 |
|
| 329 |
private ResultInterface executeQuery(int code, Value... args) { |
| 330 |
if (session.isClosed()) { |
| 331 |
throw DbException.get(ErrorCode.DATABASE_CALLED_AT_SHUTDOWN); |
| 332 |
} |
| 333 |
synchronized (session) { |
| 334 |
int objectId = session.getNextId(); |
| 335 |
for (int i = 0, count = 0; i < transferList.size(); i++) { |
| 336 |
Transfer transfer = transferList.get(i); |
| 337 |
try { |
| 338 |
session.traceOperation("GET_META", objectId); |
| 339 |
int len = args.length; |
| 340 |
transfer.writeInt(SessionRemote.GET_JDBC_META).writeInt(code).writeInt(len); |
| 341 |
for (int j = 0; j < len; j++) { |
| 342 |
transfer.writeValue(args[j]); |
| 343 |
} |
| 344 |
session.done(transfer); |
| 345 |
int columnCount = transfer.readInt(); |
| 346 |
return new ResultRemote(session, transfer, objectId, columnCount, Integer.MAX_VALUE); |
| 347 |
} catch (IOException e) { |
| 348 |
session.removeServer(e, i--, ++count); |
| 349 |
} |
| 350 |
} |
| 351 |
return null; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
private Value getIntArray(int[] array) { |
| 356 |
if (array == null) { |
| 357 |
return ValueNull.INSTANCE; |
| 358 |
} |
| 359 |
int cardinality = array.length; |
| 360 |
Value[] values = new Value[cardinality]; |
| 361 |
for (int i = 0; i < cardinality; i++) { |
| 362 |
values[i] = ValueInteger.get(array[i]); |
| 363 |
} |
| 364 |
return ValueArray.get(TypeInfo.TYPE_INTEGER, values, session); |
| 365 |
} |
| 366 |
|
| 367 |
private Value getStringArray(String[] array) { |
| 368 |
if (array == null) { |
| 369 |
return ValueNull.INSTANCE; |
| 370 |
} |
| 371 |
int cardinality = array.length; |
| 372 |
Value[] values = new Value[cardinality]; |
| 373 |
for (int i = 0; i < cardinality; i++) { |
| 374 |
values[i] = getString(array[i]); |
| 375 |
} |
| 376 |
return ValueArray.get(TypeInfo.TYPE_VARCHAR, values, session); |
| 377 |
} |
| 378 |
|
| 379 |
private Value getString(String string) { |
| 380 |
return string != null ? ValueVarchar.get(string, session) : ValueNull.INSTANCE; |
| 381 |
} |
| 382 |
|
| 383 |
} |