Search in sources :

Example 56 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class QualifierTranslator method finishedChild.

@Override
public void finishedChild(Expression node, int childIndex, boolean hasMoreChildren) {
    if (waitingForEndNode != null) {
        return;
    }
    if (!hasMoreChildren) {
        return;
    }
    Appendable out = (matchingObject) ? new StringBuilder() : this.out;
    try {
        switch(node.getType()) {
            case Expression.AND:
                out.append(" AND ");
                break;
            case Expression.OR:
                out.append(" OR ");
                break;
            case Expression.EQUAL_TO:
                // translate NULL as IS NULL
                if (childIndex == 0 && node.getOperandCount() == 2 && node.getOperand(1) == null) {
                    out.append(" IS ");
                } else {
                    out.append(" = ");
                }
                break;
            case Expression.NOT_EQUAL_TO:
                // translate NULL as IS NOT NULL
                if (childIndex == 0 && node.getOperandCount() == 2 && node.getOperand(1) == null) {
                    out.append(" IS NOT ");
                } else {
                    out.append(" <> ");
                }
                break;
            case Expression.LESS_THAN:
                out.append(" < ");
                break;
            case Expression.GREATER_THAN:
                out.append(" > ");
                break;
            case Expression.LESS_THAN_EQUAL_TO:
                out.append(" <= ");
                break;
            case Expression.GREATER_THAN_EQUAL_TO:
                out.append(" >= ");
                break;
            case Expression.IN:
                out.append(" IN ");
                break;
            case Expression.NOT_IN:
                out.append(" NOT IN ");
                break;
            case Expression.LIKE:
                out.append(" LIKE ");
                break;
            case Expression.NOT_LIKE:
                out.append(" NOT LIKE ");
                break;
            case Expression.LIKE_IGNORE_CASE:
                if (caseInsensitive) {
                    out.append(" LIKE ");
                } else {
                    out.append(") LIKE UPPER(");
                }
                break;
            case Expression.NOT_LIKE_IGNORE_CASE:
                if (caseInsensitive) {
                    out.append(" NOT LIKE ");
                } else {
                    out.append(") NOT LIKE UPPER(");
                }
                break;
            case Expression.ADD:
                out.append(" + ");
                break;
            case Expression.SUBTRACT:
                out.append(" - ");
                break;
            case Expression.MULTIPLY:
                out.append(" * ");
                break;
            case Expression.DIVIDE:
                out.append(" / ");
                break;
            case Expression.BETWEEN:
                if (childIndex == 0) {
                    out.append(" BETWEEN ");
                } else if (childIndex == 1) {
                    out.append(" AND ");
                }
                break;
            case Expression.NOT_BETWEEN:
                if (childIndex == 0) {
                    out.append(" NOT BETWEEN ");
                } else if (childIndex == 1) {
                    out.append(" AND ");
                }
                break;
            case Expression.BITWISE_OR:
                out.append(" ").append(operandForBitwiseOr()).append(" ");
                break;
            case Expression.BITWISE_AND:
                out.append(" ").append(operandForBitwiseAnd()).append(" ");
                break;
            case Expression.BITWISE_XOR:
                out.append(" ").append(operandForBitwiseXor()).append(" ");
                break;
            case Expression.BITWISE_LEFT_SHIFT:
                out.append(" ").append(operandForBitwiseLeftShift()).append(" ");
                break;
            case Expression.BITWISE_RIGHT_SHIFT:
                out.append(" ").append(operandForBitwiseRightShift()).append("");
                break;
        }
    } catch (IOException ioex) {
        throw new CayenneRuntimeException("Error appending content", ioex);
    }
    if (matchingObject) {
        objectMatchTranslator.setOperation(out.toString());
        objectMatchTranslator.setExpression(node);
    }
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) IOException(java.io.IOException)

Example 57 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException 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 58 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException 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 59 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class DataContextExtrasIT method testResolveFaultFailure.

@Test
public void testResolveFaultFailure() {
    Persistent o1 = context.findOrCreateObject(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 234));
    try {
        context.prepareForAccess(o1, null, false);
        fail("Must blow on non-existing fault.");
    } catch (CayenneRuntimeException ignored) {
    }
}
Also used : ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 60 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class CayenneTransaction method processRollback.

@Override
protected void processRollback() {
    status = BaseTransaction.STATUS_ROLLING_BACK;
    if (connections == null || connections.isEmpty()) {
        return;
    }
    Throwable deferredException = null;
    for (Connection connection : connections.values()) {
        try {
            // continue with rollback even if an exception was thrown
            // before
            connection.rollback();
        } catch (Throwable th) {
            // stores last exception
            // TODO: chain exceptions...
            deferredException = th;
        }
    }
    if (deferredException != null) {
        throw new CayenneRuntimeException(deferredException);
    }
}
Also used : Connection(java.sql.Connection) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

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