Commit bfd47bcf authored by terrymanu's avatar terrymanu
Browse files

refactor TransactionOperationType.getOperationType

parent c6c0d474
Loading
Loading
Loading
Loading
+25 −1
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@

package io.shardingsphere.core.constant.transaction;

import com.google.common.base.Optional;

/**
 * Transaction operation type.
 *
@@ -24,5 +26,27 @@ package io.shardingsphere.core.constant.transaction;
 */
public enum TransactionOperationType {
    
    BEGIN, COMMIT, ROLLBACK
    BEGIN, COMMIT, ROLLBACK;
    
    /**
     * Get operation type.
     * 
     * @param sql SQL
     * @return transaction operation type
     */
    // TODO :hongjun move to TCLParser, need parse comment etc
    public static Optional<TransactionOperationType> getOperationType(final String sql) {
        switch (sql.toUpperCase()) {
            case "BEGIN":
            case "START TRANSACTION":
            case "SET AUTOCOMMIT=0":
                return Optional.of(TransactionOperationType.BEGIN);
            case "COMMIT":
                return Optional.of(TransactionOperationType.COMMIT);
            case "ROLLBACK":
                return Optional.of(TransactionOperationType.ROLLBACK);
            default:
                return Optional.absent();
        }
    }
}
+1 −5
Original line number Diff line number Diff line
@@ -24,12 +24,8 @@ package io.shardingsphere.proxy.backend.jdbc.transaction;
 */
public final class DefaultTransactionEngine extends TransactionEngine {
    
    public DefaultTransactionEngine(final String sql) {
        super(sql);
    }
    
    @Override
    public boolean execute() {
        return parseSQL().isPresent();
        return false;
    }
}
+0 −20
Original line number Diff line number Diff line
@@ -17,8 +17,6 @@

package io.shardingsphere.proxy.backend.jdbc.transaction;

import com.google.common.base.Optional;
import io.shardingsphere.core.constant.transaction.TransactionOperationType;
import lombok.RequiredArgsConstructor;

import java.sql.SQLException;
@@ -31,24 +29,6 @@ import java.sql.SQLException;
@RequiredArgsConstructor
public abstract class TransactionEngine {
    
    private final String sql;
    
    // TODO :yonglun move to TCLParser
    protected final Optional<TransactionOperationType> parseSQL() {
        switch (sql.toUpperCase()) {
            case "BEGIN": 
            case "START TRANSACTION":
            case "SET AUTOCOMMIT=0":
                return Optional.of(TransactionOperationType.BEGIN);
            case "COMMIT":
                return Optional.of(TransactionOperationType.COMMIT);
            case "ROLLBACK":
                return Optional.of(TransactionOperationType.ROLLBACK);
            default:
                return Optional.absent();
        }
    }
    
    /**
     * Execute transaction with binding transaction manager.
     *
+6 −8
Original line number Diff line number Diff line
@@ -22,27 +22,25 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * Create transaction engine based on current transaction type.
 * Transaction engine factory.
 *
 * @author zhaojun
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TransactionEngineFactory {
    
    private static final RuleRegistry RULE_REGISTRY = RuleRegistry.getInstance();
    
    /**
     * Create transaction engine from SQL.
     * Create new instance of transaction engine.
     * 
     * @param sql SQL
     * @return transaction engine
     * @return new instance of transaction engine
     */
    public static TransactionEngine create(final String sql) {
        switch (RULE_REGISTRY.getTransactionType()) {
    public static TransactionEngine newInstance(final String sql) {
        switch (RuleRegistry.getInstance().getTransactionType()) {
            case XA:
                return new XATransactionEngine(sql);
            default:
                return new DefaultTransactionEngine(sql);
                return new DefaultTransactionEngine();
        }
    }
}
+4 −4
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.transaction.manager.ShardingTransactionManagerRegistry;
import io.shardingsphere.transaction.TransactionTypeHolder;
import io.shardingsphere.transaction.event.xa.XATransactionEvent;
import lombok.RequiredArgsConstructor;

import javax.transaction.Status;
import java.sql.SQLException;
@@ -33,15 +34,14 @@ import java.sql.SQLException;
 *
 * @author zhaojun
 */
@RequiredArgsConstructor
public final class XATransactionEngine extends TransactionEngine {
    
    public XATransactionEngine(final String sql) {
        super(sql);
    }
    private final String sql;
    
    @Override
    public boolean execute() throws SQLException {
        Optional<TransactionOperationType> operationType = parseSQL();
        Optional<TransactionOperationType> operationType = TransactionOperationType.getOperationType(sql);
        if (operationType.isPresent() && isInTransaction(operationType.get())) {
            TransactionTypeHolder.set(TransactionType.XA);
            EventBusInstance.getInstance().post(new XATransactionEvent(operationType.get()));
Loading