Search in sources :

Example 51 with SelectQuery

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

the class DefaultSelectTranslatorIT method testCreateSqlStringWithQuoteSqlIdentifiers4.

@Test
public void testCreateSqlStringWithQuoteSqlIdentifiers4() throws Exception {
    // and with QuoteSqlIdentifiers = true
    try {
        SelectQuery q = new SelectQuery(Painting.class);
        q.addPrefetch(Painting.TO_ARTIST.joint());
        DbEntity entity = context.getEntityResolver().getDbEntity("PAINTING");
        entity.getDataMap().setQuotingSQLIdentifiers(true);
        String charStart = unitAdapter.getIdentifiersStartQuote();
        String charEnd = unitAdapter.getIdentifiersEndQuote();
        String s = new DefaultSelectTranslator(q, dataNode.getAdapter(), dataNode.getEntityResolver()).getSql();
        assertTrue(s.startsWith("SELECT "));
        int iFrom = s.indexOf(" FROM ");
        assertTrue(iFrom > 0);
        int paintingDescription = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "PAINTING_DESCRIPTION" + charEnd);
        assertTrue(paintingDescription > 0 && paintingDescription < iFrom);
        int paintingTitle = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "PAINTING_TITLE" + charEnd);
        assertTrue(paintingTitle > 0 && paintingTitle < iFrom);
        int artistIdT1 = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "ARTIST_ID" + charEnd);
        assertTrue(artistIdT1 > 0 && artistIdT1 < iFrom);
        int estimatedPrice = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "ESTIMATED_PRICE" + charEnd);
        assertTrue(estimatedPrice > 0 && estimatedPrice < iFrom);
        int galleryId = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "GALLERY_ID" + charEnd);
        assertTrue(galleryId > 0 && galleryId < iFrom);
        int paintingId = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "PAINTING_ID" + charEnd);
        assertTrue(paintingId > 0 && paintingId < iFrom);
        int artistName = s.indexOf(charStart + "t1" + charEnd + "." + charStart + "ARTIST_NAME" + charEnd);
        assertTrue(artistName > 0 && artistName < iFrom);
        int artistId = s.indexOf(charStart + "t1" + charEnd + "." + charStart + "ARTIST_ID" + charEnd);
        assertTrue(artistId > 0 && artistId < iFrom);
        int dateOfBirth = s.indexOf(charStart + "t1" + charEnd + "." + charStart + "DATE_OF_BIRTH" + charEnd);
        assertTrue(dateOfBirth > 0 && dateOfBirth < iFrom);
        int iPainting = s.indexOf(charStart + "PAINTING" + charEnd + " " + charStart + "t0" + charEnd);
        assertTrue(iPainting > iFrom);
        int iLeftJoin = s.indexOf("LEFT JOIN");
        assertTrue(iLeftJoin > iFrom);
        int iArtist = s.indexOf(charStart + "ARTIST" + charEnd + " " + charStart + "t1" + charEnd);
        assertTrue(iArtist > iLeftJoin);
        int iOn = s.indexOf(" ON ");
        assertTrue(iOn > iLeftJoin);
        int iArtistId = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "ARTIST_ID" + charEnd, iLeftJoin);
        assertTrue(iArtistId > iOn);
        int iArtistIdT1 = s.indexOf(charStart + "t1" + charEnd + "." + charStart + "ARTIST_ID" + charEnd, iLeftJoin);
        assertTrue(iArtistIdT1 > iOn);
        int i = s.indexOf("=", iLeftJoin);
        assertTrue(iArtistIdT1 > i || iArtistId > i);
    } finally {
        DbEntity entity = context.getEntityResolver().getDbEntity("PAINTING");
        entity.getDataMap().setQuotingSQLIdentifiers(false);
    }
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) DbEntity(org.apache.cayenne.map.DbEntity) Test(org.junit.Test)

Example 52 with SelectQuery

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

the class DefaultSelectTranslatorIT method testCreateSqlString10.

@Test
public void testCreateSqlString10() throws Exception {
    // query with to-many joint prefetches
    SelectQuery q = new SelectQuery(Artist.class);
    q.addPrefetch(Artist.PAINTING_ARRAY.joint());
    DefaultSelectTranslator transl = new DefaultSelectTranslator(q, dataNode.getAdapter(), dataNode.getEntityResolver());
    String sql = transl.getSql();
    assertNotNull(sql);
    assertTrue(sql.startsWith("SELECT "));
    int i1 = sql.indexOf("ARTIST_ID");
    assertTrue(sql, i1 > 0);
    int i2 = sql.indexOf("FROM");
    assertTrue(sql, i2 > 0);
    assertTrue(sql, sql.indexOf("PAINTING_ID") > 0);
    // assert we have one join
    assertEquals(1, transl.joinStack.size());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Test(org.junit.Test)

Example 53 with SelectQuery

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

the class DefaultSelectTranslatorIT method testDbEntityQualifier_FlattenedRel.

@Test
public void testDbEntityQualifier_FlattenedRel() throws Exception {
    SelectQuery<Artist> q = new SelectQuery<Artist>(Artist.class, ExpressionFactory.matchExp("groupArray.name", "bar"));
    final DbEntity entity = context.getEntityResolver().getDbEntity("ARTIST");
    final DbEntity middleEntity = context.getEntityResolver().getDbEntity("ARTIST_GROUP");
    DefaultSelectTranslator transl = new DefaultSelectTranslator(q, dataNode.getAdapter(), dataNode.getEntityResolver());
    entity.setQualifier(ExpressionFactory.exp("ARTIST_NAME = \"123\""));
    middleEntity.setQualifier(ExpressionFactory.exp("GROUP_ID = 1987"));
    try {
        String generatedSql = transl.getSql();
        // do some simple assertions to make sure all parts are in
        assertNotNull(generatedSql);
        assertTrue(generatedSql.startsWith("SELECT "));
        assertTrue(generatedSql.indexOf(" FROM ") > 0);
        if (generatedSql.contains("RTRIM")) {
            assertTrue(generatedSql.indexOf("ARTIST_NAME) = ") > generatedSql.indexOf("RTRIM("));
        } else if (generatedSql.contains("TRIM")) {
            assertTrue(generatedSql.indexOf("ARTIST_NAME) = ") > generatedSql.indexOf("TRIM("));
        } else {
            assertTrue(generatedSql.indexOf("ARTIST_NAME = ") > 0);
        }
    } finally {
        entity.setQualifier(null);
        middleEntity.setQualifier(null);
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) DbEntity(org.apache.cayenne.map.DbEntity) Test(org.junit.Test)

Example 54 with SelectQuery

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

the class DefaultSelectTranslatorIT method testCreateSqlString13.

@Test
public void testCreateSqlString13() throws Exception {
    // query with invalid joint prefetches
    SelectQuery q = new SelectQuery(Painting.class);
    q.addPrefetch("invalid.invalid").setSemantics(PrefetchTreeNode.JOINT_PREFETCH_SEMANTICS);
    try {
        new DefaultSelectTranslator(q, dataNode.getAdapter(), dataNode.getEntityResolver()).getSql();
        fail("Invalid jointPrefetch must have thrown...");
    } catch (ExpressionException e) {
    // expected
    }
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ExpressionException(org.apache.cayenne.exp.ExpressionException) Test(org.junit.Test)

Example 55 with SelectQuery

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

the class DeleteObjectIT method testDeleteHollow.

@Test
public void testDeleteHollow() throws Exception {
    createHollowDataSet();
    List<Painting> paintings = context.performQuery(new SelectQuery(Painting.class));
    Painting p = paintings.get(0);
    Artist a = p.getToArtist();
    assertEquals(PersistenceState.HOLLOW, a.getPersistenceState());
    context.deleteObjects(a);
    assertEquals(PersistenceState.DELETED, a.getPersistenceState());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) Painting(org.apache.cayenne.testdo.testmap.Painting) 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