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

org.nutz.plugins.sqltpl.impl.beetl.BeetlSqlTpl Maven / Gradle / Ivy

There is a newer version: 1.r.69.v20220215
Show newest version
package org.nutz.plugins.sqltpl.impl.beetl;

import java.io.IOException;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.StringTemplateResourceLoader;
import org.nutz.dao.sql.Sql;
import org.nutz.plugins.sqltpl.VarSetMap;

/**
 * Beetl版SqlTpl实现,可用变量为 params.XXX vars.XXXX, 另外params的变量可直接访问
 * 
 * @author wendal([email protected])
 *
 */
public class BeetlSqlTpl {

    /**
     * 自定义GroupTemplate
     */
    protected static GroupTemplate gt;

    /**
     * 渲染一个Sql对象
     * 
     * @param sql
     *            需要渲染的Sql实例
     * @return 原对象,用于链式调用
     */
    public static Sql c(Sql sql) {
        String source = sql.getSourceSql();
        Template t = gt().getTemplate(source);
        t.binding(VarSetMap.asCtx(sql));
        String n = t.render();
        sql.setSourceSql(n);
        return sql;
    }

    /**
     * 自定义设置GroupTemplate
     * 
     * @param gt
     *            自定义的GroupTemplate
     */
    public static void setGroupTemplate(GroupTemplate gt) {
        BeetlSqlTpl.gt = gt;
    }

    /**
     * 获取GroupTemplate
     * 
     * @return GroupTemplate实例,如果没有自定义,就生成一个默认的
     */
    public static GroupTemplate gt() {
        if (gt == null) {
            StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
            Configuration cfg;
            try {
                cfg = Configuration.defaultConfiguration();
            }
            catch (IOException e) {
                throw new RuntimeException(e);
            }
            gt = new GroupTemplate(resourceLoader, cfg);
        }
        return gt;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy