View Javadoc
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 org.apache.shardingsphere.sql.parser.sql92.visitor.statement.type;
19  
20  import org.apache.shardingsphere.sql.parser.api.ASTNode;
21  import org.apache.shardingsphere.sql.parser.api.visitor.statement.type.DDLStatementVisitor;
22  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.AddColumnSpecificationContext;
23  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.AddConstraintSpecificationContext;
24  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.AlterDefinitionClauseContext;
25  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.AlterTableContext;
26  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.CheckConstraintDefinitionContext;
27  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.ColumnDefinitionContext;
28  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.ConstraintDefinitionContext;
29  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.ConstraintNameContext;
30  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.CreateDefinitionClauseContext;
31  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.CreateDefinitionContext;
32  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.CreateTableContext;
33  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.DataTypeOptionContext;
34  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.DropColumnSpecificationContext;
35  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.DropConstraintSpecificationContext;
36  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.DropTableContext;
37  import org.apache.shardingsphere.sql.parser.autogen.SQL92StatementParser.ModifyColumnSpecificationContext;
38  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.AlterDefinitionSegment;
39  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.CreateDefinitionSegment;
40  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.ColumnDefinitionSegment;
41  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.alter.AddColumnDefinitionSegment;
42  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.alter.DropColumnDefinitionSegment;
43  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.alter.ModifyColumnDefinitionSegment;
44  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintDefinitionSegment;
45  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
46  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.alter.AddConstraintDefinitionSegment;
47  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.alter.DropConstraintDefinitionSegment;
48  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
49  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DataTypeSegment;
50  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
51  import org.apache.shardingsphere.sql.parser.sql.common.value.collection.CollectionValue;
52  import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
53  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.ddl.SQL92AlterTableStatement;
54  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.ddl.SQL92CreateTableStatement;
55  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.ddl.SQL92DropTableStatement;
56  import org.apache.shardingsphere.sql.parser.sql92.visitor.statement.SQL92StatementVisitor;
57  
58  import java.util.Collections;
59  
60  /**
61   * DDL statement visitor for SQL92.
62   */
63  public final class SQL92DDLStatementVisitor extends SQL92StatementVisitor implements DDLStatementVisitor {
64      
65      @SuppressWarnings("unchecked")
66      @Override
67      public ASTNode visitCreateTable(final CreateTableContext ctx) {
68          SQL92CreateTableStatement result = new SQL92CreateTableStatement();
69          result.setTable((SimpleTableSegment) visit(ctx.tableName()));
70          if (null != ctx.createDefinitionClause()) {
71              CollectionValue<CreateDefinitionSegment> createDefinitions = (CollectionValue<CreateDefinitionSegment>) visit(ctx.createDefinitionClause());
72              for (CreateDefinitionSegment each : createDefinitions.getValue()) {
73                  if (each instanceof ColumnDefinitionSegment) {
74                      result.getColumnDefinitions().add((ColumnDefinitionSegment) each);
75                  } else if (each instanceof ConstraintDefinitionSegment) {
76                      result.getConstraintDefinitions().add((ConstraintDefinitionSegment) each);
77                  }
78              }
79          }
80          return result;
81      }
82      
83      @Override
84      public ASTNode visitCreateDefinitionClause(final CreateDefinitionClauseContext ctx) {
85          CollectionValue<CreateDefinitionSegment> result = new CollectionValue<>();
86          for (CreateDefinitionContext each : ctx.createDefinition()) {
87              if (null != each.columnDefinition()) {
88                  result.getValue().add((ColumnDefinitionSegment) visit(each.columnDefinition()));
89              }
90              if (null != each.constraintDefinition()) {
91                  result.getValue().add((ConstraintDefinitionSegment) visit(each.constraintDefinition()));
92              }
93              if (null != each.checkConstraintDefinition()) {
94                  result.getValue().add((ConstraintDefinitionSegment) visit(each.checkConstraintDefinition()));
95              }
96          }
97          return result;
98      }
99      
100     @Override
101     public ASTNode visitColumnDefinition(final ColumnDefinitionContext ctx) {
102         ColumnSegment column = (ColumnSegment) visit(ctx.columnName());
103         DataTypeSegment dataType = (DataTypeSegment) visit(ctx.dataType());
104         boolean isPrimaryKey = ctx.dataTypeOption().stream().anyMatch(each -> null != each.primaryKey());
105         // TODO parse not null
106         ColumnDefinitionSegment result = new ColumnDefinitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), column, dataType, isPrimaryKey, false);
107         for (DataTypeOptionContext each : ctx.dataTypeOption()) {
108             if (null != each.referenceDefinition()) {
109                 result.getReferencedTables().add((SimpleTableSegment) visit(each.referenceDefinition().tableName()));
110             }
111         }
112         return result;
113     }
114     
115     @Override
116     public ASTNode visitCheckConstraintDefinition(final CheckConstraintDefinitionContext ctx) {
117         return new ConstraintDefinitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
118     }
119     
120     @Override
121     public ASTNode visitAddConstraintSpecification(final AddConstraintSpecificationContext ctx) {
122         return new AddConstraintDefinitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (ConstraintDefinitionSegment) visit(ctx.constraintDefinition()));
123     }
124     
125     @Override
126     public ASTNode visitDropConstraintSpecification(final DropConstraintSpecificationContext ctx) {
127         return new DropConstraintDefinitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (ConstraintSegment) visit(ctx.constraintDefinition().constraintName()));
128     }
129     
130     @SuppressWarnings("unchecked")
131     @Override
132     public ASTNode visitConstraintDefinition(final ConstraintDefinitionContext ctx) {
133         ConstraintDefinitionSegment result = new ConstraintDefinitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex());
134         if (null != ctx.constraintName()) {
135             result.setConstraintName((ConstraintSegment) visit(ctx.constraintName()));
136         }
137         if (null != ctx.primaryKeyOption()) {
138             result.getPrimaryKeyColumns().addAll(((CollectionValue<ColumnSegment>) visit(ctx.primaryKeyOption().columnNames())).getValue());
139         }
140         if (null != ctx.foreignKeyOption()) {
141             result.setReferencedTable((SimpleTableSegment) visit(ctx.foreignKeyOption().referenceDefinition().tableName()));
142         }
143         return result;
144     }
145     
146     @Override
147     public ASTNode visitConstraintName(final ConstraintNameContext ctx) {
148         return new ConstraintSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (IdentifierValue) visit(ctx.identifier()));
149     }
150     
151     @SuppressWarnings("unchecked")
152     @Override
153     public ASTNode visitAlterTable(final AlterTableContext ctx) {
154         SQL92AlterTableStatement result = new SQL92AlterTableStatement();
155         result.setTable((SimpleTableSegment) visit(ctx.tableName()));
156         if (null != ctx.alterDefinitionClause()) {
157             for (AlterDefinitionSegment each : ((CollectionValue<AlterDefinitionSegment>) visit(ctx.alterDefinitionClause())).getValue()) {
158                 if (each instanceof AddColumnDefinitionSegment) {
159                     result.getAddColumnDefinitions().add((AddColumnDefinitionSegment) each);
160                 } else if (each instanceof ModifyColumnDefinitionSegment) {
161                     result.getModifyColumnDefinitions().add((ModifyColumnDefinitionSegment) each);
162                 } else if (each instanceof DropColumnDefinitionSegment) {
163                     result.getDropColumnDefinitions().add((DropColumnDefinitionSegment) each);
164                 } else if (each instanceof AddConstraintDefinitionSegment) {
165                     result.getAddConstraintDefinitions().add((AddConstraintDefinitionSegment) each);
166                 } else if (each instanceof DropConstraintDefinitionSegment) {
167                     result.getDropConstraintDefinitions().add((DropConstraintDefinitionSegment) each);
168                 }
169             }
170         }
171         return result;
172     }
173     
174     @SuppressWarnings("unchecked")
175     @Override
176     public ASTNode visitAlterDefinitionClause(final AlterDefinitionClauseContext ctx) {
177         CollectionValue<AlterDefinitionSegment> result = new CollectionValue<>();
178         if (null != ctx.addColumnSpecification()) {
179             result.getValue().addAll(((CollectionValue<AddColumnDefinitionSegment>) visit(ctx.addColumnSpecification())).getValue());
180         }
181         if (null != ctx.modifyColumnSpecification()) {
182             result.getValue().add((ModifyColumnDefinitionSegment) visit(ctx.modifyColumnSpecification()));
183         }
184         if (null != ctx.dropColumnSpecification()) {
185             result.getValue().add((DropColumnDefinitionSegment) visit(ctx.dropColumnSpecification()));
186         }
187         return result;
188     }
189     
190     @Override
191     public ASTNode visitAddColumnSpecification(final AddColumnSpecificationContext ctx) {
192         CollectionValue<AddColumnDefinitionSegment> result = new CollectionValue<>();
193         AddColumnDefinitionSegment addColumnDefinition = new AddColumnDefinitionSegment(
194                 ctx.columnDefinition().getStart().getStartIndex(), ctx.columnDefinition().getStop().getStopIndex(),
195                 Collections.singletonList((ColumnDefinitionSegment) visit(ctx.columnDefinition())));
196         result.getValue().add(addColumnDefinition);
197         return result;
198     }
199     
200     @Override
201     public ASTNode visitModifyColumnSpecification(final ModifyColumnSpecificationContext ctx) {
202         // TODO visit pk and table ref
203         return new ModifyColumnDefinitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (ColumnDefinitionSegment) visit(ctx.columnDefinition()));
204     }
205     
206     @Override
207     public ASTNode visitDropColumnSpecification(final DropColumnSpecificationContext ctx) {
208         return new DropColumnDefinitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), Collections.singleton((ColumnSegment) visit(ctx.columnName())));
209     }
210     
211     @SuppressWarnings("unchecked")
212     @Override
213     public ASTNode visitDropTable(final DropTableContext ctx) {
214         SQL92DropTableStatement result = new SQL92DropTableStatement();
215         result.getTables().addAll(((CollectionValue<SimpleTableSegment>) visit(ctx.tableNames())).getValue());
216         return result;
217     }
218 }