com.litongjava.db.activerecord.sql.SqlKit Maven / Gradle / Ivy
package com.litongjava.db.activerecord.sql;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jfinal.kit.StrKit;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import com.jfinal.template.source.ISource;
import com.litongjava.db.SqlPara;
/**
* SqlKit
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public class SqlKit {
static final String SQL_TEMPLATE_MAP_KEY = "_SQL_TEMPLATE_MAP_";
static final String SQL_PARA_KEY = "_SQL_PARA_";
static final String PARA_ARRAY_KEY = "_PARA_ARRAY_"; // 此参数保持不动,已被用于模板取值 _PARA_ARRAY_[n]
private String configName;
private boolean devMode;
private Engine engine;
private List sqlSourceList = new ArrayList();
private Map sqlTemplateMap;
public SqlKit(String configName, boolean devMode) {
this.configName = configName;
this.devMode = devMode;
engine = new Engine(configName);
engine.setDevMode(devMode);
engine.setToClassPathSourceFactory();
engine.addDirective("namespace", NameSpaceDirective.class);
engine.addDirective("sql", SqlDirective.class);
engine.addDirective("para", ParaDirective.class, true);
engine.addDirective("p", ParaDirective.class, true); // 配置 #para 指令的别名指令 #p,不建议使用,在此仅为兼容 3.0 版本
}
public SqlKit(String configName) {
this(configName, false);
}
public Engine getEngine() {
return engine;
}
public void setDevMode(boolean devMode) {
this.devMode = devMode;
engine.setDevMode(devMode);
}
public void setBaseSqlTemplatePath(String baseSqlTemplatePath) {
engine.setBaseTemplatePath(baseSqlTemplatePath);
}
public void addSqlTemplate(String sqlTemplate) {
if (StrKit.isBlank(sqlTemplate)) {
throw new IllegalArgumentException("sqlTemplate can not be blank");
}
sqlSourceList.add(new SqlSource(sqlTemplate));
}
public void addSqlTemplate(ISource sqlTemplate) {
if (sqlTemplate == null) {
throw new IllegalArgumentException("sqlTemplate can not be null");
}
sqlSourceList.add(new SqlSource(sqlTemplate));
}
public synchronized void parseSqlTemplate() {
Map sqlTemplateMap = new HashMap(512, 0.5F);
for (SqlSource ss : sqlSourceList) {
Template template = ss.isFile() ? engine.getTemplate(ss.file) : engine.getTemplate(ss.source);
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy