Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Changes raw queries about User model for compatibility.
Browse files Browse the repository at this point in the history
Issue:
 - A 'user' is used as a reserved word in some databases.
 - Some queries are using the 'distinct' and 'in' clauses unnecessarily.
 - Some queries using 'createUserSearchQueryWithRawSql' function are difficult to read.

Solution:
 - The word, `user`, is changed.
 - Clauses using 'distinct' and 'in' are changed to 'join' clauses.
 - Aliases are added to ambiguous columns and 'createUserSearchQueryWithRawSql' functions are removed.
  • Loading branch information
김창근 committed Feb 10, 2015
1 parent f798de7 commit 9088b76
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions app/models/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -614,10 +614,11 @@ public String avatarUrl() {
* @return
*/
public static List<User> findIssueAuthorsByProjectIdAndMe(User currentUser, long projectId) {
String sql = "select distinct user.id, user.name, user.login_id from issue issue, n4user user where issue.author_id = user.id";
List<User> users = createUserSearchQueryWithRawSql(sql).where()
.eq("issue.project_id", projectId)
.orderBy("user.name ASC")
String sql = "SELECT DISTINCT t0.id AS id, t0.name AS name, t0.login_id AS loginId " +
"FROM n4user t0 JOIN issue t1 ON t0.id = t1.author_id";
List<User> users = find.setRawSql(RawSqlBuilder.parse(sql).create()).where()
.eq("t1.project_id", projectId)
.orderBy().asc("t0.name")
.findList();

if (!users.contains(currentUser)) {
Expand All @@ -635,9 +636,11 @@ public static List<User> findIssueAuthorsByProjectIdAndMe(User currentUser, long
* @return
*/
public static List<User> findIssueAssigneeByProjectIdAndMe(User currentUser, long projectId) {
String sql = "SELECT id, name FROM n4user WHERE id IN (SELECT DISTINCT user_id FROM assignee WHERE id IN (SELECT DISTINCT assignee_id FROM issue WHERE project_id = " + projectId + "))";
List<User> users = User.find.setRawSql(RawSqlBuilder.parse(sql).create())
.orderBy("name ASC")
String sql = "SELECT DISTINCT t0.id AS id, t0.name AS name, t0.login_id AS loginId " +
"FROM n4user t0 JOIN assignee t1 ON t0.id = t1.user_id";
List<User> users = find.setRawSql(RawSqlBuilder.parse(sql).create()).where()
.eq("t1.project_id", projectId)
.orderBy().asc("t0.name")
.findList();

if (!users.contains(currentUser)) {
Expand All @@ -655,18 +658,14 @@ public static List<User> findIssueAssigneeByProjectIdAndMe(User currentUser, lon
* @return
*/
public static List<User> findPullRequestContributorsByProjectId(long projectId) {
String sql = "SELECT user.id, user.name, user.login_id FROM pull_request pullrequest, n4user user WHERE pullrequest.contributor_id = user.id GROUP BY pullrequest.contributor_id";
return createUserSearchQueryWithRawSql(sql).where()
.eq("pullrequest.to_project_id", projectId)
.orderBy("user.name ASC")
String sql = "SELECT DISTINCT t0.id AS id, t0.name AS name, t0.login_id AS loginId " +
"FROM n4user t0 JOIN pull_request t1 ON t0.id = t1.contributor_id";
return find.setRawSql(RawSqlBuilder.parse(sql).create()).where()
.eq("t1.to_project_id", projectId)
.orderBy().asc("t0.name")
.findList();
}

private static com.avaje.ebean.Query<User> createUserSearchQueryWithRawSql(String sql) {
RawSql rawSql = RawSqlBuilder.parse(sql).columnMapping("user.login_id", "loginId").create();
return User.find.setRawSql(rawSql);
}

/**
* find users at a project whose id is {@code projectId} and role is {@code roleType}
*
Expand Down

0 comments on commit 9088b76

Please sign in to comment.