OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SqlKeyConstraint.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 org.apache.calcite.sql.SqlCall;
20 import org.apache.calcite.sql.SqlIdentifier;
21 import org.apache.calcite.sql.SqlKind;
22 import org.apache.calcite.sql.SqlNode;
23 import org.apache.calcite.sql.SqlNodeList;
25 import org.apache.calcite.sql.SqlSpecialOperator;
26 import org.apache.calcite.sql.SqlWriter;
27 import org.apache.calcite.sql.parser.SqlParserPos;
29 import org.apache.calcite.util.ImmutableNullableList;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Map;
34 
41 public class SqlKeyConstraint extends SqlCall {
42  private static final SqlSpecialOperator UNIQUE =
43  new SqlSpecialOperator("UNIQUE", SqlKind.UNIQUE);
44 
45  protected static final SqlSpecialOperator PRIMARY =
46  new SqlSpecialOperator("PRIMARY KEY", SqlKind.PRIMARY_KEY);
47 
48  private final SqlIdentifier name;
49  private final SqlNodeList columnList;
50  private final SqlIdentifier referencesCol;
51 
53  SqlKeyConstraint(SqlParserPos pos, SqlIdentifier name, SqlNodeList columnList) {
54  this(pos, name, columnList, null);
55  }
56 
58  SqlKeyConstraint(SqlParserPos pos,
59  SqlIdentifier name,
60  SqlNodeList columnList,
61  SqlIdentifier referencesCol) {
62  super(pos);
63  this.name = name;
64  this.columnList = columnList;
65  this.referencesCol = referencesCol;
66  }
67 
69  public static SqlKeyConstraint unique(
70  SqlParserPos pos, SqlIdentifier name, SqlNodeList columnList) {
71  return new SqlKeyConstraint(pos, name, columnList);
72  }
73 
75  public static SqlKeyConstraint primary(
76  SqlParserPos pos, SqlIdentifier name, SqlNodeList columnList) {
77  return new SqlKeyConstraint(pos, name, columnList) {
78  @Override
79  public SqlOperator getOperator() {
80  return PRIMARY;
81  }
82  };
83  }
84 
86  public static SqlKeyConstraint shard(SqlParserPos pos, SqlIdentifier colName) {
87  SqlNodeList colList = SqlNodeList.of(colName);
88  return new SqlKeyConstraint(pos, new SqlIdentifier("SHARD_KEY", pos), colList);
89  }
90 
93  SqlParserPos pos, SqlIdentifier colName, SqlIdentifier referencesCol) {
94  SqlNodeList colList = SqlNodeList.of(colName);
95  return new SqlKeyConstraint(
96  pos, new SqlIdentifier("SHARED_DICT", pos), colList, referencesCol);
97  }
98 
99  @Override
101  return UNIQUE;
102  }
103 
104  @Override
105  public List<SqlNode> getOperandList() {
106  return ImmutableNullableList.of(name, columnList);
107  }
108 
109  @Override
110  public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
111  if (name != null) {
112  writer.keyword("CONSTRAINT");
113  name.unparse(writer, 0, 0);
114  }
115  writer.keyword(getOperator().getName()); // "UNIQUE" or "PRIMARY KEY"
116  columnList.unparse(writer, 1, 1);
117  }
118 
119  @Override
120  public String toString() {
122  Map<String, Object> map = jsonBuilder.map();
123 
124  jsonBuilder.put(map, "type", "SQL_COLUMN_CONSTRAINT");
125 
126  jsonBuilder.put(map, "name", this.name == null ? null : this.name.toString());
127 
128  List<String> colNamesList = new ArrayList<String>();
129  for (int i = 0; i < columnList.size(); i++) {
130  SqlNode colNode = columnList.get(i);
131  colNamesList.add(colNode.toString());
132  }
133  jsonBuilder.put(map, "columns", colNamesList);
134 
135  Map<String, Object> referencesMap = jsonBuilder.map();
136  if (referencesCol != null) {
137  if (referencesCol.isSimple()) {
138  jsonBuilder.put(referencesMap, "column", referencesCol.toString());
139  } else {
140  jsonBuilder.put(referencesMap, "table", referencesCol.getComponent(0).toString());
141  jsonBuilder.put(
142  referencesMap, "column", referencesCol.getComponent(1).toString());
143  }
144  }
145  jsonBuilder.put(map, "references", referencesMap);
146 
147  return jsonBuilder.toJsonString(map);
148  }
149 }
SqlKeyConstraint(SqlParserPos pos, SqlIdentifier name, SqlNodeList columnList, SqlIdentifier referencesCol)
static SqlKeyConstraint unique(SqlParserPos pos, SqlIdentifier name, SqlNodeList columnList)
static SqlKeyConstraint sharedDict(SqlParserPos pos, SqlIdentifier colName, SqlIdentifier referencesCol)
static SqlKeyConstraint primary(SqlParserPos pos, SqlIdentifier name, SqlNodeList columnList)
SqlKeyConstraint(SqlParserPos pos, SqlIdentifier name, SqlNodeList columnList)
static SqlKeyConstraint shard(SqlParserPos pos, SqlIdentifier colName)
void unparse(SqlWriter writer, int leftPrec, int rightPrec)