OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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.SqlBasicTypeNameSpec;
24 import org.apache.calcite.sql.SqlCall;
25 import org.apache.calcite.sql.SqlDataTypeSpec;
26 import org.apache.calcite.sql.SqlDdl;
27 import org.apache.calcite.sql.SqlIdentifier;
28 import org.apache.calcite.sql.SqlKind;
29 import org.apache.calcite.sql.SqlNode;
30 import org.apache.calcite.sql.SqlNodeList;
32 import org.apache.calcite.sql.SqlSpecialOperator;
33 import org.apache.calcite.sql.SqlWriter;
34 import org.apache.calcite.sql.parser.SqlParserPos;
35 import org.apache.calcite.sql.util.SqlBasicVisitor;
36 import org.apache.calcite.sql.util.SqlVisitor;
38 import org.apache.calcite.util.JsonBuilder;
39 
40 import java.util.List;
41 import java.util.Map;
42 
47 public class SqlAlterTable extends SqlDdl {
48  private static final SqlOperator OPERATOR =
49  new SqlSpecialOperator("ALTER_TABLE", SqlKind.OTHER_DDL);
50 
74  public enum AlterType {
80  ALTER_OPTIONS
81  }
82 
83  public static class Builder extends SqlOptionsBuilder {
84  private SqlParserPos pos;
86  private String tableName;
87  private String newTableName;
88  private String columnName;
89  private String newColumnName;
90  private SqlNodeList columnList;
91 
92  public void setPos(final SqlParserPos pos) {
93  this.pos = pos;
94  }
95 
96  public void setTableName(final String tableName) {
97  this.tableName = tableName;
98  }
99 
100  public void alterOptions() {
101  this.alterType = AlterType.ALTER_OPTIONS;
102  // Options should be read in directly to base class
103  }
104 
105  public void alterTableName(final String newTableName) {
106  this.alterType = AlterType.RENAME_TABLE;
107  this.newTableName = newTableName;
108  }
109 
110  public void alterColumnName(final String columnName, final String newColumnName) {
111  this.alterType = AlterType.RENAME_COLUMN;
112  this.columnName = columnName;
113  this.newColumnName = newColumnName;
114  }
115 
116  public void addColumnList(final SqlNodeList columnList) {
117  this.alterType = AlterType.ADD_COLUMN;
118  this.columnList = columnList;
119  }
120 
121  public void addAlterColumnList(final SqlNodeList columnList) {
122  this.alterType = AlterType.ALTER_COLUMN;
123  this.columnList = columnList;
124  }
125 
126  public void dropColumn(final SqlNodeList columnList) {
127  this.alterType = AlterType.DROP_COLUMN;
128  this.columnList = columnList;
129  }
130 
131  public SqlAlterTable build() {
132  return new SqlAlterTable(pos,
133  alterType,
134  tableName,
135  newTableName,
136  columnName,
138  columnList,
139  super.options);
140  }
141  }
142 
143  @Expose
145  @Expose
146  private String tableName;
147  @Expose
148  private String newTableName;
149  @Expose
150  private String columnName;
151  @Expose
152  private String newColumnName;
153  @Expose
154  private String command;
155  @Expose
156  private SqlNodeList columnList;
157  @Expose
158  private Map<String, String> options;
159 
160  public SqlAlterTable(final SqlParserPos pos, final SqlIdentifier name) {
161  super(OPERATOR, pos);
162  this.tableName = name.toString();
163  }
164 
165  public SqlAlterTable(final SqlParserPos pos,
166  final AlterType alterType,
167  final String tableName,
168  final String newTableName,
169  final String columnName,
170  final String newColumnName,
171  final SqlNodeList columnList,
172  final Map<String, String> options) {
173  super(OPERATOR, pos);
174  this.alterType = alterType;
175  this.tableName = tableName;
176  this.newTableName = newTableName;
177  this.columnName = columnName;
178  this.newColumnName = newColumnName;
179  this.options = options;
180  this.columnList = columnList;
181  this.command = OPERATOR.getName();
182  }
183 
184  // @Override
185  public List<SqlNode> getOperandList() {
186  // Add the operands here
187  return null;
188  }
189 
190  @Override
191  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
192  writer.keyword("ALTER");
193  writer.keyword("TABLE");
194  // add other options data here when/as necessary
195  }
196 
197  @Override
198  public String toString() {
199  JsonBuilder jsonBuilder = new EscapedStringJsonBuilder();
200  Map<String, Object> map = jsonBuilder.map();
201 
202  map.put("command", "ALTER_TABLE");
203  map.put("tableName", this.tableName.toString());
204  switch (this.alterType) {
205  case RENAME_TABLE:
206  map.put("alterType", "RENAME_TABLE");
207  map.put("newTableName", this.newTableName.toString());
208  break;
209  case RENAME_COLUMN:
210  map.put("alterType", "RENAME_COLUMN");
211  map.put("columnName", this.columnName.toString());
212  map.put("newColumnName", this.newColumnName.toString());
213  break;
214  case ADD_COLUMN:
215  map.put("alterType", "ADD_COLUMN");
216  if (this.columnList != null) {
217  List<Object> elements_list = jsonBuilder.list();
218  for (SqlNode elementNode : this.columnList) {
219  if (!(elementNode instanceof SqlCall)) {
220  throw new CalciteException("Column definition for table "
221  + this.columnName.toString()
222  + " is invalid: " + elementNode.toString(),
223  null);
224  }
225  elements_list.add(elementNode);
226  }
227  map.put("columnData", elements_list);
228  }
229 
230  break;
231  case ALTER_COLUMN:
232  map.put("alterType", "ALTER_COLUMN");
233  if (this.columnList != null) {
234  List<Object> elements_list = jsonBuilder.list();
235  for (SqlNode elementNode : this.columnList) {
236  if (!(elementNode instanceof SqlCall)) {
237  throw new CalciteException("Column definition " + this.tableName.toString()
238  + " is invalid: " + elementNode.toString(),
239  null);
240  }
241 
242  SqlVisitor<Object> sqlVisitor = new SqlBasicVisitor<Object>() {
243  @Override
244  public Object visit(SqlIdentifier id) {
245  column_name = id.toString();
246  return null;
247  }
248  @Override
249  public Object visit(SqlDataTypeSpec spec) {
250  if (!(spec.getTypeNameSpec() instanceof SqlBasicTypeNameSpec)) {
251  throw new CalciteException("Type definition for column "
252  + column_name.toString()
253  + " is invalid: " + spec.toString(),
254  null);
255  }
256  return null;
257  }
258 
259  String column_name = "<UNSET>";
260  };
261  elementNode.accept(sqlVisitor);
262 
263  elements_list.add(elementNode);
264  }
265  map.put("alterData", elements_list);
266  }
267  break;
268  case DROP_COLUMN:
269  map.put("alterType", "DROP_COLUMN");
270  map.put("columnData", this.columnList.toString());
271  break;
272  case ALTER_OPTIONS:
273  map.put("alterType", "ALTER_OPTIONS");
274  map.put("options", this.options);
275  break;
276  }
277 
278  Map<String, Object> payload = jsonBuilder.map();
279  payload.put("payload", map);
280  return jsonBuilder.toJsonString(payload);
281  }
282 }
void unparse(SqlWriter writer, int leftPrec, int rightPrec)
void addColumnList(final SqlNodeList columnList)
SqlAlterTable(final SqlParserPos pos, final SqlIdentifier name)
void alterColumnName(final String columnName, final String newColumnName)
void addAlterColumnList(final SqlNodeList columnList)
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)
string name
Definition: setup.in.py:72