OmniSciDB  a7179b2938
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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;
24 import org.apache.calcite.sql.SqlOperator;
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 replace,
52  boolean ifNotExists,
53  SqlIdentifier name,
54  SqlNodeList columnList,
55  SqlNode query) {
56  super(OPERATOR, pos, replace, ifNotExists);
57  this.name = Objects.requireNonNull(name);
58  this.columnList = columnList; // may be null
59  this.query = Objects.requireNonNull(query);
60  }
61 
62  public List<SqlNode> getOperandList() {
63  return ImmutableNullableList.of(name, columnList, query);
64  }
65 
66  @Override
67  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
68  if (getReplace()) {
69  writer.keyword("CREATE OR REPLACE");
70  } else {
71  writer.keyword("CREATE");
72  }
73  writer.keyword("VIEW");
74  name.unparse(writer, leftPrec, rightPrec);
75  if (columnList != null) {
76  SqlWriter.Frame frame = writer.startList("(", ")");
77  for (SqlNode c : columnList) {
78  writer.sep(",");
79  c.unparse(writer, 0, 0);
80  }
81  writer.endList(frame);
82  }
83  writer.keyword("AS");
84  writer.newlineAndIndent();
85  query.unparse(writer, 0, 0);
86  }
87 
88  @Override
89  public String toString() {
91  Map<String, Object> map = jsonBuilder.map();
92 
93  jsonBuilder.put(map, "name", this.name.toString());
94 
95  SqlWriterConfig c = SqlPrettyWriter.config()
96  .withDialect(CalciteSqlDialect.DEFAULT)
97  .withQuoteAllIdentifiers(false)
98  .withSelectListItemsOnSeparateLines(false)
99  .withWhereListItemsOnSeparateLines(false)
100  .withValuesListNewline(false);
101  SqlPrettyWriter writer = new SqlPrettyWriter(c);
102  this.query.unparse(writer, 0, 0);
103  jsonBuilder.put(map, "query", writer.toString());
104 
105  jsonBuilder.put(map, "ifNotExists", this.ifNotExists);
106 
107  map.put("command", "CREATE_VIEW");
108  Map<String, Object> payload = jsonBuilder.map();
109  payload.put("payload", map);
110  return jsonBuilder.toJsonString(payload);
111  }
112 }
void unparse(SqlWriter writer, int leftPrec, int rightPrec)
SqlCreateView(SqlParserPos pos, boolean replace, boolean ifNotExists, SqlIdentifier name, SqlNodeList columnList, SqlNode query)