Skip to content

Commit

Permalink
新增了几个Zealot的调用方法
Browse files Browse the repository at this point in the history
  • Loading branch information
blinkfox committed Apr 23, 2018
1 parent f03ca89 commit d8a4f54
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void loadZealotConfig(String configClass) {
throw new ConfigNotFoundException("未获取到ZealotConfig配置信息");
}

log.info("zealot加载器开始加载,Zealot配置类为:" + configClass);
log.info("Zealot加载器开始加载,Zealot配置类为:" + configClass);
Object temp;
try {
temp = Class.forName(configClass).newInstance();
Expand Down Expand Up @@ -145,10 +145,9 @@ private void cachingXmlZealots() {
// 获取该文档下所有的zealot元素,
List<Node> zealotNodes = doc.selectNodes(ZealotConst.ZEALOT_TAG);
for (Node zealotNode: zealotNodes) {
Node idNode = zealotNode.selectSingleNode(ZealotConst.ATTR_ID);
String zealotId = XmlNodeHelper.getNodeText(idNode);
String zealotId = XmlNodeHelper.getNodeText(zealotNode.selectSingleNode(ZealotConst.ATTR_ID));
if (StringHelper.isBlank(zealotId)) {
throw new NodeNotFoundException("未获取到zealot节点,zealotId为空!");
throw new NodeNotFoundException("该xml文件中有zealot节点的zealotId属性为空,请检查!文件为:" + filePath);
}

// zealot节点缓存到Map中,key是由nameSpace和zealot id组成,用"@@"符号分隔,value是zealotNode
Expand Down
58 changes: 49 additions & 9 deletions src/main/java/com/blinkfox/zealot/core/Zealot.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.blinkfox.zealot.config.entity.NormalConfig;
import com.blinkfox.zealot.consts.ZealotConst;
import com.blinkfox.zealot.exception.NodeNotFoundException;
import com.blinkfox.zealot.exception.ValidFailException;
import com.blinkfox.zealot.helpers.ParseHelper;
import com.blinkfox.zealot.helpers.SqlInfoPrinter;
import com.blinkfox.zealot.helpers.StringHelper;
Expand All @@ -29,22 +30,58 @@ private Zealot() {
}

/**
* 获取sqlInfo信息.
* 通过传入zealot xml文件对应的命名空间及zealot节点的ID的结合体,来简单快速的生成和获取sqlInfo信息(无参的SQL).
*
* @param nsAtZealotId xml命名空间nameSpace+@@+zealotId的值.如:"student@@queryStudentById"
* @return 返回SqlInfo对象
*/
public static SqlInfo getSqlInfoSimply(String nsAtZealotId) {
return getSqlInfoSimply(nsAtZealotId, null);
}

/**
* 通过传入zealot xml文件对应的命名空间、zealot节点的ID以及参数对象(一般是JavaBean或者Map)的结合体,来简单快速的生成和获取sqlInfo信息(有参的SQL).
*
* @param nsAtZealotId xml命名空间nameSpace+@@+zealotId的值.如:"student@@queryStudentById"
* @param paramObj 参数对象(一般是JavaBean对象或者Map)
* @return 返回SqlInfo对象
*/
public static SqlInfo getSqlInfoSimply(String nsAtZealotId, Object paramObj) {
String[] arr = nsAtZealotId.split(ZealotConst.SP_AT);
if (arr.length != 2) {
throw new ValidFailException("nsAtZealotId参数的值必须是xml文件中的 nameSpace + '@@' + zealotId 节点的值,"
+ "如:'student@@queryStudentById'.其中student为nameSpace, queryStudentById为XML文件中SQL的zealotId.");
}
return getSqlInfo(arr[0], arr[1], paramObj);
}

/**
* 通过传入zealot xml文件对应的命名空间和zealot节点的ID来生成和获取sqlInfo信息(无参的SQL).
*
* @param nameSpace xml命名空间
* @param zealotId xml中的zealotId
* @param paramObj 参数对象
* @return 返回SqlInfo对象
*/
public static SqlInfo getSqlInfo(String nameSpace, String zealotId) {
return getSqlInfo(nameSpace, zealotId, null);
}

/**
* 通过传入zealot xml文件对应的命名空间、zealot节点的ID以及参数对象(一般是JavaBean或者Map)来生成和获取sqlInfo信息(有参的SQL).
*
* @param nameSpace xml命名空间
* @param zealotId xml中的zealotId
* @param paramObj 参数对象(一般是JavaBean对象或者Map)
* @return 返回SqlInfo对象
*/
public static SqlInfo getSqlInfo(String nameSpace, String zealotId, Object paramObj) {
// 获取nameSpace文档中的指定sql的zealotId的节点对应的Node节点,如果是debug模式,则实时获取;否则从缓存中获取.
Node zealotNode;
if (NormalConfig.getInstance().isDebug()) {
zealotNode = XmlNodeHelper.getNodeBySpaceAndId(nameSpace, zealotId);
} else {
zealotNode = AbstractZealotConfig.getZealots()
.get(StringHelper.concat(nameSpace, ZealotConst.SP_AT, zealotId));
if (StringHelper.isBlank(nameSpace) || StringHelper.isBlank(zealotId)) {
throw new ValidFailException("请输入有效的nameSpace或者zealotId的值!");
}

// 获取nameSpace文档中的指定sql的zealotId的节点对应的Node节点,如果是debug模式,则实时获取;否则从缓存中获取.
Node zealotNode = NormalConfig.getInstance().isDebug() ? XmlNodeHelper.getNodeBySpaceAndId(nameSpace, zealotId)
: AbstractZealotConfig.getZealots().get(StringHelper.concat(nameSpace, ZealotConst.SP_AT, zealotId));
if (zealotNode == null) {
throw new NodeNotFoundException("未找到nameSpace为:" + nameSpace + ",zealotId为:" + zealotId + "的节点!");
}
Expand All @@ -57,6 +94,7 @@ public static SqlInfo getSqlInfo(String nameSpace, String zealotId, Object param

/**
* 构建完整的SqlInfo对象.
*
* @param nameSpace xml命名空间
* @param sqlInfo SqlInfo对象
* @param node dom4j对象节点
Expand All @@ -82,6 +120,7 @@ public static SqlInfo buildSqlInfo(String nameSpace, SqlInfo sqlInfo, Node node,

/**
* 构建新的、完整的SqlInfo对象.
*
* @param nameSpace xml命名空间
* @param node dom4j对象节点
* @param paramObj 参数对象
Expand All @@ -93,6 +132,7 @@ private static SqlInfo buildNewSqlInfo(String nameSpace, Node node, Object param

/**
* 根据标签拼接的SQL信息来生成最终的SQL.
*
* @param sqlInfo sql及参数信息
* @param paramObj 参数对象信息
* @return 返回SqlInfo对象
Expand Down
14 changes: 11 additions & 3 deletions src/test/java/com/blinkfox/zealot/test/core/ZealotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ public void testInSql() {
*/
@Test(expected = FieldEmptyException.class)
public void testInExceptionSql() {
Zealot.getSqlInfo(MyZealotConfig.STUDENT_ZEALOT, "queryWithInException", null);
Zealot.getSqlInfo(MyZealotConfig.STUDENT_ZEALOT, "queryWithInException");
}

/**
* 系统全面的测试使用`isNull`标签构建的sql片段.
*/
@Test
public void testIsNullSql() {
SqlInfo sqlInfo = Zealot.getSqlInfo(MyZealotConfig.STUDENT_ZEALOT, "queryWithIsNull",
SqlInfo sqlInfo = Zealot.getSqlInfoSimply("studentZealot@@queryWithIsNull",
ParamWrapper.newInstance("id", 123).toMap());
String expectedSql = "SELECT * FROM t_student AS s WHERE s.n_age IS NULL AND s.n_age IS NULL "
+ "OR s.n_age IS NULL AND s.n_age IS NOT NULL AND s.n_age IS NOT NULL OR s.n_age IS NOT NULL";
Expand All @@ -298,7 +298,15 @@ public void testIsNullSql() {
*/
@Test(expected = FieldEmptyException.class)
public void testIsNullExceptionSql() {
Zealot.getSqlInfo(MyZealotConfig.STUDENT_ZEALOT, "queryWithIsNullException", null);
Zealot.getSqlInfoSimply("studentZealot@@queryWithIsNullException");
}

/**
* 测试zealot异常传值时构建的sql片段.
*/
@Test(expected = ValidFailException.class)
public void testZealotExceptionSql() {
Zealot.getSqlInfoSimply("studentZealot@queryWithIsNullException");
}

}
3 changes: 2 additions & 1 deletion src/test/resources/zealot-student.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<zealots>
<!-- 查询学生相关的SQL信息,命名空间nameSpace为zealots的根节点属性,各xml文件的nameSpace不能相同,如果不填nameSpace则需在ZealotConfig中配置与xml的nameSpace映射值. -->
<zealots nameSpace="student">

<zealot id="commonStuCondition">
<andMoreEqual match="?age > 0" field="s.n_age" value="age"/>
Expand Down

0 comments on commit d8a4f54

Please sign in to comment.