From 2732daee60b9c5c906f7ce36e04e26f04637a97e Mon Sep 17 00:00:00 2001 From: Hideki Sugimoto Date: Wed, 30 Oct 2024 22:00:00 +0900 Subject: [PATCH] Changed the internal field that SqlInfo has from a Path type to a URL type (#345) --- .../jp/co/future/uroborosql/SqlAgentImpl.java | 4 +-- .../uroborosql/store/SqlResourceManager.java | 7 ++-- .../store/SqlResourceManagerImpl.java | 32 +++++++++---------- .../store/SqlResourceManagerTest.java | 11 ++++--- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/main/java/jp/co/future/uroborosql/SqlAgentImpl.java b/src/main/java/jp/co/future/uroborosql/SqlAgentImpl.java index f14ab9bc..d478d595 100644 --- a/src/main/java/jp/co/future/uroborosql/SqlAgentImpl.java +++ b/src/main/java/jp/co/future/uroborosql/SqlAgentImpl.java @@ -1885,8 +1885,8 @@ private void transformContext(final ExecutionContext executionContext) { .log(); } else if (getSqlResourceManager().existSql(sqlName)) { debugWith(SQL_LOG) - .setMessage("SQLPath : {}") - .addArgument(() -> getSqlResourceManager().getSqlPath(sqlName)) + .setMessage("SQLUrl : {}") + .addArgument(() -> getSqlResourceManager().getSqlUrl(sqlName)) .log(); } } diff --git a/src/main/java/jp/co/future/uroborosql/store/SqlResourceManager.java b/src/main/java/jp/co/future/uroborosql/store/SqlResourceManager.java index f51393d7..0d11b66b 100644 --- a/src/main/java/jp/co/future/uroborosql/store/SqlResourceManager.java +++ b/src/main/java/jp/co/future/uroborosql/store/SqlResourceManager.java @@ -6,6 +6,7 @@ */ package jp.co.future.uroborosql.store; +import java.net.URL; import java.nio.file.Path; import java.util.List; @@ -107,12 +108,12 @@ public interface SqlResourceManager extends ServiceLoggingSupport { String getSqlName(final Path path); /** - * SQL名に対して現在有効なファイルパスを取得する + * SQL名に対して現在有効なURLを取得する * * @param sqlName SQL名 - * @return 現在有効なファイルパス。存在しないSQL名の場合はUroborosqlRuntimeExceptionがスローされる + * @return 現在有効なURL。存在しないSQL名の場合はUroborosqlRuntimeExceptionがスローされる */ - Path getSqlPath(String sqlName); + URL getSqlUrl(String sqlName); /** * ロードしたSQLのパス一覧を取得する diff --git a/src/main/java/jp/co/future/uroborosql/store/SqlResourceManagerImpl.java b/src/main/java/jp/co/future/uroborosql/store/SqlResourceManagerImpl.java index c503938f..bf411f25 100644 --- a/src/main/java/jp/co/future/uroborosql/store/SqlResourceManagerImpl.java +++ b/src/main/java/jp/co/future/uroborosql/store/SqlResourceManagerImpl.java @@ -288,12 +288,12 @@ protected boolean generateSqlInfo(final String sqlName) throws IOException { try { var scheme = url.toURI().getScheme().toLowerCase(); var sqlBody = loadSql(url); - var sqlInfo = new SqlInfo(path, loadPath, scheme, sqlBody); + var sqlInfo = new SqlInfo(url, loadPath, scheme, sqlBody); this.sqlInfos.put(sqlName, sqlInfo); traceWith(LOG) - .setMessage("SqlInfo - sqlName : {}, path : {}, rootPath : {}, scheme : {}, sqlBody : {}.") + .setMessage("SqlInfo - sqlName : {}, url : {}, rootPath : {}, scheme : {}, sqlBody : {}.") .addArgument(sqlName) - .addArgument(sqlInfo.getPath()) + .addArgument(sqlInfo.getUrl()) .addArgument(sqlInfo.getRootPath()) .addArgument(sqlInfo.getScheme()) .addArgument(sqlInfo.getSqlBody()) @@ -359,16 +359,16 @@ public String getSqlName(final Path path) { * * {@inheritDoc} * - * @see jp.co.future.uroborosql.store.SqlResourceManager#getSqlPath(java.lang.String) + * @see jp.co.future.uroborosql.store.SqlResourceManager#getSqlUrl(java.lang.String) */ @Override - public Path getSqlPath(final String sqlName) { + public URL getSqlUrl(final String sqlName) { if (existSql(sqlName)) { - return sqlInfos.get(sqlName).getPath(); + return sqlInfos.get(sqlName).getUrl(); } else { try { if (generateSqlInfo(sqlName)) { - return sqlInfos.get(sqlName).getPath(); + return sqlInfos.get(sqlName).getUrl(); } else { throw new UroborosqlRuntimeException("sql file not found. sqlName : " + sqlName); } @@ -469,8 +469,8 @@ private Path relativePath(final Path path) { * SQLファイルの情報を保持するオブジェクト */ protected static class SqlInfo { - /** sqlNameに対応するPath. */ - private final Path path; + /** sqlNameに対応するURL. */ + private final URL url; /** 読み込みを行ったSQLルートパス */ private final Path rootPath; @@ -483,24 +483,24 @@ protected static class SqlInfo { /** * コンストラクタ - * @param path Path + * @param url URL * @param rootPath 読み込みを行ったSQLルートパス * @param scheme 読み込んだファイルのscheme * @param sqlBody SqlBody */ - public SqlInfo(final Path path, final Path rootPath, final String scheme, final String sqlBody) { - this.path = path; + public SqlInfo(final URL url, final Path rootPath, final String scheme, final String sqlBody) { + this.url = url; this.rootPath = rootPath; this.scheme = scheme; this.sqlBody = sqlBody; } /** - * sqlNameに対応するPath.を取得します. - * @return sqlNameに対応するPath. + * sqlNameに対応するURLを取得します. + * @return sqlNameに対応するURL. */ - public Path getPath() { - return path; + public URL getUrl() { + return url; } /** diff --git a/src/test/java/jp/co/future/uroborosql/store/SqlResourceManagerTest.java b/src/test/java/jp/co/future/uroborosql/store/SqlResourceManagerTest.java index 10d558fb..f312f0ba 100644 --- a/src/test/java/jp/co/future/uroborosql/store/SqlResourceManagerTest.java +++ b/src/test/java/jp/co/future/uroborosql/store/SqlResourceManagerTest.java @@ -67,10 +67,13 @@ void testGetSqlPath() throws Exception { List.of("example/select_test") .forEach(sqlName -> manager.getSql(sqlName)); - assertThat(manager.getSqlPath("example/select_test"), is(Paths.get("sql/h2/example/select_test.sql"))); - assertThat(manager.getSqlPath("example/select_test2"), is(Paths.get("sql/example/select_test2.sql"))); - assertThat(manager.getSqlPath("example/select_test3"), is(Paths.get("sql/h2/example/select_test3.sql"))); - assertThrows(UroborosqlRuntimeException.class, () -> manager.getSqlPath("example/select_test4")); + assertThat(manager.getSqlUrl("example/select_test").getPath(), + containsString("/sql/h2/example/select_test.sql")); + assertThat(manager.getSqlUrl("example/select_test2").getPath(), + containsString("/sql/example/select_test2.sql")); + assertThat(manager.getSqlUrl("example/select_test3").getPath(), + containsString("/sql/h2/example/select_test3.sql")); + assertThrows(UroborosqlRuntimeException.class, () -> manager.getSqlUrl("example/select_test4")); } @Test