Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DropTableOrViewStmt.java
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.analysis;
16 
21 import com.cloudera.impala.thrift.TDropTableOrViewParams;
22 import com.cloudera.impala.thrift.TTableName;
23 import com.google.common.base.Preconditions;
24 
28 public class DropTableOrViewStmt extends StatementBase {
29  protected final TableName tableName_;
30  protected final boolean ifExists_;
31 
32  // True if we are dropping a table. False if we are dropping a view.
33  protected final boolean dropTable_;
34 
35  // Set during analysis
36  protected String dbName_;
37 
41  public DropTableOrViewStmt(TableName tableName, boolean ifExists, boolean dropTable) {
42  this.tableName_ = tableName;
43  this.ifExists_ = ifExists;
44  this.dropTable_ = dropTable;
45  }
46 
47  @Override
48  public String toSql() {
49  StringBuilder sb = new StringBuilder("DROP " + ((dropTable_) ? "TABLE" : "VIEW"));
50  if (ifExists_) sb.append("IF EXISTS ");
51  if (tableName_.getDb() != null) sb.append(tableName_.getDb() + ".");
52  sb.append(tableName_.getTbl());
53  return sb.toString();
54  }
55 
56  public TDropTableOrViewParams toThrift() {
57  TDropTableOrViewParams params = new TDropTableOrViewParams();
58  params.setTable_name(new TTableName(getDb(), getTbl()));
59  params.setIf_exists(getIfExists());
60  return params;
61  }
62 
70  @Override
71  public void analyze(Analyzer analyzer) throws AnalysisException {
72  dbName_ = analyzer.getTargetDbName(tableName_);
73  try {
74  Table table = analyzer.getTable(tableName_, Privilege.DROP);
75  Preconditions.checkNotNull(table);
76  if (table instanceof View && dropTable_) {
77  throw new AnalysisException(String.format(
78  "DROP TABLE not allowed on a view: %s.%s", dbName_, getTbl()));
79  }
80  if (!(table instanceof View) && !dropTable_) {
81  throw new AnalysisException(String.format(
82  "DROP VIEW not allowed on a table: %s.%s", dbName_, getTbl()));
83  }
84  } catch (AnalysisException e) {
85  if (ifExists_ && analyzer.getMissingTbls().isEmpty()) return;
86  throw e;
87  }
88  }
89 
94  public String getDb() {
95  Preconditions.checkNotNull(dbName_);
96  return dbName_;
97  }
98 
99  public String getTbl() { return tableName_.getTbl(); }
100  public boolean getIfExists() { return this.ifExists_; }
101  public boolean isDropTable() { return dropTable_; }
102 }
DropTableOrViewStmt(TableName tableName, boolean ifExists, boolean dropTable)