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