Search in sources :

Example 41 with DbAdapter

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

the class SybaseMergerTokenFactory method createAddColumnToDb.

/**
 * @since 3.0
 */
@Override
public MergerToken createAddColumnToDb(DbEntity entity, final DbAttribute column) {
    return new AddColumnToDb(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(" ADD ");
            boolean magnatory = column.isMandatory();
            column.setMandatory(false);
            adapter.createTableAppendColumn(sqlBuffer, column);
            if (magnatory) {
                column.setMandatory(magnatory);
            }
            return Collections.singletonList(sqlBuffer.toString());
        }
    };
}
Also used : AddColumnToDb(org.apache.cayenne.dbsync.merge.token.db.AddColumnToDb) DbAdapter(org.apache.cayenne.dba.DbAdapter) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 42 with DbAdapter

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

the class CreateTableToDb method execute.

@Override
public void execute(MergerContext mergerContext) {
    try {
        DataNode node = mergerContext.getDataNode();
        DbAdapter adapter = node.getAdapter();
        if (needAutoPkSupport()) {
            adapter.getPkGenerator().createAutoPk(node, Collections.singletonList(getEntity()));
        }
        executeSql(mergerContext, adapter.createTable(getEntity()));
    } catch (Exception e) {
        mergerContext.getValidationResult().addFailure(new SimpleValidationFailure(this, e.getMessage()));
    }
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) DataNode(org.apache.cayenne.access.DataNode)

Example 43 with DbAdapter

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

the class DbImportConfigurationValidator method validate.

public void validate() throws Exception {
    DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
    DbAdapter adapter;
    try {
        DataSource dataSource = injector.getInstance(DataSourceFactory.class).getDataSource(dataNodeDescriptor);
        adapter = injector.getInstance(DbAdapterFactory.class).createAdapter(dataNodeDescriptor, dataSource);
    } catch (Exception ex) {
        throw new Exception("Error creating DataSource or DbAdapter for DataNodeDescriptor (" + dataNodeDescriptor + ")", ex);
    }
    if (adapter != null && !adapter.supportsCatalogsOnReverseEngineering() && !isReverseEngineeringCatalogsEmpty()) {
        String message = "Your database does not support catalogs on reverse engineering. " + "It allows to connect to only one at the moment. " + "Please don't note catalogs in <dbimport> configuration.";
        throw new Exception(message);
    }
}
Also used : DataSourceFactory(org.apache.cayenne.configuration.server.DataSourceFactory) DbAdapter(org.apache.cayenne.dba.DbAdapter) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) DataSource(javax.sql.DataSource)

Example 44 with DbAdapter

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

the class BatchAction method runAsIndividualQueries.

/**
 * Executes batch as individual queries over the same prepared statement.
 */
protected void runAsIndividualQueries(Connection connection, BatchTranslator translator, OperationObserver delegate, boolean generatesKeys) throws SQLException, Exception {
    JdbcEventLogger logger = dataNode.getJdbcEventLogger();
    boolean useOptimisticLock = query.isUsingOptimisticLocking();
    String queryStr = translator.getSql();
    // log batch SQL execution
    logger.log(queryStr);
    // run batch queries one by one
    DbAdapter adapter = dataNode.getAdapter();
    try (PreparedStatement statement = prepareStatement(connection, queryStr, adapter, generatesKeys)) {
        for (BatchQueryRow row : query.getRows()) {
            DbAttributeBinding[] bindings = translator.updateBindings(row);
            logger.logQueryParameters("bind", bindings);
            bind(adapter, statement, bindings);
            int updated = statement.executeUpdate();
            if (useOptimisticLock && updated != 1) {
                throw new OptimisticLockException(row.getObjectId(), query.getDbEntity(), queryStr, row.getQualifier());
            }
            delegate.nextCount(query, updated);
            if (generatesKeys) {
                processGeneratedKeys(statement, delegate, row);
            }
            logger.logUpdateCount(updated);
        }
    }
}
Also used : BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) DbAdapter(org.apache.cayenne.dba.DbAdapter) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) OptimisticLockException(org.apache.cayenne.access.OptimisticLockException) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

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