
package com.lideeyunji.tool.framework.sql.chain.parser;


import com.lideeyunji.tool.framework.sql.build.ISQLExpression;
import com.lideeyunji.tool.framework.sql.build.buildmodel.SQLInterpretContext;
import com.lideeyunji.tool.framework.sql.chain.AbstractSQLParser;
import com.lideeyunji.tool.framework.sql.entity.DbColunmTypesEntity;
import com.lideeyunji.tool.framework.sql.entity.SqlInfoEntity;
import com.lideeyunji.tool.framework.sql.core.model.SqlFormatModel;
import com.lideeyunji.tool.framework.yunji.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();
        }
    }



}
