Back to home page

OSCL-LXR

 
 

    


0001 /**
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements.  See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership.  The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License.  You may obtain a copy of the License at
0009  *
0010  *     http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing, software
0013  * distributed under the License is distributed on an "AS IS" BASIS,
0014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015  * See the License for the specific language governing permissions and
0016  * limitations under the License.
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  * GetSchemasOperation.
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   /* (non-Javadoc)
0076    * @see org.apache.hive.service.cli.Operation#getResultSetSchema()
0077    */
0078   @Override
0079   public TableSchema getResultSetSchema() throws HiveSQLException {
0080     assertState(OperationState.FINISHED);
0081     return RESULT_SET_SCHEMA;
0082   }
0083 
0084   /* (non-Javadoc)
0085    * @see org.apache.hive.service.cli.Operation#getNextRowSet(org.apache.hive.service.cli.FetchOrientation, long)
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 }