use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class OpenBasePkGenerator method longPkFromDatabase.
/**
* Generates new (unique and non-repeating) primary key for specified
* DbEntity. Executed SQL looks like this:
*
* <pre>
* NEWID FOR Table Column
* </pre>
*
* COLUMN must be marked as UNIQUE in order for this to work properly.
*
* @since 3.0
*/
@Override
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
String sql = newIDString(entity);
adapter.getJdbcEventLogger().log(sql);
try (Connection con = node.getDataSource().getConnection()) {
try (Statement st = con.createStatement()) {
try (ResultSet rs = st.executeQuery(sql)) {
// Object pk = null;
if (!rs.next()) {
throw new CayenneRuntimeException("Error generating pk for DbEntity %s", entity.getName());
}
return rs.getLong(1);
}
}
}
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class Oracle8LOBBatchAction method writeClob.
/**
* Override the Oracle writeClob() method to be compatible with Oracle8
* drivers.
*/
protected void writeClob(Clob clob, char[] value) {
Method getWriterMethod = Oracle8Adapter.getWriterFromClobMethod();
try {
try (Writer out = (Writer) getWriterMethod.invoke(clob, (Object[]) null)) {
out.write(value);
out.flush();
}
} catch (Exception e) {
throw new CayenneRuntimeException("Error processing CLOB.", Util.unwindException(e));
}
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class Oracle8LOBBatchAction method writeBlob.
/**
* Override the Oracle writeBlob() method to be compatible with Oracle8
* drivers.
*/
protected void writeBlob(Blob blob, byte[] value) {
// Fix for CAY-1307. For Oracle8, get the method found by reflection in
// OracleAdapter. (Code taken from Cayenne 2.)
Method getBinaryStreamMethod = Oracle8Adapter.getOutputStreamFromBlobMethod();
try {
try (OutputStream out = (OutputStream) getBinaryStreamMethod.invoke(blob, (Object[]) null)) {
out.write(value);
out.flush();
}
} catch (Exception e) {
throw new CayenneRuntimeException("Error processing BLOB.", Util.unwindException(e));
}
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class Oracle8LOBBatchAction method writeClob.
/**
* Override the Oracle writeClob() method to be compatible with Oracle8
* drivers.
*/
protected void writeClob(Clob clob, String value) {
Method getWriterMethod = Oracle8Adapter.getWriterFromClobMethod();
try {
try (Writer out = (Writer) getWriterMethod.invoke(clob, (Object[]) null)) {
out.write(value);
out.flush();
}
} catch (Exception e) {
throw new CayenneRuntimeException("Error processing CLOB.", Util.unwindException(e));
}
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class Oracle8LOBBatchAction method processLOBRow.
void processLOBRow(Connection con, Oracle8LOBBatchTranslator queryBuilder, Oracle8LOBBatchQueryWrapper selectQuery, List<DbAttribute> qualifierAttributes, BatchQueryRow row) throws SQLException, Exception {
List<DbAttribute> lobAttributes = selectQuery.getDbAttributesForUpdatedLOBColumns();
if (lobAttributes.size() == 0) {
return;
}
final boolean isLoggable = logger.isLoggable();
List<Object> qualifierValues = selectQuery.getValuesForLOBSelectQualifier(row);
List<Object> lobValues = selectQuery.getValuesForUpdatedLOBColumns();
int parametersSize = qualifierValues.size();
int lobSize = lobAttributes.size();
String selectStr = queryBuilder.createLOBSelectString(lobAttributes, qualifierAttributes);
try (PreparedStatement selectStatement = con.prepareStatement(selectStr)) {
DbAttributeBinding[] attributeBindings = null;
if (isLoggable) {
attributeBindings = new DbAttributeBinding[parametersSize];
}
for (int i = 0; i < parametersSize; i++) {
DbAttribute attribute = qualifierAttributes.get(i);
Object value = qualifierValues.get(i);
ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
DbAttributeBinding binding = new DbAttributeBinding(attribute);
binding.setStatementPosition(i + 1);
binding.setValue(value);
binding.setExtendedType(extendedType);
adapter.bindParameter(selectStatement, binding);
if (isLoggable) {
attributeBindings[i] = binding;
}
}
if (isLoggable) {
logger.logQuery(selectStr, attributeBindings);
}
try (ResultSet result = selectStatement.executeQuery()) {
if (!result.next()) {
throw new CayenneRuntimeException("Missing LOB row.");
}
// read the only expected row
for (int i = 0; i < lobSize; i++) {
DbAttribute attribute = lobAttributes.get(i);
int type = attribute.getType();
if (type == Types.CLOB) {
Clob clob = result.getClob(i + 1);
Object clobVal = lobValues.get(i);
if (clobVal instanceof char[]) {
writeClob(clob, (char[]) clobVal);
} else {
writeClob(clob, clobVal.toString());
}
} else if (type == Types.BLOB) {
Blob blob = result.getBlob(i + 1);
Object blobVal = lobValues.get(i);
if (blobVal instanceof byte[]) {
writeBlob(blob, (byte[]) blobVal);
} else {
String className = (blobVal != null) ? blobVal.getClass().getName() : null;
throw new CayenneRuntimeException("Unsupported class of BLOB value: %s", className);
}
} else {
throw new CayenneRuntimeException("Only BLOB or CLOB is expected here, got: %s", type);
}
}
if (result.next()) {
throw new CayenneRuntimeException("More than one LOB row found.");
}
}
}
}
Aggregations