All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.devocative.adroit.sql.SqlHelper Maven / Gradle / Ivy

There is a newer version: 2.1
Show newest version
package org.devocative.adroit.sql;

import com.thoughtworks.xstream.XStream;
import org.devocative.adroit.sql.plugin.INpsPlugin;
import org.devocative.adroit.sql.result.EColumnNameCase;
import org.devocative.adroit.sql.result.QueryVO;
import org.devocative.adroit.sql.result.ResultSetProcessor;
import org.devocative.adroit.xml.AdroitXStream;

import java.io.InputStream;
import java.sql.*;
import java.util.*;
import java.util.Date;

public class SqlHelper {
	private final Connection connection;
	private boolean ignoreExtraPassedParam = true;
	private EColumnNameCase nameCase = EColumnNameCase.LOWER;
	private Class dateClassReplacement = java.sql.Date.class;

	private Map xQueryMap = new HashMap<>();

	// ------------------------------

	public SqlHelper(Connection connection) {
		this.connection = connection;
	}

	// ------------------------------

	public SqlHelper setIgnoreExtraPassedParam(boolean ignoreExtraPassedParam) {
		this.ignoreExtraPassedParam = ignoreExtraPassedParam;
		return this;
	}

	public SqlHelper setNameCase(EColumnNameCase nameCase) {
		this.nameCase = nameCase;
		return this;
	}

	public SqlHelper setDateClassReplacement(Class dateClassReplacement) {
		this.dateClassReplacement = dateClassReplacement;
		return this;
	}

	public SqlHelper setXMLQueryFile(InputStream in) {
		XStream xStream = new AdroitXStream();
		xStream.processAnnotations(XQuery.class);

		List xQueries = (List) xStream.fromXML(in);
		for (XQuery xQuery : xQueries) {
			xQueryMap.put(xQuery.getName(), xQuery);
		}
		return this;
	}

	// ---------------

	public NamedParameterStatement createNPS(String name) {
		return createNPS(xQueryMap.get(name));
	}

	public NamedParameterStatement createNPS(XQuery sql) {
		return new NamedParameterStatement(connection, sql.getSql())
			.setIgnoreExtraPassedParam(ignoreExtraPassedParam)
			.setDateClassReplacement(dateClassReplacement);
	}

	// ---------------

	public  Map twoCellsAsMap(String name) throws SQLException {
		return twoCellsAsMap(name, new HashMap<>());
	}

	public  Map twoCellsAsMap(String name, Map params, INpsPlugin... plugins) throws SQLException {
		return twoCellsAsMap(xQueryMap.get(name), params, plugins);
	}

	public  Map twoCellsAsMap(XQuery sql) throws SQLException {
		return twoCellsAsMap(sql, new HashMap<>());
	}

	public  Map twoCellsAsMap(XQuery sql, Map params, INpsPlugin... plugins) throws SQLException {
		NamedParameterStatement nps = createNPS(sql, params, plugins);

		ResultSet rs = nps.executeQuery();
		ResultSetMetaData metaData = rs.getMetaData();
		int col1Type = metaData.getColumnType(1);
		int col2Type = metaData.getColumnType(2);

		Map result = new LinkedHashMap<>();
		while (rs.next()) {
			K key = (K) ResultSetProcessor.getValue(rs, 1, col1Type);
			V value = (V) ResultSetProcessor.getValue(rs, 2, col2Type);
			result.put(key, value);
		}

		nps.close();

		return result;
	}

	// -----

	public List firstRowAsList(String name) throws SQLException {
		return firstRowAsList(name, new HashMap<>());
	}

	public List firstRowAsList(String name, Map params, INpsPlugin... plugins) throws SQLException {
		return firstRowAsList(xQueryMap.get(name), params, plugins);
	}

	public List firstRowAsList(XQuery sql) throws SQLException {
		return firstRowAsList(sql, new HashMap<>());
	}

	public List firstRowAsList(XQuery sql, Map params, INpsPlugin... plugins) throws SQLException {
		NamedParameterStatement nps = createNPS(sql, params, plugins);

		ResultSet rs = nps.executeQuery();
		int columnCount = rs.getMetaData().getColumnCount();

		List result = new ArrayList<>();
		if (rs.next()) {
			for (int i = 1; i <= columnCount; i++) {
				result.add(rs.getObject(i));
			}
		}
		nps.close();
		return result;
	}

	// -----

	public  List firstColAsList(String name) throws SQLException {
		return firstColAsList(name, new HashMap<>());
	}

	public  List firstColAsList(String name, Map params, INpsPlugin... plugins) throws SQLException {
		return firstColAsList(xQueryMap.get(name), params, plugins);
	}

	public  List firstColAsList(XQuery sql) throws SQLException {
		return firstColAsList(sql, new HashMap<>());
	}

	public  List firstColAsList(XQuery sql, Map params, INpsPlugin... plugins) throws SQLException {
		NamedParameterStatement nps = createNPS(sql, params, plugins);

		ResultSet rs = nps.executeQuery();
		int col1Type = rs.getMetaData().getColumnType(1);

		List result = new ArrayList<>();
		while (rs.next()) {
			result.add((T) ResultSetProcessor.getValue(rs, 1, col1Type));
		}
		nps.close();
		return result;
	}

	// -----

	public Object firstCell(String name) throws SQLException {
		return firstCell(name, new HashMap<>());
	}

	public Object firstCell(String name, Map params, INpsPlugin... plugins) throws SQLException {
		return firstCell(xQueryMap.get(name), params, plugins);
	}

	public Object firstCell(XQuery sql) throws SQLException {
		return firstCell(sql, new HashMap<>());
	}

	public Object firstCell(XQuery sql, Map params, INpsPlugin... plugins) throws SQLException {
		NamedParameterStatement nps = createNPS(sql, params, plugins);

		ResultSet rs = nps.executeQuery();
		ResultSetMetaData metaData = rs.getMetaData();

		Object result = null;
		if (rs.next()) {
			result = ResultSetProcessor.getValue(rs, 1, metaData.getColumnType(1));
		}

		nps.close();

		return result;
	}

	// -----

	public QueryVO selectAll(String name) throws SQLException {
		return selectAll(name, new HashMap<>());
	}

	public QueryVO selectAll(String name, Map params, INpsPlugin... plugins) throws SQLException {
		return selectAll(xQueryMap.get(name), params, plugins);
	}

	public QueryVO selectAll(XQuery sql) throws SQLException {
		return selectAll(sql, new HashMap<>());
	}

	public QueryVO selectAll(XQuery sql, Map params, INpsPlugin... plugins) throws SQLException {
		NamedParameterStatement nps = createNPS(sql, params, plugins);
		ResultSet resultSet = nps.executeQuery();
		QueryVO queryVO = ResultSetProcessor.process(resultSet, nameCase);
		nps.close();
		return queryVO;
	}

	// -----

	public void executeDDL(String name, Map params) throws SQLException {
		String ddl = xQueryMap.get(name).getSql();
		for (Map.Entry entry : params.entrySet()) {
			ddl = ddl.replaceAll("[:]" + entry.getKey(), entry.getValue().toString());
		}

		Statement st = connection.createStatement();
		st.executeUpdate(ddl);
		st.close();
	}

	// ------------------------------

	private NamedParameterStatement createNPS(XQuery sql, Map params, INpsPlugin... plugins) {
		NamedParameterStatement nps = createNPS(sql)
			.setParameters(params);

		if (plugins != null) {
			for (INpsPlugin plugin : plugins) {
				if (plugin != null) {
					nps.addPlugin(plugin);
				}
			}
		}

		return nps;
	}
}