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

me.icymint.libra.jdbc.LibraTemplate Maven / Gradle / Ivy

package me.icymint.libra.jdbc;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.sql.DataSource;

import me.icymint.libra.jdbc.dialect.Dialect;
import me.icymint.libra.jdbc.result.Result;

/**
 * libra-jdbc项目核心类,其执行基础Jdbc操作。
 * 
 * @author Daniel Yu
 * @date 2013-3-13
 * 
 */
public class LibraTemplate extends JdbcSupport {

	public LibraTemplate(DataSource ds) {
		super(ds);
	}

	/**
	 * 单个执行操作。
	 * 
	 * @param sql
	 * @param types
	 * @param param
	 */
	public void execute(String sql, int[] types, Object[] param)
			throws JdbcAccessException {
		executeBatch(sql, types, Collections.singletonList(param));
	}

	/**
	 * 批量执行操作。
	 * 
	 * @param sql
	 * @param types
	 * @param params
	 */
	public void executeBatch(String sql, int[] types,
			Collection params) throws JdbcAccessException {
		super.execute(sql, PARAMETER_DEFAULT(types), params);
	}

	@Override
	protected void initDao(DataSource ds, Dialect dialect) {
	}

	/**
	 * 
	 * 查询标量,尽量不要使用基础类型,而要使用包装类型。
	 * 

* 注意捕获三个运行时违例。 * * @param sql * @param types * @param param * @throws JdbcAccessException * @throws ClassCastException * @throws NullPointerException */ @SuppressWarnings("unchecked") public T query(String sql, int[] types, Object... param) throws JdbcAccessException, ClassCastException, NullPointerException { return (T) this.query(sql, RESULT_SCALAR, types, param); } /** * 轻量级查询母方法。 * * @param sql * @param r * @param types * @param param */ public T query(String sql, Result r, int[] types, Object... param) throws JdbcAccessException { return super.query(sql, PARAMETER_DEFAULT(types), r, param); } /** * 查询单个Object数组。 * * @param sql * @param types * @param param */ public Object[] queryArray(String sql, int[] types, Object... param) throws JdbcAccessException { return this.query(sql, RESULT_SCALAR_ARRAY, types, param); } /** * 查询Object数组列表。 * * @param sql * @param types * @param param */ public List queryArrayList(String sql, int[] types, Object... param) throws JdbcAccessException { return this.query(sql, RESULT_LIST_ARRAY, types, param); } /** * 查询Object列表。 * * @param sql * @param types * @param param */ public List queryList(String sql, int[] types, Object... param) throws JdbcAccessException { return this.query(sql, RESULT_LIST, types, param); } }