From b0992869c0cf9caf865f5e41016d0fb652dd7a1c Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Thu, 15 Dec 2016 03:53:22 +0000 Subject: [PATCH] add method for indicating purity of an expression, possible use for future caching git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/exprs@1069 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/expr/Function.java | 1 + .../jackcess/impl/expr/DefaultFunctions.java | 4 + .../jackcess/impl/expr/Expressionator.java | 73 +++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/src/main/java/com/healthmarketscience/jackcess/expr/Function.java b/src/main/java/com/healthmarketscience/jackcess/expr/Function.java index 9b250b3..10ecc2b 100644 --- a/src/main/java/com/healthmarketscience/jackcess/expr/Function.java +++ b/src/main/java/com/healthmarketscience/jackcess/expr/Function.java @@ -24,4 +24,5 @@ public interface Function { public String getName(); public Value eval(Value... params); + public boolean isPure(); } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java index 70ff3cc..613c276 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java @@ -55,6 +55,10 @@ public class DefaultFunctions return _name; } + public boolean isPure() { + return true; + } + protected void validateNumParams(Value[] params) { int num = params.length; if((num < _minParams) || (num > _maxParams)) { diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/Expressionator.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/Expressionator.java index 5d9c1d1..f8f6e12 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/Expressionator.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/Expressionator.java @@ -360,6 +360,9 @@ public class Expressionator private static final Expr THIS_COL_VALUE = new Expr() { + @Override public boolean isPure() { + return false; + } @Override protected Value eval(RowContext ctx) { return ctx.getThisColumnValue(); } @@ -1170,6 +1173,24 @@ public class Expressionator return paramVals; } + private static boolean arePure(List exprs) { + for(Expr expr : exprs) { + if(!expr.isPure()) { + return false; + } + } + return true; + } + + private static boolean arePure(Expr... exprs) { + for(Expr expr : exprs) { + if(!expr.isPure()) { + return false; + } + } + return true; + } + private static void literalStrToString(String str, StringBuilder sb) { sb.append("\"") .append(str.replace("\"", "\"\"")) @@ -1394,6 +1415,8 @@ public class Expressionator return outerExpr; } + public abstract boolean isPure(); + protected abstract Value eval(RowContext ctx); protected abstract void toExprString(StringBuilder sb, boolean isDebug); @@ -1409,6 +1432,11 @@ public class Expressionator _str = str; } + @Override + public boolean isPure() { + return true; + } + @Override protected Value eval(RowContext ctx) { return _val; @@ -1429,6 +1457,11 @@ public class Expressionator _val = toLiteralValue(valType, value, sdf); } + @Override + public boolean isPure() { + return true; + } + @Override public Value eval(RowContext ctx) { return _val; @@ -1459,6 +1492,11 @@ public class Expressionator _fieldName = fieldName; } + @Override + public boolean isPure() { + return false; + } + @Override public Value eval(RowContext ctx) { return ctx.getRowValue(_collectionName, _objName, _fieldName); @@ -1484,6 +1522,11 @@ public class Expressionator _expr = expr; } + @Override + public boolean isPure() { + return _expr.isPure(); + } + @Override protected Value eval(RowContext ctx) { return _expr.eval(ctx); @@ -1507,6 +1550,11 @@ public class Expressionator _params = params; } + @Override + public boolean isPure() { + return _func.isPure() && arePure(_params); + } + @Override protected Value eval(RowContext ctx) { return _func.eval(exprListToValues(_params, ctx)); @@ -1537,6 +1585,11 @@ public class Expressionator _right = right; } + @Override + public boolean isPure() { + return arePure(_left, _right); + } + public OpType getOp() { return _op; } @@ -1588,6 +1641,11 @@ public class Expressionator _expr = expr; } + @Override + public boolean isPure() { + return _expr.isPure(); + } + public OpType getOp() { return _op; } @@ -1654,6 +1712,11 @@ public class Expressionator _expr = expr; } + @Override + public boolean isPure() { + return _expr.isPure(); + } + public OpType getOp() { return _op; } @@ -1722,6 +1785,11 @@ public class Expressionator _exprs = exprs; } + @Override + public boolean isPure() { + return super.isPure() && arePure(_exprs); + } + @Override protected Value eval(RowContext ctx) { return _op.eval(_expr.eval(ctx), @@ -1750,6 +1818,11 @@ public class Expressionator _endRangeExpr = endRangeExpr; } + @Override + public boolean isPure() { + return _expr.isPure() && arePure(_startRangeExpr, _endRangeExpr); + } + public Expr getRight() { return _endRangeExpr; } -- 2.39.5