0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 package org.apache.hive.service.cli.operation;
0020
0021 import org.apache.hadoop.hive.metastore.IMetaStoreClient;
0022 import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType;
0023 import org.apache.hive.service.cli.FetchOrientation;
0024 import org.apache.hive.service.cli.HiveSQLException;
0025 import org.apache.hive.service.cli.OperationState;
0026 import org.apache.hive.service.cli.OperationType;
0027 import org.apache.hive.service.cli.RowSet;
0028 import org.apache.hive.service.cli.RowSetFactory;
0029 import org.apache.hive.service.cli.TableSchema;
0030 import org.apache.hive.service.cli.session.HiveSession;
0031
0032
0033
0034
0035
0036 public class GetSchemasOperation extends MetadataOperation {
0037 private final String catalogName;
0038 private final String schemaName;
0039
0040 private static final TableSchema RESULT_SET_SCHEMA = new TableSchema()
0041 .addStringColumn("TABLE_SCHEM", "Schema name.")
0042 .addStringColumn("TABLE_CATALOG", "Catalog name.");
0043
0044 protected RowSet rowSet;
0045
0046 protected GetSchemasOperation(HiveSession parentSession,
0047 String catalogName, String schemaName) {
0048 super(parentSession, OperationType.GET_SCHEMAS);
0049 this.catalogName = catalogName;
0050 this.schemaName = schemaName;
0051 this.rowSet = RowSetFactory.create(RESULT_SET_SCHEMA, getProtocolVersion(), false);
0052 }
0053
0054 @Override
0055 public void runInternal() throws HiveSQLException {
0056 setState(OperationState.RUNNING);
0057 if (isAuthV2Enabled()) {
0058 String cmdStr = "catalog : " + catalogName + ", schemaPattern : " + schemaName;
0059 authorizeMetaGets(HiveOperationType.GET_SCHEMAS, null, cmdStr);
0060 }
0061 try {
0062 IMetaStoreClient metastoreClient = getParentSession().getMetaStoreClient();
0063 String schemaPattern = convertSchemaPattern(schemaName);
0064 for (String dbName : metastoreClient.getDatabases(schemaPattern)) {
0065 rowSet.addRow(new Object[] {dbName, DEFAULT_HIVE_CATALOG});
0066 }
0067 setState(OperationState.FINISHED);
0068 } catch (Exception e) {
0069 setState(OperationState.ERROR);
0070 throw new HiveSQLException(e);
0071 }
0072 }
0073
0074
0075
0076
0077
0078 @Override
0079 public TableSchema getResultSetSchema() throws HiveSQLException {
0080 assertState(OperationState.FINISHED);
0081 return RESULT_SET_SCHEMA;
0082 }
0083
0084
0085
0086
0087 @Override
0088 public RowSet getNextRowSet(FetchOrientation orientation, long maxRows) throws HiveSQLException {
0089 assertState(OperationState.FINISHED);
0090 validateDefaultFetchOrientation(orientation);
0091 if (orientation.equals(FetchOrientation.FETCH_FIRST)) {
0092 rowSet.setStartOffset(0);
0093 }
0094 return rowSet.extractSubset((int)maxRows);
0095 }
0096 }