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

com.github.gkutiel.Db Maven / Gradle / Ivy

package com.github.gkutiel;

import java.lang.reflect.Constructor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.gkutiel.ParamHandler.IntSetter;
import com.github.gkutiel.ParamHandler.StringSetter;
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;

public class Db {

	public class Stmt {

		private final Object[] params;
		private final String sql;

		Stmt(final String sql, final Object... params) {
			this.sql = sql;
			this.params = params;
		}

		public void execute() throws SQLException {
			try (final Connection con = pool.getConnection(); PreparedStatement ps = stmt(con);) {
				ps.execute();
			}
		}

		public  T firstOf(final Class type) throws SQLException {
			try (Connection con = pool.getConnection(); PreparedStatement ps = stmt(con); ResultSet rs = ps.executeQuery();) {
				if (!rs.next()) return null;
				return to(rs, type);
			}
		}

		public  List listOf(final Class type) throws SQLException {
			try (Connection con = pool.getConnection(); PreparedStatement ps = stmt(con); ResultSet rs = ps.executeQuery();) {
				final List res = new ArrayList<>();
				while (rs.next())
					res.add(to(rs, type));
				return res;
			}
		}

		private Object[] args(final ResultSet rs, final Class[] types) throws SQLException {
			final Object[] args = new Object[types.length];

			for (int i = 0; i < args.length; i++)
				args[i] = paramHandlers.get(types[i]).get(rs, i + 1);

			return args;
		}

		private void setParam(final PreparedStatement ps, final int paramIndex, final Object param) throws SQLException {
			paramHandlers.get(param.getClass()).set(ps, paramIndex, param);
		}

		private PreparedStatement stmt(final Connection con) throws SQLException {
			final PreparedStatement ps = con.prepareStatement(sql);
			for (int paramIndex = 0; paramIndex < params.length; paramIndex++)
				setParam(ps, paramIndex + 1, params[paramIndex]);
			return ps;
		}

		private  T to(final ResultSet rs, final Class type) {
			try {
				@SuppressWarnings("unchecked")
				final Constructor ctor = (Constructor) type.getConstructors()[0];
				final Class[] parameterTypes = ctor.getParameterTypes();
				return ctor.newInstance(args(rs, parameterTypes));
			} catch (final Exception e) {
				throw new RuntimeException(e);
			}
		}
	}

	private static Map, ParamHandler> paramHandlers = paramSetters();

	public static Db from(final String url) throws SQLException {
		return from(url, "", "");
	}

	public static Db from(final String url, final String usr, final String pwd) throws SQLException {
		final BoneCPConfig config = new BoneCPConfig();
		config.setJdbcUrl(url);
		config.setMinConnectionsPerPartition(1);
		config.setMaxConnectionsPerPartition(8);
		config.setPartitionCount(8);
		config.setUsername(usr);
		config.setPassword(pwd);
		return new Db(new BoneCP(config));
	}

	private static Map, ParamHandler> paramSetters() {
		final Map, ParamHandler> paramSetters = new HashMap<>();

		final IntSetter intSetter = new IntSetter();
		paramSetters.put(int.class, intSetter);
		paramSetters.put(Integer.class, intSetter);
		paramSetters.put(String.class, new StringSetter());

		return paramSetters;
	}

	private final BoneCP pool;

	private Db(final BoneCP pool) {
		this.pool = pool;
	}

	public Stmt stmt(final String sql, final Object... args) {
		return new Stmt(sql, args);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy