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

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

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

import cn.jiangzeyin.StringUtil;
import cn.jiangzeyin.database.DbWriteService;
import cn.jiangzeyin.database.base.ReadBase;
import cn.jiangzeyin.database.config.DatabaseContextHolder;
import cn.jiangzeyin.database.config.SystemColumn;
import cn.jiangzeyin.database.util.SqlUtil;
import cn.jiangzeyin.database.util.Util;
import cn.jiangzeyin.system.DbLog;
import com.alibaba.druid.util.JdbcUtils;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

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

/**
 * 查询数据库操作
 *
 * @author jiangzeyin
 */
@SuppressWarnings("unchecked")
public class Select extends ReadBase {

    private String orderBy;
    private String sql;
    /**
     * 查询数据重多少开始
     */
    private int limitStart;
    /**
     * 查询数据个数
     */
    private int limitCount;


    public int getLimitStart() {
        return limitStart;
    }

    /**
     * 设置查询开始位置
     *
     * @param limitStart 开始行
     * @return this
     * @author jiangzeyin
     */
    public Select setLimitStart(int limitStart) {
        this.limitStart = limitStart;
        return this;
    }

    public int getLimitCount() {
        return limitCount;
    }

    /**
     * 设置查询数量
     *
     * @param limitCount 共几行
     * @return this
     * @author jiangzeyin
     */
    public Select setLimitCount(int limitCount) {
        this.limitCount = limitCount;
        return this;
    }

    public Select() {
        // setTclass(DbReflectUtil.getTClass(this));
    }

    public Select(int isDelete) {
        setIsDelete(isDelete);
    }

    public Select(String tag) {
        super.setTag(tag);
    }


    public String getOrderBy() {
        return orderBy;
    }

    public Select setOrderBy(String orderBy) {
        this.orderBy = orderBy;
        return this;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public void setSql(String sql, Result resultType) {
        this.sql = sql;
        setResultType(resultType);
    }

    public T run(Result resultType) {
        setResultType(resultType);
        return run();
    }

    /**
     * 查询
     *
     * @return 结果
     * @author jiangzeyin
     */
    @Override
    public  t run() {
        try {
            if (getResultType() == Result.JsonObject) {
                setLimitCount(1);
            }
            String tag = getTag() == null ? DbWriteService.getInstance().getDatabaseName(getTclass()) : getTag();
            setTag(tag);
            DataSource dataSource = DatabaseContextHolder.getReadDataSource(tag);
            String runSql = getSql();
            if (StringUtils.isEmpty(runSql)) {
                runSql = SqlUtil.getSelectSql(this);
            }
            setRunSql(runSql);
            DbLog.getInstance().info(getTransferLog() + runSql);
            List> result = JdbcUtils.executeQuery(dataSource, runSql, getParameters());
            switch (getResultType()) {
                case JsonArray:
                    return (t) JSON.toJSON(result);
                case JsonObject: {
                    if (result == null || result.size() < 1) {
                        return null;
                    }
                    Map map = result.get(0);
                    return (t) new JSONObject(map);
                }
                case Entity:
                    return (t) Util.convertList(this, result);
                case ListMap:
                    return (t) result;
                case String:
                case Integer: {
                    if (result == null || result.size() < 1) {
                        return null;
                    }
                    Map map = result.get(0);
                    if (map == null) {
                        return null;
                    }
                    //Object object = null;
                    String column = getColumns();
                    if (SystemColumn.getDefaultSelectColumns().equals(column)) {
                        // 默认取第一行一列数据
                        return (t) map.values().toArray()[0];
                    }
                    // 取指定列
                    String[] columns = StringUtil.stringToArray(column);
                    if (columns.length == 1) {
                        return (t) map.get(columns[0]);
                    }
                    return (t) map;
                }
                default:
                    break;
            }
        } catch (Exception e) {
            // TODO: handle exception
            isThrows(e);
        } finally {
            runEnd();
            recycling();
        }
        return null;
    }

    /**
     * 查询一条数据 返回实体 会自动追加 limit 1
     *
     * @return 结果
     * @author jiangzeyin
     */
    public T runOne() {
        setLimitCount(1);
        List list = run();
        if (list == null) {
            return null;
        }
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy