private Expression getOrCreatePathExpression() { if (pathExpression == null) { // as we expect both Db and Obj paths here, let's pass the path // through the parser to generate the correct expression template... this.pathExpression = ExpressionFactory.exp(dbPath); } return pathExpression; }
private Expression getOrCreateKeyValueExpression() { if (keyValueExpression == null) { // as we expect both Db and Obj paths here, let's pass the path // through the parser to generate the correct expression template... this.keyValueExpression = ExpressionFactory.exp(dbPath + " = $v"); } return keyValueExpression; }
/** * Parses string, converting it to Expression. If string does not represent * a semantically correct expression, an ExpressionException is thrown. * * @since 1.1 * @deprecated since 4.0 use * {@link ExpressionFactory#exp(String, Object...)} */ @Deprecated public static Expression fromString(String expressionString) { return exp(expressionString); }
void setQualifier(String qualifier) { if (qualifier == null || qualifier.trim().length() == 0) { this.qualifier = null; } else { this.qualifier = ExpressionFactory.exp(qualifier.trim()); } }
/** * Appends a qualifier expression of this query, using provided expression * String and an array of position parameters. This is an equivalent to * calling "and". * * @return this object */ public ColumnSelect<T> where(String expressionString, Object... parameters) { return and(ExpressionFactory.exp(expressionString, parameters)); }
/** * Appends a qualifier expression of this query, using provided expression * String and an array of position parameters. This is an equivalent to * calling "and". * * @return this object */ public ObjectSelect<T> where(String expressionString, Object... parameters) { return and(ExpressionFactory.exp(expressionString, parameters)); }
/** * Appends a having qualifier expression of this query, using provided expression * String and an array of position parameters. This is an equivalent to * calling "and". * * @return this object */ public ColumnSelect<T> having(String expressionString, Object... parameters) { havingExpressionIsActive = true; return and(ExpressionFactory.exp(expressionString, parameters)); }
/** * Returns the result of evaluation of expression with object. */ public Object cayenneExp(Object object, String expression) { return ExpressionFactory.exp(expression).evaluate(object); } }
@Test public void testProcess_Map_Params_Date_Local_TZ() { CayenneExp exp = new CayenneExp("cTimestamp=$d", Collections.singletonMap("d", "2014-02-03T14:06:35")); merger.merge(e4Entity, exp); Expression e = e4Entity.getQualifier(); assertNotNull(e); GregorianCalendar cal = new GregorianCalendar(2014, 1, 3, 14, 6, 35); cal.setTimeZone(TimeZone.getDefault()); Date date = cal.getTime(); Expression expected = exp("cTimestamp=$d", date); assertEquals(expected, e); }
@Test public void testProcess_Map_Params_Date_TZ_Zulu() { CayenneExp exp = new CayenneExp("cTimestamp=$d", Collections.singletonMap("d", "2014-02-03T22:06:35Z")); merger.merge(e4Entity, exp); Expression e = e4Entity.getQualifier(); assertNotNull(e); GregorianCalendar cal = new GregorianCalendar(2014, 1, 3, 14, 6, 35); cal.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles")); Date date = cal.getTime(); Expression expected = exp("cTimestamp=$d", date); assertEquals(expected, e); }
@Test public void testProcess_Map_Params_Boolean_True() { CayenneExp exp = new CayenneExp("cBoolean=$b", Collections.singletonMap("b", true)); merger.merge(e4Entity, exp); Expression e = e4Entity.getQualifier(); assertNotNull(e); assertEquals(exp("cBoolean=true"), e); }
@Test public void testProcess_Map_Params_Boolean_False() { CayenneExp exp = new CayenneExp("cBoolean=$b", Collections.singletonMap("b", false)); merger.merge(e4Entity, exp); Expression e = e4Entity.getQualifier(); assertNotNull(e); assertEquals(exp("cBoolean=false"), e); }
@Test public void testProcess_Map_Params_String() { CayenneExp exp = new CayenneExp("cVarchar=$s", Collections.singletonMap("s", "x")); merger.merge(e4Entity, exp); Expression e = e4Entity.getQualifier(); assertNotNull(e); assertEquals(exp("cVarchar='x'"), e); }
@Test public void testProcess_Map_Params_Float() { CayenneExp exp = new CayenneExp("cDecimal=$n", Collections.singletonMap("n", 4.4009)); merger.merge(e4Entity, exp); Expression e = e4Entity.getQualifier(); assertNotNull(e); assertEquals(exp("cDecimal=4.4009"), e); }
@Test public void testQualifier() { @SuppressWarnings("unchecked") ResourceEntity<E2> e2 = new ResourceEntity<>(mock(AgPersistentEntity.class)); assertNull(e2.getQualifier()); e2.andQualifier(ExpressionFactory.exp("a = 1")); assertEquals("a = 1", e2.getQualifier().toString()); e2.andQualifier(ExpressionFactory.exp("b = 2")); assertEquals("(a = 1) and (b = 2)", e2.getQualifier().toString()); } }
@Test public void testProcess_List_Params_Multiple() { CayenneExp exp = new CayenneExp( "cVarchar=$s or cVarchar =$x or cVarchar =$s","x", "y"); merger.merge(e4Entity, exp); Expression e = e4Entity.getQualifier(); assertNotNull(e); assertEquals(exp("cVarchar='x' or cVarchar='y' or cVarchar='x'"), e); }
@Test public void testProcess_Functions() { CayenneExp exp = new CayenneExp("length(cVarchar) > 5"); merger.merge(e4Entity, exp); Expression e = e4Entity.getQualifier(); assertNotNull(e); assertEquals(exp("length(cVarchar) > 5"), e); }
@Test public void testProcess_List_Params_String() { CayenneExp exp = new CayenneExp("cVarchar=$s","x"); merger.merge(e4Entity, exp); Expression e = e4Entity.getQualifier(); assertNotNull(e); assertEquals(exp("cVarchar='x'"), e); }
@Test public void testProcess_Bare() { CayenneExp exp = new CayenneExp("cInt = 12345 and cVarchar = 'John Smith' and cBoolean = true"); merger.merge(e4Entity, exp); Expression e = e4Entity.getQualifier(); assertNotNull(e); assertEquals(exp("cInt = 12345 and cVarchar = 'John Smith' and cBoolean = true"), e); }
@Test public void testSelectRequest_CayenneExp() { SelectContext<E2> context = new SelectContext<>(E2.class); CayenneExp cayenneExp = new CayenneExp("name = 'John Smith'"); context.setRawRequest(AgRequest.builder().cayenneExp(cayenneExp).build()); createEntityStage.execute(context); ResourceEntity<E2> resourceEntity = context.getEntity(); assertNotNull(resourceEntity.getQualifier()); assertEquals(exp("name = 'John Smith'"), resourceEntity.getQualifier()); } }