Search in sources :

Example 76 with DbEntity

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

the class SchemaBuilder method filterDataMap.

/**
 * Remote binary pk {@link DbEntity} for {@link DbAdapter} not supporting
 * that and so on.
 */
protected void filterDataMap(DataMap map) {
    boolean supportsBinaryPK = unitDbAdapter.supportsBinaryPK();
    if (supportsBinaryPK) {
        return;
    }
    List<DbEntity> entitiesToRemove = new ArrayList<DbEntity>();
    for (DbEntity ent : map.getDbEntities()) {
        for (DbAttribute attr : ent.getAttributes()) {
            // check for BIN PK or FK to BIN Pk
            if (attr.getType() == Types.BINARY || attr.getType() == Types.VARBINARY || attr.getType() == Types.LONGVARBINARY) {
                if (attr.isPrimaryKey() || attr.isForeignKey()) {
                    entitiesToRemove.add(ent);
                    break;
                }
            }
        }
    }
    for (DbEntity e : entitiesToRemove) {
        map.removeDbEntity(e.getName(), true);
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) ArrayList(java.util.ArrayList) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 77 with DbEntity

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

the class SchemaBuilder method dropSchema.

private void dropSchema(DataNode node, DataMap map) throws Exception {
    List<DbEntity> list = dbEntitiesInInsertOrder(node, map);
    try (Connection conn = dataSourceFactory.getSharedDataSource().getConnection()) {
        DatabaseMetaData md = conn.getMetaData();
        List<String> allTables = new ArrayList<String>();
        try (ResultSet tables = md.getTables(null, null, "%", null)) {
            while (tables.next()) {
                // 'toUpperCase' is needed since most databases
                // are case insensitive, and some will convert names to
                // lower
                // case
                // (PostgreSQL)
                String name = tables.getString("TABLE_NAME");
                if (name != null)
                    allTables.add(name.toUpperCase());
            }
        }
        unitDbAdapter.willDropTables(conn, map, allTables);
        // drop all tables in the map
        try (Statement stmt = conn.createStatement()) {
            ListIterator<DbEntity> it = list.listIterator(list.size());
            while (it.hasPrevious()) {
                DbEntity ent = it.previous();
                if (!allTables.contains(ent.getName().toUpperCase())) {
                    continue;
                }
                for (String dropSql : node.getAdapter().dropTableStatements(ent)) {
                    try {
                        logger.info(dropSql);
                        stmt.execute(dropSql);
                    } catch (SQLException sqe) {
                        logger.warn("Can't drop table " + ent.getName() + ", ignoring...", sqe);
                    }
                }
            }
        }
        unitDbAdapter.droppedTables(conn, map);
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 78 with DbEntity

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

the class SchemaBuilder method dbEntitiesInDeleteOrder.

protected List<DbEntity> dbEntitiesInDeleteOrder(DataMap dataMap) {
    DataMap map = domain.getDataMap(dataMap.getName());
    List<DbEntity> entities = new ArrayList<>(map.getDbEntities());
    dbEntitiesFilter(entities);
    domain.getEntitySorter().sortDbEntities(entities, true);
    return entities;
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) ArrayList(java.util.ArrayList) DataMap(org.apache.cayenne.map.DataMap)

Example 79 with DbEntity

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

the class SchemaBuilder method dbEntitiesFilter.

private void dbEntitiesFilter(List<DbEntity> entities) {
    // filter various unsupported tests...
    // LOBs
    boolean excludeLOB = !unitDbAdapter.supportsLobs();
    boolean excludeBinPK = !unitDbAdapter.supportsBinaryPK();
    if (excludeLOB || excludeBinPK) {
        List<DbEntity> filtered = new ArrayList<DbEntity>();
        for (DbEntity ent : entities) {
            // check for LOB attributes
            if (excludeLOB) {
                if (Arrays.binarySearch(EXTRA_EXCLUDED_FOR_NO_LOB, ent.getName()) >= 0) {
                    continue;
                }
                boolean hasLob = false;
                for (final DbAttribute attr : ent.getAttributes()) {
                    if (attr.getType() == Types.BLOB || attr.getType() == Types.CLOB) {
                        hasLob = true;
                        break;
                    }
                }
                if (hasLob) {
                    continue;
                }
            }
            // check for BIN PK
            if (excludeBinPK) {
                boolean skip = false;
                for (final DbAttribute attr : ent.getAttributes()) {
                    // check for BIN PK or FK to BIN Pk
                    if (attr.getType() == Types.BINARY || attr.getType() == Types.VARBINARY || attr.getType() == Types.LONGVARBINARY) {
                        if (attr.isPrimaryKey() || attr.isForeignKey()) {
                            skip = true;
                            break;
                        }
                    }
                }
                if (skip) {
                    continue;
                }
            }
            filtered.add(ent);
        }
        entities = filtered;
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) ArrayList(java.util.ArrayList) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 80 with DbEntity

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

the class BatchActionLockingIT method testRunAsIndividualQueriesOptimisticLockingFailure.

@Test
public void testRunAsIndividualQueriesOptimisticLockingFailure() throws Exception {
    EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
    // test with adapter that supports keys...
    JdbcAdapter adapter = buildAdapter(true);
    DbEntity dbEntity = resolver.getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
    List<DbAttribute> qualifierAttributes = Arrays.asList(dbEntity.getAttribute("LOCKING_TEST_ID"), dbEntity.getAttribute("NAME"));
    Collection<String> nullAttributeNames = Collections.singleton("NAME");
    Map<String, Object> qualifierSnapshot = new HashMap<>();
    qualifierSnapshot.put("LOCKING_TEST_ID", 1);
    DeleteBatchQuery batchQuery = new DeleteBatchQuery(dbEntity, qualifierAttributes, nullAttributeNames, 5);
    batchQuery.setUsingOptimisticLocking(true);
    batchQuery.add(qualifierSnapshot);
    DeleteBatchTranslator batchQueryBuilder = new DeleteBatchTranslator(batchQuery, adapter, null);
    MockConnection mockConnection = new MockConnection();
    PreparedStatementResultSetHandler preparedStatementResultSetHandler = mockConnection.getPreparedStatementResultSetHandler();
    preparedStatementResultSetHandler.setExactMatch(false);
    preparedStatementResultSetHandler.setCaseSensitive(false);
    preparedStatementResultSetHandler.prepareUpdateCount("DELETE", 0);
    boolean generatesKeys = false;
    DataNode node = new DataNode();
    node.setAdapter(adapter);
    node.setEntityResolver(resolver);
    node.setRowReaderFactory(mock(RowReaderFactory.class));
    BatchAction action = new BatchAction(batchQuery, node, false);
    try {
        action.runAsIndividualQueries(mockConnection, batchQueryBuilder, new MockOperationObserver(), generatesKeys);
        fail("No OptimisticLockingFailureException thrown.");
    } catch (OptimisticLockException e) {
    }
    assertEquals(0, mockConnection.getNumberCommits());
    assertEquals(0, mockConnection.getNumberRollbacks());
}
Also used : JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) HashMap(java.util.HashMap) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) SimpleLockingTestEntity(org.apache.cayenne.testdo.locking.SimpleLockingTestEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) RowReaderFactory(org.apache.cayenne.access.jdbc.reader.RowReaderFactory) OptimisticLockException(org.apache.cayenne.access.OptimisticLockException) EntityResolver(org.apache.cayenne.map.EntityResolver) MockConnection(com.mockrunner.mock.jdbc.MockConnection) DbEntity(org.apache.cayenne.map.DbEntity) PreparedStatementResultSetHandler(com.mockrunner.jdbc.PreparedStatementResultSetHandler) DataNode(org.apache.cayenne.access.DataNode) DeleteBatchTranslator(org.apache.cayenne.access.translator.batch.DeleteBatchTranslator) MockOperationObserver(org.apache.cayenne.access.MockOperationObserver) 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