001/* 
002 * JKNIV, SQLegance keeping queries maintainable.
003 * 
004 * Copyright (C) 2017, the original author or authors.
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 2.1 of the License.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with this library; if not, write to the Free Software Foundation, Inc., 
018 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
019 */
020package net.sf.jkniv.sqlegance.transaction;
021
022import java.sql.Connection;
023
024/**
025 * Enumeration that represents transaction isolation levels for use with the connection data source.
026 * <ul>
027 *  <li>NONE</li>
028 *  <li>READ_UNCOMMITTED</li>
029 *  <li>READ_COMMITTED</li>
030 *  <li>REPEATABLE_READ</li>
031 *  <li>SERIALIZABLE</li>
032 * </ul>
033 * @author Alisson Gomes
034 *
035 */
036public enum Isolation
037{
038    /** Use the default isolation level of the underlying connection data source. 
039     * This level cannot be set at connection isolation.*/
040    DEFAULT {
041        /**
042         * @throws UnsupportedOperationException
043         */
044        public int level() { throw new UnsupportedOperationException("java.sql.Connection haven't this isolation level. Get a valid level."); }
045    }, 
046    /** Transactions are not supported */
047    NONE {
048        public int level() { return Connection.TRANSACTION_NONE; }
049    },
050    /** Dirty reads, non-repeatable reads and phantom reads can occur. */
051    READ_UNCOMMITTED {
052        public int level() { return Connection.TRANSACTION_READ_UNCOMMITTED; }
053    },
054    /** Dirty reads are prevented; non-repeatable reads and phantom reads can occur. */
055    READ_COMMITTED {
056        public int level() { return Connection.TRANSACTION_READ_COMMITTED; }
057    },
058    /** Dirty reads and non-repeatable reads are prevented; phantom reads can occur. */
059    REPEATABLE_READ {
060        public int level() { return Connection.TRANSACTION_REPEATABLE_READ; }
061    },
062    /** Dirty reads, non-repeatable reads and phantom reads are prevented. */
063    SERIALIZABLE {
064        public int level() { return Connection.TRANSACTION_SERIALIZABLE; }
065    };
066    
067    public abstract int level();
068    
069    public static Isolation get(int level)
070    {
071        Isolation isolation = Isolation.DEFAULT;
072        switch (level)
073        {
074            case Connection.TRANSACTION_NONE:
075                isolation = NONE;
076                break;
077            case Connection.TRANSACTION_READ_COMMITTED:
078                isolation = READ_COMMITTED;
079                break;
080            case Connection.TRANSACTION_READ_UNCOMMITTED:
081                isolation = READ_UNCOMMITTED;
082                break;
083            case Connection.TRANSACTION_REPEATABLE_READ:
084                isolation = Isolation.REPEATABLE_READ;
085                break;
086            case Connection.TRANSACTION_SERIALIZABLE:
087                isolation = SERIALIZABLE;
088                break;
089        }
090        return isolation;
091    }
092    
093    /**
094     * @param type String that represent enum ignoring case
095     * @return the value of <code>type</code>, type of not found return <code>DEFAULT</code> enum. 
096     */
097    public static Isolation get(String type)
098    {
099        Isolation isolation = Isolation.DEFAULT;
100        for (Isolation iso : Isolation.values())
101        {
102            if (String.valueOf(type).equalsIgnoreCase(iso.toString()))
103            {
104                isolation = iso;
105                break;
106            }
107        }
108        return isolation;
109    }
110
111}