fix(sqlserver): return informational server messages
This commit is contained in:
1 parent
368351d70e
commit
9679d48824
6 files changed
+468
-65
No files matched your search
@@ -6,13 +6,16 @@ import java.sql.Clob;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLWarning;
|
||||
import java.sql.SQLXML;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
@@ -104,20 +107,22 @@ public final class JdbcExecutor {
|
||||
// Do not translate them to Connection.commit(), which requires autoCommit=false.
|
||||
boolean hasResultSet = stmt.execute(trimmedSql);
|
||||
long elapsed = System.currentTimeMillis() - start;
|
||||
QueryResult result;
|
||||
if (hasResultSet) {
|
||||
try (ResultSet rs = stmt.getResultSet()) {
|
||||
return readResultSet(rs, elapsed, effectiveMaxRows, valueReader);
|
||||
result = readResultSet(rs, elapsed, effectiveMaxRows, valueReader);
|
||||
}
|
||||
} else {
|
||||
int updateCount = stmt.getUpdateCount();
|
||||
result = new QueryResult(
|
||||
Collections.emptyList(),
|
||||
Collections.emptyList(),
|
||||
updateCount >= 0 ? updateCount : 0,
|
||||
elapsed,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
int updateCount = stmt.getUpdateCount();
|
||||
return new QueryResult(
|
||||
Collections.emptyList(),
|
||||
Collections.emptyList(),
|
||||
updateCount >= 0 ? updateCount : 0,
|
||||
elapsed,
|
||||
false
|
||||
);
|
||||
return withStatementWarnings(result, stmt);
|
||||
} finally {
|
||||
activeStatements.remove(stmt);
|
||||
}
|
||||
@@ -653,6 +658,39 @@ public final class JdbcExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private static QueryResult withStatementWarnings(QueryResult result, Statement stmt) {
|
||||
if (!result.getColumns().isEmpty() || !result.getRows().isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
|
||||
List<List<Object>> rows = new ArrayList<>();
|
||||
try {
|
||||
Set<SQLWarning> seen = Collections.newSetFromMap(new IdentityHashMap<>());
|
||||
for (SQLWarning warning = stmt.getWarnings(); warning != null && seen.add(warning); warning = warning.getNextWarning()) {
|
||||
String message = warning.getMessage();
|
||||
if (message != null && !message.trim().isEmpty()) {
|
||||
rows.add(Collections.singletonList(message));
|
||||
}
|
||||
}
|
||||
stmt.clearWarnings();
|
||||
} catch (SQLException ignored) {
|
||||
// Warning retrieval is advisory; a driver bug here must not turn a
|
||||
// successfully executed statement into a query failure.
|
||||
}
|
||||
|
||||
if (rows.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
return new QueryResult(
|
||||
Collections.singletonList("Message"),
|
||||
Collections.singletonList("nvarchar"),
|
||||
rows,
|
||||
result.getAffected_rows(),
|
||||
result.getExecution_time_ms(),
|
||||
result.getTruncated()
|
||||
);
|
||||
}
|
||||
|
||||
private QueryResult emptyQueryResult(long start) {
|
||||
return new QueryResult(
|
||||
Collections.emptyList(),
|
||||
|
||||
Reference in new issue
Block a user