Search in sources :

Example 1 with QuotingStrategy

use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.

the class DefaultBatchTranslator method appendDbAttribute.

/**
 * Appends the name of the column to the query buffer. Subclasses use this
 * method to append column names in the WHERE clause, i.e. for the columns
 * that are not being updated.
 */
protected void appendDbAttribute(StringBuilder buf, DbAttribute dbAttribute) {
    // TODO: (Andrus) is there a need for trimming binary types?
    boolean trim = dbAttribute.getType() == Types.CHAR && trimFunction != null;
    if (trim) {
        buf.append(trimFunction).append('(');
    }
    QuotingStrategy strategy = adapter.getQuotingStrategy();
    buf.append(strategy.quotedName(dbAttribute));
    if (trim) {
        buf.append(')');
    }
}
Also used : QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 2 with QuotingStrategy

use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.

the class DeleteBatchTranslator method createSql.

@Override
protected String createSql() {
    QuotingStrategy strategy = adapter.getQuotingStrategy();
    StringBuilder buffer = new StringBuilder("DELETE FROM ");
    buffer.append(strategy.quotedFullyQualifiedName(query.getDbEntity()));
    applyQualifier(buffer);
    return buffer.toString();
}
Also used : QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 3 with QuotingStrategy

use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.

the class UpdateBatchTranslator method createSql.

@Override
protected String createSql() {
    UpdateBatchQuery updateBatch = (UpdateBatchQuery) query;
    QuotingStrategy strategy = adapter.getQuotingStrategy();
    List<DbAttribute> qualifierAttributes = updateBatch.getQualifierAttributes();
    List<DbAttribute> updatedDbAttributes = updateBatch.getUpdatedAttributes();
    StringBuilder buffer = new StringBuilder("UPDATE ");
    buffer.append(strategy.quotedFullyQualifiedName(query.getDbEntity()));
    buffer.append(" SET ");
    int len = updatedDbAttributes.size();
    for (int i = 0; i < len; i++) {
        if (i > 0) {
            buffer.append(", ");
        }
        DbAttribute attribute = updatedDbAttributes.get(i);
        buffer.append(strategy.quotedName(attribute));
        buffer.append(" = ?");
    }
    buffer.append(" WHERE ");
    Iterator<DbAttribute> i = qualifierAttributes.iterator();
    while (i.hasNext()) {
        DbAttribute attribute = i.next();
        appendDbAttribute(buffer, attribute);
        buffer.append(updateBatch.isNull(attribute) ? " IS NULL" : " = ?");
        if (i.hasNext()) {
            buffer.append(" AND ");
        }
    }
    return buffer.toString();
}
Also used : UpdateBatchQuery(org.apache.cayenne.query.UpdateBatchQuery) DbAttribute(org.apache.cayenne.map.DbAttribute) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 4 with QuotingStrategy

use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.

the class EJBQLPathAnaliserTranslator method visitSize.

@Override
public boolean visitSize(EJBQLExpression expression) {
    if (expression.getChildrenCount() != 1) {
        throw new EJBQLException("SIZE must have exactly one child, got: " + expression.getChildrenCount());
    }
    if (!(expression.getChild(0) instanceof EJBQLPath)) {
        throw new EJBQLException("First child of SIZE must be a collection path, got: " + expression.getChild(1));
    }
    QuotingStrategy quoter = context.getQuotingStrategy();
    EJBQLPath path = (EJBQLPath) expression.getChild(0);
    String id = path.getAbsolutePath();
    String correlatedEntityId = path.getId();
    ClassDescriptor correlatedEntityDescriptor = context.getEntityDescriptor(correlatedEntityId);
    String correlatedTableName = quoter.quotedFullyQualifiedName(correlatedEntityDescriptor.getEntity().getDbEntity());
    String correlatedTableAlias = context.getTableAlias(correlatedEntityId, correlatedTableName);
    String subqueryId = context.createIdAlias(id);
    ClassDescriptor targetDescriptor = context.getEntityDescriptor(subqueryId);
    if (expression.isNegated()) {
        context.append(" NOT");
    }
    context.append(" EXISTS (SELECT 1 FROM ");
    String subqueryTableName = quoter.quotedFullyQualifiedName(targetDescriptor.getEntity().getDbEntity());
    String subqueryRootAlias = context.getTableAlias(subqueryId, subqueryTableName);
    ObjRelationship relationship = correlatedEntityDescriptor.getEntity().getRelationship(path.getRelativePath());
    if (relationship.getDbRelationshipPath().contains(".")) {
        // if the DbRelationshipPath contains '.', the relationship is
        // flattened
        subqueryRootAlias = processFlattenedRelationShip(subqueryRootAlias, relationship);
    } else {
        // not using "AS" to separate table name and alias name - OpenBase
        // doesn't
        // support "AS", and the rest of the databases do not care
        context.append(subqueryTableName).append(' ').append(subqueryRootAlias);
    }
    context.append(" WHERE");
    DbRelationship correlatedJoinRelationship = context.getIncomingRelationships(new EJBQLTableId(id)).get(0);
    Iterator<DbJoin> it = correlatedJoinRelationship.getJoins().iterator();
    while (it.hasNext()) {
        DbJoin join = it.next();
        context.append(' ').append(subqueryRootAlias).append('.').append(join.getTargetName()).append(" = ");
        context.append(correlatedTableAlias).append('.').append(quoter.quotedSourceName(join));
        if (it.hasNext()) {
            context.append(" AND");
        }
    }
    context.append(")");
    return false;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DbRelationship(org.apache.cayenne.map.DbRelationship) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbJoin(org.apache.cayenne.map.DbJoin) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy) EJBQLPath(org.apache.cayenne.ejbql.parser.EJBQLPath)

Example 5 with QuotingStrategy

use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.

the class EJBQLJoinAppender method generateJoinsForFlattenedAttributes.

/**
 * Generates Joins statements for those flattened attributes that appear after the
 * FROM clause, e.g. in WHERE, ORDER BY, etc clauses. Flattened attributes of the
 * entity from the SELECT clause are processed earlier and therefore are omitted.
 *
 * @param id table to JOIN id
 */
private void generateJoinsForFlattenedAttributes(EJBQLTableId id) {
    String entityName = context.getEntityDescriptor(id.getEntityId()).getEntity().getName();
    // if the dbPath is not null, all attributes of the entity are processed earlier
    boolean isProcessingOmitted = id.getDbPath() != null;
    String sourceExpression = context.getCompiledExpression().getSource();
    List<Object> resultSetMapping = context.getMetadata().getResultSetMapping();
    for (Object mapping : resultSetMapping) {
        if (mapping instanceof EntityResultSegment) {
            if (entityName.equals(((EntityResultSegment) mapping).getClassDescriptor().getEntity().getName())) {
                // if entity is included into SELECT clause, all its attributes are processed earlier
                isProcessingOmitted = true;
                break;
            }
        }
    }
    if (!isProcessingOmitted) {
        QuotingStrategy quoter = context.getQuotingStrategy();
        Collection<ObjAttribute> attributes = context.getEntityDescriptor(id.getEntityId()).getEntity().getAttributes();
        for (ObjAttribute objAttribute : attributes) {
            if (objAttribute.isFlattened() && sourceExpression.contains(id.getEntityId() + "." + objAttribute.getName())) {
                // joins for attribute are generated if it is flattened and appears in original statement
                Iterator<CayenneMapEntry> dbPathIterator = objAttribute.getDbPathIterator();
                while (dbPathIterator.hasNext()) {
                    CayenneMapEntry next = dbPathIterator.next();
                    if (next instanceof DbRelationship) {
                        DbRelationship rel = (DbRelationship) next;
                        context.append(" LEFT OUTER JOIN ");
                        String targetEntityName = quoter.quotedFullyQualifiedName(rel.getTargetEntity());
                        String subqueryTargetAlias = context.getTableAlias(id.getEntityId(), targetEntityName);
                        context.append(targetEntityName).append(' ').append(subqueryTargetAlias);
                        generateJoiningExpression(rel, context.getTableAlias(id.getEntityId(), quoter.quotedFullyQualifiedName(rel.getSourceEntity())), subqueryTargetAlias);
                    }
                }
            }
        }
    }
}
Also used : CayenneMapEntry(org.apache.cayenne.util.CayenneMapEntry) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbRelationship(org.apache.cayenne.map.DbRelationship) EntityResultSegment(org.apache.cayenne.query.EntityResultSegment) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Aggregations

QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)42 DbAdapter (org.apache.cayenne.dba.DbAdapter)11 DbAttribute (org.apache.cayenne.map.DbAttribute)11 DbEntity (org.apache.cayenne.map.DbEntity)5 DbJoin (org.apache.cayenne.map.DbJoin)5 DbRelationship (org.apache.cayenne.map.DbRelationship)5 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)4 SetAllowNullToDb (org.apache.cayenne.dbsync.merge.token.db.SetAllowNullToDb)3 SetNotNullToDb (org.apache.cayenne.dbsync.merge.token.db.SetNotNullToDb)3 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DropColumnToDb (org.apache.cayenne.dbsync.merge.token.db.DropColumnToDb)2 EJBQLPath (org.apache.cayenne.ejbql.parser.EJBQLPath)2 DataMap (org.apache.cayenne.map.DataMap)2 ObjRelationship (org.apache.cayenne.map.ObjRelationship)2 UpdateBatchQuery (org.apache.cayenne.query.UpdateBatchQuery)2 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)2