OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SqlAlterTable.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 
18 package com.mapd.parser.extension.ddl;
19 
20 import com.google.gson.annotations.Expose;
21 
22 import org.apache.calcite.runtime.CalciteException;
23 import org.apache.calcite.sql.SqlCall;
24 import org.apache.calcite.sql.SqlDdl;
25 import org.apache.calcite.sql.SqlIdentifier;
26 import org.apache.calcite.sql.SqlKind;
27 import org.apache.calcite.sql.SqlNode;
28 import org.apache.calcite.sql.SqlNodeList;
29 import org.apache.calcite.sql.SqlOperator;
30 import org.apache.calcite.sql.SqlSpecialOperator;
31 import org.apache.calcite.sql.SqlWriter;
32 import org.apache.calcite.sql.parser.SqlParserPos;
34 import org.apache.calcite.util.JsonBuilder;
35 
36 import java.util.List;
37 import java.util.Map;
38 
43 public class SqlAlterTable extends SqlDdl {
44  private static final SqlOperator OPERATOR =
45  new SqlSpecialOperator("ALTER_TABLE", SqlKind.OTHER_DDL);
46 
66  public enum AlterType {
71  ALTER_OPTIONS
72  }
73 
74  public static class Builder extends SqlOptionsBuilder {
75  private SqlParserPos pos;
77  private String tableName;
78  private String newTableName;
79  private String columnName;
80  private String newColumnName;
81  private SqlNodeList columnList;
82 
83  public void setPos(final SqlParserPos pos) {
84  this.pos = pos;
85  }
86 
87  public void setTableName(final String tableName) {
88  this.tableName = tableName;
89  }
90 
91  public void alterOptions() {
92  this.alterType = AlterType.ALTER_OPTIONS;
93  // Options should be read in directly to base class
94  }
95 
96  public void alterTableName(final String newTableName) {
97  this.alterType = AlterType.RENAME_TABLE;
98  this.newTableName = newTableName;
99  }
100 
101  public void alterColumnName(final String columnName, final String newColumnName) {
102  this.alterType = AlterType.RENAME_COLUMN;
103  this.columnName = columnName;
104  this.newColumnName = newColumnName;
105  }
106 
107  public void addColumnList(final SqlNodeList columnList) {
108  this.alterType = AlterType.ADD_COLUMN;
109  this.columnList = columnList;
110  }
111 
112  public void dropColumn(final SqlNodeList columnList) {
113  this.alterType = AlterType.DROP_COLUMN;
114  this.columnList = columnList;
115  }
116 
117  public SqlAlterTable build() {
118  return new SqlAlterTable(pos,
119  alterType,
120  tableName,
121  newTableName,
122  columnName,
124  columnList,
125  super.options);
126  }
127  }
128 
129  @Expose
131  @Expose
132  private String tableName;
133  @Expose
134  private String newTableName;
135  @Expose
136  private String columnName;
137  @Expose
138  private String newColumnName;
139  @Expose
140  private String command;
141  @Expose
142  private SqlNodeList columnList;
143  @Expose
144  private Map<String, String> options;
145 
146  public SqlAlterTable(final SqlParserPos pos, final SqlIdentifier name) {
147  super(OPERATOR, pos);
148  this.tableName = name.toString();
149  }
150 
151  public SqlAlterTable(final SqlParserPos pos,
152  final AlterType alterType,
153  final String tableName,
154  final String newTableName,
155  final String columnName,
156  final String newColumnName,
157  final SqlNodeList columnList,
158  final Map<String, String> options) {
159  super(OPERATOR, pos);
160  this.alterType = alterType;
161  this.tableName = tableName;
162  this.newTableName = newTableName;
163  this.columnName = columnName;
164  this.newColumnName = newColumnName;
165  this.options = options;
166  this.columnList = columnList;
167  this.command = OPERATOR.getName();
168  }
169 
170  // @Override
171  public List<SqlNode> getOperandList() {
172  // Add the operands here
173  return null;
174  }
175 
176  @Override
177  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
178  writer.keyword("ALTER");
179  writer.keyword("TABLE");
180  // add other options data here when/as necessary
181  }
182 
183  @Override
184  public String toString() {
185  JsonBuilder jsonBuilder = new EscapedStringJsonBuilder();
186  Map<String, Object> map = jsonBuilder.map();
187 
188  map.put("command", "ALTER_TABLE");
189  map.put("tableName", this.tableName.toString());
190  switch (this.alterType) {
191  case RENAME_TABLE:
192  map.put("alterType", "RENAME_TABLE");
193  map.put("newTableName", this.newTableName.toString());
194  break;
195  case RENAME_COLUMN:
196  map.put("alterType", "RENAME_COLUMN");
197  map.put("columnName", this.columnName.toString());
198  map.put("newColumnName", this.newColumnName.toString());
199  break;
200  case ADD_COLUMN:
201  map.put("alterType", "ADD_COLUMN");
202  if (this.columnList != null) {
203  List<Object> elements_list = jsonBuilder.list();
204  for (SqlNode elementNode : this.columnList) {
205  if (!(elementNode instanceof SqlCall)) {
206  throw new CalciteException("Column definition for table "
207  + this.columnName.toString()
208  + " is invalid: " + elementNode.toString(),
209  null);
210  }
211  elements_list.add(elementNode);
212  }
213  map.put("columnData", elements_list);
214  }
215 
216  break;
217  case DROP_COLUMN:
218  map.put("alterType", "DROP_COLUMN");
219  map.put("columnData", this.columnList.toString());
220  break;
221  case ALTER_OPTIONS:
222  map.put("alterType", "ALTER_OPTIONS");
223  map.put("options", this.options);
224  break;
225  }
226 
227  Map<String, Object> payload = jsonBuilder.map();
228  payload.put("payload", map);
229  return jsonBuilder.toJsonString(payload);
230  }
231 }
void unparse(SqlWriter writer, int leftPrec, int rightPrec)
void alterTableName(final String newTableName)
void addColumnList(final SqlNodeList columnList)
SqlAlterTable(final SqlParserPos pos, final SqlIdentifier name)
string name
Definition: setup.in.py:72
void alterColumnName(final String columnName, final String newColumnName)
void dropColumn(final SqlNodeList columnList)
SqlAlterTable(final SqlParserPos pos, final AlterType alterType, final String tableName, final String newTableName, final String columnName, final String newColumnName, final SqlNodeList columnList, final Map< String, String > options)