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.mysql.visitor.statement.type;
19  
20  import org.antlr.v4.runtime.Token;
21  import org.apache.shardingsphere.sql.parser.api.ASTNode;
22  import org.apache.shardingsphere.sql.parser.api.visitor.statement.type.TCLStatementVisitor;
23  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BeginTransactionContext;
24  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CommitContext;
25  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IsolationTypesContext;
26  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OptionTypeContext;
27  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ReleaseSavepointContext;
28  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.RollbackContext;
29  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SavepointContext;
30  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SetAutoCommitContext;
31  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.SetTransactionContext;
32  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TransactionAccessModeContext;
33  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.XaBeginContext;
34  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.XaCommitContext;
35  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.XaEndContext;
36  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.XaPrepareContext;
37  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.XaRecoveryContext;
38  import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.XaRollbackContext;
39  import org.apache.shardingsphere.sql.parser.mysql.visitor.statement.MySQLStatementVisitor;
40  import org.apache.shardingsphere.sql.parser.statement.core.enums.OperationScope;
41  import org.apache.shardingsphere.sql.parser.statement.core.enums.TransactionAccessType;
42  import org.apache.shardingsphere.sql.parser.statement.core.enums.TransactionIsolationLevel;
43  import org.apache.shardingsphere.sql.parser.statement.core.segment.tcl.AutoCommitSegment;
44  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.BeginTransactionStatement;
45  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.CommitStatement;
46  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.ReleaseSavepointStatement;
47  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.RollbackStatement;
48  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.SavepointStatement;
49  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.SetAutoCommitStatement;
50  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.SetTransactionStatement;
51  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.xa.XABeginStatement;
52  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.xa.XACommitStatement;
53  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.xa.XAEndStatement;
54  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.xa.XAPrepareStatement;
55  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.xa.XARecoveryStatement;
56  import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.xa.XARollbackStatement;
57  import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
58  
59  /**
60   * TCL statement visitor for MySQL.
61   */
62  public final class MySQLTCLStatementVisitor extends MySQLStatementVisitor implements TCLStatementVisitor {
63      
64      @Override
65      public ASTNode visitSetTransaction(final SetTransactionContext ctx) {
66          return new SetTransactionStatement(getOperationScope(ctx.optionType()),
67                  getTransactionIsolationLevel(null == ctx.transactionCharacteristics().isolationLevel() ? null : ctx.transactionCharacteristics().isolationLevel().isolationTypes()),
68                  getTransactionAccessType(ctx.transactionCharacteristics().transactionAccessMode()));
69      }
70      
71      private OperationScope getOperationScope(final OptionTypeContext ctx) {
72          if (null == ctx) {
73              return null;
74          }
75          if (null != ctx.SESSION()) {
76              return OperationScope.SESSION;
77          }
78          if (null != ctx.GLOBAL()) {
79              return OperationScope.GLOBAL;
80          }
81          return null;
82      }
83      
84      private TransactionIsolationLevel getTransactionIsolationLevel(final IsolationTypesContext ctx) {
85          if (null == ctx) {
86              return null;
87          }
88          if (null != ctx.SERIALIZABLE()) {
89              return TransactionIsolationLevel.SERIALIZABLE;
90          }
91          if (null != ctx.COMMITTED()) {
92              return TransactionIsolationLevel.READ_COMMITTED;
93          }
94          if (null != ctx.UNCOMMITTED()) {
95              return TransactionIsolationLevel.READ_UNCOMMITTED;
96          }
97          if (null != ctx.REPEATABLE()) {
98              return TransactionIsolationLevel.REPEATABLE_READ;
99          }
100         return null;
101     }
102     
103     private TransactionAccessType getTransactionAccessType(final TransactionAccessModeContext ctx) {
104         if (null == ctx) {
105             return null;
106         }
107         if (null != ctx.ONLY()) {
108             return TransactionAccessType.READ_ONLY;
109         }
110         if (null != ctx.WRITE()) {
111             return TransactionAccessType.READ_WRITE;
112         }
113         return null;
114     }
115     
116     @Override
117     public ASTNode visitSetAutoCommit(final SetAutoCommitContext ctx) {
118         return new SetAutoCommitStatement(generateAutoCommitSegment(ctx.autoCommitValue).isAutoCommit());
119     }
120     
121     private AutoCommitSegment generateAutoCommitSegment(final Token ctx) {
122         boolean autoCommit = "1".equals(ctx.getText()) || "ON".equals(ctx.getText());
123         return new AutoCommitSegment(ctx.getStartIndex(), ctx.getStopIndex(), autoCommit);
124     }
125     
126     @Override
127     public ASTNode visitBeginTransaction(final BeginTransactionContext ctx) {
128         return new BeginTransactionStatement();
129     }
130     
131     @Override
132     public ASTNode visitCommit(final CommitContext ctx) {
133         return new CommitStatement();
134     }
135     
136     @Override
137     public ASTNode visitRollback(final RollbackContext ctx) {
138         return null == ctx.identifier() ? new RollbackStatement() : new RollbackStatement(((IdentifierValue) visit(ctx.identifier())).getValue());
139     }
140     
141     @Override
142     public ASTNode visitSavepoint(final SavepointContext ctx) {
143         return new SavepointStatement(((IdentifierValue) visit(ctx.identifier())).getValue());
144     }
145     
146     @Override
147     public ASTNode visitReleaseSavepoint(final ReleaseSavepointContext ctx) {
148         return new ReleaseSavepointStatement(((IdentifierValue) visit(ctx.identifier())).getValue());
149     }
150     
151     @Override
152     public ASTNode visitXaBegin(final XaBeginContext ctx) {
153         return new XABeginStatement(ctx.xid().getText());
154     }
155     
156     @Override
157     public ASTNode visitXaPrepare(final XaPrepareContext ctx) {
158         return new XAPrepareStatement(ctx.xid().getText());
159     }
160     
161     @Override
162     public ASTNode visitXaCommit(final XaCommitContext ctx) {
163         return new XACommitStatement(ctx.xid().getText());
164     }
165     
166     @Override
167     public ASTNode visitXaRollback(final XaRollbackContext ctx) {
168         return new XARollbackStatement(ctx.xid().getText());
169     }
170     
171     @Override
172     public ASTNode visitXaEnd(final XaEndContext ctx) {
173         return new XAEndStatement(ctx.xid().getText());
174     }
175     
176     @Override
177     public ASTNode visitXaRecovery(final XaRecoveryContext ctx) {
178         return new XARecoveryStatement();
179     }
180 }