use of org.apache.cayenne.access.translator.DbAttributeBinding 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.");
}
}
}
}
use of org.apache.cayenne.access.translator.DbAttributeBinding in project cayenne by apache.
the class Oracle8LOBBatchTranslator method doUpdateBindings.
@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
int len = bindings.length;
for (int i = 0, j = 1; i < len; i++) {
DbAttributeBinding b = bindings[i];
Object value = row.getValue(i);
DbAttribute attribute = b.getAttribute();
int type = attribute.getType();
// qualifier
if (isUpdateableColumn(value, type)) {
ExtendedType extendedType = value != null ? adapter.getExtendedTypes().getRegisteredType(value.getClass()) : adapter.getExtendedTypes().getDefaultType();
b.include(j++, value, extendedType);
} else {
b.exclude();
}
}
return bindings;
}
use of org.apache.cayenne.access.translator.DbAttributeBinding in project cayenne by apache.
the class Slf4jJdbcEventLogger method appendParameters.
@SuppressWarnings("unchecked")
private void appendParameters(StringBuilder buffer, String label, ParameterBinding[] bindings) {
int len = bindings.length;
if (len > 0) {
boolean hasIncluded = false;
for (int i = 0, j = 1; i < len; i++) {
ParameterBinding b = bindings[i];
if (b.isExcluded()) {
continue;
}
if (hasIncluded) {
buffer.append(", ");
} else {
hasIncluded = true;
buffer.append("[").append(label).append(": ");
}
buffer.append(j++);
if (b instanceof DbAttributeBinding) {
DbAttribute attribute = ((DbAttributeBinding) b).getAttribute();
if (attribute != null) {
buffer.append("->");
buffer.append(attribute.getName());
}
}
buffer.append(":");
if (b.getExtendedType() != null) {
buffer.append(b.getExtendedType().toString(b.getValue()));
} else if (b.getValue() == null) {
buffer.append("NULL");
} else {
buffer.append(b.getValue().getClass().getName()).append("@").append(System.identityHashCode(b.getValue()));
}
}
if (hasIncluded) {
buffer.append("]");
}
}
}
use of org.apache.cayenne.access.translator.DbAttributeBinding in project cayenne by apache.
the class DefaultBatchTranslatorIT method testConstructor.
@Test
public void testConstructor() throws Exception {
DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
DefaultBatchTranslator builder = new DefaultBatchTranslator(mock(BatchQuery.class), adapter, null) {
@Override
protected String createSql() {
return null;
}
@Override
protected DbAttributeBinding[] createBindings() {
return new DbAttributeBinding[0];
}
@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
return new DbAttributeBinding[0];
}
};
assertSame(adapter, builder.adapter);
}
use of org.apache.cayenne.access.translator.DbAttributeBinding in project cayenne by apache.
the class DefaultBatchTranslatorIT method testAppendDbAttribute2.
@Test
public void testAppendDbAttribute2() throws Exception {
DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
DefaultBatchTranslator builder = new DefaultBatchTranslator(mock(BatchQuery.class), adapter, null) {
@Override
protected String createSql() {
return null;
}
@Override
protected DbAttributeBinding[] createBindings() {
return new DbAttributeBinding[0];
}
@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
return new DbAttributeBinding[0];
}
};
StringBuilder buf = new StringBuilder();
DbEntity entity = new DbEntity("Test");
DbAttribute attr = new DbAttribute("testAttr", Types.CHAR, null);
attr.setEntity(entity);
builder.appendDbAttribute(buf, attr);
assertEquals("testAttr", buf.toString());
buf = new StringBuilder();
attr = new DbAttribute("testAttr", Types.VARCHAR, null);
attr.setEntity(entity);
builder.appendDbAttribute(buf, attr);
assertEquals("testAttr", buf.toString());
}
Aggregations