Search in sources :

Example 91 with CayenneRuntimeException

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);
            }
        }
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 92 with CayenneRuntimeException

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));
    }
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Method(java.lang.reflect.Method) Writer(java.io.Writer) SQLException(java.sql.SQLException) CayenneException(org.apache.cayenne.CayenneException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 93 with CayenneRuntimeException

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));
    }
}
Also used : OutputStream(java.io.OutputStream) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Method(java.lang.reflect.Method) SQLException(java.sql.SQLException) CayenneException(org.apache.cayenne.CayenneException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 94 with CayenneRuntimeException

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));
    }
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Method(java.lang.reflect.Method) Writer(java.io.Writer) SQLException(java.sql.SQLException) CayenneException(org.apache.cayenne.CayenneException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 95 with CayenneRuntimeException

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.");
            }
        }
    }
}
Also used : Blob(java.sql.Blob) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) PreparedStatement(java.sql.PreparedStatement) DbAttributeBinding(org.apache.cayenne.access.translator.DbAttributeBinding) ResultSet(java.sql.ResultSet) ExtendedType(org.apache.cayenne.access.types.ExtendedType) Clob(java.sql.Clob)

Aggregations

CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)168 Test (org.junit.Test)25 DbAttribute (org.apache.cayenne.map.DbAttribute)21 DataMap (org.apache.cayenne.map.DataMap)19 ObjectId (org.apache.cayenne.ObjectId)18 ObjEntity (org.apache.cayenne.map.ObjEntity)18 Persistent (org.apache.cayenne.Persistent)17 Expression (org.apache.cayenne.exp.Expression)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 DbEntity (org.apache.cayenne.map.DbEntity)14 IOException (java.io.IOException)13 List (java.util.List)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)12 DbRelationship (org.apache.cayenne.map.DbRelationship)10 DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)10 File (java.io.File)9 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9