private Query createQuery(boolean forCount) { SQLSerializer serializer = new SQLSerializer(configuration); if (union != null) { serializer.serializeUnion(union, queryMixin.getMetadata(), unionAll); } else { serializer.serialize(queryMixin.getMetadata(), forCount); } // create Query if (logger.isDebugEnabled()) { logger.debug(serializer.toString()); } Query query = persistenceManager.newQuery("javax.jdo.query.SQL",serializer.toString()); orderedConstants = serializer.getConstants(); queries.add(query); if (!forCount) { List<? extends Expression<?>> projection = queryMixin.getMetadata().getProjection(); if (projection.get(0) instanceof FactoryExpression) { this.projection = (FactoryExpression<?>)projection.get(0); } } else { query.setResultClass(Long.class); } return query; }
protected SQLSerializer serialize(boolean forCountRow) { SQLSerializer serializer = createSerializer(); if (union != null) { if (queryMixin.getMetadata().getProjection().isEmpty() || expandProjection(queryMixin.getMetadata().getProjection()).equals( expandProjection(firstUnionSubQuery.getMetadata().getProjection()))) { serializer.serializeUnion(union, queryMixin.getMetadata(), unionAll); } else { QueryMixin mixin2 = new QueryMixin(queryMixin.getMetadata().clone()); Set<Path<?>> paths = getRootPaths(expandProjection(mixin2.getMetadata().getProjection())); if (paths.isEmpty()) { mixin2.from(ExpressionUtils.as((Expression) union, defaultQueryAlias)); } else if (paths.size() == 1) { mixin2.from(ExpressionUtils.as((Expression) union, paths.iterator().next())); } else { throw new IllegalStateException("Unable to create serialize union"); } serializer.serialize(mixin2.getMetadata(), forCountRow); } } else { serializer.serialize(queryMixin.getMetadata(), forCountRow); } return serializer; }