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.infra.binder.context.statement.ddl;
19  
20  import lombok.Getter;
21  import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
22  import org.apache.shardingsphere.infra.binder.context.statement.CommonSQLStatementContext;
23  import org.apache.shardingsphere.infra.binder.context.type.ConstraintAvailable;
24  import org.apache.shardingsphere.infra.binder.context.type.IndexAvailable;
25  import org.apache.shardingsphere.infra.binder.context.type.TableAvailable;
26  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.ColumnDefinitionSegment;
27  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintDefinitionSegment;
28  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
29  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
30  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
31  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
32  import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;
33  
34  import java.util.Collection;
35  import java.util.LinkedList;
36  
37  /**
38   * Create table statement context.
39   */
40  @Getter
41  public final class CreateTableStatementContext extends CommonSQLStatementContext implements TableAvailable, IndexAvailable, ConstraintAvailable {
42      
43      private final TablesContext tablesContext;
44      
45      public CreateTableStatementContext(final CreateTableStatement sqlStatement) {
46          super(sqlStatement);
47          tablesContext = new TablesContext(sqlStatement.getTable(), getDatabaseType());
48      }
49      
50      @Override
51      public CreateTableStatement getSqlStatement() {
52          return (CreateTableStatement) super.getSqlStatement();
53      }
54      
55      @Override
56      public Collection<SimpleTableSegment> getAllTables() {
57          Collection<SimpleTableSegment> result = new LinkedList<>();
58          result.add(getSqlStatement().getTable());
59          for (ColumnDefinitionSegment each : getSqlStatement().getColumnDefinitions()) {
60              result.addAll(each.getReferencedTables());
61          }
62          for (ConstraintDefinitionSegment each : getSqlStatement().getConstraintDefinitions()) {
63              if (each.getReferencedTable().isPresent()) {
64                  result.add(each.getReferencedTable().get());
65              }
66          }
67          return result;
68      }
69      
70      @Override
71      public Collection<IndexSegment> getIndexes() {
72          Collection<IndexSegment> result = new LinkedList<>();
73          for (ConstraintDefinitionSegment each : getSqlStatement().getConstraintDefinitions()) {
74              each.getIndexName().ifPresent(result::add);
75          }
76          return result;
77      }
78      
79      @Override
80      public Collection<ConstraintSegment> getConstraints() {
81          Collection<ConstraintSegment> result = new LinkedList<>();
82          for (ConstraintDefinitionSegment each : getSqlStatement().getConstraintDefinitions()) {
83              each.getConstraintName().ifPresent(result::add);
84          }
85          return result;
86      }
87      
88      @Override
89      public Collection<ColumnSegment> getIndexColumns() {
90          Collection<ColumnSegment> result = new LinkedList<>();
91          for (ConstraintDefinitionSegment each : getSqlStatement().getConstraintDefinitions()) {
92              result.addAll(each.getIndexColumns());
93          }
94          return result;
95      }
96  }