OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqlColumnDeclaration.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 com.google.common.collect.ImmutableList;
23 
24 import org.apache.calcite.schema.ColumnStrategy;
25 import org.apache.calcite.sql.SqlBasicTypeNameSpec;
26 import org.apache.calcite.sql.SqlCall;
27 import org.apache.calcite.sql.SqlIdentifier;
28 import org.apache.calcite.sql.SqlKind;
29 import org.apache.calcite.sql.SqlNode;
31 import org.apache.calcite.sql.SqlSpecialOperator;
32 import org.apache.calcite.sql.SqlTypeNameSpec;
33 import org.apache.calcite.sql.SqlWriter;
34 import org.apache.calcite.sql.parser.SqlParserPos;
36 
37 import java.util.List;
38 import java.util.Map;
39 
46 public class SqlColumnDeclaration extends SqlCall {
47  private static final SqlSpecialOperator OPERATOR =
48  new SqlSpecialOperator("COLUMN_DECL", SqlKind.COLUMN_DECL);
49 
50  public final SqlIdentifier name;
52  public final SqlNode defaultValue;
53  public final ColumnStrategy strategy;
54 
56  SqlColumnDeclaration(SqlParserPos pos,
57  SqlIdentifier name,
59  SqlNode defaultValue,
60  ColumnStrategy strategy) {
61  super(pos);
62  this.name = name;
63  this.dataType = dataType;
64  this.defaultValue = defaultValue;
65  this.strategy = strategy;
66  }
67 
68  @Override
70  return OPERATOR;
71  }
72 
73  @Override
74  public List<SqlNode> getOperandList() {
75  return ImmutableList.of(name, dataType);
76  }
77 
78  @Override
79  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
80  name.unparse(writer, 0, 0);
81  dataType.unparse(writer, 0, 0);
82  if (dataType.getNullable() != null && !dataType.getNullable()) {
83  writer.keyword("NOT NULL");
84  }
85  if (defaultValue != null) {
86  switch (strategy) {
87  case DEFAULT:
88  writer.keyword("DEFAULT");
89  exp(writer);
90  break;
91  default:
92  throw new AssertionError("unexpected: " + strategy);
93  }
94  }
95  }
96 
97  private void exp(SqlWriter writer) {
98  if (writer.isAlwaysUseParentheses()) {
99  defaultValue.unparse(writer, 0, 0);
100  } else {
101  writer.sep("(");
102  defaultValue.unparse(writer, 0, 0);
103  writer.sep(")");
104  }
105  }
106 
107  @Override
108  public String toString() {
110  Map<String, Object> map = jsonBuilder.map();
111 
112  jsonBuilder.put(map, "type", "SQL_COLUMN_DECLARATION");
113 
114  jsonBuilder.put(map, "name", name == null ? null : name.toString());
115 
116  jsonBuilder.put(
117  map, "default", defaultValue == null ? null : defaultValue.toString());
118  jsonBuilder.put(map, "nullable", !Boolean.FALSE.equals(dataType.getNullable()));
119  jsonBuilder.put(map, "encodingType", dataType.getEncodingString());
120  jsonBuilder.put(map, "encodingSize", dataType.getEncodingSize());
121 
122  SqlTypeNameSpec dataTypeSpec = dataType.getTypeNameSpec();
123  if (dataTypeSpec instanceof HeavyDBGeoTypeNameSpec) {
124  map = ((HeavyDBGeoTypeNameSpec) dataTypeSpec).toJsonMap(map);
125  } else {
126  boolean isText = false;
127  if (dataTypeSpec instanceof HeavyDBTypeNameSpec) {
128  HeavyDBTypeNameSpec heavyDBDataTypeSpec = (HeavyDBTypeNameSpec) dataTypeSpec;
129  if (heavyDBDataTypeSpec.getIsArray()) {
130  jsonBuilder.put(map, "arraySize", heavyDBDataTypeSpec.getArraySize());
131  }
132  isText = heavyDBDataTypeSpec.getIsText();
133  }
134  jsonBuilder.put(
135  map, "precision", ((SqlBasicTypeNameSpec) dataTypeSpec).getPrecision());
136  jsonBuilder.put(map, "scale", ((SqlBasicTypeNameSpec) dataTypeSpec).getScale());
137  if (isText) {
138  jsonBuilder.put(map, "sqltype", "TEXT");
139  } else {
140  jsonBuilder.put(map,
141  "sqltype",
142  dataType == null ? null : dataTypeSpec.getTypeName().toString());
143  }
144  }
145  return jsonBuilder.toJsonString(map);
146  }
147 }
void unparse(SqlWriter writer, int leftPrec, int rightPrec)
SqlColumnDeclaration(SqlParserPos pos, SqlIdentifier name, HeavyDBSqlDataTypeSpec dataType, SqlNode defaultValue, ColumnStrategy strategy)