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

io.github.jinghui70.rainbow.dbaccess.object.PropInfo Maven / Gradle / Ivy

There is a newer version: 5.2.11
Show newest version
package io.github.jinghui70.rainbow.dbaccess.object;

import cn.hutool.core.bean.PropDesc;
import io.github.jinghui70.rainbow.dbaccess.annotation.ArrayField;
import io.github.jinghui70.rainbow.dbaccess.annotation.Id;
import io.github.jinghui70.rainbow.dbaccess.fieldmapper.FieldMapper;
import io.github.jinghui70.rainbow.dbaccess.fieldmapper.FieldValue;
import org.springframework.jdbc.support.JdbcUtils;

import java.lang.reflect.Array;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PropInfo {

    private final String fieldName;
    private final PropDesc propDesc;
    private final FieldMapper mapper;
    private final int index;
    private Id id;

    public String getFieldName() {
        return fieldName;
    }

    public PropDesc getPropDesc() {
        return propDesc;
    }

    public int getIndex() {
        return index;
    }

    public Id getId() {
        return id;
    }

    public PropInfo(String fieldName, PropDesc propDesc, FieldMapper mapper, Id id) {
        this(fieldName, propDesc, mapper, -1);
        this.id = id;
    }

    public PropInfo(String fieldName, PropDesc propDesc, FieldMapper mapper, int index) {
        this.fieldName = fieldName;
        this.propDesc = propDesc;
        this.mapper = mapper;
        this.index = index;
    }

    /**
     * 如果是数组属性,创建一个新数组
     *
     * @return
     */
    public Object newArray() {
        Class componentType = propDesc.getFieldClass().getComponentType();
        ArrayField annotation = propDesc.getField().getAnnotation(ArrayField.class);
        return Array.newInstance(componentType, annotation.length());
    }

    /**
     * 从对象中取值,准备用来保存到数据库中
     *
     * @param object
     * @return
     */
    public Object getValue(Object object) {
        Object value = propDesc.getValue(object);
        if (value == null) return null;
        if (index >= 0) {
            try {
                value = Array.get(value, index);
            } catch (ArrayIndexOutOfBoundsException e) {
                return null;
            }
        }
        return mapper == null ? value : new FieldValue(value, mapper);
    }

    /**
     * 从数据库中取值
     *
     * @param rs
     * @param index
     * @return
     * @throws SQLException
     */
    public Object getValue(ResultSet rs, int index) throws SQLException {
        if (mapper != null)
            return mapper.formDB(rs, index);
        Class type = propDesc.getFieldClass();
        if (this.index >= 0) type = type.getComponentType();
        return JdbcUtils.getResultSetValue(rs, index, type);
    }

    /**
     * 保存一个值到对象中
     *
     * @param object
     * @param value
     */
    public void setValue(Object object, Object value) {
        if (index >= 0) {
            Object array = propDesc.getValue(object);
            if (array == null) {
                array = newArray();
                propDesc.setValue(object, array);
            }
            if (value != null)
                Array.set(array, index, value);
        } else {
            propDesc.setValue(object, value);
        }
    }

    public boolean isAutoIncrement() {
        return id != null && id.autoIncrement();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy