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.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
0032
0033
0034 public class GetCrossReferenceOperation extends MetadataOperation {
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
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
0141
0142
0143 @Override
0144 public TableSchema getResultSetSchema() throws HiveSQLException {
0145 assertState(OperationState.FINISHED);
0146 return RESULT_SET_SCHEMA;
0147 }
0148
0149
0150
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 }