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

cn.jiangzeyin.database.run.read.SelectFunction Maven / Gradle / Ivy

There is a newer version: 1.2.11
Show newest version
package cn.jiangzeyin.database.run.read;

import cn.jiangzeyin.database.base.ReadBase;
import cn.jiangzeyin.database.config.DatabaseContextHolder;
import cn.jiangzeyin.database.util.SqlUtil;
import cn.jiangzeyin.system.SystemDbLog;
import com.alibaba.druid.util.JdbcUtils;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 查询函数
 *
 * @author jiangzeyin
 */
public class SelectFunction extends ReadBase {

    private String name;

    public SelectFunction(String name, String tag) {
        // TODO Auto-generated constructor stub
        this.name = name;
        super.setTag(tag);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @SuppressWarnings("unchecked")
    @Override
    public T run() {
        try {
            DataSource dataSource = DatabaseContextHolder.getReadDataSource(getTag());
            String sql = SqlUtil.function(getName(), getParameters());
            setRunSql(sql);
            SystemDbLog.getInstance().info(sql);
            List> list = JdbcUtils.executeQuery(dataSource, sql, getParameters());
            if (list == null || list.size() < 1)
                return null;
            Map map = list.get(0);
            if (map == null)
                return null;
            Collection collection = map.values();
            return (T) collection.toArray()[0];
        } catch (Exception e) {
            // TODO: handle exception
            isThrows(e);
        } finally {
            recycling();
            runEnd();
        }
        return null;
    }
}