Package com.fiveht.tick.data

Source Code of com.fiveht.tick.data.Properties

/*
* Copyright (C) 2012 FiveHT Media Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.fiveht.tick.data;

import com.fiveht.tick.Data;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* This class provides a convenience framework for CRUD operations on the
* properties table.
*
* @author Nathan Crause
*/
public class Properties {
   
    private static Properties instance;
   
    public static Properties getInstance() throws SQLException {
        if (instance == null) instance = new Properties();
       
        return instance;
    }
   
    private Data data;
   
    private Properties() throws SQLException {
        data = new Data();
    }
   
    /**
     * Performs a lookup for a named value
     *
     * @param name the name of the property we're looking for
     * @return the value, or null if no such named value was found
     */
    protected String get(String name) throws SQLException {
        PreparedStatement stmt = data.getConnection().prepareStatement(
                "SELECT value FROM properties WHERE name=?");
       
        try {
            stmt.setString(1, name);

            ResultSet rs = stmt.executeQuery();

            try {
                if (!rs.next()) return null;

                return rs.getString(1);
            }
            finally {
                rs.close();
            }
        }
        finally {
            stmt.close();
        }
    }
   
    public String getString(String name, String defaultValue) throws SQLException {
        String src = get(name);
       
        return src == null ? defaultValue : get(name);
    }
   
    public String getString(String name) throws SQLException {
        return getString(name, null);
    }
   
    public int getInt(String name, int defaultValue) throws SQLException {
        String src = get(name);
       
        return src == null ? defaultValue : Integer.parseInt(src);
    }
   
    public int getInt(String name) throws SQLException {
        return getInt(name, 0);
    }
   
    public long getLong(String name, long defaultValue) throws SQLException {
        String src = get(name);
       
        return src == null ? defaultValue : Long.parseLong(src);
    }
   
    public long getLong(String name) throws SQLException {
        return getLong(name, 0l);
    }
   
    public double getDouble(String name, double defaultValue) throws SQLException {
        String src = get(name);
       
        return src == null ? defaultValue : Double.parseDouble(src);
    }
   
    public double getDouble(String name) throws SQLException {
        return getDouble(name, 0d);
    }
   
    public boolean getBoolean(String name, boolean defaultValue) throws SQLException {
        String src = get(name);
       
        return src == null ? defaultValue : Boolean.parseBoolean(src);
    }
   
    public boolean getBoolean(String name) throws SQLException {
        return getBoolean(name, false);
    }
   
    public boolean exists(String name) throws SQLException {
        return (get(name) != null);
    }
   
    public void set(String name, Object value) throws SQLException {
        if (exists(name))
            update(name, value);
        else
            insert(name, value);
    }

    private void update(String name, Object value) throws SQLException {
        PreparedStatement stmt = data.getConnection().prepareStatement(
                "UPDATE properties SET value=? WHERE name=?");
       
        stmt.setString(1, value.toString());
        stmt.setString(2, name);
       
        stmt.execute();
    }

    private void insert(String name, Object value) throws SQLException {
        PreparedStatement stmt = data.getConnection().prepareStatement(
                "INSERT INTO properties (name, value) VALUES (?, ?)");
       
        stmt.setString(1, name);
        stmt.setString(2, value.toString());
       
        stmt.execute();
    }
   
}
TOP

Related Classes of com.fiveht.tick.data.Properties

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.