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.sql.common.segment.ddl.routine;
19  
20  import lombok.Getter;
21  import lombok.RequiredArgsConstructor;
22  import lombok.Setter;
23  import org.apache.shardingsphere.sql.parser.sql.common.segment.SQLSegment;
24  import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
25  import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.AlterTableStatement;
26  import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;
27  import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropTableStatement;
28  import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.TruncateStatement;
29  import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
30  import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
31  import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
32  import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
33  
34  import java.util.Optional;
35  
36  /**
37   * Valid statement segment.
38   */
39  @RequiredArgsConstructor
40  @Getter
41  @Setter
42  public class ValidStatementSegment implements SQLSegment {
43      
44      private final int startIndex;
45      
46      private final int stopIndex;
47      
48      private SQLStatement sqlStatement;
49      
50      /**
51       * Get create table statement.
52       *
53       * @return create table statement
54       */
55      public Optional<CreateTableStatement> getCreateTable() {
56          return sqlStatement instanceof CreateTableStatement ? Optional.of((CreateTableStatement) sqlStatement) : Optional.empty();
57      }
58      
59      /**
60       * Get alter table statement.
61       *
62       * @return alter table statement
63       */
64      public Optional<AlterTableStatement> getAlterTable() {
65          return sqlStatement instanceof AlterTableStatement ? Optional.of((AlterTableStatement) sqlStatement) : Optional.empty();
66      }
67      
68      /**
69       * Get drop table statement.
70       *
71       * @return drop table statement
72       */
73      public Optional<DropTableStatement> getDropTable() {
74          return sqlStatement instanceof DropTableStatement ? Optional.of((DropTableStatement) sqlStatement) : Optional.empty();
75      }
76      
77      /**
78       * Get truncate statement.
79       *
80       * @return truncate statement
81       */
82      public Optional<TruncateStatement> getTruncate() {
83          return sqlStatement instanceof TruncateStatement ? Optional.of((TruncateStatement) sqlStatement) : Optional.empty();
84      }
85      
86      /**
87       * Get insert statement.
88       *
89       * @return insert statement
90       */
91      public Optional<InsertStatement> getInsert() {
92          return sqlStatement instanceof InsertStatement ? Optional.of((InsertStatement) sqlStatement) : Optional.empty();
93      }
94      
95      /**
96       * Get replace statement.
97       *
98       * @return replace statement
99       */
100     public Optional<InsertStatement> getReplace() {
101         return sqlStatement instanceof InsertStatement ? Optional.of((InsertStatement) sqlStatement) : Optional.empty();
102     }
103     
104     /**
105      * Get update statement.
106      *
107      * @return update statement
108      */
109     public Optional<UpdateStatement> getUpdate() {
110         return sqlStatement instanceof UpdateStatement ? Optional.of((UpdateStatement) sqlStatement) : Optional.empty();
111     }
112     
113     /**
114      * Get delete statement.
115      *
116      * @return delete statement
117      */
118     public Optional<DeleteStatement> getDelete() {
119         return sqlStatement instanceof DeleteStatement ? Optional.of((DeleteStatement) sqlStatement) : Optional.empty();
120     }
121     
122     /**
123      * Get select statement.
124      *
125      * @return select statement
126      */
127     public Optional<SelectStatement> getSelect() {
128         return sqlStatement instanceof SelectStatement ? Optional.of((SelectStatement) sqlStatement) : Optional.empty();
129     }
130 }