/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.lind.core.database;

import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;

/**
 *
 * @author xcarriba
 */
public abstract class AbstractConnection implements Connection {
    
    protected final JdbcTemplate jdbcTemplate;
    
    public AbstractConnection(DataSource dataSource) {
	jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public Properties getClientInfo() throws SQLException {
	Properties clientInfo = null;
	java.sql.Connection connection = null;
	
	try {
	    connection = jdbcTemplate.getDataSource().getConnection();
	    clientInfo = connection.getClientInfo();	    
	} finally {
	    if (connection != null) {
		connection.close();
	    }
	}
	
	return clientInfo;
    }
    
    @Override
    public SqlRowSet query(String sql, Object... args) {
	return jdbcTemplate.queryForRowSet(sql, args);
    }
    
    @Override
    public <T> T queryForObject(String sql, Class<T> elementType) {
	return jdbcTemplate.queryForObject(sql, elementType);
    }

    @Override
    public void execute(String sql) {
	jdbcTemplate.execute(sql);
    }
    
}
