Skip to content

Commit 62e8acc

Browse files
authored
Allow overloading for methods annotated with org.seasar.doma.Sql (#724)
1 parent b948ba9 commit 62e8acc

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

doma-core/src/main/java/org/seasar/doma/jdbc/AbstractSqlFileRepository.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.seasar.doma.jdbc;
22

33
import java.lang.reflect.Method;
4+
import java.util.Objects;
45
import org.seasar.doma.DomaIllegalArgumentException;
56
import org.seasar.doma.DomaNullPointerException;
67
import org.seasar.doma.Sql;
@@ -117,4 +118,29 @@ protected String getSql(String path) {
117118
throw new JdbcException(Message.DOMA2010, cause, path, cause);
118119
}
119120
}
121+
122+
protected static final class CacheKey {
123+
private final Method method;
124+
private final String path;
125+
126+
public CacheKey(Method method, String path) {
127+
Objects.requireNonNull(method);
128+
Objects.requireNonNull(path);
129+
this.method = method.isAnnotationPresent(Sql.class) ? method : null;
130+
this.path = path;
131+
}
132+
133+
@Override
134+
public boolean equals(Object o) {
135+
if (this == o) return true;
136+
if (o == null || getClass() != o.getClass()) return false;
137+
CacheKey cacheKey = (CacheKey) o;
138+
return Objects.equals(method, cacheKey.method) && path.equals(cacheKey.path);
139+
}
140+
141+
@Override
142+
public int hashCode() {
143+
return Objects.hash(method, path);
144+
}
145+
}
120146
}

doma-core/src/main/java/org/seasar/doma/jdbc/GreedyCacheSqlFileRepository.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
/** An SQL file repository that caches the results of SQL parsing without limit. */
99
public class GreedyCacheSqlFileRepository extends AbstractSqlFileRepository {
1010

11-
protected final ConcurrentMap<String, SqlFile> sqlFileMap = new ConcurrentHashMap<>(200);
11+
protected final ConcurrentMap<CacheKey, SqlFile> sqlFileMap = new ConcurrentHashMap<>(200);
1212

1313
@Override
1414
protected SqlFile getSqlFileWithCacheControl(Method method, String path, Dialect dialect) {
15-
SqlFile file = sqlFileMap.get(path);
15+
CacheKey key = new CacheKey(method, path);
16+
SqlFile file = sqlFileMap.get(key);
1617
if (file != null) {
1718
return file;
1819
}
1920
file = createSqlFile(method, path, dialect);
20-
SqlFile current = sqlFileMap.putIfAbsent(path, file);
21+
SqlFile current = sqlFileMap.putIfAbsent(key, file);
2122
return current != null ? current : file;
2223
}
2324

0 commit comments

Comments
 (0)