Search in sources :

Example 96 with DbEntity

use of org.apache.cayenne.map.DbEntity 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 97 with DbEntity

use of org.apache.cayenne.map.DbEntity 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 98 with DbEntity

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

the class IdentityColumnsIT method testGeneratedJoinInFlattenedRelationship.

@Test
public void testGeneratedJoinInFlattenedRelationship() throws Exception {
    // before saving objects, let's manually access PKGenerator to get a
    // base PK value
    // for comparison
    DbEntity joinTableEntity = context.getEntityResolver().getDbEntity(joinTable.getTableName());
    DbAttribute pkAttribute = joinTableEntity.getAttribute("ID");
    Number pk = (Number) adapter.getPkGenerator().generatePk(node, pkAttribute);
    GeneratedF1 f1 = context.newObject(GeneratedF1.class);
    GeneratedF2 f2 = context.newObject(GeneratedF2.class);
    f1.addToF2(f2);
    context.commitChanges();
    int id = joinTable.getInt("ID");
    assertTrue(id > 0);
    // base value
    if (adapter.supportsGeneratedKeys()) {
        assertFalse("Looks like auto-increment wasn't used for the join table. ID: " + id, id == pk.intValue() + 1);
    } else {
        assertEquals(id, pk.intValue() + 1);
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) GeneratedF1(org.apache.cayenne.testdo.generated.GeneratedF1) GeneratedF2(org.apache.cayenne.testdo.generated.GeneratedF2) Test(org.junit.Test)

Example 99 with DbEntity

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

the class QuotedIdentifiersIT method testPrefetchQuote.

@Test
public void testPrefetchQuote() throws Exception {
    DbEntity entity = context.getEntityResolver().getObjEntity(QuoteAdress.class).getDbEntity();
    List idAttributes = Collections.singletonList(entity.getAttribute("City"));
    List updatedAttributes = Collections.singletonList(entity.getAttribute("City"));
    UpdateBatchQuery updateQuery = new UpdateBatchQuery(entity, idAttributes, updatedAttributes, Collections.<String>emptySet(), 1);
    List objects3 = context.performQuery(updateQuery);
    assertEquals(0, objects3.size());
    SelectQuery qQuote_Person2 = new SelectQuery(Quote_Person.class);
    List objects4 = context.performQuery(qQuote_Person2);
    assertEquals(2, objects4.size());
    SelectQuery qQuote_Person3 = new SelectQuery(Quote_Person.class, ExpressionFactory.matchExp("salary", 100));
    List objects5 = context.performQuery(qQuote_Person3);
    assertEquals(1, objects5.size());
    SelectQuery qQuote_Person4 = new SelectQuery(Quote_Person.class, ExpressionFactory.matchExp("group", "107324"));
    List objects6 = context.performQuery(qQuote_Person4);
    assertEquals(1, objects6.size());
    SelectQuery quoteAdress1 = new SelectQuery(QuoteAdress.class, ExpressionFactory.matchExp("group", "324"));
    List objects7 = context.performQuery(quoteAdress1);
    assertEquals(1, objects7.size());
    ObjectIdQuery queryObjectId = new ObjectIdQuery(new ObjectId("QuoteAdress", QuoteAdress.GROUP.getName(), "324"));
    List objects8 = context.performQuery(queryObjectId);
    assertEquals(1, objects8.size());
    ObjectIdQuery queryObjectId2 = new ObjectIdQuery(new ObjectId("Quote_Person", "GROUP", "1111"));
    List objects9 = context.performQuery(queryObjectId2);
    assertEquals(1, objects9.size());
    SelectQuery person2Query = new SelectQuery(Quote_Person.class, ExpressionFactory.matchExp("name", "Name"));
    Quote_Person quote_Person2 = (Quote_Person) context.performQuery(person2Query).get(0);
    RelationshipQuery relationshipQuery = new RelationshipQuery(quote_Person2.getObjectId(), "address_Rel");
    List objects10 = context.performQuery(relationshipQuery);
    assertEquals(1, objects10.size());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) QuoteAdress(org.apache.cayenne.testdo.quotemap.QuoteAdress) List(java.util.List) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Quote_Person(org.apache.cayenne.testdo.quotemap.Quote_Person) Test(org.junit.Test)

Example 100 with DbEntity

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

the class DefaultDbAdapterFactoryTest method testCreatedAdapter_Auto.

@Test
public void testCreatedAdapter_Auto() throws Exception {
    final DbAdapter adapter = mock(DbAdapter.class);
    when(adapter.createTable(any(DbEntity.class))).thenReturn("XXXXX");
    List<DbAdapterDetector> detectors = new ArrayList<DbAdapterDetector>();
    detectors.add(new DbAdapterDetector() {

        public DbAdapter createAdapter(DatabaseMetaData md) throws SQLException {
            return adapter;
        }
    });
    MockConnection connection = new MockConnection();
    MockDataSource dataSource = new MockDataSource();
    dataSource.setupConnection(connection);
    Module testModule = binder -> {
        ServerModule.contributeProperties(binder);
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
        binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
        binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DefaultDbAdapterFactory factory = new DefaultDbAdapterFactory(detectors);
    injector.injectMembers(factory);
    DbAdapter createdAdapter = factory.createAdapter(new DataNodeDescriptor(), dataSource);
    assertTrue(createdAdapter instanceof AutoAdapter);
    assertEquals("XXXXX", createdAdapter.createTable(new DbEntity("Test")));
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DefaultValueObjectTypeRegistry(org.apache.cayenne.access.types.DefaultValueObjectTypeRegistry) Module(org.apache.cayenne.di.Module) DatabaseMetaData(java.sql.DatabaseMetaData) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) ValueObjectTypeRegistry(org.apache.cayenne.access.types.ValueObjectTypeRegistry) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Key(org.apache.cayenne.di.Key) BatchTranslatorFactory(org.apache.cayenne.access.translator.batch.BatchTranslatorFactory) ResourceLocator(org.apache.cayenne.resource.ResourceLocator) ClassLoaderResourceLocator(org.apache.cayenne.resource.ClassLoaderResourceLocator) Constants(org.apache.cayenne.configuration.Constants) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ClassLoaderManager(org.apache.cayenne.di.ClassLoaderManager) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) List(java.util.List) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) SybaseAdapter(org.apache.cayenne.dba.sybase.SybaseAdapter) Assert(org.junit.Assert) MockDataSource(com.mockrunner.mock.jdbc.MockDataSource) Mockito.mock(org.mockito.Mockito.mock) DefaultAdhocObjectFactory(org.apache.cayenne.di.spi.DefaultAdhocObjectFactory) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DatabaseMetaData(java.sql.DatabaseMetaData) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) MockDataSource(com.mockrunner.mock.jdbc.MockDataSource) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) 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