package com.xjrsoft.common.handler;

import cn.hutool.db.Entity;
import cn.hutool.db.handler.RsHandler;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class XjrEntityHandler implements RsHandler<Entity> {
    /** 是否大小写不敏感 */
    private final boolean caseInsensitive;

    /**
     * 创建一个 EntityHandler对象
     * @return EntityHandler对象
     */
    public static XjrEntityHandler create() {
        return new XjrEntityHandler();
    }

    /**
     * 构造
     */
    public XjrEntityHandler() {
        this(false);
    }

    /**
     * 构造
     *
     * @param caseInsensitive 是否大小写不敏感
     */
    public XjrEntityHandler(boolean caseInsensitive) {
        this.caseInsensitive = caseInsensitive;
    }

    @Override
    public Entity handle(ResultSet rs) throws SQLException {
        final ResultSetMetaData meta = rs.getMetaData();
        final int columnCount = meta.getColumnCount();

        return rs.next() ? XjrHandleHelper.handleRow(columnCount, meta, rs, this.caseInsensitive) : null;
    }
}
