Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CreateDbStmt.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 
17 import org.apache.hadoop.fs.permission.FsAction;
18 import org.apache.hadoop.hive.metastore.MetaStoreUtils;
19 
23 import com.cloudera.impala.thrift.TCreateDbParams;
24 
28 public class CreateDbStmt extends StatementBase {
29  private final String dbName_;
30  private final HdfsUri location_;
31  private final String comment_;
32  private final boolean ifNotExists_;
33 
37  public CreateDbStmt(String dbName) {
38  this(dbName, null, null, false);
39  }
40 
47  public CreateDbStmt(String dbName, String comment, HdfsUri location,
48  boolean ifNotExists) {
49  this.dbName_ = dbName;
50  this.comment_ = comment;
51  this.location_ = location;
52  this.ifNotExists_ = ifNotExists;
53  }
54 
55  public String getComment() { return comment_; }
56  public String getDb() { return dbName_; }
57  public boolean getIfNotExists() { return ifNotExists_; }
58  public HdfsUri getLocation() { return location_; }
59 
60  @Override
61  public String toSql() {
62  StringBuilder sb = new StringBuilder("CREATE DATABASE");
63  if (ifNotExists_) sb.append(" IF NOT EXISTS");
64  sb.append(dbName_);
65  if (comment_ != null) sb.append(" COMMENT '" + comment_ + "'");
66  if (location_ != null) sb.append(" LOCATION '" + location_ + "'");
67  return sb.toString();
68  }
69 
70  public TCreateDbParams toThrift() {
71  TCreateDbParams params = new TCreateDbParams();
72  params.setDb(getDb());
73  params.setComment(getComment());
74  params.setLocation(location_ == null ? null : location_.toString());
75  params.setIf_not_exists(getIfNotExists());
76  return params;
77  }
78 
79  @Override
80  public void analyze(Analyzer analyzer) throws AnalysisException {
81  // Check whether the db name meets the Metastore's requirements.
82  if (!MetaStoreUtils.validateName(dbName_)) {
83  throw new AnalysisException("Invalid database name: " + dbName_);
84  }
85 
86  // Note: It is possible that a database with the same name was created external to
87  // this Impala instance. If that happens, the caller will not get an
88  // AnalysisException when creating the database, they will get a Hive
89  // AlreadyExistsException once the request has been sent to the metastore.
90  Db db = analyzer.getDb(getDb(), Privilege.CREATE, false);
91  if (db != null && !ifNotExists_) {
93  }
94 
95  if (location_ != null) {
96  location_.analyze(analyzer, Privilege.ALL, FsAction.READ_WRITE);
97  }
98  }
99 }
static final String DB_ALREADY_EXISTS_ERROR_MSG
Definition: Analyzer.java:108
CreateDbStmt(String dbName, String comment, HdfsUri location, boolean ifNotExists)