/**
 * END USER LICENSE AGREEMENT (“EULA”)
 *
 * READ THIS AGREEMENT CAREFULLY (date: 9/13/2011):
 * http://www.akiban.com/licensing/20110913
 *
 * BY INSTALLING OR USING ALL OR ANY PORTION OF THE SOFTWARE, YOU ARE ACCEPTING
 * ALL OF THE TERMS AND CONDITIONS OF THIS AGREEMENT. YOU AGREE THAT THIS
 * AGREEMENT IS ENFORCEABLE LIKE ANY WRITTEN AGREEMENT SIGNED BY YOU.
 *
 * IF YOU HAVE PAID A LICENSE FEE FOR USE OF THE SOFTWARE AND DO NOT AGREE TO
 * THESE TERMS, YOU MAY RETURN THE SOFTWARE FOR A FULL REFUND PROVIDED YOU (A) DO
 * NOT USE THE SOFTWARE AND (B) RETURN THE SOFTWARE WITHIN THIRTY (30) DAYS OF
 * YOUR INITIAL PURCHASE.
 *
 * IF YOU WISH TO USE THE SOFTWARE AS AN EMPLOYEE, CONTRACTOR, OR AGENT OF A
 * CORPORATION, PARTNERSHIP OR SIMILAR ENTITY, THEN YOU MUST BE AUTHORIZED TO SIGN
 * FOR AND BIND THE ENTITY IN ORDER TO ACCEPT THE TERMS OF THIS AGREEMENT. THE
 * LICENSES GRANTED UNDER THIS AGREEMENT ARE EXPRESSLY CONDITIONED UPON ACCEPTANCE
 * BY SUCH AUTHORIZED PERSONNEL.
 *
 * IF YOU HAVE ENTERED INTO A SEPARATE WRITTEN LICENSE AGREEMENT WITH AKIBAN FOR
 * USE OF THE SOFTWARE, THE TERMS AND CONDITIONS OF SUCH OTHER AGREEMENT SHALL
 * PREVAIL OVER ANY CONFLICTING TERMS OR CONDITIONS IN THIS AGREEMENT.
 */

package com.akiban.server.service.dxl;

import com.akiban.server.service.session.Session;
import com.akiban.server.service.transaction.TransactionService;

public class DXLTransactionHook implements DXLFunctionsHook {
    private static final Session.StackKey<Boolean> AUTO_TRX_CLOSE = Session.StackKey.stackNamed("AUTO_TRX_CLOSE");
    private final TransactionService txnService;

    public DXLTransactionHook(TransactionService txnService) {
        this.txnService = txnService;
    }

    @Override
    public void hookFunctionIn(Session session, DXLFunction function) {
        if(!needsTransaction(function)) {
            return;
        }
        if(!txnService.isTransactionActive(session)) {
            txnService.beginTransaction(session);
            session.push(AUTO_TRX_CLOSE, Boolean.TRUE);
        } else {
            session.push(AUTO_TRX_CLOSE, Boolean.FALSE);
        }
    }

    @Override
    public void hookFunctionCatch(Session session, DXLFunction function, Throwable throwable) {
        if(!needsTransaction(function)) {
            return;
        }
        Boolean doAuto = session.pop(AUTO_TRX_CLOSE);
        if(doAuto != null && doAuto) {
            txnService.rollbackTransaction(session);
        }
    }

    @Override
    public void hookFunctionFinally(Session session, DXLFunction function, Throwable throwable) {
        if(!needsTransaction(function)) {
            return;
        }
        Boolean doAuto = session.pop(AUTO_TRX_CLOSE);
        if(doAuto != null && doAuto) {
            txnService.commitTransaction(session);
        }
    }
    
    private static boolean needsTransaction(DXLFunction function) {
        switch(function) {
            case CREATE_TABLE:
            case RENAME_TABLE:
            case DROP_TABLE:
            case ALTER_TABLE:
            case CREATE_VIEW:
            case DROP_VIEW:
            case DROP_SCHEMA:
            case DROP_GROUP:
            case CREATE_INDEXES:
            case DROP_INDEXES:
            case GET_DDLS:
            case GET_TABLE_STATISTICS:
            case SCAN_SOME:
            case WRITE_ROW:
            case DELETE_ROW:
            case UPDATE_ROW:
            case TRUNCATE_TABLE:
            case UPDATE_TABLE_STATISTICS:
            case CHECK_AND_FIX_INDEXES:
            case CREATE_SEQUENCE:
            case DROP_SEQUENCE:
            case CREATE_ROUTINE:
            case DROP_ROUTINE:
            case CREATE_SQLJ_JAR:
            case REPLACE_SQLJ_JAR:
            case DROP_SQLJ_JAR:
                return true;

            case GET_AIS:
            case GET_TABLE_ID:
            case GET_TABLE_BY_ID:
            case GET_TABLE_BY_NAME:
            case GET_USER_TABLE_BY_NAME:
            case GET_USER_TABLE_BY_ID:
            case GET_ROWDEF:
            case GET_SCHEMA_ID:
            case GET_SCHEMA_TIMESTAMP:
            case OPEN_CURSOR:
            case GET_CURSOR_STATE:
            case CLOSE_CURSOR:
            case GET_CURSORS:
            case CONVERT_NEW_ROW:
            case CONVERT_ROW_DATA:
            case CONVERT_ROW_DATAS:
                return false;
        }

        throw new IllegalArgumentException("Unexpected function for hook " + function);
    }
}

@@ -4103,19 +4103,23 @@
 ase 
-OPEN_CURSOR
+CONVERT_NEW_ROW
 :%0A  
@@ -4137,24 +4137,24 @@
 ase 
-GET_CURSOR_STATE
+CONVERT_ROW_DATA
 :%0A  
@@ -4173,19 +4173,24 @@
 se C
-LOSE_CURSOR
+ONVERT_ROW_DATAS
 :%0A  
@@ -4208,21 +4208,51 @@
 ase 
-GET
+OPEN
 _CURSOR
-S
 :%0A
+                return true;%0A%0A
     
@@ -4260,39 +4260,40 @@
        case 
-CONVERT_NEW_ROW
+GET_CURSOR_STATE
 :%0A          
@@ -4296,39 +4296,35 @@
       case C
-ONVERT_ROW_DATA
+LOSE_CURSOR
 :%0A          
@@ -4330,32 +4330,26 @@
    case 
-CONVERT_ROW_DATA
+GET_CURSOR
 S:%0A     

Update assumption in transaction hook