Search in sources :

Example 91 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class UpdateBatchTranslatorIT method testCreateSqlStringWithNullsWithIdentifiersQuote.

@Test
public void testCreateSqlStringWithNullsWithIdentifiersQuote() throws Exception {
    DbEntity entity = runtime.getDataDomain().getEntityResolver().getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
    try {
        entity.getDataMap().setQuotingSQLIdentifiers(true);
        List idAttributes = Arrays.asList(entity.getAttribute("LOCKING_TEST_ID"), entity.getAttribute("NAME"));
        List updatedAttributes = Collections.singletonList(entity.getAttribute("DESCRIPTION"));
        Collection nullAttributes = Collections.singleton("NAME");
        UpdateBatchQuery updateQuery = new UpdateBatchQuery(entity, idAttributes, updatedAttributes, nullAttributes, 1);
        JdbcAdapter adapter = (JdbcAdapter) this.adapter;
        UpdateBatchTranslator builder = new UpdateBatchTranslator(updateQuery, adapter, null);
        String generatedSql = builder.getSql();
        assertNotNull(generatedSql);
        String charStart = unitAdapter.getIdentifiersStartQuote();
        String charEnd = unitAdapter.getIdentifiersEndQuote();
        assertEquals("UPDATE " + charStart + entity.getName() + charEnd + " SET " + charStart + "DESCRIPTION" + charEnd + " = ? WHERE " + charStart + "LOCKING_TEST_ID" + charEnd + " = ? AND " + charStart + "NAME" + charEnd + " IS NULL", generatedSql);
    } finally {
        entity.getDataMap().setQuotingSQLIdentifiers(false);
    }
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbEntity(org.apache.cayenne.map.DbEntity) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) Collection(java.util.Collection) List(java.util.List) Test(org.junit.Test)

Example 92 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class DefaultSelectTranslatorIT method testDbEntityQualifier_RelatedMatch.

@Test
public void testDbEntityQualifier_RelatedMatch() throws Exception {
    SelectQuery<Artist> q = new SelectQuery(Painting.class, ExpressionFactory.matchExp("toArtist.artistName", "foo"));
    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 93 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class DefaultSelectTranslatorIT method testCreateSqlStringWithQuoteSqlIdentifiers.

@Test
public void testCreateSqlStringWithQuoteSqlIdentifiers() throws Exception {
    try {
        SelectQuery q = new SelectQuery(Artist.class);
        DbEntity entity = context.getEntityResolver().getDbEntity("ARTIST");
        entity.getDataMap().setQuotingSQLIdentifiers(true);
        q.addOrdering("dateOfBirth", SortOrder.ASCENDING);
        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 artistName = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "ARTIST_NAME" + charEnd);
        assertTrue(artistName > 0 && artistName < iFrom);
        int artistId = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "ARTIST_ID" + charEnd);
        assertTrue(artistId > 0 && artistId < iFrom);
        int dateOfBirth = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "DATE_OF_BIRTH" + charEnd);
        assertTrue(dateOfBirth > 0 && dateOfBirth < iFrom);
        int iArtist = s.indexOf(charStart + "ARTIST" + charEnd + " " + charStart + "t0" + charEnd);
        assertTrue(iArtist > iFrom);
        int iOrderBy = s.indexOf(" ORDER BY ");
        int dateOfBirth2 = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "DATE_OF_BIRTH" + charEnd, iOrderBy);
        assertTrue(iOrderBy > iArtist);
        assertTrue(dateOfBirth2 > iOrderBy);
    } finally {
        DbEntity entity = context.getEntityResolver().getDbEntity("ARTIST");
        entity.getDataMap().setQuotingSQLIdentifiers(false);
    }
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) DbEntity(org.apache.cayenne.map.DbEntity) Test(org.junit.Test)

Example 94 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class DefaultSelectTranslatorIT method testDbEntityQualifier_OuterJoin.

@Test
public void testDbEntityQualifier_OuterJoin() throws Exception {
    SelectQuery<Painting> q = SelectQuery.query(Painting.class);
    q.addOrdering("toArtist+.artistName", SortOrder.ASCENDING);
    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 : DbEntity(org.apache.cayenne.map.DbEntity) CompoundPainting(org.apache.cayenne.testdo.testmap.CompoundPainting) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 95 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class DefaultSelectTranslatorIT method testCreateSqlStringWithQuoteSqlIdentifiers3.

@Test
public void testCreateSqlStringWithQuoteSqlIdentifiers3() throws Exception {
    // and with QuoteSqlIdentifiers = true
    try {
        SelectQuery q = new SelectQuery(Artist.class, ExpressionFactory.exp("paintingArray.paintingTitle = 'a'"));
        q.addPrefetch(Artist.PAINTING_ARRAY.joint());
        DbEntity entity = context.getEntityResolver().getDbEntity("ARTIST");
        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 DISTINCT "));
        int iFrom = s.indexOf(" FROM ");
        assertTrue(iFrom > 0);
        int artistName = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "ARTIST_NAME" + charEnd);
        assertTrue(artistName > 0 && artistName < iFrom);
        int artistId = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "ARTIST_ID" + charEnd);
        assertTrue(artistId > 0 && artistId < iFrom);
        int dateOfBirth = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "DATE_OF_BIRTH" + charEnd);
        assertTrue(dateOfBirth > 0 && dateOfBirth < iFrom);
        int estimatedPrice = s.indexOf(charStart + "t1" + charEnd + "." + charStart + "ESTIMATED_PRICE" + charEnd);
        assertTrue(estimatedPrice > 0 && estimatedPrice < iFrom);
        int paintingDescription = s.indexOf(charStart + "t1" + charEnd + "." + charStart + "PAINTING_DESCRIPTION" + charEnd);
        assertTrue(paintingDescription > 0 && paintingDescription < iFrom);
        int paintingTitle = s.indexOf(charStart + "t1" + charEnd + "." + charStart + "PAINTING_TITLE" + charEnd);
        assertTrue(paintingTitle > 0 && paintingTitle < iFrom);
        int artistIdT1 = s.indexOf(charStart + "t1" + charEnd + "." + charStart + "ARTIST_ID" + charEnd);
        assertTrue(artistIdT1 > 0 && artistIdT1 < iFrom);
        int galleryId = s.indexOf(charStart + "t1" + charEnd + "." + charStart + "GALLERY_ID" + charEnd);
        assertTrue(galleryId > 0 && galleryId < iFrom);
        int paintingId = s.indexOf(charStart + "t1" + charEnd + "." + charStart + "PAINTING_ID" + charEnd);
        assertTrue(paintingId > 0 && paintingId < iFrom);
        int iArtist = s.indexOf(charStart + "ARTIST" + charEnd + " " + charStart + "t0" + charEnd);
        assertTrue(iArtist > iFrom);
        int iLeftJoin = s.indexOf("LEFT JOIN");
        assertTrue(iLeftJoin > iFrom);
        int iPainting = s.indexOf(charStart + "PAINTING" + charEnd + " " + charStart + "t1" + charEnd);
        assertTrue(iPainting > 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);
        int iJoin = s.indexOf("JOIN");
        assertTrue(iJoin > iLeftJoin);
        int iPainting2 = s.indexOf(charStart + "PAINTING" + charEnd + " " + charStart + "t2" + charEnd);
        assertTrue(iPainting2 > iJoin);
        int iOn2 = s.indexOf(" ON ");
        assertTrue(iOn2 > iJoin);
        int iArtistId2 = s.indexOf(charStart + "t0" + charEnd + "." + charStart + "ARTIST_ID" + charEnd, iJoin);
        assertTrue(iArtistId2 > iOn2);
        int iArtistId2T2 = s.indexOf(charStart + "t2" + charEnd + "." + charStart + "ARTIST_ID" + charEnd, iJoin);
        assertTrue(iArtistId2T2 > iOn2);
        int i2 = s.indexOf("=", iJoin);
        assertTrue(iArtistId2T2 > i2 || iArtistId2 > i2);
        int iWhere = s.indexOf(" WHERE ");
        assertTrue(iWhere > iJoin);
        int paintingTitle2 = s.indexOf(charStart + "t2" + charEnd + "." + charStart + "PAINTING_TITLE" + charEnd + " = ?");
        assertTrue(paintingTitle2 > iWhere);
    } finally {
        DbEntity entity = context.getEntityResolver().getDbEntity("ARTIST");
        entity.getDataMap().setQuotingSQLIdentifiers(false);
    }
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) DbEntity(org.apache.cayenne.map.DbEntity) Test(org.junit.Test)

Aggregations

DbEntity (org.apache.cayenne.map.DbEntity)273 DbAttribute (org.apache.cayenne.map.DbAttribute)106 Test (org.junit.Test)106 ObjEntity (org.apache.cayenne.map.ObjEntity)64 DbRelationship (org.apache.cayenne.map.DbRelationship)55 DataMap (org.apache.cayenne.map.DataMap)47 ObjAttribute (org.apache.cayenne.map.ObjAttribute)26 ArrayList (java.util.ArrayList)25 DbJoin (org.apache.cayenne.map.DbJoin)24 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)20 ObjRelationship (org.apache.cayenne.map.ObjRelationship)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)16 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)16 Entity (org.apache.cayenne.map.Entity)16 List (java.util.List)15 DbAdapter (org.apache.cayenne.dba.DbAdapter)15 EntityEvent (org.apache.cayenne.map.event.EntityEvent)14 HashMap (java.util.HashMap)12 SelectQuery (org.apache.cayenne.query.SelectQuery)12 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)11