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.metastore.api.PrimaryKeysRequest;
0023 import org.apache.hadoop.hive.metastore.api.SQLPrimaryKey;
0024 import org.apache.hadoop.hive.serde2.thrift.Type;
0025 import org.apache.hive.service.cli.*;
0026 import org.apache.hive.service.cli.session.HiveSession;
0027 
0028 import java.util.List;
0029 
0030 /**
0031  * GetPrimaryKeysOperation.
0032  *
0033  */
0034 public class GetPrimaryKeysOperation extends MetadataOperation {
0035   /**
0036   TABLE_CAT String => table catalog (may be null)
0037   TABLE_SCHEM String => table schema (may be null)
0038   TABLE_NAME String => table name
0039   COLUMN_NAME String => column name
0040   KEY_SEQ short => sequence number within primary key( a value of 1 represents the first column of the primary key, a value of 2 would represent the second column within the primary key).
0041   PK_NAME String => primary key name (may be null)
0042   */
0043   private static final TableSchema RESULT_SET_SCHEMA = new TableSchema()
0044   .addPrimitiveColumn("TABLE_CAT", Type.STRING_TYPE,
0045       "Table catalog (may be null)")
0046   .addPrimitiveColumn("TABLE_SCHEM", Type.STRING_TYPE,
0047       "Table schema (may be null)")
0048   .addPrimitiveColumn("TABLE_NAME", Type.STRING_TYPE,
0049       "Table name")
0050   .addPrimitiveColumn("COLUMN_NAME", Type.STRING_TYPE,
0051       "Column name")
0052   .addPrimitiveColumn("KEQ_SEQ", Type.INT_TYPE,
0053       "Sequence number within primary key")
0054   .addPrimitiveColumn("PK_NAME", Type.STRING_TYPE,
0055       "Primary key name (may be null)");
0056 
0057   private final String catalogName;
0058   private final String schemaName;
0059   private final String tableName;
0060 
0061   private final RowSet rowSet;
0062 
0063   public GetPrimaryKeysOperation(HiveSession parentSession,
0064                                  String catalogName, String schemaName, String tableName) {
0065     super(parentSession, OperationType.GET_FUNCTIONS);
0066     this.catalogName = catalogName;
0067     this.schemaName = schemaName;
0068     this.tableName = tableName;
0069     this.rowSet = RowSetFactory.create(RESULT_SET_SCHEMA, getProtocolVersion(), false);
0070   }
0071 
0072   @Override
0073   public void runInternal() throws HiveSQLException {
0074     setState(OperationState.RUNNING);
0075     try {
0076       IMetaStoreClient metastoreClient = getParentSession().getMetaStoreClient();
0077       PrimaryKeysRequest sqlReq = new PrimaryKeysRequest(schemaName, tableName);
0078       List<SQLPrimaryKey> pks = metastoreClient.getPrimaryKeys(sqlReq);
0079       if (pks == null) {
0080         return;
0081       }
0082       for (SQLPrimaryKey pk : pks) {
0083         rowSet.addRow(new Object[] {catalogName, pk.getTable_db(),
0084           pk.getTable_name(), pk.getColumn_name(), pk.getKey_seq(), pk.getPk_name()});
0085       }
0086       setState(OperationState.FINISHED);
0087     } catch (Exception e) {
0088       setState(OperationState.ERROR);
0089       throw new HiveSQLException(e);
0090     }
0091   }
0092 
0093 
0094   /* (non-Javadoc)
0095    * @see org.apache.hive.service.cli.Operation#getResultSetSchema()
0096    */
0097   @Override
0098   public TableSchema getResultSetSchema() throws HiveSQLException {
0099     assertState(OperationState.FINISHED);
0100     return RESULT_SET_SCHEMA;
0101   }
0102 
0103   /* (non-Javadoc)
0104    * @see org.apache.hive.service.cli.Operation#getNextRowSet(org.apache.hive.service.cli.FetchOrientation, long)
0105    */
0106   @Override
0107   public RowSet getNextRowSet(FetchOrientation orientation, long maxRows) throws HiveSQLException {
0108     assertState(OperationState.FINISHED);
0109     validateDefaultFetchOrientation(orientation);
0110     if (orientation.equals(FetchOrientation.FETCH_FIRST)) {
0111       rowSet.setStartOffset(0);
0112     }
0113     return rowSet.extractSubset((int)maxRows);
0114   }
0115 }