Search in sources :

Example 36 with DbAttribute

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

the class ExportedKeyLoaderIT method testExportedKeyLoad.

@Test
public void testExportedKeyLoad() throws Exception {
    boolean supportsFK = accessStackAdapter.supportsFKConstraints();
    if (!supportsFK) {
        return;
    }
    createEntity(nameForDb("ARTIST"));
    createEntity(nameForDb("GALLERY"));
    createEntity(nameForDb("PAINTING"));
    DbEntity artist = getDbEntity("ARTIST");
    DbAttribute artistId = new DbAttribute("ARTIST_ID");
    artist.addAttribute(artistId);
    DbEntity gallery = getDbEntity("GALLERY");
    DbAttribute galleryId = new DbAttribute("GALLERY_ID");
    gallery.addAttribute(galleryId);
    DbEntity painting = getDbEntity("PAINTING");
    DbAttribute paintingId = new DbAttribute("PAINTING_ID");
    DbAttribute paintingArtistId = new DbAttribute("ARTIST_ID");
    DbAttribute paintingGalleryId = new DbAttribute("GALLERY_ID");
    painting.addAttribute(paintingId);
    painting.addAttribute(paintingArtistId);
    painting.addAttribute(paintingGalleryId);
    ExportedKeyLoader loader = new ExportedKeyLoader(EMPTY_CONFIG, new DefaultDbLoaderDelegate());
    loader.load(connection.getMetaData(), store);
    assertEquals(2, store.getExportedKeysEntrySet().size());
    ExportedKey artistIdFk = findArtistExportedKey();
    assertNotNull(artistIdFk);
    assertEquals("ARTIST", artistIdFk.getPk().getTable().toUpperCase());
    assertEquals("ARTIST_ID", artistIdFk.getPk().getColumn().toUpperCase());
    assertEquals("PAINTING", artistIdFk.getFk().getTable().toUpperCase());
    assertEquals("ARTIST_ID", artistIdFk.getFk().getColumn().toUpperCase());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 37 with DbAttribute

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

the class PrimaryKeyLoaderIT method testPrimaryKeyLoad.

@Test
public void testPrimaryKeyLoad() throws Exception {
    createDbEntities();
    DbEntity artist = getDbEntity(nameForDb("ARTIST"));
    DbAttribute artistId = new DbAttribute(nameForDb("ARTIST_ID"));
    DbAttribute artistName = new DbAttribute(nameForDb("ARTIST_NAME"));
    DbAttribute artistId1 = new DbAttribute(nameForDb("ARTIST_ID1"));
    artist.addAttribute(artistId);
    artist.addAttribute(artistName);
    artist.addAttribute(artistId1);
    assertFalse(artistId.isPrimaryKey());
    assertFalse(artistName.isPrimaryKey());
    assertFalse(artistId1.isPrimaryKey());
    PrimaryKeyLoader loader = new PrimaryKeyLoader(EMPTY_CONFIG, new DefaultDbLoaderDelegate());
    loader.load(connection.getMetaData(), store);
    assertTrue(artistId.isPrimaryKey());
    assertFalse(artistId1.isPrimaryKey());
    assertFalse(artistName.isPrimaryKey());
    Collection<DbAttribute> pk = artist.getPrimaryKeys();
    assertEquals(1, pk.size());
    assertEquals(artistId, pk.iterator().next());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 38 with DbAttribute

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

the class CryptoRowReaderFactoryDecorator method encryptedRowDescriptor.

protected RowDescriptor encryptedRowDescriptor(RowDescriptor descriptor, ExtendedTypeMap typeMap) {
    // need to tweak the original descriptor to ensure binary columns are read as binary, eben if the plain Java
    // type is not a byte[]
    ColumnDescriptor[] originalColumns = descriptor.getColumns();
    int len = originalColumns.length;
    ExtendedType[] originalConverters = descriptor.getConverters();
    ExtendedType[] encryptedConverters = new ExtendedType[len];
    for (int i = 0; i < len; i++) {
        DbAttribute attribute = originalColumns[i].getAttribute();
        ExtendedType t = originalConverters[i];
        if (attribute != null && columnMapper.isEncrypted(attribute)) {
            // only char or binary columns can store encrypted data
            if (TypesMapping.isBinary(attribute.getType())) {
                t = typeMap.getRegisteredType(byte[].class);
            } else if (TypesMapping.isCharacter(attribute.getType())) {
                t = typeMap.getRegisteredType(String.class);
            }
        // else - warning?
        }
        encryptedConverters[i] = t;
    }
    return new RowDescriptor(originalColumns, encryptedConverters);
}
Also used : ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) DbAttribute(org.apache.cayenne.map.DbAttribute) ExtendedType(org.apache.cayenne.access.types.ExtendedType) RowDescriptor(org.apache.cayenne.access.jdbc.RowDescriptor)

Example 39 with DbAttribute

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

the class DefaultValueTransformerFactoryIT method testGetJavaType.

@Test
public void testGetJavaType() {
    DbAttribute t1_ct = t1.getAttribute("CRYPTO_STRING");
    assertEquals("java.lang.String", f.getJavaType(t1_ct));
    DbAttribute t2_cb = t2.getAttribute("CRYPTO_BYTES");
    assertEquals("byte[]", f.getJavaType(t2_cb));
    DbEntity fakeEntity = mock(DbEntity.class);
    when(fakeEntity.getDataMap()).thenReturn(t1.getDataMap());
    DbAttribute fakeA1 = mock(DbAttribute.class);
    when(fakeA1.getName()).thenReturn("fake1");
    when(fakeA1.getEntity()).thenReturn(fakeEntity);
    when(fakeA1.getType()).thenReturn(Types.VARBINARY);
    assertEquals("byte[]", f.getJavaType(fakeA1));
    DbAttribute fakeA2 = mock(DbAttribute.class);
    when(fakeA2.getName()).thenReturn("fake2");
    when(fakeA2.getEntity()).thenReturn(fakeEntity);
    when(fakeA2.getType()).thenReturn(Types.VARCHAR);
    assertEquals("java.lang.String", f.getJavaType(fakeA2));
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 40 with DbAttribute

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

the class DefaultValueTransformerFactoryIT method testCreateDecryptor.

@Test
public void testCreateDecryptor() {
    DbAttribute t1_ct = t1.getAttribute("CRYPTO_STRING");
    ValueDecryptor t1 = f.createDecryptor(t1_ct);
    assertNotNull(t1);
    assertTrue(t1 instanceof DefaultValueDecryptor);
    assertSame(Base64StringConverter.INSTANCE, ((DefaultValueDecryptor) t1).getPreConverter());
    assertSame(Utf8StringConverter.INSTANCE, ((DefaultValueDecryptor) t1).getPostConverter());
    DbAttribute t2_cb = t2.getAttribute("CRYPTO_BYTES");
    ValueDecryptor t2 = f.createDecryptor(t2_cb);
    assertNotNull(t2);
    assertTrue(t2 instanceof DefaultValueDecryptor);
    assertSame(BytesToBytesConverter.INSTANCE, ((DefaultValueDecryptor) t2).getPreConverter());
    assertSame(BytesToBytesConverter.INSTANCE, ((DefaultValueDecryptor) t2).getPostConverter());
    DbAttribute t3_cb = t3.getAttribute("CRYPTO_BYTES");
    ValueDecryptor t3 = f.createDecryptor(t3_cb);
    assertNotNull(t3);
    assertTrue(t3 instanceof DefaultValueDecryptor);
    assertSame(BytesToBytesConverter.INSTANCE, ((DefaultValueDecryptor) t3).getPreConverter());
    assertSame(Utf8StringConverter.INSTANCE, ((DefaultValueDecryptor) t3).getPostConverter());
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Aggregations

DbAttribute (org.apache.cayenne.map.DbAttribute)194 DbEntity (org.apache.cayenne.map.DbEntity)109 Test (org.junit.Test)67 ObjEntity (org.apache.cayenne.map.ObjEntity)36 DbRelationship (org.apache.cayenne.map.DbRelationship)35 ObjAttribute (org.apache.cayenne.map.ObjAttribute)32 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)21 DbJoin (org.apache.cayenne.map.DbJoin)18 HashMap (java.util.HashMap)16 ObjRelationship (org.apache.cayenne.map.ObjRelationship)16 ArrayList (java.util.ArrayList)14 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)12 DataMap (org.apache.cayenne.map.DataMap)11 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)10 QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)10 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)10 DeleteBatchQuery (org.apache.cayenne.query.DeleteBatchQuery)10 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)10 ObjectId (org.apache.cayenne.ObjectId)9 Expression (org.apache.cayenne.exp.Expression)8