diff --git a/src/main/java/jp/co/future/uroborosql/client/command/ExitCommand.java b/src/main/java/jp/co/future/uroborosql/client/command/ExitCommand.java index 8d80fcc0..793ca5c3 100644 --- a/src/main/java/jp/co/future/uroborosql/client/command/ExitCommand.java +++ b/src/main/java/jp/co/future/uroborosql/client/command/ExitCommand.java @@ -42,7 +42,7 @@ public boolean execute(final LineReader reader, final String[] parts, final SqlC try { var sqlResourceManager = sqlConfig.getSqlResourceManager(); - sqlResourceManager.shutdown(); + sqlResourceManager.clearCache(); } catch (Exception ex) { // do nothing } diff --git a/src/main/java/jp/co/future/uroborosql/client/command/ParseCommand.java b/src/main/java/jp/co/future/uroborosql/client/command/ParseCommand.java index e43e4554..815ac127 100644 --- a/src/main/java/jp/co/future/uroborosql/client/command/ParseCommand.java +++ b/src/main/java/jp/co/future/uroborosql/client/command/ParseCommand.java @@ -53,60 +53,59 @@ public boolean execute(final LineReader reader, final String[] parts, final SqlC writer.println("PARSE:"); if (parts.length > 1) { - sqlConfig.getSqlResourceManager().getSqlPathList().stream() - .filter(p -> p.equalsIgnoreCase(parts[1])) - .findFirst() - .ifPresentOrElse(path -> { - var sql = sqlConfig.getSqlResourceManager().getSql(path); - - // 対象SQLの出力 - writer.println(""); - writer.println("SQL :"); - var sqlLines = sql.split("\\r\\n|\\r|\\n"); - for (var sqlLine : sqlLines) { - writer.println(sqlLine); - } - - // IF分岐の出力 - writer.println(""); - writer.println("BRANCHES :"); - var parser = new SqlParserImpl(sql, sqlConfig.getExpressionParser(), - sqlConfig.getDialect().isRemoveTerminator(), true); - var transformer = parser.parse(); - var rootNode = transformer.getRoot(); - Set bindParams = new LinkedHashSet<>(); - traverseNode(writer, 0, sqlConfig.getExpressionParser(), rootNode, bindParams); - - // 利用されているバインドパラメータの出力 - writer.println(""); - var constPrefix = sqlConfig.getExecutionContextProvider().getConstParamPrefix(); - var ctx = sqlConfig.getExecutionContextProvider().createExecutionContext(); - writer.println("BIND_PARAMS :"); - // 定数以外のバインドパラメータ - bindParams.stream().filter(param -> !param.startsWith(constPrefix)) - .sorted() - .forEach(param -> write(writer, 1, null, param, null)); - // 定数バインドパラメータ - bindParams.stream().filter(param -> param.startsWith(constPrefix)) - .sorted().forEach(param -> { - // 定数についてはバインド値が取得できるので、実際の値を追加で表示 - var parameter = ctx.getParam(param); - if (parameter != null) { - var value = parameter.getValue(); - String suffix = null; - if (value instanceof String) { - suffix = String.format(" ('%s')", value); - } else { - suffix = String.format(" (%s)", value); - } - write(writer, 1, null, param, suffix); - } else { - write(writer, 1, null, param, " (not found)"); - } - }); - }, () -> { - writer.println("sqlName : " + parts[1] + " not found."); - }); + var sqlResourceManager = sqlConfig.getSqlResourceManager(); + var sqlName = parts[1].replace('.', '/'); + if (sqlResourceManager.reloadSql(sqlName)) { + var sql = sqlResourceManager.getSql(sqlName); + + // 対象SQLの出力 + writer.println(""); + writer.println("SQL :"); + var sqlLines = sql.split("\\r\\n|\\r|\\n"); + for (var sqlLine : sqlLines) { + writer.println(sqlLine); + } + + // IF分岐の出力 + writer.println(""); + writer.println("BRANCHES :"); + var parser = new SqlParserImpl(sql, sqlConfig.getExpressionParser(), + sqlConfig.getDialect().isRemoveTerminator(), true); + var transformer = parser.parse(); + var rootNode = transformer.getRoot(); + Set bindParams = new LinkedHashSet<>(); + traverseNode(writer, 0, sqlConfig.getExpressionParser(), rootNode, bindParams); + + // 利用されているバインドパラメータの出力 + writer.println(""); + var constPrefix = sqlConfig.getExecutionContextProvider().getConstParamPrefix(); + var ctx = sqlConfig.getExecutionContextProvider().createExecutionContext(); + writer.println("BIND_PARAMS :"); + // 定数以外のバインドパラメータ + bindParams.stream().filter(param -> !param.startsWith(constPrefix)) + .sorted() + .forEach(param -> write(writer, 1, null, param, null)); + // 定数バインドパラメータ + bindParams.stream().filter(param -> param.startsWith(constPrefix)) + .sorted().forEach(param -> { + // 定数についてはバインド値が取得できるので、実際の値を追加で表示 + var parameter = ctx.getParam(param); + if (parameter != null) { + var value = parameter.getValue(); + String suffix = null; + if (value instanceof String) { + suffix = String.format(" ('%s')", value); + } else { + suffix = String.format(" (%s)", value); + } + write(writer, 1, null, param, suffix); + } else { + write(writer, 1, null, param, " (not found)"); + } + }); + } else { + writer.println("sqlName : " + sqlName + " not found."); + } } else { writer.println("sqlName must be specified."); } diff --git a/src/main/java/jp/co/future/uroborosql/client/command/QueryCommand.java b/src/main/java/jp/co/future/uroborosql/client/command/QueryCommand.java index 8375876d..f78f43d1 100644 --- a/src/main/java/jp/co/future/uroborosql/client/command/QueryCommand.java +++ b/src/main/java/jp/co/future/uroborosql/client/command/QueryCommand.java @@ -45,11 +45,12 @@ public boolean execute(final LineReader reader, final String[] parts, final SqlC final Properties props) { var writer = reader.getTerminal().writer(); if (parts.length >= 2) { + var sqlResourceManager = sqlConfig.getSqlResourceManager(); var sqlName = parts[1].replace('.', '/'); - if (sqlConfig.getSqlResourceManager().existSql(sqlName)) { + if (sqlResourceManager.reloadSql(sqlName)) { try (var agent = sqlConfig.agent()) { var ctx = agent.context().setSqlName(sqlName); - ctx.setSql(sqlConfig.getSqlResourceManager().getSql(ctx.getSqlName())); + ctx.setSql(sqlResourceManager.getSql(ctx.getSqlName())); var params = Arrays.copyOfRange(parts, 2, parts.length); SqlParamUtils.setSqlParams(sqlConfig, ctx, params); diff --git a/src/main/java/jp/co/future/uroborosql/client/command/UpdateCommand.java b/src/main/java/jp/co/future/uroborosql/client/command/UpdateCommand.java index c0be3460..ded2db01 100644 --- a/src/main/java/jp/co/future/uroborosql/client/command/UpdateCommand.java +++ b/src/main/java/jp/co/future/uroborosql/client/command/UpdateCommand.java @@ -44,12 +44,12 @@ public boolean execute(final LineReader reader, final String[] parts, final SqlC final Properties props) { var writer = reader.getTerminal().writer(); if (parts.length >= 2) { + var sqlResourceManager = sqlConfig.getSqlResourceManager(); var sqlName = parts[1].replace('.', '/'); - if (sqlConfig.getSqlResourceManager().existSql(sqlName)) { - + if (sqlResourceManager.reloadSql(sqlName)) { try (var agent = sqlConfig.agent()) { var ctx = agent.context().setSqlName(sqlName); - ctx.setSql(sqlConfig.getSqlResourceManager().getSql(ctx.getSqlName())); + ctx.setSql(sqlResourceManager.getSql(ctx.getSqlName())); var params = Arrays.copyOfRange(parts, 2, parts.length); SqlParamUtils.setSqlParams(sqlConfig, ctx, params); try { diff --git a/src/main/java/jp/co/future/uroborosql/client/command/ViewCommand.java b/src/main/java/jp/co/future/uroborosql/client/command/ViewCommand.java index 6ce5196e..9c341b73 100644 --- a/src/main/java/jp/co/future/uroborosql/client/command/ViewCommand.java +++ b/src/main/java/jp/co/future/uroborosql/client/command/ViewCommand.java @@ -39,9 +39,10 @@ public boolean execute(final LineReader reader, final String[] parts, final SqlC final Properties props) { var writer = reader.getTerminal().writer(); if (parts.length >= 2) { + var sqlResourceManager = sqlConfig.getSqlResourceManager(); var sqlName = parts[1].replace('.', '/'); - if (sqlConfig.getSqlResourceManager().existSql(sqlName)) { - var sql = sqlConfig.getSqlResourceManager().getSql(sqlName); + if (sqlResourceManager.reloadSql(sqlName)) { + var sql = sqlResourceManager.getSql(sqlName); var sqlLines = sql.split("\\r\\n|\\r|\\n"); for (var sqlLine : sqlLines) { writer.println(sqlLine); 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 f9980a54..f51393d7 100644 --- a/src/main/java/jp/co/future/uroborosql/store/SqlResourceManager.java +++ b/src/main/java/jp/co/future/uroborosql/store/SqlResourceManager.java @@ -27,9 +27,9 @@ public interface SqlResourceManager extends ServiceLoggingSupport { void initialize(); /** - * 停止処理
+ * キャッシュクリア処理
*/ - void shutdown(); + void clearCache(); /** * SQL文取得
@@ -46,6 +46,14 @@ public interface SqlResourceManager extends ServiceLoggingSupport { */ boolean existSql(String sqlName); + /** + * 指定したSQL名のSQLをロードし直します. + * + * @param sqlName SQL名 + * @return sqlNameに対するSQLが存在する場合はtrue + */ + boolean reloadSql(String sqlName); + /** * SqlNameを与えられたPathから生成する
* 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 dd776407..081338bf 100644 --- a/src/main/java/jp/co/future/uroborosql/store/SqlResourceManagerImpl.java +++ b/src/main/java/jp/co/future/uroborosql/store/SqlResourceManagerImpl.java @@ -174,10 +174,10 @@ public void initialize() { /** * {@inheritDoc} * - * @see jp.co.future.uroborosql.store.SqlResourceManager#shutdown() + * @see jp.co.future.uroborosql.store.SqlResourceManager#clearCache() */ @Override - public void shutdown() { + public void clearCache() { this.sqlInfos.clear(); } @@ -232,6 +232,21 @@ public boolean existSql(final String sqlName) { return sqlInfos.containsKey(sqlName); } + /** + * {@inheritDoc} + * + * @see jp.co.future.uroborosql.store.SqlResourceManager#reloadSql(java.lang.String) + */ + @Override + public boolean reloadSql(final String sqlName) { + sqlInfos.remove(sqlName); + try { + return generateSqlInfo(sqlName); + } catch (IOException e) { + return false; + } + } + /** * {@inheritDoc} * 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 ea4dcc94..10d558fb 100644 --- a/src/test/java/jp/co/future/uroborosql/store/SqlResourceManagerTest.java +++ b/src/test/java/jp/co/future/uroborosql/store/SqlResourceManagerTest.java @@ -87,6 +87,20 @@ void testGetSqlName() throws Exception { assertThat(manager.getSqlName(Paths.get("nosql/example/select_test4.sql")), is("nosql/example/select_test4")); } + @Test + void testReloadSql() throws Exception { + var manager = new SqlResourceManagerImpl(); + manager.setDialect(new H2Dialect()); + manager.initialize(); + List.of("example/select_test") + .forEach(sqlName -> manager.getSql(sqlName)); + + assertThat(manager.reloadSql("example/select_test"), is(true)); + assertThat(manager.reloadSql("example/select_test2"), is(true)); + assertThat(manager.reloadSql("example/select_test3"), is(true)); + assertThat(manager.reloadSql("example/select_test4"), is(false)); + } + @Test void testGetSql() throws Exception { var manager = new SqlResourceManagerImpl(); @@ -180,7 +194,7 @@ void testGetSqlH2() throws Exception { assertThat(manager.existSql("example/select_test2"), is(true)); assertThat(manager.existSql("example/select_test3"), is(true)); } finally { - manager.shutdown(); + manager.clearCache(); } } @@ -204,7 +218,7 @@ void testGetSqlPostgresql() throws Exception { assertThat(manager.existSql("example/select_test2"), is(true)); assertThat(manager.existSql("example/select_test3"), is(true)); } finally { - manager.shutdown(); + manager.clearCache(); } } @@ -293,7 +307,7 @@ void testGetSqlH2LoadPathHasChildDir() throws Exception { assertThat(manager.existSql("example/select_test2"), is(true)); assertThat(manager.existSql("example/select_test3"), is(true)); } finally { - manager.shutdown(); + manager.clearCache(); } } @@ -317,7 +331,7 @@ void testGetSqlPostgresqlLoadPathHasChildDir() throws Exception { assertThat(manager.existSql("example/select_test2"), is(true)); assertThat(manager.existSql("example/select_test3"), is(true)); } finally { - manager.shutdown(); + manager.clearCache(); } } }