Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
AnalyzerBeans Copyright (C) 2014 Neopost - Customer Information Management This copyrighted material is made available to anyone wishing to use, modify, copy, or redistribute it subject to the terms and conditions of the GNU Lesser General Public License, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this distribution; if not, write to: Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA
 
 package org.eobjects.analyzer.storage;
 
 import java.util.Map;
 import java.util.Set;
 
 
 final class SqlDatabaseMap<K, V> extends AbstractMap<K, V> implements Map<K, V>, SqlDatabaseCollection {
 
     private final Map<K, V> _cache = CollectionUtils2.<K, V> createCache(1000, 60).asMap();
     
     private final Connection _connection;
     private final String _tableName;
     private final PreparedStatement _getPreparedStatement;
     private final PreparedStatement _updatePreparedStatement;
     private final PreparedStatement _insertPreparedStatement;
     private volatile int _size;
 
     public SqlDatabaseMap(Connection connectionString tableNameString keyTypeNameString valueTypeName) {
          = connection;
          = tableName;
 
         SqlDatabaseUtils.performUpdate(. + tableName + " (map_key "
                 + keyTypeName + " PRIMARY KEY, map_value " + valueTypeName + ")");
 
         try {
              = .prepareStatement("SELECT map_value FROM " + 
                     + " WHERE map_key = ?;");
              = .prepareStatement("UPDATE " + 
                     + " SET map_value = ? WHERE map_key = ?;");
              = .prepareStatement("INSERT INTO  " +  + " VALUES (?,?);");
         } catch (SQLException e) {
             throw new IllegalStateException(e);
         }
     }
 
     @Override
     public int size() {
         return ;
     }
 
     @SuppressWarnings("unchecked")
     @Override
     public synchronized V get(Object key) {
         V v = .get(key);
         if (v != null) {
             return v;
         }
 
         ResultSet rs = null;
         try {
             .setObject(1, key);
             rs = .executeQuery();
             if (rs.next()) {
                 return (V) rs.getObject(1);
             }
             return null;
         } catch (SQLException e) {
             throw new IllegalStateException(e);
         } finally {
             SqlDatabaseUtils.safeClose(rsnull);
         }
     }
 
     @Override
     public synchronized boolean containsKey(Object key) {
         if (.containsKey(key)) {
             return true;
         }
 
         ResultSet rs = null;
         PreparedStatement st = null;
         try {
             st = .prepareStatement("SELECT COUNT(*) FROM  " +  + " WHERE map_key = ?");
            st.setObject(1, key);
            rs = st.executeQuery();
            if (rs.next()) {
                return rs.getInt(1) > 0;
            }
            return false;
        } catch (SQLException e) {
            throw new IllegalStateException(e);
        } finally {
            SqlDatabaseUtils.safeClose(rsst);
        }
    }
    public synchronized V put(K key, V value) {
        try {
            final V v;
            if (containsKey(key)) {
                v = get(key);
                .setObject(1, value);
                .setObject(2, key);
                .executeUpdate();
            } else {
                .setObject(1, key);
                .setObject(2, value);
                .executeUpdate();
                ++;
                v = null;
            }
            .put(keyvalue);
            return v;
        } catch (SQLException e) {
            throw new IllegalStateException(e);
        }
    };
    @Override
    public synchronized V remove(Object key) {
        if (containsKey(key)) {
            V result = get(key);
            PreparedStatement st = null;
            try {
                st = .prepareStatement("DELETE FROM " +  + " WHERE map_key = ?");
                st.setObject(1, key);
                st.executeUpdate();
                --;
            } catch (SQLException e) {
                throw new IllegalStateException(e);
            } finally {
                SqlDatabaseUtils.safeClose(nullst);
            }
            .remove(key);
            return result;
        }
        return null;
    }
    @Override
    public synchronized Set<java.util.Map.Entry<K, V>> entrySet() {
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            Set<Entry<K, V>> result = new HashSet<Map.Entry<K, V>>();
            st = .prepareStatement("SELECT map_key FROM " +  + " ORDER BY map_key ASC;");
            rs = st.executeQuery();
            while (rs.next()) {
                @SuppressWarnings("unchecked")
                K key = (K) rs.getObject(1);
                result.add(new SqlDatabaseEntry(key));
            }
            return result;
        } catch (SQLException e) {
            throw new IllegalStateException(e);
        } finally {
            SqlDatabaseUtils.safeClose(rsst);
        }
    }
    @Override
    public String getTableName() {
        return ;
    }
    private class SqlDatabaseEntry implements Entry<K, V> {
        private final K _key;
        public SqlDatabaseEntry(K key) {
             = key;
        }
        @Override
        public K getKey() {
            return ;
        }
        @Override
        public V getValue() {
            return get();
        }
        @Override
        public V setValue(V value) {
            return put(value);
        }
        @Override
        public int hashCode() {
            return .hashCode();
        }
    }
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        SqlDatabaseUtils.performUpdate("DROP TABLE " + getTableName());
    }
New to GrepCode? Check out our FAQ X