/** * Build a predicate that will only include users if this directory is their canonical directory. * i.e. ignore users that are shadowed by a higher priority directory. * * This method can return null if a predicate is not necessary. QueryDSL will gracefully ignore the null predicate. * * @param u the user table * @return the predicate, or null if there are no higher priority directories. */ @Nullable private BooleanExpression userIsNotShadowedPredicate(final QUser u) { BooleanExpression userIsNotShadowedPredicate = null; if (!higherPriorityDirectoryIds.isEmpty()) { QUser u2 = new QUser("u2"); userIsNotShadowedPredicate = new SQLSubQuery() .from(u2) .where(u2.lowerUserName.eq(u.lowerUserName) .and(u2.directoryId.in(higherPriorityDirectoryIds))) .notExists(); } return userIsNotShadowedPredicate; } }
@Test @ExcludeIn({Target.DERBY, Target.POSTGRES}) public void Union2() { List<Tuple> rows = query().union( new SQLSubQuery().from(cat).where(cat.name.eq("Beck")).distinct().list(cat.name, cat.id), new SQLSubQuery().from(cat).where(cat.name.eq("Kate")).distinct().list(cat.name, null)) .list(); assertEquals(2, rows.size()); for (Tuple row : rows) { System.err.println(row); } }
.where(permission.targetResourceId.eq(targetResourceId).and( actionPredicate.and(authorizedResourceIdPredicate))) .exists();
.set(it.avatar, new SQLSubQuery() .from(a) .where(a.avatarType.eq(ISSUE_TYPE_AVATAR) .and(a.systemAvatar.eq(TRUE)) .and(a.fileName.eq(newIconFileName)))
/** * This version of the query performs pretty well on all databases... except for MySql * @param dbConnection the database connection * @return the user count */ private Long runStandardQuery(final DbConnection dbConnection) { QMembership m = new QMembership("m"); QUser u = new QUser("u"); // Predicate to only include users in their canonical directory (i.e. they are not shadowed) BooleanExpression userIsNotShadowed = userIsNotShadowedPredicate(u); // Predicate to find users that are a member of one or more of the nested groups, in this directory. // Use exists() to ensure we count each user only once, even if they are a member of multiple groups BooleanExpression userIsMemberOfGroups = new SQLSubQuery() .from(m).where(m.lowerChildName.eq(u.lowerUserName) .and(m.membershipType.eq("GROUP_USER")) .and(m.directoryId.eq(directoryId)) .and(sqlPredicates.partitionedIn(m.lowerParentName, lowerCaseGroupNames))) .exists(); // Query to count active users who are not shadowed, and are a member of at least one of these groups SQLQuery query = dbConnection.newSqlQuery() .from(u).where(u.active.eq(1).and(u.directoryId.eq(directoryId)) .and(userIsNotShadowed) .and(userIsMemberOfGroups)); // SQL Server has a limit of 2000 parameters per query. If we hit that, use literals instead if (dbConfig.isSqlServer() && lowerCaseGroupNames.size() > SQL_SERVER_USE_LITERALS_THRESHOLD) { query.setUseLiterals(true); } return query.singleResult(u.lowerUserName.count()); }
/** * This version of the query runs better on MySql (but worse on other databases, particularly MS SQL Server) * @param dbConnection the database connection * @return the user count */ private Long runMySqlQuery(final DbConnection dbConnection) { QMembership m = new QMembership("m"); QUser u = new QUser("u"); // Predicate to only include users in their canonical directory (i.e. they are not shadowed) BooleanExpression userIsNotShadowed = userIsNotShadowedPredicate(u); // Query to find only active users that are not shadowed, and that are a member of one or more of the nested // groups in this directory. // Use groupBy() to ensure we count each user only once, even if they are a member of multiple groups final SQLSubQuery uniqueUsersSubQuery = new SQLSubQuery() .from(m).innerJoin(u).on(u.id.eq(m.childId)) .where(u.active.eq(1) .and(m.membershipType.eq("GROUP_USER")) .and(m.directoryId.eq(directoryId)) .and(userIsNotShadowed) .and(m.lowerParentName.in(lowerCaseGroupNames))) .groupBy(m.lowerChildName); final DslExpression<List<String>> uniqueUsers = uniqueUsersSubQuery.list(m.lowerChildName).as("uniqueUsers"); // An outer count(*) query SQLQuery countQuery = dbConnection.newSqlQuery(); countQuery.from(uniqueUsers); return countQuery.singleResult(Wildcard.count); }
.where(property.id.eq(new SQLSubQuery() .from(entry) .where(entry.propertyKey.eq(DEFAULT_PROJECT_AVATAR_KEY)) .unique(entry.id))) .set(property.value, defaultProjectAvatarId.toString())