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

com.shulie.druid.support.opds.udf.SqlParams Maven / Gradle / Ivy

package com.shulie.druid.support.opds.udf;

import com.shulie.druid.DbType;
import com.shulie.druid.sql.parser.ParserException;
import com.shulie.druid.sql.visitor.ParameterizedOutputVisitorUtils;
import com.shulie.druid.support.json.JSONUtils;
import com.aliyun.odps.udf.UDF;

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

public class SqlParams extends UDF {
    public String evaluate(String sql) {
        return evaluate(sql, null, false);
    }

    public String evaluate(String sql, String dbTypeName) {
        return evaluate(sql, dbTypeName, false);
    }

    public String evaluate(String sql, String dbTypeName, boolean throwError) {
        try {
            DbType dbType = dbTypeName == null ? null : DbType.valueOf(dbTypeName);
            List outParameters = new ArrayList();
            ParameterizedOutputVisitorUtils.parameterize(sql, dbType, outParameters);
            return JSONUtils.toJSONString(outParameters);
        } catch (ParserException ex) {
            if (throwError) {
                throw new IllegalArgumentException("error sql : \n" + sql, ex);
            }

            return null;
        }
    }
}