Skip to content

Commit

Permalink
Add SqlResourceManager#reloadSql and clearCache method.
Browse files Browse the repository at this point in the history
  • Loading branch information
HidekiSugimoto189 committed Sep 23, 2024
1 parent 41f266e commit 4676e70
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
107 changes: 53 additions & 54 deletions src/main/java/jp/co/future/uroborosql/client/command/ParseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public interface SqlResourceManager extends ServiceLoggingSupport {
void initialize();

/**
* 停止処理<br>
* キャッシュクリア処理<br>
*/
void shutdown();
void clearCache();

/**
* SQL文取得<br>
Expand All @@ -46,6 +46,14 @@ public interface SqlResourceManager extends ServiceLoggingSupport {
*/
boolean existSql(String sqlName);

/**
* 指定したSQL名のSQLをロードし直します.
*
* @param sqlName SQL名
* @return sqlNameに対するSQLが存在する場合は<code>true</code>
*/
boolean reloadSql(String sqlName);

/**
* SqlNameを与えられたPathから生成する<br>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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();
}
}

Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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();
}
}
}

0 comments on commit 4676e70

Please sign in to comment.