OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqlAttributeDefinition.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;
20 
21 import org.apache.calcite.sql.SqlCall;
22 import org.apache.calcite.sql.SqlCollation;
23 import org.apache.calcite.sql.SqlDataTypeSpec;
24 import org.apache.calcite.sql.SqlIdentifier;
25 import org.apache.calcite.sql.SqlKind;
26 import org.apache.calcite.sql.SqlNode;
28 import org.apache.calcite.sql.SqlSpecialOperator;
29 import org.apache.calcite.sql.SqlWriter;
30 import org.apache.calcite.sql.parser.SqlParserPos;
31 
32 import java.util.List;
33 
38 public class SqlAttributeDefinition extends SqlCall {
39  private static final SqlSpecialOperator OPERATOR =
40  new SqlSpecialOperator("ATTRIBUTE_DEF", SqlKind.ATTRIBUTE_DEF);
41 
42  public final SqlIdentifier name;
43  public final SqlDataTypeSpec dataType;
44  final SqlNode expression;
45  final SqlCollation collation;
46 
48  SqlAttributeDefinition(SqlParserPos pos,
49  SqlIdentifier name,
50  SqlDataTypeSpec dataType,
51  SqlNode expression,
52  SqlCollation collation) {
53  super(pos);
54  this.name = name;
55  this.dataType = dataType;
56  this.expression = expression;
57  this.collation = collation;
58  }
59 
60  @Override
62  return OPERATOR;
63  }
64 
65  @Override
66  public List<SqlNode> getOperandList() {
67  return ImmutableList.of(name, dataType);
68  }
69 
70  @Override
71  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
72  name.unparse(writer, 0, 0);
73  dataType.unparse(writer, 0, 0);
74  if (collation != null) {
75  writer.keyword("COLLATE");
76  collation.unparse(writer);
77  }
78  if (dataType.getNullable() != null && !dataType.getNullable()) {
79  writer.keyword("NOT NULL");
80  }
81  if (expression != null) {
82  writer.keyword("DEFAULT");
83  exp(writer);
84  }
85  }
86 
87  // TODO: refactor this to a util class to share with SqlColumnDeclaration
88  private void exp(SqlWriter writer) {
89  if (writer.isAlwaysUseParentheses()) {
90  expression.unparse(writer, 0, 0);
91  } else {
92  writer.sep("(");
93  expression.unparse(writer, 0, 0);
94  writer.sep(")");
95  }
96  }
97 
98  @Override
99  public String toString() {
100  return "NULL";
101  }
102 }
void unparse(SqlWriter writer, int leftPrec, int rightPrec)
SqlAttributeDefinition(SqlParserPos pos, SqlIdentifier name, SqlDataTypeSpec dataType, SqlNode expression, SqlCollation collation)