Search in sources :

Example 41 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class VerticalInheritanceIT method testSelectQuery_SuperSub.

@Test
public void testSelectQuery_SuperSub() throws Exception {
    TableHelper ivRootTable = new TableHelper(dbHelper, "IV_ROOT");
    ivRootTable.setColumns("ID", "NAME", "DISCRIMINATOR").setColumnTypes(Types.INTEGER, Types.VARCHAR, Types.VARCHAR);
    TableHelper ivSub1Table = new TableHelper(dbHelper, "IV_SUB1");
    ivSub1Table.setColumns("ID", "SUB1_NAME");
    // insert
    ivRootTable.insert(1, "xROOT", null);
    ivRootTable.insert(2, "xSUB1_ROOT", "IvSub1");
    ivSub1Table.insert(2, "xSUB1");
    SelectQuery query = new SelectQuery(IvRoot.class);
    List<IvRoot> results = context.performQuery(query);
    assertEquals(2, results.size());
    // since we don't have ordering, need to analyze results in an order
    // agnostic
    // fashion
    Map<String, IvRoot> resultTypes = new HashMap<>();
    for (IvRoot result : results) {
        resultTypes.put(result.getClass().getName(), result);
    }
    assertEquals(2, resultTypes.size());
    IvRoot root = resultTypes.get(IvRoot.class.getName());
    assertNotNull(root);
    assertEquals("xROOT", root.getName());
    assertNull(root.getDiscriminator());
    IvSub1 sub1 = (IvSub1) resultTypes.get(IvSub1.class.getName());
    assertNotNull(sub1);
    assertEquals("xSUB1_ROOT", sub1.getName());
    assertEquals("IvSub1", sub1.getDiscriminator());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) HashMap(java.util.HashMap) TableHelper(org.apache.cayenne.test.jdbc.TableHelper) Test(org.junit.Test)

Example 42 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class VerticalInheritanceIT method testSelectQuery_MiddleLeaf.

@Test
public void testSelectQuery_MiddleLeaf() throws Exception {
    TableHelper ivRootTable = new TableHelper(dbHelper, "IV_ROOT");
    ivRootTable.setColumns("ID", "NAME", "DISCRIMINATOR").setColumnTypes(Types.INTEGER, Types.VARCHAR, Types.VARCHAR);
    TableHelper ivSub1Table = new TableHelper(dbHelper, "IV_SUB1");
    ivSub1Table.setColumns("ID", "SUB1_NAME");
    TableHelper ivSub2Table = new TableHelper(dbHelper, "IV_SUB2");
    ivSub2Table.setColumns("ID", "SUB2_NAME");
    TableHelper ivSub1Sub1Table = new TableHelper(dbHelper, "IV_SUB1_SUB1");
    ivSub1Sub1Table.setColumns("ID", "SUB1_SUB1_NAME");
    // insert
    ivRootTable.insert(1, "xROOT", null);
    ivRootTable.insert(2, "xSUB1_ROOT", "IvSub1");
    ivSub1Table.insert(2, "xSUB1");
    ivRootTable.insert(3, "xSUB1_SUB1_ROOT", "IvSub1Sub1");
    ivSub1Table.insert(3, "xSUB1_SUB1_SUBROOT");
    ivSub1Sub1Table.insert(3, "xSUB1_SUB1");
    ivRootTable.insert(4, "xROOT_SUB2", "IvSub2");
    ivSub2Table.insert(4, "xSUB2");
    SelectQuery query = new SelectQuery(IvSub1.class);
    List<IvRoot> results = context.performQuery(query);
    assertEquals(2, results.size());
    // since we don't have ordering, need to analyze results in an order
    // agnostic
    // fashion
    Map<String, IvRoot> resultTypes = new HashMap<>();
    for (IvRoot result : results) {
        resultTypes.put(result.getClass().getName(), result);
    }
    assertEquals(2, resultTypes.size());
    IvSub1 sub1 = (IvSub1) resultTypes.get(IvSub1.class.getName());
    assertNotNull(sub1);
    assertEquals("xSUB1_ROOT", sub1.getName());
    assertEquals("IvSub1", sub1.getDiscriminator());
    IvSub1Sub1 sub1Sub1 = (IvSub1Sub1) resultTypes.get(IvSub1Sub1.class.getName());
    assertNotNull(sub1Sub1);
    assertEquals("xSUB1_SUB1_ROOT", sub1Sub1.getName());
    assertEquals("IvSub1Sub1", sub1Sub1.getDiscriminator());
    assertEquals("xSUB1_SUB1_SUBROOT", sub1Sub1.getSub1Name());
    assertEquals("xSUB1_SUB1", sub1Sub1.getSub1Sub1Name());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) HashMap(java.util.HashMap) TableHelper(org.apache.cayenne.test.jdbc.TableHelper) Test(org.junit.Test)

Example 43 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class SelectActionIT method testFetchLimit_DistinctResultIterator.

@Test
public void testFetchLimit_DistinctResultIterator() throws Exception {
    if (accessStackAdapter.supportsLobs()) {
        insertClobDb();
        Expression qual = ExpressionFactory.exp("clobValue.value = 100");
        SelectQuery select = new SelectQuery(ClobTestEntity.class, qual);
        select.setFetchLimit(25);
        List<DataRow> resultRows = context.performQuery(select);
        assertNotNull(resultRows);
        assertEquals(25, resultRows.size());
    }
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Expression(org.apache.cayenne.exp.Expression) DataRow(org.apache.cayenne.DataRow) Test(org.junit.Test)

Example 44 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class SelectActionWithUnsupportedDistinctTypesIT method testCompositionSelectManyToManyQuery.

@Test
public void testCompositionSelectManyToManyQuery() throws SQLException {
    createCompositionManyToManyDataSet();
    SelectQuery query = new SelectQuery(Product.class);
    query.addPrefetch("contained");
    query.addPrefetch("base");
    List<Product> result = context.performQuery(query);
    assertNotNull(result);
    for (Product product : result) {
        List<Product> productsContained = product.getContained();
        assertNotNull(productsContained);
        List<Product> productsBase = product.getBase();
        assertNotNull(productsBase);
        assertEquals(3, productsContained.size() + productsBase.size());
    }
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Product(org.apache.cayenne.testdo.unsupported_distinct_types.Product) Test(org.junit.Test)

Example 45 with SelectQuery

use of org.apache.cayenne.query.SelectQuery in project cayenne by apache.

the class DefaultSelectTranslatorIT method testCreateSqlString9.

@Test
public void testCreateSqlString9() throws Exception {
    // query for a compound ObjEntity with qualifier
    SelectQuery q = new SelectQuery(CompoundPainting.class, ExpressionFactory.likeExp("artistName", "a%"));
    String sql = new DefaultSelectTranslator(q, dataNode.getAdapter(), dataNode.getEntityResolver()).getSql();
    // do some simple assertions to make sure all parts are in
    assertNotNull(sql);
    assertTrue(sql.startsWith("SELECT "));
    int i1 = sql.indexOf(" FROM ");
    assertTrue(i1 > 0);
    int i2 = sql.indexOf("PAINTING");
    assertTrue(i2 > 0);
    int i3 = sql.indexOf("ARTIST");
    assertTrue(i3 > 0);
    int i4 = sql.indexOf("GALLERY");
    assertTrue(i4 > 0);
    int i5 = sql.indexOf("PAINTING_INFO");
    assertTrue(i5 > 0);
    int i6 = sql.indexOf("ARTIST_NAME");
    assertTrue(i6 > 0);
    int i7 = sql.indexOf("ESTIMATED_PRICE");
    assertTrue(i7 > 0);
    int i8 = sql.indexOf("GALLERY_NAME");
    assertTrue(i8 > 0);
    int i9 = sql.indexOf("PAINTING_TITLE");
    assertTrue(i9 > 0);
    int i10 = sql.indexOf("TEXT_REVIEW");
    assertTrue(i10 > 0);
    int i11 = sql.indexOf("PAINTING_ID");
    assertTrue(i11 > 0);
    int i12 = sql.indexOf("ARTIST_ID");
    assertTrue(i12 > 0);
    int i13 = sql.indexOf("GALLERY_ID");
    assertTrue(i13 > 0);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Test(org.junit.Test)

Aggregations

SelectQuery (org.apache.cayenne.query.SelectQuery)360 Test (org.junit.Test)348 Artist (org.apache.cayenne.testdo.testmap.Artist)128 Painting (org.apache.cayenne.testdo.testmap.Painting)75 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)64 Expression (org.apache.cayenne.exp.Expression)47 List (java.util.List)43 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)26 ValueHolder (org.apache.cayenne.ValueHolder)21 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)18 ArrayList (java.util.ArrayList)16 Date (java.util.Date)13 DataRow (org.apache.cayenne.DataRow)12 ObjectContext (org.apache.cayenne.ObjectContext)12 DbEntity (org.apache.cayenne.map.DbEntity)12 SQLTemplate (org.apache.cayenne.query.SQLTemplate)11 ROArtist (org.apache.cayenne.testdo.testmap.ROArtist)11 HashMap (java.util.HashMap)10 ObjectId (org.apache.cayenne.ObjectId)10 Query (org.apache.cayenne.query.Query)10