package sharin.util;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;

import javax.sql.DataSource;

public class SqlUtils {

    public static Connection getConnection(DataSource ds) {

        try {
            return ds.getConnection();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static PreparedStatement prepareStatement(Connection conn, String sql) {

        try {
            return conn.prepareStatement(sql);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(Connection conn) {

        try {
            conn.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void setObject(PreparedStatement pstmt, int parameterIndex,
            Object x) {

        try {
            pstmt.setObject(parameterIndex, x);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static ResultSet executeQuery(PreparedStatement pstmt) {

        try {
            return pstmt.executeQuery();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(Statement stmt) {

        try {
            stmt.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean next(ResultSet rs) {

        try {
            return rs.next();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(ResultSet rs) {

        try {
            rs.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static int executeUpdate(PreparedStatement pstmt) {

        try {
            return pstmt.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static ResultSet getGeneratedKeys(Statement stmt) {

        try {
            return stmt.getGeneratedKeys();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static ResultSetMetaData getMetaData(ResultSet rs) {

        try {
            return rs.getMetaData();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static int getColumnCount(ResultSetMetaData metaData) {

        try {
            return metaData.getColumnCount();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getColumnLabel(ResultSetMetaData metaData, int column) {

        try {
            return metaData.getColumnLabel(column);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static Object getObject(ResultSet rs, int columnIndex) {

        try {
            return rs.getObject(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static BigDecimal getBigDecimal(ResultSet rs, int columnIndex) {

        try {
            return rs.getBigDecimal(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getString(ResultSet rs, int columnIndex) {

        try {
            return rs.getString(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean getBoolean(ResultSet rs, int columnIndex) {

        try {
            return rs.getBoolean(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte getByte(ResultSet rs, int columnIndex) {

        try {
            return rs.getByte(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] getBytes(ResultSet rs, int columnIndex) {

        try {
            return rs.getBytes(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static Date getDate(ResultSet rs, int columnIndex) {

        try {
            return rs.getDate(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static double getDouble(ResultSet rs, int columnIndex) {

        try {
            return rs.getDouble(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static float getFloat(ResultSet rs, int columnIndex) {

        try {
            return rs.getFloat(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static int getInt(ResultSet rs, int columnIndex) {

        try {
            return rs.getInt(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static long getLong(ResultSet rs, int columnIndex) {

        try {
            return rs.getLong(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static short getShort(ResultSet rs, int columnIndex) {

        try {
            return rs.getShort(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static Time getTime(ResultSet rs, int columnIndex) {

        try {
            return rs.getTime(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static Timestamp getTimestamp(ResultSet rs, int columnIndex) {

        try {
            return rs.getTimestamp(columnIndex);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean wasNull(ResultSet rs) {

        try {
            return rs.wasNull();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
