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

com.github.youyinnn.youdbutils.dao.model.ModelHandler Maven / Gradle / Ivy

There is a newer version: 2.2.3
Show newest version
package com.github.youyinnn.youdbutils.dao.model;

import com.github.youyinnn.youdbutils.dao.SqlExecutor;
import com.github.youyinnn.youdbutils.druid.ThreadLocalPropContainer;
import com.github.youyinnn.youdbutils.exceptions.NoneffectiveUpdateExecuteException;
import com.github.youyinnn.youdbutils.utils.ReflectionUtils;
import com.github.youyinnn.youdbutils.utils.SqlStringUtils;
import com.github.youyinnn.youdbutils.utils.YouCollectionsUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 见接口注释
 *
 * @author youyinnn
 */
public class ModelHandler extends SqlExecutor implements com.github.youyinnn.youdbutils.dao.interfaces.ModelHandler {

    private ModelResultFactory modelResultFactory;

    private Class modelClass;

    private String modelName;

    public ModelHandler(Class modelClass) {
        this.modelResultFactory = new ModelResultFactory<>(modelClass);
        this.modelClass = modelClass;
        this.modelName = modelClass.getSimpleName();
    }

    public Class getModelClass() {
        return modelClass;
    }

    public ModelResultFactory getModelResultFactory() {
        return modelResultFactory;
    }

    private ArrayList getStatementResultModelList(String sql){
        ResultSet resultSet = null;
        ArrayList resultModelList = null;
        Statement statement = null;
        try {
            resultSet = executeStatementQuery(sql);
            statement = resultSet.getStatement();
            resultModelList = modelResultFactory.getResultModelList(resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ThreadLocalPropContainer.release(resultSet,statement,null);
        }
        return resultModelList;
    }

    @Override
    public ArrayList getList(String sql) {

        return getStatementResultModelList(sql);
    }

    @Override
    public ArrayList getListForAll(ArrayList queryFieldList){

        try {
            queryFieldList = MappingHandler.mappingHandle(modelName,queryFieldList);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        String sql = SqlStringUtils.getSelectAllSql(modelName,queryFieldList);

        return getStatementResultModelList(sql);

    }

    @Override
    public ArrayList getListWhere(HashMap conditionsMap, ArrayList queryFieldList,String separateMark) {


        ResultSet resultSet = null;
        ArrayList resultModelList = null;
        Statement statement = null;
        try {
            queryFieldList = MappingHandler.mappingHandle(modelName,queryFieldList);
            conditionsMap = MappingHandler.mappingHandle(modelName,conditionsMap);
            resultSet = executePreparedStatementQuery(modelName,queryFieldList,conditionsMap,separateMark);
            statement = resultSet.getStatement();
            resultModelList = modelResultFactory.getResultModelList(resultSet);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ThreadLocalPropContainer.release(resultSet,statement,null);
        }

        return resultModelList;
    }

    @Override
    public ArrayList getListWhereLike(HashMap conditionsMap, ArrayList queryFieldList,String separateMark) {

        try {
            queryFieldList = MappingHandler.mappingHandle(modelName,queryFieldList);
            conditionsMap = MappingHandler.mappingHandle(modelName,conditionsMap);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        String sql = SqlStringUtils.getSelectFromWhereLikeSql(modelName,conditionsMap,separateMark,queryFieldList);

        return getStatementResultModelList(sql);
    }

    @Override
    public int saveModel(T model) throws NoneffectiveUpdateExecuteException {

        Class aClass = model.getClass();

        HashMap newFieldValuesMap = new HashMap<>(10);

        for (String field : ModelTableMessage.getModelFieldList(aClass.getSimpleName())) {
            Object fieldValue = ReflectionUtils.getFieldValue(model, field);
            newFieldValuesMap.put(field,fieldValue);
        }

        try {
            newFieldValuesMap = MappingHandler.mappingHandle(modelName,newFieldValuesMap);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        return executePreparedStatementInsert(aClass.getSimpleName(), newFieldValuesMap);
    }

    @Override
    public T getModel(String sql) {
        T resultModel = null;
        try {
            ResultSet resultSet = executeStatementQuery(sql);

            if (!resultSet.isBeforeFirst()) {
                return null;
            }

            resultModel = modelResultFactory.getResultModel(resultSet);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultModel;
    }

    @Override
    public T getModel(String sql, ArrayList conditionValues) {
        T resultModel = null;
        try {
            ResultSet resultSet = executePreparedStatementQuery(sql,conditionValues);

            if (!resultSet.isBeforeFirst()) {
                return null;
            }

            resultModel = modelResultFactory.getResultModel(resultSet);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultModel;
    }

    @Override
    public T getModel(HashMap conditionsMap, ArrayList queryFieldList,String separateMark) {


        T resultModel = null;
        try {
            queryFieldList = MappingHandler.mappingHandle(modelName,queryFieldList);
            conditionsMap = MappingHandler.mappingHandle(modelName,conditionsMap);

            ResultSet resultSet = executePreparedStatementQuery(modelName, queryFieldList, conditionsMap,separateMark);

            if (!resultSet.isBeforeFirst()) {
                return null;
            }

            resultModel = modelResultFactory.getResultModel(resultSet);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultModel;
    }

    @Override
    public Object getModelFieldValue(String fieldName, HashMap conditionsMap,String separateMark) {

        Object value = null;
        try {
            conditionsMap = MappingHandler.mappingHandle(modelName,conditionsMap);
            fieldName = MappingHandler.mappingHandle(modelName,fieldName);

            ResultSet resultSet = executePreparedStatementQuery(modelName, YouCollectionsUtils.getYouArrayList(fieldName), conditionsMap,separateMark);

            if (!resultSet.isBeforeFirst()) {
                return null;
            }

            value = resultSet.getObject(fieldName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return value;
    }

    @Override
    public int updateModel(HashMap newFieldValuesMap, HashMap conditionsMap,String separateMark) throws NoneffectiveUpdateExecuteException {

        try {
            newFieldValuesMap = MappingHandler.mappingHandle(modelName,newFieldValuesMap);
            conditionsMap = MappingHandler.mappingHandle(modelName,conditionsMap);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        return executePreparedStatementUpdate(modelName,newFieldValuesMap,conditionsMap,separateMark);
    }

    @Override
    public int deleteModel(HashMap conditionsMap,String separateMark) throws NoneffectiveUpdateExecuteException {

        try {
            conditionsMap = MappingHandler.mappingHandle(modelName,conditionsMap);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        return executePreparedStatementDelete(modelName,conditionsMap,separateMark);
    }

    @Override
    public int addition(String modelField, double b, HashMap conditionsMap) throws NoneffectiveUpdateExecuteException {

        return basicArithmetic(modelField,b,conditionsMap,"+");
    }

    @Override
    public int subtraction(String modelField, double b, HashMap conditionsMap) throws NoneffectiveUpdateExecuteException {
        return basicArithmetic(modelField,b,conditionsMap, "-");
    }

    @Override
    public int multiplication(String modelField, double b, HashMap conditionsMap) throws NoneffectiveUpdateExecuteException {
        return basicArithmetic(modelField,b,conditionsMap, "*");
    }

    @Override
    public int division(String modelField, double b, HashMap conditionsMap) throws NoneffectiveUpdateExecuteException {
        return basicArithmetic(modelField,b,conditionsMap, "/");
    }

    private int basicArithmetic(String modelField, double b, HashMap conditionsMap, String op) throws NoneffectiveUpdateExecuteException {

        String tableField = null;
        try {
            tableField = MappingHandler.mappingHandle(modelName, modelField);
            conditionsMap = MappingHandler.mappingHandle(modelName,conditionsMap);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        StringBuffer sql = new StringBuffer("UPDATE ")
                .append(modelName)
                .append(" SET ")
                .append(tableField)
                .append(" = ")
                .append(tableField)
                .append(op)
                .append(" ?")
                .append(SqlStringUtils.getWhereSubStr(conditionsMap.keySet(),"AND"));

        ArrayList conditionValues = new ArrayList();

        conditionValues.addAll(conditionsMap.values());

        return executePreparedStatementUpdate(sql.toString(), YouCollectionsUtils.getYouArrayList(b+""), conditionValues);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy