Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development: Improve JPQL style for programming repositories #7975

Merged
merged 10 commits into from
Feb 13, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public interface BuildJobRepository extends JpaRepository<BuildJob, Long> {
@Query("""
SELECT new de.tum.in.www1.artemis.service.connectors.localci.dto.DockerImageBuild(
b.dockerImage,
max(b.buildStartDate)
MAX(b.buildStartDate)
)
FROM BuildJob b
WHERE b.dockerImage IN :#{#dockerImageNames}
WHERE b.dockerImage IN :dockerImageNames
GROUP BY b.dockerImage
""")
Set<DockerImageBuild> findLastBuildDatesForDockerImages(@Param("dockerImageNames") List<String> dockerImageNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@
public interface BuildLogStatisticsEntryRepository extends JpaRepository<BuildLogStatisticsEntry, Long> {

@Query("""
SELECT new de.tum.in.www1.artemis.web.rest.dto.BuildLogStatisticsDTO(count(b.id), avg(b.agentSetupDuration), avg(b.testDuration), avg(b.scaDuration), avg(b.totalJobDuration), avg(b.dependenciesDownloadedCount))
FROM BuildLogStatisticsEntry b, Submission s, Participation p
WHERE b.programmingSubmission = s
AND s.participation = p
AND (
p.exercise = :#{#exercise}
OR :#{#exercise.solutionParticipation != null ? #exercise.solutionParticipation.id : #exercise.solutionParticipation} = p.id
OR :#{#exercise.templateParticipation != null ? #exercise.templateParticipation.id : #exercise.templateParticipation} = p.id
)
SELECT new de.tum.in.www1.artemis.web.rest.dto.BuildLogStatisticsDTO(
COUNT(b.id),
AVG(b.agentSetupDuration),
AVG(b.testDuration),
AVG(b.scaDuration),
AVG(b.totalJobDuration),
AVG(b.dependenciesDownloadedCount)
)
FROM BuildLogStatisticsEntry b
LEFT JOIN b.programmingSubmission s
LEFT JOIN s.participation p
LEFT JOIN TREAT (p.exercise as ProgrammingExercise ) e
WHERE e = :exercise
OR e.solutionParticipation = p
OR e.templateParticipation = p
""")
BuildLogStatisticsDTO findAverageBuildLogStatisticsEntryForExercise(@Param("exercise") ProgrammingExercise exercise);

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public interface ProgrammingExerciseStudentParticipationRepository extends JpaRe
LEFT JOIN FETCH pr.submission
WHERE p.id = :participationId
AND (pr.id = (
SELECT max(prr.id)
SELECT MAX(prr.id)
FROM p.results prr
WHERE (prr.assessmentType = 'AUTOMATIC'
WHERE (prr.assessmentType = de.tum.in.www1.artemis.domain.enumeration.AssessmentType.AUTOMATIC
OR (prr.completionDate IS NOT NULL
AND (p.exercise.assessmentDueDate IS NULL
OR p.exercise.assessmentDueDate < :#{#dateTime}))))
OR pr.id IS NULL)
AND (p.exercise.assessmentDueDate IS NULL OR p.exercise.assessmentDueDate < :dateTime)
)
)
) OR pr.id IS NULL)
""")
Optional<ProgrammingExerciseStudentParticipation> findByIdWithLatestResultAndFeedbacksAndRelatedSubmissions(@Param("participationId") Long participationId,
@Param("dateTime") ZonedDateTime dateTime);
Expand All @@ -52,12 +53,11 @@ Optional<ProgrammingExerciseStudentParticipation> findByIdWithLatestResultAndFee
List<ProgrammingExerciseStudentParticipation> findWithResultsAndExerciseAndTeamStudentsByBuildPlanId(String buildPlanId);

@Query("""
SELECT DISTINCT p
FROM ProgrammingExerciseStudentParticipation p
LEFT JOIN FETCH p.results
WHERE p.buildPlanId IS NOT NULL
AND (p.student IS NOT NULL
OR p.team IS NOT NULL)
SELECT DISTINCT p
FROM ProgrammingExerciseStudentParticipation p
LEFT JOIN FETCH p.results
WHERE p.buildPlanId IS NOT NULL
AND (p.student IS NOT NULL OR p.team IS NOT NULL)
""")
List<ProgrammingExerciseStudentParticipation> findAllWithBuildPlanIdWithResults();

Expand Down Expand Up @@ -85,8 +85,8 @@ default ProgrammingExerciseStudentParticipation findWithSubmissionsByExerciseIdA
FROM ProgrammingExerciseStudentParticipation participation
LEFT JOIN FETCH participation.team team
LEFT JOIN FETCH team.students
WHERE participation.exercise.id = :#{#exerciseId}
AND participation.team.shortName = :#{#teamShortName}
WHERE participation.exercise.id = :exerciseId
AND participation.team.shortName = :teamShortName
""")
Optional<ProgrammingExerciseStudentParticipation> findWithEagerStudentsByExerciseIdAndTeamShortName(@Param("exerciseId") Long exerciseId,
@Param("teamShortName") String teamShortName);
Expand All @@ -97,8 +97,8 @@ Optional<ProgrammingExerciseStudentParticipation> findWithEagerStudentsByExercis
LEFT JOIN FETCH participation.submissions
LEFT JOIN FETCH participation.team team
LEFT JOIN FETCH team.students
WHERE participation.exercise.id = :#{#exerciseId}
AND participation.team.shortName = :#{#teamShortName}
WHERE participation.exercise.id = :exerciseId
AND participation.team.shortName = :teamShortName
""")
Optional<ProgrammingExerciseStudentParticipation> findWithSubmissionsAndEagerStudentsByExerciseIdAndTeamShortName(@Param("exerciseId") Long exerciseId,
@Param("teamShortName") String teamShortName);
Expand All @@ -125,8 +125,8 @@ Optional<ProgrammingExerciseStudentParticipation> findWithSubmissionsAndEagerStu
SELECT participation
FROM ProgrammingExerciseStudentParticipation participation
LEFT JOIN FETCH participation.submissions
WHERE participation.exercise.id = :#{#exerciseId}
AND participation.id IN :#{#participationIds}
WHERE participation.exercise.id = :exerciseId
AND participation.id IN :participationIds
""")
List<ProgrammingExerciseStudentParticipation> findWithSubmissionsByExerciseIdAndParticipationIds(@Param("exerciseId") Long exerciseId,
@Param("participationIds") Collection<Long> participationIds);
Expand All @@ -135,9 +135,9 @@ List<ProgrammingExerciseStudentParticipation> findWithSubmissionsByExerciseIdAnd
SELECT participation
FROM ProgrammingExerciseStudentParticipation participation
LEFT JOIN FETCH participation.submissions
WHERE participation.exercise.id = :#{#exerciseId}
AND participation.student.login = :#{#username}
AND participation.testRun = :#{#testRun}
WHERE participation.exercise.id = :exerciseId
AND participation.student.login = :username
AND participation.testRun = :testRun
""")
Optional<ProgrammingExerciseStudentParticipation> findWithSubmissionsByExerciseIdAndStudentLoginAndTestRun(@Param("exerciseId") Long exerciseId,
@Param("username") String username, @Param("testRun") boolean testRun);
Expand All @@ -146,8 +146,8 @@ Optional<ProgrammingExerciseStudentParticipation> findWithSubmissionsByExerciseI
SELECT participation
FROM ProgrammingExerciseStudentParticipation participation
LEFT JOIN FETCH participation.submissions
WHERE participation.exercise.id = :#{#exerciseId}
AND participation.student.login = :#{#username}
WHERE participation.exercise.id = :exerciseId
AND participation.student.login = :username
ORDER BY participation.testRun ASC
""")
List<ProgrammingExerciseStudentParticipation> findAllWithSubmissionsByExerciseIdAndStudentLogin(@Param("exerciseId") Long exerciseId, @Param("username") String username);
Expand Down Expand Up @@ -175,22 +175,23 @@ default ProgrammingExerciseStudentParticipation findWithTeamStudentsByIdElseThro
@Modifying
@Query("""
UPDATE ProgrammingExerciseStudentParticipation p
SET p.locked = :#{#locked}
WHERE p.id = :#{#participationId}
SET p.locked = :locked
WHERE p.id = :participationId
""")
void updateLockedById(@Param("participationId") Long participationId, @Param("locked") boolean locked);

@Query("""
SELECT DISTINCT p
FROM ProgrammingExerciseStudentParticipation p
WHERE p.buildPlanId IS NOT NULL
WHERE p.buildPlanId IS NOT NULL
""")
Page<ProgrammingExerciseStudentParticipation> findAllWithBuildPlanId(Pageable pageable);

@Query("""
SELECT DISTINCT p
FROM ProgrammingExerciseStudentParticipation p
WHERE p.buildPlanId IS NOT NULL or p.repositoryUri IS NOT NULL
WHERE p.buildPlanId IS NOT NULL
OR p.repositoryUri IS NOT NULL
""")
Page<ProgrammingExerciseStudentParticipation> findAllWithRepositoryUriOrBuildPlanId(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ default ProgrammingExerciseTestCase findByIdWithExerciseElseThrow(Long testCaseI
* @return all test cases with the associated solution entries
*/
@Query("""
SELECT DISTINCT tc FROM ProgrammingExerciseTestCase tc
SELECT DISTINCT tc
FROM ProgrammingExerciseTestCase tc
LEFT JOIN FETCH tc.solutionEntries se
WHERE tc.exercise.id = :exerciseId
AND tc.active = :active
Expand All @@ -77,10 +78,10 @@ default ProgrammingExerciseTestCase findByIdWithExerciseElseThrow(Long testCaseI
* @return the number of test cases marked as {@link de.tum.in.www1.artemis.domain.enumeration.Visibility#AFTER_DUE_DATE}.
*/
@Query("""
SELECT count(DISTINCT testCase)
SELECT COUNT(DISTINCT testCase)
FROM ProgrammingExerciseTestCase testCase
WHERE testCase.exercise.id = :exerciseId
AND testCase.visibility = 'AFTER_DUE_DATE'
AND testCase.visibility = de.tum.in.www1.artemis.domain.enumeration.Visibility.AFTER_DUE_DATE
""")
long countAfterDueDateByExerciseId(@Param("exerciseId") Long exerciseId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ default ProgrammingSubmission findFirstByParticipationIdAndCommitHashOrderByIdDe
}

@Query("""
SELECT s FROM ProgrammingSubmission s
LEFT JOIN FETCH s.results
WHERE (s.type <> 'ILLEGAL' or s.type is null)
AND s.participation.id = :#{#participationId}
AND s.id = (SELECT max(s2.id) FROM ProgrammingSubmission s2 WHERE s2.participation.id = :#{#participationId} AND (s2.type <> 'ILLEGAL' or s2.type is null))
SELECT s
FROM ProgrammingSubmission s
LEFT JOIN FETCH s.results
WHERE (s.type <> de.tum.in.www1.artemis.domain.enumeration.SubmissionType.ILLEGAL OR s.type IS NULL)
AND s.participation.id = :participationId
AND s.id = (
SELECT MAX(s2.id)
FROM ProgrammingSubmission s2
WHERE s2.participation.id = :participationId
AND (s2.type <> de.tum.in.www1.artemis.domain.enumeration.SubmissionType.ILLEGAL OR s2.type IS NULL)
)
""")
Optional<ProgrammingSubmission> findFirstByParticipationIdOrderByLegalSubmissionDateDesc(@Param("participationId") Long participationId);

Expand All @@ -75,8 +81,21 @@ default ProgrammingSubmission findFirstByParticipationIdAndCommitHashOrderByIdDe
* @param pageable Pageable
* @return ProgrammingSubmission list (can be empty!)
*/
@EntityGraph(type = LOAD, attributePaths = "results")
@Query("select s from ProgrammingSubmission s left join s.participation p left join p.exercise e where p.id = :#{#participationId} and (s.type = 'INSTRUCTOR' or s.type = 'TEST' or e.dueDate is null or s.submissionDate <= e.dueDate) order by s.submissionDate desc")
@Query("""
SELECT s
FROM ProgrammingSubmission s
LEFT JOIN s.participation p
LEFT JOIN p.exercise e
LEFT JOIN FETCH s.results r
WHERE p.id = :participationId
AND (
s.type = de.tum.in.www1.artemis.domain.enumeration.SubmissionType.INSTRUCTOR
OR s.type = de.tum.in.www1.artemis.domain.enumeration.SubmissionType.TEST
OR e.dueDate IS NULL
OR s.submissionDate <= e.dueDate
)
ORDER BY s.submissionDate DESC
""")
Strohgelaender marked this conversation as resolved.
Show resolved Hide resolved
List<ProgrammingSubmission> findGradedByParticipationIdOrderBySubmissionDateDesc(@Param("participationId") Long participationId, Pageable pageable);

@EntityGraph(type = LOAD, attributePaths = "results")
Expand All @@ -91,11 +110,20 @@ default ProgrammingSubmission findFirstByParticipationIdAndCommitHashOrderByIdDe
@EntityGraph(type = LOAD, attributePaths = { "buildLogEntries" })
Optional<ProgrammingSubmission> findWithEagerBuildLogEntriesById(Long submissionId);

@Query("select s from ProgrammingSubmission s left join fetch s.results r where r.id = :#{#resultId}")
@Query("""
SELECT s
FROM ProgrammingSubmission s
LEFT JOIN FETCH s.results r
WHERE r.id = :resultId
""")
Optional<ProgrammingSubmission> findByResultId(@Param("resultId") Long resultId);

@EntityGraph(type = LOAD, attributePaths = "results")
@Query("select s from ProgrammingSubmission s where s.participation.id = :#{#participationId}")
@Query("""
SELECT DISTINCT s
FROM ProgrammingSubmission s
LEFT JOIN FETCH s.results r
WHERE s.participation.id = :participationId
""")
Strohgelaender marked this conversation as resolved.
Show resolved Hide resolved
List<ProgrammingSubmission> findAllByParticipationIdWithResults(@Param("participationId") Long participationId);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
@Repository
public interface SolutionProgrammingExerciseParticipationRepository extends JpaRepository<SolutionProgrammingExerciseParticipation, Long> {

@EntityGraph(type = LOAD, attributePaths = { "results", "programmingExercise", "programmingExercise.templateParticipation" })
@Query("select p from SolutionProgrammingExerciseParticipation p where p.buildPlanId = :#{#buildPlanId}")
@Query("""
SELECT p
FROM SolutionProgrammingExerciseParticipation p
LEFT JOIN FETCH p.results r
LEFT JOIN FETCH p.programmingExercise e
LEFT JOIN FETCH e.templateParticipation tp
WHERE p.buildPlanId = :buildPlanId
""")
Optional<SolutionProgrammingExerciseParticipation> findByBuildPlanIdWithResults(@Param("buildPlanId") String buildPlanId);

@EntityGraph(type = LOAD, attributePaths = { "results", "submissions", "submissions.results" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public interface StaticCodeAnalysisCategoryRepository extends JpaRepository<Stat
Set<StaticCodeAnalysisCategory> findByExerciseId(Long exerciseId);

@Query("""
SELECT s
FROM StaticCodeAnalysisCategory s
LEFT JOIN FETCH s.exercise
WHERE s.exercise.id = :exerciseId
SELECT s
FROM StaticCodeAnalysisCategory s
LEFT JOIN FETCH s.exercise
WHERE s.exercise.id = :exerciseId
""")
Set<StaticCodeAnalysisCategory> findWithExerciseByExerciseId(@Param("exerciseId") Long exerciseId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
public interface TemplateProgrammingExerciseParticipationRepository extends JpaRepository<TemplateProgrammingExerciseParticipation, Long> {

@EntityGraph(type = LOAD, attributePaths = { "results", "programmingExercise" })
@Query("select p from TemplateProgrammingExerciseParticipation p where p.buildPlanId = :#{#buildPlanId}")
@Query("""
SELECT p
FROM TemplateProgrammingExerciseParticipation p
LEFT JOIN FETCH p.results r
LEFT JOIN FETCH p.programmingExercise e
WHERE p.buildPlanId = :buildPlanId
""")
Optional<TemplateProgrammingExerciseParticipation> findByBuildPlanIdWithResults(@Param("buildPlanId") String buildPlanId);

@EntityGraph(type = LOAD, attributePaths = { "results", "submissions" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface ProgrammingExerciseStudentParticipationTestRepository extends J
@Modifying
@Query("""
UPDATE ProgrammingExerciseStudentParticipation p
SET p.buildPlanId = :#{#buildPlanId}
SET p.buildPlanId = :buildPlanId
""")
void updateBuildPlanIdOfAll(@Param("buildPlanId") Long buildPlanId);

Expand Down
Loading