OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqlCreateView.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 
19 import org.apache.calcite.sql.SqlCreate;
20 import org.apache.calcite.sql.SqlIdentifier;
21 import org.apache.calcite.sql.SqlKind;
22 import org.apache.calcite.sql.SqlNode;
23 import org.apache.calcite.sql.SqlNodeList;
25 import org.apache.calcite.sql.SqlSpecialOperator;
26 import org.apache.calcite.sql.SqlWriter;
27 import org.apache.calcite.sql.SqlWriterConfig;
28 import org.apache.calcite.sql.dialect.CalciteSqlDialect;
29 import org.apache.calcite.sql.parser.SqlParserPos;
30 import org.apache.calcite.sql.pretty.SqlPrettyWriter;
32 import org.apache.calcite.util.ImmutableNullableList;
33 
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Objects;
37 
41 public class SqlCreateView extends SqlCreate {
42  public final SqlIdentifier name;
43  public final SqlNodeList columnList;
44  public final SqlNode query;
45 
46  private static final SqlOperator OPERATOR =
47  new SqlSpecialOperator("CREATE VIEW", SqlKind.CREATE_VIEW);
48 
50  SqlCreateView(SqlParserPos pos,
51  boolean ifNotExists,
52  SqlIdentifier name,
53  SqlNodeList columnList,
54  SqlNode query) {
55  super(OPERATOR, pos, false, ifNotExists);
56  this.name = Objects.requireNonNull(name);
57  this.columnList = columnList; // may be null
58  this.query = Objects.requireNonNull(query);
59  }
60 
61  public List<SqlNode> getOperandList() {
62  return ImmutableNullableList.of(name, columnList, query);
63  }
64 
65  @Override
66  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
67  writer.keyword("CREATE VIEW");
68  name.unparse(writer, leftPrec, rightPrec);
69  if (columnList != null) {
70  SqlWriter.Frame frame = writer.startList("(", ")");
71  for (SqlNode c : columnList) {
72  writer.sep(",");
73  c.unparse(writer, 0, 0);
74  }
75  writer.endList(frame);
76  }
77  writer.keyword("AS");
78  writer.newlineAndIndent();
79  query.unparse(writer, 0, 0);
80  }
81 
82  @Override
83  public String toString() {
85  Map<String, Object> map = jsonBuilder.map();
86 
87  jsonBuilder.put(map, "name", this.name.toString());
88 
89  SqlWriterConfig c = SqlPrettyWriter.config()
90  .withDialect(CalciteSqlDialect.DEFAULT)
91  .withQuoteAllIdentifiers(false)
92  .withSelectListItemsOnSeparateLines(false)
93  .withWhereListItemsOnSeparateLines(false)
94  .withValuesListNewline(false);
95  SqlPrettyWriter writer = new SqlPrettyWriter(c);
96  this.query.unparse(writer, 0, 0);
97  jsonBuilder.put(map, "query", writer.toString());
98  jsonBuilder.put(map, "ifNotExists", this.ifNotExists);
99 
100  map.put("command", "CREATE_VIEW");
101  Map<String, Object> payload = jsonBuilder.map();
102  payload.put("payload", map);
103  return jsonBuilder.toJsonString(payload);
104  }
105 }
void unparse(SqlWriter writer, int leftPrec, int rightPrec)
SqlCreateView(SqlParserPos pos, boolean ifNotExists, SqlIdentifier name, SqlNodeList columnList, SqlNode query)