Search in sources :

Example 6 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class ClientExpressionIT method testExpressionFactoryMatch.

@Test
public void testExpressionFactoryMatch() throws Exception {
    createDataSet();
    SelectQuery<ClientMtTable1> table1Query = new SelectQuery<ClientMtTable1>(ClientMtTable1.class);
    table1Query.addOrdering(new Ordering("db:TABLE1_ID", SortOrder.ASCENDING));
    List<ClientMtTable1> table1List = context.select(table1Query);
    assertNotNull(table1List);
    ClientMtTable1 element_1 = table1List.get(0);
    ClientMtTable1 element_2 = table1List.get(1);
    Expression exp = ExpressionFactory.matchExp(ClientMtTable2.TABLE1_PROPERTY, element_1);
    SelectQuery<ClientMtTable2> table2Query = new SelectQuery<ClientMtTable2>(ClientMtTable2.class, exp);
    List<ClientMtTable2> table2List = context.select(table2Query);
    assertNotNull(table2List);
    assertEquals(2, table2List.size());
    exp = ExpressionFactory.matchExp(element_2);
    table2Query = new SelectQuery<ClientMtTable2>(ClientMtTable2.class, exp);
    table2List = context.select(table2Query);
    assertNotNull(table2List);
    assertEquals(2, table2List.size());
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) Expression(org.apache.cayenne.exp.Expression) Test(org.junit.Test)

Example 7 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class ClientExpressionIT method testArrayInASTList.

@Test
public void testArrayInASTList() throws Exception {
    ClientMtTable1 t1 = context.newObject(ClientMtTable1.class);
    ClientMtTable1 t2 = context.newObject(ClientMtTable1.class);
    ClientMtTable1 t3 = context.newObject(ClientMtTable1.class);
    context.commitChanges();
    Object[] tArray = new Object[3];
    tArray[0] = t1;
    tArray[1] = t2;
    // send array in expression factory
    Expression list = ExpressionFactory.inExp(ClientMtTable2.TABLE1_PROPERTY, tArray);
    tArray[2] = t3;
    Object[] values = (Object[]) ((ASTList) list.getOperand(1)).getOperand(0);
    assertEquals(tArray.length, values.length);
    assertNotSame(tArray[2], values[2]);
    assertEquals(t1.getObjectId(), values[0]);
    assertEquals(t2.getObjectId(), values[1]);
    ObjectId t1Id = new ObjectId("MtTable1", "TABLE1_ID", 1);
    ObjectId t2Id = new ObjectId("MtTable1", "TABLE1_ID", 2);
    t1.setObjectId(t1Id);
    t2.setObjectId(t2Id);
    // Expression and client have different arrays
    assertNotSame(t1.getObjectId(), values[0]);
    assertNotSame(t2.getObjectId(), values[1]);
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) Expression(org.apache.cayenne.exp.Expression) ObjectId(org.apache.cayenne.ObjectId) Test(org.junit.Test)

Example 8 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class ClientExpressionIT method testPersistentValueInExpression.

@Test
public void testPersistentValueInExpression() throws Exception {
    ClientMtTable1 t1 = context.newObject(ClientMtTable1.class);
    ClientMtTable1 t2 = context.newObject(ClientMtTable1.class);
    context.commitChanges();
    Expression scalar = ExpressionFactory.matchExp(null, t1);
    Expression list = ExpressionFactory.matchAllExp("|", Arrays.asList(t1, t2));
    assertEquals(t1.getObjectId(), scalar.getOperand(1));
    assertEquals(t1.getObjectId(), ((ASTEqual) list.getOperand(0)).getOperand(1));
    assertEquals(t2.getObjectId(), ((ASTEqual) list.getOperand(1)).getOperand(1));
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) Expression(org.apache.cayenne.exp.Expression) Test(org.junit.Test)

Example 9 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class ClientExpressionIT method testParams.

@Test
public void testParams() throws Exception {
    createDataSet();
    SelectQuery<ClientMtTable1> table1Query = new SelectQuery<ClientMtTable1>(ClientMtTable1.class);
    table1Query.addOrdering(new Ordering("db:TABLE1_ID", SortOrder.ASCENDING));
    List<ClientMtTable1> table1List = context.select(table1Query);
    ClientMtTable1 element_1 = table1List.get(0);
    ClientMtTable1 element_5 = table1List.get(4);
    Expression exp = ExpressionFactory.exp("table1 = $attr");
    exp = exp.params(Collections.singletonMap("attr", element_1));
    SelectQuery<ClientMtTable2> table2Query = new SelectQuery<ClientMtTable2>(ClientMtTable2.class, exp);
    List<ClientMtTable2> table2List = context.select(table2Query);
    assertEquals(2, table2List.size());
    exp = exp.andExp(ExpressionFactory.exp("table1 = $attr")).params(Collections.singletonMap("attr", element_5));
    table2Query = new SelectQuery<ClientMtTable2>(ClientMtTable2.class, exp);
    table2List = context.select(table2Query);
    assertEquals(0, table2List.size());
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) Expression(org.apache.cayenne.exp.Expression) Test(org.junit.Test)

Example 10 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class ClientExpressionIT method testExpressionFactoryMatchAny.

@Test
public void testExpressionFactoryMatchAny() throws Exception {
    createDataSet();
    SelectQuery<ClientMtTable2> table2Query = new SelectQuery<ClientMtTable2>(ClientMtTable2.class);
    table2Query.addOrdering(new Ordering("db:TABLE2_ID", SortOrder.ASCENDING));
    List<ClientMtTable2> table2List = context.select(table2Query);
    ClientMtTable2 element_1 = table2List.get(0);
    ClientMtTable2 element_2 = table2List.get(10);
    Expression exp = ExpressionFactory.matchAnyExp(element_1, element_2);
    table2Query = new SelectQuery<ClientMtTable2>(ClientMtTable2.class, exp);
    table2List = context.select(table2Query);
    assertEquals(2, table2List.size());
}
Also used : ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) Expression(org.apache.cayenne.exp.Expression) Test(org.junit.Test)

Aggregations

Expression (org.apache.cayenne.exp.Expression)298 Test (org.junit.Test)265 Artist (org.apache.cayenne.testdo.testmap.Artist)69 SelectQuery (org.apache.cayenne.query.SelectQuery)47 Painting (org.apache.cayenne.testdo.testmap.Painting)29 BigDecimal (java.math.BigDecimal)23 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)17 DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)16 ObjEntity (org.apache.cayenne.map.ObjEntity)13 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)12 DbEntity (org.apache.cayenne.map.DbEntity)10 List (java.util.List)9 HashMap (java.util.HashMap)8 DataRow (org.apache.cayenne.DataRow)8 DbAttribute (org.apache.cayenne.map.DbAttribute)8 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)7 DbRelationship (org.apache.cayenne.map.DbRelationship)6 ObjAttribute (org.apache.cayenne.map.ObjAttribute)6 ObjRelationship (org.apache.cayenne.map.ObjRelationship)6 Gallery (org.apache.cayenne.testdo.testmap.Gallery)6