OmniSciDB  a7179b2938
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SqlCreateTable.java
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to you under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package com.mapd.parser.extension.ddl;
18 
20 
21 import org.apache.calcite.sql.SqlCreate;
22 import org.apache.calcite.sql.SqlIdentifier;
23 import org.apache.calcite.sql.SqlKind;
24 import org.apache.calcite.sql.SqlNode;
25 import org.apache.calcite.sql.SqlNodeList;
26 import org.apache.calcite.sql.SqlOperator;
27 import org.apache.calcite.sql.SqlSpecialOperator;
28 import org.apache.calcite.sql.SqlWriter;
29 import org.apache.calcite.sql.parser.SqlParserPos;
31 import org.apache.calcite.util.ImmutableNullableList;
32 import org.apache.calcite.util.JsonBuilder;
33 
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Objects;
37 
41 public class SqlCreateTable extends SqlCreate {
42  public final SqlIdentifier name;
43  public final SqlNodeList columnList;
44  public final SqlNode query;
45  private final OmniSciOptionsMap options;
46 
47  private static final SqlOperator OPERATOR =
48  new SqlSpecialOperator("CREATE TABLE", SqlKind.CREATE_TABLE);
49 
51  protected SqlCreateTable(SqlParserPos pos,
52  boolean replace,
53  boolean ifNotExists,
54  SqlIdentifier name,
55  SqlNodeList columnList,
56  OmniSciOptionsMap withOptions,
57  SqlNode query) {
58  super(OPERATOR, pos, replace, ifNotExists);
59  this.name = Objects.requireNonNull(name);
60  this.options = withOptions;
61  this.columnList = columnList; // may be null
62  this.query = query; // for "CREATE TABLE ... AS query"; may be null
63  }
64 
65  public List<SqlNode> getOperandList() {
66  return ImmutableNullableList.of(name, columnList, query);
67  }
68 
69  @Override
70  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
71  writer.keyword("CREATE");
72  writer.keyword("TABLE");
73  if (ifNotExists) {
74  writer.keyword("IF NOT EXISTS");
75  }
76  name.unparse(writer, leftPrec, rightPrec);
77  if (columnList != null) {
78  SqlWriter.Frame frame = writer.startList("(", ")");
79  for (SqlNode c : columnList) {
80  writer.sep(",");
81  c.unparse(writer, 0, 0);
82  }
83  writer.endList(frame);
84  }
85  if (query != null) {
86  writer.keyword("AS");
87  writer.newlineAndIndent();
88  query.unparse(writer, 0, 0);
89  }
90  }
91 
92  @Override
93  public String toString() {
94  JsonBuilder jsonBuilder = new EscapedStringJsonBuilder();
95  Map<String, Object> map = jsonBuilder.map();
96 
97  jsonBuilder.put(map, "name", this.name.toString());
98  jsonBuilder.put(
99  map, "query", this.query == null ? this.query : this.query.toString());
100 
101  List<Object> elements_list = jsonBuilder.list();
102  for (SqlNode elementNode : this.columnList) {
103  elements_list.add(elementNode);
104  }
105  jsonBuilder.put(map, "elements", elements_list);
106 
107  jsonBuilder.put(map, "ifNotExists", this.ifNotExists);
108 
109  map.put("command", "CREATE_TABLE");
110  map.put("options", this.options);
111  Map<String, Object> payload = jsonBuilder.map();
112  payload.put("payload", map);
113  return jsonBuilder.toJsonString(payload);
114  }
115 }
SqlCreateTable(SqlParserPos pos, boolean replace, boolean ifNotExists, SqlIdentifier name, SqlNodeList columnList, OmniSciOptionsMap withOptions, SqlNode query)
void unparse(SqlWriter writer, int leftPrec, int rightPrec)