OmniSciDB  a5dc49c757
 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.SqlDdl;
6 import org.apache.calcite.sql.SqlKind;
7 import org.apache.calcite.sql.SqlNode;
8 import org.apache.calcite.sql.SqlNodeList;
10 import org.apache.calcite.sql.SqlSpecialOperator;
11 import org.apache.calcite.sql.SqlWriter;
12 import org.apache.calcite.sql.SqlWriterConfig;
13 import org.apache.calcite.sql.dialect.CalciteSqlDialect;
14 import org.apache.calcite.sql.parser.SqlParserPos;
15 import org.apache.calcite.sql.pretty.SqlPrettyWriter;
17 import org.apache.calcite.util.JsonBuilder;
18 
19 import java.util.List;
20 import java.util.Map;
21 
26 public class SqlInsertIntoTable extends SqlDdl {
27  public final SqlNode name;
28  public final SqlNodeList columnList;
29  public SqlNode query = null;
30 
31  private static final SqlOperator OPERATOR =
32  new SqlSpecialOperator("INSERT_INTO_TABLE_AS_SELECT", SqlKind.OTHER_DDL);
33 
35  SqlParserPos pos, SqlNode table, SqlNode query, SqlNodeList columnList) {
36  super(OPERATOR, pos);
37  this.name = table;
38  this.query = query;
39  this.columnList = columnList; // may be null
40  }
41 
42  @Override
43  public List<SqlNode> getOperandList() {
44  return null;
45  }
46 
47  @Override
48  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
49  writer.keyword("INSERT");
50  writer.keyword("INTO");
51  name.unparse(writer, leftPrec, rightPrec);
52  if (columnList != null) {
53  SqlWriter.Frame frame = writer.startList("(", ")");
54  for (SqlNode c : columnList) {
55  writer.sep(",");
56  c.unparse(writer, 0, 0);
57  }
58  writer.endList(frame);
59  }
60  if (query != null) {
61  writer.newlineAndIndent();
62  query.unparse(writer, 0, 0);
63  }
64  }
65 
66  @Override
67  public String toString() {
68  JsonBuilder jsonBuilder = new EscapedStringJsonBuilder();
69  Map<String, Object> map = jsonBuilder.map();
70 
71  map.put("command", "INSERT_INTO_TABLE_AS_SELECT");
72  map.put("name", this.name.toString());
73 
74  // By default ... toString() seems to single-quote too much stuff
75  // for the SELECT stmt to be executed later
76  // ->
77  // use PrettyWriter to output a cleaner SQL statement
78  //
79  SqlWriterConfig c = SqlPrettyWriter.config()
80  .withDialect(CalciteSqlDialect.DEFAULT)
81  .withQuoteAllIdentifiers(false)
82  .withSelectListItemsOnSeparateLines(false)
83  .withWhereListItemsOnSeparateLines(false)
84  .withValuesListNewline(false);
85  SqlPrettyWriter writer = new SqlPrettyWriter(c);
86  this.query.unparse(writer, 0, 0);
87  map.put("query", writer.toString());
88 
89  if (columnList != null) {
90  List<Object> col_list = jsonBuilder.list();
91  for (SqlNode col : this.columnList) {
92  col_list.add(col.toString());
93  }
94  jsonBuilder.put(map, "columns", col_list);
95  }
96 
97  Map<String, Object> payload = jsonBuilder.map();
98  payload.put("payload", map);
99 
100  return jsonBuilder.toJsonString(payload);
101  }
102 }
void unparse(SqlWriter writer, int leftPrec, int rightPrec)
SqlInsertIntoTable(SqlParserPos pos, SqlNode table, SqlNode query, SqlNodeList columnList)