Browse Source

新增 #like 指令用于生成 sql 中的 like 子句

James 3 years ago
parent
commit
af6e9c895d

+ 120 - 0
src/main/java/com/jfinal/plugin/activerecord/sql/LikeDirective.java

@@ -0,0 +1,120 @@
+package com.jfinal.plugin.activerecord.sql;
+
+import com.jfinal.plugin.activerecord.SqlPara;
+import com.jfinal.template.Directive;
+import com.jfinal.template.Env;
+import com.jfinal.template.TemplateException;
+import com.jfinal.template.expr.ast.Const;
+import com.jfinal.template.expr.ast.Expr;
+import com.jfinal.template.expr.ast.ExprList;
+import com.jfinal.template.expr.ast.Id;
+import com.jfinal.template.io.Writer;
+import com.jfinal.template.stat.ParseException;
+import com.jfinal.template.stat.Scope;
+
+/**
+ * #like 指令用于在 sql 模板中根据参数名生成 like 子句问号占位以及查询参数
+ * 基于 #para 指令修改而来
+ *
+ * 备忘:like 子句生成时参数值前后要添加百分号字符,例如:"%" + value + "%"
+ *       https://jfinal.com/doc/5-13
+ *
+ * <pre>
+ * 一、参数为表达式的用法
+ * 1:模板内容
+ *   #sql("find")
+ *     select * from user where #like(nickName)
+ *   #end
+ *
+ * 2: java 代码
+ *   SqlPara sp = getSqlPara("find", Kv.of("nickName", "prettyGirl"));
+ *   user.find(sp)
+ *   或者:
+ *   user.find(sp.getSql(), sp.getPara());
+ *
+ * 3:以上用法会在 #like(expr) 处生成问号占位字符,并且实际的参数放入 SqlPara 对象的参数列表中
+ *   后续可以通过 sqlPara.getPara() 获取到参数并直接用于查询
+ *
+ *
+ * 二、参数为 int 型数字的用法(#like 指令前方需要指定字段名)
+ * 1:模板内容
+ *   #sql("find")
+ *     select * from user where nickName #like(0)
+ *   #end
+ *
+ * 2: java 代码
+ *   SqlPara sp = getSqlPara("find", "james");
+ *   user.find(sp)
+ *
+ * 3:以上用法会在 #like(0) 与 #like(1) 处生成问号占位字符,并且将参数 "james" 放入
+ *    SqlPara 对象的参数列表中,后续可以通过 sqlPara.getPara() 获取到参数并直接用于查询
+ * </pre>
+ */
+public class LikeDirective extends Directive {
+	
+	private int index = -1;
+	private String paraName = null;
+	private static boolean checkParaAssigned = true;
+	
+	public static void setCheckParaAssigned(boolean checkParaAssigned) {
+		LikeDirective.checkParaAssigned = checkParaAssigned;
+	}
+	
+	public void setExprList(ExprList exprList) {
+		if (exprList.length() == 0) {
+			throw new ParseException("The parameter of #like directive can not be blank", location);
+		}
+		
+		if (exprList.length() == 1) {
+			Expr expr = exprList.getExpr(0);
+			if (expr instanceof Const && ((Const)expr).isInt()) {
+				index = ((Const)expr).getInt();
+				if (index < 0) {
+					throw new ParseException("The index of para array must greater than -1", location);
+				}
+			}
+		}
+		
+		if (checkParaAssigned && exprList.getLastExpr() instanceof Id) {
+			Id id = (Id)exprList.getLastExpr();
+			paraName = id.getId();
+		}
+		
+		this.exprList = exprList;
+	}
+	
+	public void exec(Env env, Scope scope, Writer writer) {
+		SqlPara sqlPara = (SqlPara)scope.get(SqlKit.SQL_PARA_KEY);
+		if (sqlPara == null) {
+			throw new TemplateException("#like directive invoked by getSqlPara(...) method only", location);
+		}
+		
+		if (index == -1) {
+			// #like(paraName) 中的 paraName 没有赋值时抛出异常
+			// issue: https://jfinal.com/feedback/1832
+			if (checkParaAssigned && paraName != null && !scope.exists(paraName)) {
+				throw new TemplateException("The parameter \""+ paraName +"\" must be assigned", location);
+			}
+			
+			// 自动添加 like 前方的字段名,例如: #like(title) 生成 title like ?
+			write(writer, paraName);
+			write(writer, " like ?");
+			sqlPara.addPara("%" + exprList.eval(scope) + "%");
+		} else {
+			Object[] paras = (Object[])scope.get(SqlKit.PARA_ARRAY_KEY);
+			if (paras == null) {
+				throw new TemplateException("The #like(" + index + ") directive must invoked by getSqlPara(String, Object...) method", location);
+			}
+			if (index >= paras.length) {
+				throw new TemplateException("The index of #like directive is out of bounds: " + index, location);
+			}
+			
+			// like 前方的字段名写在 sql 模板中,例如: title #like(0) 生成 title like ?
+			write(writer, "like ?");
+			sqlPara.addPara("%" + paras[index] + "%");
+		}
+	}
+}
+
+
+

+ 2 - 0
src/main/java/com/jfinal/plugin/activerecord/sql/SqlKit.java

@@ -55,6 +55,8 @@ public class SqlKit {
 		
 		engine.addDirective("para", ParaDirective.class, true);
 		engine.addDirective("p", ParaDirective.class, true);		// 配置 #para 指令的别名指令 #p,不建议使用,在此仅为兼容 3.0 版本
+		
+		engine.addDirective("like", LikeDirective.class, true);
 	}
 	
 	public SqlKit(String configName) {