
package com.jeelowcode.framework.plus.chain.parser;


import com.jeelowcode.framework.plus.build.ISQLExpression;
import com.jeelowcode.framework.plus.build.buildmodel.SQLInterpretContext;
import com.jeelowcode.framework.plus.chain.AbstractSQLParser;
import com.jeelowcode.framework.plus.entity.DbColunmTypesEntity;
import com.jeelowcode.framework.plus.entity.SqlInfoEntity;
import com.jeelowcode.framework.plus.core.model.SqlFormatModel;
import com.jeelowcode.framework.utils.utils.FuncBase;

import java.util.ArrayList;
import java.util.List;

/**
 * 标准sql解析
 */
public class StandardSQLParser extends AbstractSQLParser {

    public StandardSQLParser(int level) {
        this.level = level;
    }

    @Override
    protected void sqlParse(SqlInfoEntity sqlInfoEntity) {
        try {
            ISQLExpression<SqlFormatModel> sqlExpression = sqlInfoEntity.getSqlExpression();
            if(FuncBase.isEmpty(sqlExpression)){
                return;
            }
            SQLInterpretContext context = sqlInfoEntity.getContext();
            if(FuncBase.isEmpty(context)){
                return;
            }
            DbColunmTypesEntity dbColunmTypes = context.getDbColunmTypes();

            //处理数据库类型
            context.setDbColunmTypes(dbColunmTypes);

            Object obj =sqlExpression.interpret(context);

            List<SqlFormatModel> sqlFormatModelList=new ArrayList<>();
            if (FuncBase.isEmpty(obj)) {
                return;
            }

            if(obj instanceof SqlFormatModel){
                sqlFormatModelList.add((SqlFormatModel)obj);
            }else if(obj instanceof List){
                sqlFormatModelList.addAll((List<SqlFormatModel>)obj);
            }
            sqlInfoEntity.setSqlModelList(sqlFormatModelList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
