Search in sources :

Example 1 with DbAdapter

use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.

the class DbGenerator method buildStatements.

/**
 * Creates and stores internally a set of statements for database schema
 * creation, ignoring configured schema creation preferences. Statements are
 * NOT executed in this method.
 */
protected void buildStatements() {
    dropTables = new HashMap<>();
    createTables = new HashMap<>();
    createConstraints = new HashMap<>();
    DbAdapter adapter = getAdapter();
    for (final DbEntity dbe : this.dbEntitiesInInsertOrder) {
        String name = dbe.getName();
        // build "DROP TABLE"
        dropTables.put(name, adapter.dropTableStatements(dbe));
        // build "CREATE TABLE"
        createTables.put(name, adapter.createTable(dbe));
        // build constraints
        createConstraints.put(name, createConstraintsQueries(dbe));
    }
    PkGenerator pkGenerator = adapter.getPkGenerator();
    dropPK = pkGenerator.dropAutoPkStatements(dbEntitiesRequiringAutoPK);
    createPK = pkGenerator.createAutoPkStatements(dbEntitiesRequiringAutoPK);
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) PkGenerator(org.apache.cayenne.dba.PkGenerator)

Example 2 with DbAdapter

use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.

the class BatchAction method runAsBatch.

protected void runAsBatch(Connection con, BatchTranslator translator, OperationObserver delegate) throws SQLException, Exception {
    String sql = translator.getSql();
    JdbcEventLogger logger = dataNode.getJdbcEventLogger();
    boolean isLoggable = logger.isLoggable();
    // log batch SQL execution
    logger.log(sql);
    // run batch
    DbAdapter adapter = dataNode.getAdapter();
    try (PreparedStatement statement = con.prepareStatement(sql)) {
        for (BatchQueryRow row : query.getRows()) {
            DbAttributeBinding[] bindings = translator.updateBindings(row);
            logger.logQueryParameters("batch bind", bindings);
            bind(adapter, statement, bindings);
            statement.addBatch();
        }
        // execute the whole batch
        int[] results = statement.executeBatch();
        delegate.nextBatchCount(query, results);
        if (isLoggable) {
            int totalUpdateCount = 0;
            for (int result : results) {
                // Statement.EXECUTE_FAILED
                if (result < 0) {
                    totalUpdateCount = Statement.SUCCESS_NO_INFO;
                    break;
                }
                totalUpdateCount += result;
            }
            logger.logUpdateCount(totalUpdateCount);
        }
    }
}
Also used : BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) DbAdapter(org.apache.cayenne.dba.DbAdapter) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

Example 3 with DbAdapter

use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.

the class FirebirdMergerTokenFactory method createSetNotNullToDb.

@Override
public MergerToken createSetNotNullToDb(DbEntity entity, DbAttribute column) {
    return new SetNotNullToDb(entity, column) {

        public List<String> createSql(DbAdapter adapter) {
            QuotingStrategy context = adapter.getQuotingStrategy();
            String entityName = context.quotedFullyQualifiedName(getEntity());
            String columnName = context.quotedName(getColumn());
            // but this might be achived by modyfication of system tables
            return Collections.singletonList(String.format("UPDATE RDB$RELATION_FIELDS SET RDB$NULL_FLAG = 1 " + "WHERE RDB$FIELD_NAME = '%s' AND RDB$RELATION_NAME = '%s'", columnName, entityName));
        }
    };
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) SetNotNullToDb(org.apache.cayenne.dbsync.merge.token.db.SetNotNullToDb) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 4 with DbAdapter

use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.

the class MySQLMergerTokenFactory method createSetNotNullToDb.

@Override
public MergerToken createSetNotNullToDb(final DbEntity entity, final DbAttribute column) {
    return new SetNotNullToDb(entity, column) {

        @Override
        public List<String> createSql(DbAdapter adapter) {
            StringBuffer sqlBuffer = new StringBuffer();
            QuotingStrategy context = adapter.getQuotingStrategy();
            sqlBuffer.append("ALTER TABLE ");
            sqlBuffer.append(context.quotedFullyQualifiedName(getEntity()));
            sqlBuffer.append(" CHANGE ");
            sqlBuffer.append(context.quotedName(getColumn()));
            sqlBuffer.append(" ");
            adapter.createTableAppendColumn(sqlBuffer, column);
            return Collections.singletonList(sqlBuffer.toString());
        }
    };
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) SetNotNullToDb(org.apache.cayenne.dbsync.merge.token.db.SetNotNullToDb) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 5 with DbAdapter

use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.

the class OpenBaseMergerTokenFactory method createDropRelationshipToDb.

@Override
public MergerToken createDropRelationshipToDb(final DbEntity entity, final DbRelationship rel) {
    return new DropRelationshipToDb(entity, rel) {

        @Override
        public List<String> createSql(DbAdapter adapter) {
            // FK_NAME form jdbc metadata seem to be wrong. It contain a column name
            // and not the 'relationshipName'
            // TODO: tell openbase developer mail list
            DbEntity source = getEntity();
            DbEntity dest = rel.getTargetEntity();
            // only use the first. See adapter
            // TODO: can we be sure this is the first and same as used by the adapter?
            DbJoin join = rel.getJoins().get(0);
            return Collections.singletonList("delete from _SYS_RELATIONSHIP where " + " source_table = '" + dest.getFullyQualifiedName() + "'" + " and source_column = '" + join.getTargetName() + "'" + " and dest_table = '" + source.getFullyQualifiedName() + "'" + " and dest_column = '" + join.getSourceName() + "'");
        }
    };
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) DropRelationshipToDb(org.apache.cayenne.dbsync.merge.token.db.DropRelationshipToDb) DbJoin(org.apache.cayenne.map.DbJoin)

Aggregations

DbAdapter (org.apache.cayenne.dba.DbAdapter)44 Test (org.junit.Test)17 DbEntity (org.apache.cayenne.map.DbEntity)16 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)11 QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)11 UnitDbAdapter (org.apache.cayenne.unit.UnitDbAdapter)10 Injector (org.apache.cayenne.di.Injector)8 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)7 List (java.util.List)6 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)6 JdbcEventLogger (org.apache.cayenne.log.JdbcEventLogger)6 ArrayList (java.util.ArrayList)5 DbAttributeBinding (org.apache.cayenne.access.translator.DbAttributeBinding)5 AutoAdapter (org.apache.cayenne.dba.AutoAdapter)5 DataMap (org.apache.cayenne.map.DataMap)5 BatchQueryRow (org.apache.cayenne.query.BatchQueryRow)5 SimpleLockingTestEntity (org.apache.cayenne.testdo.locking.SimpleLockingTestEntity)5 MockConnection (com.mockrunner.mock.jdbc.MockConnection)4 MockDataSource (com.mockrunner.mock.jdbc.MockDataSource)4 DatabaseMetaData (java.sql.DatabaseMetaData)4