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.ForeignKeysRequest;
0023 import org.apache.hadoop.hive.metastore.api.SQLForeignKey;
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  * GetCrossReferenceOperation.
0032  *
0033  */
0034 public class GetCrossReferenceOperation extends MetadataOperation {
0035   /**
0036   PKTABLE_CAT String => parent key table catalog (may be null)
0037   PKTABLE_SCHEM String => parent key table schema (may be null)
0038   PKTABLE_NAME String => parent key table name
0039   PKCOLUMN_NAME String => parent key column name
0040   FKTABLE_CAT String => foreign key table catalog (may be null) being exported (may be null)
0041   FKTABLE_SCHEM String => foreign key table schema (may be null) being exported (may be null)
0042   FKTABLE_NAME String => foreign key table name being exported
0043   FKCOLUMN_NAME String => foreign key column name being exported
0044   KEY_SEQ short => sequence number within foreign key( a value of 1 represents the first column of the foreign key, a value of 2 would represent the second column within the foreign key).
0045   UPDATE_RULE short => What happens to foreign key when parent key is updated:
0046   importedNoAction - do not allow update of parent key if it has been imported
0047   importedKeyCascade - change imported key to agree with parent key update
0048   importedKeySetNull - change imported key to NULL if its parent key has been updated
0049   importedKeySetDefault - change imported key to default values if its parent key has been updated
0050   importedKeyRestrict - same as importedKeyNoAction (for ODBC 2.x compatibility)
0051   DELETE_RULE short => What happens to the foreign key when parent key is deleted.
0052   importedKeyNoAction - do not allow delete of parent key if it has been imported
0053   importedKeyCascade - delete rows that import a deleted key
0054   importedKeySetNull - change imported key to NULL if its primary key has been deleted
0055   importedKeyRestrict - same as importedKeyNoAction (for ODBC 2.x compatibility)
0056   importedKeySetDefault - change imported key to default if its parent key has been deleted
0057   FK_NAME String => foreign key name (may be null)
0058   PK_NAME String => parent key name (may be null)
0059   DEFERRABILITY short => can the evaluation of foreign key constraints be deferred until commit
0060   importedKeyInitiallyDeferred - see SQL92 for definition
0061   importedKeyInitiallyImmediate - see SQL92 for definition
0062   importedKeyNotDeferrable - see SQL92 for definition
0063  */
0064   private static final TableSchema RESULT_SET_SCHEMA = new TableSchema()
0065   .addPrimitiveColumn("PKTABLE_CAT", Type.STRING_TYPE,
0066       "Parent key table catalog (may be null)")
0067   .addPrimitiveColumn("PKTABLE_SCHEM", Type.STRING_TYPE,
0068       "Parent key table schema (may be null)")
0069   .addPrimitiveColumn("PKTABLE_NAME", Type.STRING_TYPE,
0070       "Parent Key table name")
0071   .addPrimitiveColumn("PKCOLUMN_NAME", Type.STRING_TYPE,
0072       "Parent Key column name")
0073   .addPrimitiveColumn("FKTABLE_CAT", Type.STRING_TYPE,
0074       "Foreign key table catalog (may be null)")
0075   .addPrimitiveColumn("FKTABLE_SCHEM", Type.STRING_TYPE,
0076       "Foreign key table schema (may be null)")
0077   .addPrimitiveColumn("FKTABLE_NAME", Type.STRING_TYPE,
0078       "Foreign Key table name")
0079   .addPrimitiveColumn("FKCOLUMN_NAME", Type.STRING_TYPE,
0080       "Foreign Key column name")
0081   .addPrimitiveColumn("KEQ_SEQ", Type.INT_TYPE,
0082       "Sequence number within primary key")
0083   .addPrimitiveColumn("UPDATE_RULE", Type.INT_TYPE,
0084       "What happens to foreign key when parent key is updated")
0085   .addPrimitiveColumn("DELETE_RULE", Type.INT_TYPE,
0086       "What happens to foreign key when parent key is deleted")
0087   .addPrimitiveColumn("FK_NAME", Type.STRING_TYPE,
0088       "Foreign key name (may be null)")
0089   .addPrimitiveColumn("PK_NAME", Type.STRING_TYPE,
0090       "Primary key name (may be null)")
0091   .addPrimitiveColumn("DEFERRABILITY", Type.INT_TYPE,
0092       "Can the evaluation of foreign key constraints be deferred until commit");
0093   private final String parentCatalogName;
0094   private final String parentSchemaName;
0095   private final String parentTableName;
0096   private final String foreignCatalogName;
0097   private final String foreignSchemaName;
0098   private final String foreignTableName;
0099   private final RowSet rowSet;
0100 
0101   public GetCrossReferenceOperation(HiveSession parentSession,
0102                                     String parentCatalogName, String parentSchemaName, String parentTableName,
0103                                     String foreignCatalog, String foreignSchema, String foreignTable) {
0104     super(parentSession, OperationType.GET_FUNCTIONS);
0105     this.parentCatalogName = parentCatalogName;
0106     this.parentSchemaName = parentSchemaName;
0107     this.parentTableName = parentTableName;
0108     this.foreignCatalogName = foreignCatalog;
0109     this.foreignSchemaName = foreignSchema;
0110     this.foreignTableName = foreignTable;
0111     this.rowSet = RowSetFactory.create(RESULT_SET_SCHEMA, getProtocolVersion(), false);
0112   }
0113 
0114   @Override
0115   public void runInternal() throws HiveSQLException {
0116     setState(OperationState.RUNNING);
0117     try {
0118        IMetaStoreClient metastoreClient = getParentSession().getMetaStoreClient();
0119      ForeignKeysRequest fkReq = new ForeignKeysRequest(parentSchemaName, parentTableName, foreignSchemaName, foreignTableName);
0120      List<SQLForeignKey> fks = metastoreClient.getForeignKeys(fkReq);
0121       if (fks == null) {
0122         return;
0123       }
0124       for (SQLForeignKey fk : fks) {
0125         rowSet.addRow(new Object[] {parentCatalogName,
0126         fk.getPktable_db(), fk.getPktable_name(), fk.getPkcolumn_name(),
0127         foreignCatalogName,
0128         fk.getFktable_db(), fk.getFktable_name(), fk.getFkcolumn_name(),
0129         fk.getKey_seq(), fk.getUpdate_rule(), fk.getDelete_rule(), fk.getFk_name(),
0130         fk.getPk_name(), 0});
0131       }
0132       setState(OperationState.FINISHED);
0133     } catch (Exception e) {
0134       setState(OperationState.ERROR);
0135       throw new HiveSQLException(e);
0136     }
0137   }
0138 
0139 
0140   /* (non-Javadoc)
0141    * @see org.apache.hive.service.cli.Operation#getResultSetSchema()
0142    */
0143   @Override
0144   public TableSchema getResultSetSchema() throws HiveSQLException {
0145     assertState(OperationState.FINISHED);
0146     return RESULT_SET_SCHEMA;
0147   }
0148 
0149   /* (non-Javadoc)
0150    * @see org.apache.hive.service.cli.Operation#getNextRowSet(org.apache.hive.service.cli.FetchOrientation, long)
0151    */
0152   @Override
0153   public RowSet getNextRowSet(FetchOrientation orientation, long maxRows) throws HiveSQLException {
0154     assertState(OperationState.FINISHED);
0155     validateDefaultFetchOrientation(orientation);
0156     if (orientation.equals(FetchOrientation.FETCH_FIRST)) {
0157       rowSet.setStartOffset(0);
0158     }
0159     return rowSet.extractSubset((int)maxRows);
0160   }
0161 }