use of org.dbflute.hook.SqlResultInfo in project dbflute-core by dbflute.
the class BehaviorCommandInvoker method callbackSqlResultHanler.
protected <RESULT> void callbackSqlResultHanler(BehaviorCommand<RESULT> behaviorCommand, SqlResultHandler sqlResultHander, Object ret, Long commandBefore, Long commandAfter, RuntimeException cause) {
final SqlLogInfo sqlLogInfo = getResultSqlLogInfo(behaviorCommand);
final Long sqlBefore = InternalMapContext.getSqlBeforeTimeMillis();
final Long sqlAfter = InternalMapContext.getSqlAfterTimeMillis();
final ExecutionTimeInfo timeInfo = new ExecutionTimeInfo(commandBefore, commandAfter, sqlBefore, sqlAfter);
final SqlResultInfo info = new SqlResultInfo(behaviorCommand, ret, sqlLogInfo, timeInfo, cause);
sqlResultHander.handle(info);
}
use of org.dbflute.hook.SqlResultInfo in project dbflute-core by dbflute.
the class TnAbstractBasicSqlHandlerTest method test_logSql_whitebox_sqlResultHandlerOnly.
public void test_logSql_whitebox_sqlResultHandlerOnly() {
// ## Arrange ##
final List<String> markList = new ArrayList<String>();
TnAbstractBasicSqlHandler handler = new TnAbstractBasicSqlHandler(null, null, "select ...") {
@Override
protected String buildDisplaySql(String sql, Object[] args) {
markList.add("buildDisplaySql");
return "select ...";
}
@Override
protected void logDisplaySql(String displaySql) {
throw new IllegalStateException("log should not be called!");
}
@Override
protected void log(String msg) {
throw new IllegalStateException("log should not be called!");
}
@Override
protected void saveResultSqlLogInfo(SqlLogInfo sqlLogInfo) {
markList.add("saveResultSqlLogInfo");
super.saveResultSqlLogInfo(sqlLogInfo);
}
@Override
protected boolean isLogEnabled() {
return false;
}
@Override
protected void assertObjectNotNull(String variableName, Object value) {
// for no check of constructor
}
};
// ## Act ##
try {
CallbackContext callbackContext = new CallbackContext();
callbackContext.setSqlResultHandler(new SqlResultHandler() {
public void handle(SqlResultInfo sqlResultInfo) {
throw new IllegalStateException("handle should not be called!");
}
});
CallbackContext.setCallbackContextOnThread(callbackContext);
handler.logSql(null, null);
assertEquals("select ...", InternalMapContext.getResultSqlLogInfo().getDisplaySql());
} finally {
CallbackContext.clearCallbackContextOnThread();
InternalMapContext.clearInternalMapContextOnThread();
}
// ## Assert ##
assertEquals(2, markList.size());
assertEquals("saveResultSqlLogInfo", markList.get(0));
assertEquals("buildDisplaySql", markList.get(1));
}
use of org.dbflute.hook.SqlResultInfo in project dbflute-core by dbflute.
the class BehaviorCommandInvokerTest method test_callbackSqlResultHanler_basic.
public void test_callbackSqlResultHanler_basic() {
// ## Arrange ##
BehaviorCommandInvoker invoker = new BehaviorCommandInvoker();
final long before = 123;
final long after = 456;
final Object ret = new Object();
MockBehaviorCommand mockCmd = new MockBehaviorCommand() {
@Override
public String getTableDbName() {
return "FOO";
}
@Override
public String getCommandName() {
return "BAR";
}
};
final HashSet<String> markSet = new HashSet<String>();
try {
SqlLogInfo sqlLogInfo = new SqlLogInfo(mockCmd, "select ...", new Object[] {}, new Class<?>[] {}, new SqlLogDisplaySqlBuilder() {
public String build(String executedSql, Object[] bindArgs, Class<?>[] bindArgTypes) {
return "select ...";
}
});
InternalMapContext.setResultSqlLogInfo(sqlLogInfo);
// ## Act & Assert ##
invoker.callbackSqlResultHanler(mockCmd, new SqlResultHandler() {
public void handle(SqlResultInfo info) {
long actualBefore = info.getExecutionTimeInfo().getCommandBeforeTimeMillis();
long actualAfter = info.getExecutionTimeInfo().getCommandAfterTimeMillis();
assertEquals(ret, info.getResult());
assertEquals("FOO", info.getMeta().getTableDbName());
assertEquals("BAR", info.getMeta().getCommandName());
String displaySql = info.getSqlLogInfo().getDisplaySql();
assertEquals("select ...", displaySql);
assertEquals(before, actualBefore);
assertEquals(after, actualAfter);
assertNull(info.getCause());
markSet.add("handle()");
log(info.getResult() + ":" + displaySql + ":" + actualBefore + ":" + actualAfter);
}
}, ret, before, after, null);
assertTrue(markSet.size() == 1);
assertTrue(markSet.contains("handle()"));
} finally {
InternalMapContext.clearInternalMapContextOnThread();
}
}
use of org.dbflute.hook.SqlResultInfo in project dbflute-core by dbflute.
the class TnAbstractBasicSqlHandlerTest method test_logSql_whitebox_bigThree.
public void test_logSql_whitebox_bigThree() {
// ## Arrange ##
final List<String> markList = new ArrayList<String>();
final Object[] args = new Object[] {};
final Class<?>[] argsTypes = new Class<?>[] {};
TnAbstractBasicSqlHandler handler = new TnAbstractBasicSqlHandler(null, null, null) {
@Override
protected String buildDisplaySql(String sql, Object[] args) {
markList.add("getDisplaySql");
return "select ..." + ln() + " from ...";
}
@Override
protected void logDisplaySql(String displaySql) {
markList.add("logDisplaySql");
assertEquals("select ..." + ln() + " from ...", displaySql);
super.logDisplaySql(displaySql);
}
@Override
protected void log(String msg) {
markList.add("log");
assertEquals(ln() + "select ..." + ln() + " from ...", msg);
}
@Override
protected void saveResultSqlLogInfo(SqlLogInfo sqlLogInfo) {
markList.add("saveResultSqlLogInfo");
super.saveResultSqlLogInfo(sqlLogInfo);
}
@Override
protected boolean isLogEnabled() {
return true;
}
@Override
protected void assertObjectNotNull(String variableName, Object value) {
// for no check of constructor
}
};
// ## Act ##
try {
CallbackContext callbackContext = new CallbackContext();
callbackContext.setSqlLogHandler(new SqlLogHandler() {
public void handle(SqlLogInfo info) {
markList.add("handle");
assertEquals("select ..." + ln() + " from ...", info.getDisplaySql());
assertEquals(newArrayList(args), newArrayList(info.getBindArgs()));
assertEquals(newArrayList(argsTypes), newArrayList(info.getBindArgTypes()));
}
});
callbackContext.setSqlResultHandler(new SqlResultHandler() {
public void handle(SqlResultInfo sqlResultInfo) {
throw new IllegalStateException("handle should not be called!");
}
});
CallbackContext.setCallbackContextOnThread(callbackContext);
handler.logSql(args, argsTypes);
assertEquals("select ..." + ln() + " from ...", InternalMapContext.getResultSqlLogInfo().getDisplaySql());
} finally {
CallbackContext.clearCallbackContextOnThread();
InternalMapContext.clearInternalMapContextOnThread();
}
// ## Assert ##
assertEquals(5, markList.size());
assertEquals("getDisplaySql", markList.get(0));
assertEquals("logDisplaySql", markList.get(1));
assertEquals("log", markList.get(2));
assertEquals("handle", markList.get(3));
assertEquals("saveResultSqlLogInfo", markList.get(4));
}
use of org.dbflute.hook.SqlResultInfo in project dbflute-core by dbflute.
the class BehaviorCommandInvokerTest method test_callbackSqlResultHanler_notExistsDisplaySql.
public void test_callbackSqlResultHanler_notExistsDisplaySql() {
// ## Arrange ##
BehaviorCommandInvoker invoker = new BehaviorCommandInvoker();
final long before = 123;
final long after = 456;
final Object ret = new Object();
MockBehaviorCommand mockCmd = new MockBehaviorCommand() {
@Override
public String getTableDbName() {
return "FOO";
}
@Override
public String getCommandName() {
return "BAR";
}
};
final HashSet<String> markSet = new HashSet<String>();
SqlLogInfo sqlLogInfo = new SqlLogInfo(mockCmd, "select ...", new Object[] {}, new Class<?>[] {}, new SqlLogDisplaySqlBuilder() {
public String build(String executedSql, Object[] bindArgs, Class<?>[] bindArgTypes) {
return "select ...";
}
});
InternalMapContext.setResultSqlLogInfo(sqlLogInfo);
try {
// ## Act & Assert ##
invoker.callbackSqlResultHanler(mockCmd, new SqlResultHandler() {
public void handle(SqlResultInfo info) {
long actualBefore = info.getExecutionTimeInfo().getCommandBeforeTimeMillis();
long actualAfter = info.getExecutionTimeInfo().getCommandAfterTimeMillis();
assertEquals(ret, info.getResult());
assertEquals("FOO", info.getMeta().getTableDbName());
assertEquals("BAR", info.getMeta().getCommandName());
String displaySql = info.getSqlLogInfo().getDisplaySql();
assertEquals("select ...", displaySql);
assertEquals(before, actualBefore);
assertEquals(after, actualAfter);
assertNotNull(info.getCause());
markSet.add("handle()");
log(info.getResult() + ":" + displaySql + ":" + actualBefore + ":" + actualAfter);
}
}, ret, before, after, new IllegalStateException());
assertTrue(markSet.size() == 1);
assertTrue(markSet.contains("handle()"));
} finally {
InternalMapContext.clearInternalMapContextOnThread();
}
}
Aggregations