OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqlInsertIntoTable.java
Go to the documentation of this file.
1 package com.mapd.parser.extension.ddl;
2 
3 import com.google.gson.annotations.Expose;
4 
5 import org.apache.calcite.sql.SqlCall;
6 import org.apache.calcite.sql.SqlDdl;
7 import org.apache.calcite.sql.SqlIdentifier;
8 import org.apache.calcite.sql.SqlInsert;
9 import org.apache.calcite.sql.SqlKind;
10 import org.apache.calcite.sql.SqlNode;
11 import org.apache.calcite.sql.SqlNodeList;
12 import org.apache.calcite.sql.SqlOperator;
13 import org.apache.calcite.sql.SqlSpecialOperator;
14 import org.apache.calcite.sql.SqlWriter;
15 import org.apache.calcite.sql.SqlWriterConfig;
16 import org.apache.calcite.sql.dialect.CalciteSqlDialect;
17 import org.apache.calcite.sql.parser.SqlParserPos;
18 import org.apache.calcite.sql.pretty.SqlPrettyWriter;
20 import org.apache.calcite.util.ImmutableNullableList;
21 import org.apache.calcite.util.JsonBuilder;
22 import org.apache.calcite.util.Pair;
23 
24 import java.io.*;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 
33 public class SqlInsertIntoTable extends SqlDdl {
34  public final SqlNode name;
35  public final SqlNodeList columnList;
36  public SqlNode query = null;
37 
38  private static final SqlOperator OPERATOR =
39  new SqlSpecialOperator("INSERT_INTO_TABLE_AS_SELECT", SqlKind.OTHER_DDL);
40 
42  SqlParserPos pos, SqlNode table, SqlNode query, SqlNodeList columnList) {
43  super(OPERATOR, pos);
44  this.name = table;
45  this.query = query;
46  this.columnList = columnList; // may be null
47  }
48 
49  @Override
50  public List<SqlNode> getOperandList() {
51  return ImmutableNullableList.of(name, columnList, query);
52  }
53 
54  @Override
55  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
56  writer.keyword("INSERT");
57  writer.keyword("INTO");
58  name.unparse(writer, leftPrec, rightPrec);
59  if (columnList != null) {
60  SqlWriter.Frame frame = writer.startList("(", ")");
61  for (SqlNode c : columnList) {
62  writer.sep(",");
63  c.unparse(writer, 0, 0);
64  }
65  writer.endList(frame);
66  }
67  if (query != null) {
68  writer.newlineAndIndent();
69  query.unparse(writer, 0, 0);
70  }
71  }
72 
73  @Override
74  public String toString() {
75  JsonBuilder jsonBuilder = new EscapedStringJsonBuilder();
76  Map<String, Object> map = jsonBuilder.map();
77 
78  map.put("command", "INSERT_INTO_TABLE_AS_SELECT");
79  map.put("name", this.name.toString());
80 
81  // By default ... toString() seems to single-quote too much stuff
82  // for the SELECT stmt to be executed later
83  // ->
84  // use PrettyWriter to output a cleaner SQL statement
85  //
86  SqlWriterConfig c = SqlPrettyWriter.config()
87  .withDialect(CalciteSqlDialect.DEFAULT)
88  .withQuoteAllIdentifiers(false)
89  .withSelectListItemsOnSeparateLines(false)
90  .withWhereListItemsOnSeparateLines(false)
91  .withValuesListNewline(false);
92  SqlPrettyWriter writer = new SqlPrettyWriter(c);
93  this.query.unparse(writer, 0, 0);
94  map.put("query", writer.toString());
95 
96  if (columnList != null) {
97  List<Object> col_list = jsonBuilder.list();
98  for (SqlNode col : this.columnList) {
99  col_list.add(col.toString());
100  }
101  jsonBuilder.put(map, "columns", col_list);
102  }
103 
104  Map<String, Object> payload = jsonBuilder.map();
105  payload.put("payload", map);
106 
107  return jsonBuilder.toJsonString(payload);
108  }
109 }
void unparse(SqlWriter writer, int leftPrec, int rightPrec)
SqlInsertIntoTable(SqlParserPos pos, SqlNode table, SqlNode query, SqlNodeList columnList)