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();
}
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);
}
}
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();
}
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);
}
}
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;
}
Aggregations