view src/luan/modules/sql/Database.java @ 1268:725e52076f03

remove broken pooling
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 12 Nov 2018 03:54:04 -0700
parents 9fa8b8389578
children 25746915a241
line wrap: on
line source

package luan.modules.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.HashMap;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import luan.LuanState;
import luan.LuanTable;
import luan.LuanException;


public final class Database {
	private static final Logger logger = LoggerFactory.getLogger(Database.class);

	public final Connection con;
	private final Map<String,PreparedStatement> pstmts = new HashMap<String,PreparedStatement>();

	public Database(Connection con) {
		this.con = con;
	}

	public Database(LuanTable specTbl)
		throws LuanException, ClassNotFoundException, SQLException
	{
		Map<Object,Object> spec = specTbl.asMap();
		String cls = getString(spec,"class");
		Class.forName(cls);
		String url = getString(spec,"url");
		Properties props = new Properties();
		props.putAll(spec);
		this.con = DriverManager.getConnection(url,props);
	}

	private static String getString(Map spec,String key) throws LuanException {
		Object val = spec.remove(key);
		if( val==null )
			throw new LuanException( "parameter '"+key+"' is required" );
		if( !(val instanceof String) )
			throw new LuanException( "parameter '"+key+"' must be a string" );
		return (String)val;
	}

	private PreparedStatement prepareStatement(String sql,Object[] args) throws SQLException {
		PreparedStatement pstmt = pstmts.get(sql);
		if( pstmt==null ) {
			pstmt = con.prepareStatement(sql);
			pstmts.put(sql,pstmt);
		}
		for( int i=0; i<args.length; i++ ) {
			pstmt.setObject(i+1,args[i]);
		}
		return pstmt;
	}

	public ResultSet query(String sql,Object... args) throws SQLException {
		if( args.length == 0 ) {
			Statement stmt = con.createStatement();
			return stmt.executeQuery(sql);
		} else {
			PreparedStatement pstmt = prepareStatement(sql,args);
			return pstmt.executeQuery();
		}
	}

	public int update(String sql,Object... args) throws SQLException {
		if( args.length == 0 ) {
			Statement stmt = con.createStatement();
			int n = stmt.executeUpdate(sql);
			stmt.close();
			return n;
		} else {
			PreparedStatement pstmt = prepareStatement(sql,args);
			return pstmt.executeUpdate();
		}
	}

}