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(')');
}
}
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();
}
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();
}
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;
}
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);
}
}
}
}
}
}
Aggregations