Search in sources :

Example 86 with DbAttribute

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

the class JdbcAdapter method createUniqueConstraint.

/**
 * Returns a DDL string to create a unique constraint over a set of columns.
 *
 * @since 1.1
 */
@Override
public String createUniqueConstraint(DbEntity source, Collection<DbAttribute> columns) {
    if (columns == null || columns.isEmpty()) {
        throw new CayenneRuntimeException("Can't create UNIQUE constraint - no columns specified.");
    }
    StringBuilder buf = new StringBuilder();
    buf.append("ALTER TABLE ");
    buf.append(quotingStrategy.quotedFullyQualifiedName(source));
    buf.append(" ADD UNIQUE (");
    Iterator<DbAttribute> it = columns.iterator();
    DbAttribute first = it.next();
    buf.append(quotingStrategy.quotedName(first));
    while (it.hasNext()) {
        DbAttribute next = it.next();
        buf.append(", ");
        buf.append(quotingStrategy.quotedName(next));
    }
    buf.append(")");
    return buf.toString();
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 87 with DbAttribute

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

the class DataContextExtrasIT method testCommitChangesError.

@Test
public void testCommitChangesError() {
    DataDomain domain = context.getParentDataDomain();
    // setup mockup PK generator that will blow on PK request
    // to emulate an exception
    JdbcAdapter jdbcAdapter = objectFactory.newInstance(JdbcAdapter.class, JdbcAdapter.class.getName());
    PkGenerator newGenerator = new JdbcPkGenerator(jdbcAdapter) {

        @Override
        public Object generatePk(DataNode node, DbAttribute pk) throws Exception {
            throw new CayenneRuntimeException("Intentional");
        }
    };
    PkGenerator oldGenerator = domain.getDataNodes().iterator().next().getAdapter().getPkGenerator();
    JdbcAdapter adapter = (JdbcAdapter) domain.getDataNodes().iterator().next().getAdapter();
    adapter.setPkGenerator(newGenerator);
    try {
        Artist newArtist = context.newObject(Artist.class);
        newArtist.setArtistName("aaa");
        context.commitChanges();
        fail("Exception expected but not thrown due to missing PK generation routine.");
    } catch (CayenneRuntimeException ex) {
    // exception expected
    } finally {
        adapter.setPkGenerator(oldGenerator);
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) PkGenerator(org.apache.cayenne.dba.PkGenerator) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) Test(org.junit.Test)

Example 88 with DbAttribute

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

the class HSQLDBAdapter method createUniqueConstraint.

/**
 * Returns a DDL string to create a unique constraint over a set of columns.
 *
 * @since 1.1
 */
@Override
public String createUniqueConstraint(DbEntity source, Collection<DbAttribute> columns) {
    if (columns == null || columns.isEmpty()) {
        throw new CayenneRuntimeException("Can't create UNIQUE constraint - no columns specified.");
    }
    String srcName = getTableName(source);
    StringBuilder buf = new StringBuilder();
    buf.append("ALTER TABLE ").append(srcName);
    buf.append(" ADD CONSTRAINT ");
    String name = "U_" + source.getName() + "_" + (long) (System.currentTimeMillis() / (Math.random() * 100000));
    buf.append(quotingStrategy.quotedIdentifier(source, source.getSchema(), name));
    buf.append(" UNIQUE (");
    Iterator<DbAttribute> it = columns.iterator();
    DbAttribute first = it.next();
    buf.append(quotingStrategy.quotedName(first));
    while (it.hasNext()) {
        DbAttribute next = it.next();
        buf.append(", ");
        buf.append(quotingStrategy.quotedName(next));
    }
    buf.append(")");
    return buf.toString();
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 89 with DbAttribute

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

the class Oracle8LOBBatchAction method performAction.

@Override
public void performAction(Connection connection, OperationObserver observer) throws SQLException, Exception {
    Oracle8LOBBatchTranslator translator;
    if (query instanceof InsertBatchQuery) {
        translator = new Oracle8LOBInsertBatchTranslator((InsertBatchQuery) query, adapter, OracleAdapter.TRIM_FUNCTION);
    } else if (query instanceof UpdateBatchQuery) {
        translator = new Oracle8LOBUpdateBatchTranslator((UpdateBatchQuery) query, adapter, OracleAdapter.TRIM_FUNCTION);
    } else {
        throw new CayenneException("Unsupported batch type for special LOB processing: " + query);
    }
    translator.setNewBlobFunction(OracleAdapter.NEW_BLOB_FUNCTION);
    translator.setNewClobFunction(OracleAdapter.NEW_CLOB_FUNCTION);
    // no batching is done, queries are translated
    // for each batch set, since prepared statements
    // may be different depending on whether LOBs are NULL or not..
    Oracle8LOBBatchQueryWrapper selectQuery = new Oracle8LOBBatchQueryWrapper(query);
    List<DbAttribute> qualifierAttributes = selectQuery.getDbAttributesForLOBSelectQualifier();
    for (BatchQueryRow row : query.getRows()) {
        selectQuery.indexLOBAttributes(row);
        int updated;
        String updateStr = translator.createSql(row);
        // 1. run row update
        logger.log(updateStr);
        try (PreparedStatement statement = connection.prepareStatement(updateStr)) {
            DbAttributeBinding[] bindings = translator.updateBindings(row);
            logger.logQueryParameters("bind", bindings);
            bind(adapter, statement, bindings);
            updated = statement.executeUpdate();
            logger.logUpdateCount(updated);
        }
        // 2. run row LOB update (SELECT...FOR UPDATE and writing out LOBs)
        processLOBRow(connection, translator, selectQuery, qualifierAttributes, row);
        // finally, notify delegate that the row was updated
        observer.nextCount(query, updated);
    }
}
Also used : InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) PreparedStatement(java.sql.PreparedStatement) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) BatchQueryRow(org.apache.cayenne.query.BatchQueryRow) UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) CayenneException(org.apache.cayenne.CayenneException)

Example 90 with DbAttribute

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

the class Oracle8LOBBatchTranslator method createBindings.

@Override
protected DbAttributeBinding[] createBindings() {
    List<DbAttribute> dbAttributes = query.getDbAttributes();
    int len = dbAttributes.size();
    DbAttributeBinding[] bindings = new DbAttributeBinding[len];
    for (int i = 0; i < len; i++) {
        bindings[i] = new DbAttributeBinding(dbAttributes.get(i));
    }
    return bindings;
}
Also used : DbAttribute(org.apache.cayenne.map.DbAttribute) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding)

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