do not use an object array for parameters
This commit is contained in:
parent
1578483688
commit
bfe943606e
8 changed files with 44 additions and 55 deletions
|
@ -6,8 +6,6 @@ import org.junit.jupiter.api.BeforeAll;
|
|||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.MariaDBContainer;
|
||||
import org.xbib.jdbc.query.Config;
|
||||
import org.xbib.jdbc.query.ConfigSupplier;
|
||||
import org.xbib.jdbc.query.DatabaseProvider;
|
||||
import org.xbib.jdbc.query.OptionsOverride;
|
||||
import org.xbib.jdbc.query.Schema;
|
||||
|
@ -45,12 +43,8 @@ public class MariaDBTest extends CommonTest {
|
|||
|
||||
@Override
|
||||
protected DatabaseProvider createDatabaseProvider(OptionsOverride options) {
|
||||
Config config = ConfigSupplier.of()
|
||||
.property("database.url", mariaDBContainer.getJdbcUrl())
|
||||
.property("database.user", "testUser")
|
||||
.property("database.password", "testPassword")
|
||||
.get();
|
||||
return DatabaseProvider.builder(config)
|
||||
return DatabaseProvider.builder(getClass().getClassLoader(),
|
||||
mariaDBContainer.getJdbcUrl(), null, null, "testUser", "testPassword")
|
||||
.withSqlParameterLogging()
|
||||
.withSqlInExceptionMessages()
|
||||
.withOptions(options)
|
||||
|
|
|
@ -7,8 +7,6 @@ import org.junit.jupiter.api.Disabled;
|
|||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.OracleContainer;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import org.xbib.jdbc.query.Config;
|
||||
import org.xbib.jdbc.query.ConfigSupplier;
|
||||
import org.xbib.jdbc.query.DatabaseProvider;
|
||||
import org.xbib.jdbc.query.OptionsOverride;
|
||||
import org.xbib.jdbc.query.Schema;
|
||||
|
@ -46,12 +44,8 @@ public class OracleTest extends CommonTest {
|
|||
|
||||
@Override
|
||||
protected DatabaseProvider createDatabaseProvider(OptionsOverride options) throws Exception {
|
||||
Config config = ConfigSupplier.of()
|
||||
.property("database.url", oracleContainer.getJdbcUrl())
|
||||
.property("database.user", "testUser")
|
||||
.property("database.password", "testPassword")
|
||||
.get();
|
||||
return DatabaseProvider.builder(config)
|
||||
return DatabaseProvider.builder(getClass().getClassLoader(),
|
||||
oracleContainer.getJdbcUrl(), null, null, "testUser", "testPassword")
|
||||
.withSqlParameterLogging()
|
||||
.withSqlInExceptionMessages()
|
||||
.withOptions(options)
|
||||
|
|
|
@ -20,7 +20,7 @@ public class MixedParameterSql {
|
|||
|
||||
private final String sqlToExecute;
|
||||
|
||||
private final Object[] args;
|
||||
private final List<Object> args;
|
||||
|
||||
public MixedParameterSql(String sql, List<Object> positionalArgs, Map<String, Object> nameToArg) {
|
||||
if (positionalArgs == null) {
|
||||
|
@ -95,12 +95,12 @@ public class MixedParameterSql {
|
|||
}
|
||||
}
|
||||
this.sqlToExecute = newSql.toString();
|
||||
args = argsList.toArray(new Object[argsList.size()]);
|
||||
args = argsList;
|
||||
if (currentPositionalArg != positionalArgs.size()) {
|
||||
throw new DatabaseException("Wrong number of positional parameters were provided (expected: "
|
||||
+ currentPositionalArg + ", actual: " + positionalArgs.size() + ")");
|
||||
}
|
||||
if (nameToArg.size() > args.length - Math.max(0, positionalArgs.size() - 1) + rewrittenArgs.size()) {
|
||||
if (nameToArg.size() > args.size() - Math.max(0, positionalArgs.size() - 1) + rewrittenArgs.size()) {
|
||||
Set<String> unusedNames = new HashSet<>(nameToArg.keySet());
|
||||
unusedNames.removeAll(argNamesList);
|
||||
unusedNames.removeAll(rewrittenArgs);
|
||||
|
@ -114,7 +114,7 @@ public class MixedParameterSql {
|
|||
return sqlToExecute;
|
||||
}
|
||||
|
||||
public Object[] getArgs() {
|
||||
public List<Object> getArgs() {
|
||||
return args;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -412,8 +412,8 @@ public class SqlInsertImpl implements SqlInsert {
|
|||
PreparedStatement ps = null;
|
||||
Metric metric = new Metric(log.isLoggable(Level.FINE));
|
||||
String executeSql = sql;
|
||||
Object[] firstRowParameters = null;
|
||||
List<Object[]> parameters = new ArrayList<>();
|
||||
List<Object> firstRowParameters = null;
|
||||
List<List<Object>> parameters = new ArrayList<>();
|
||||
boolean isSuccess = false;
|
||||
String errorCode = null;
|
||||
Exception logEx = null;
|
||||
|
@ -434,7 +434,7 @@ public class SqlInsertImpl implements SqlInsert {
|
|||
if (connection != null) {
|
||||
ps = connection.prepareStatement(executeSql);
|
||||
|
||||
for (Object[] params : parameters) {
|
||||
for (List<Object> params : parameters) {
|
||||
adaptor.addParameters(ps, params);
|
||||
ps.addBatch();
|
||||
}
|
||||
|
@ -470,7 +470,7 @@ public class SqlInsertImpl implements SqlInsert {
|
|||
PreparedStatement ps = null;
|
||||
Metric metric = new Metric(log.isLoggable(Level.FINE));
|
||||
String executeSql = sql;
|
||||
Object[] parameters = null;
|
||||
List<Object> parameters = null;
|
||||
boolean isSuccess = false;
|
||||
String errorCode = null;
|
||||
Exception logEx = null;
|
||||
|
@ -520,7 +520,7 @@ public class SqlInsertImpl implements SqlInsert {
|
|||
ResultSet rs = null;
|
||||
Metric metric = new Metric(log.isLoggable(Level.FINE));
|
||||
String executeSql = sql;
|
||||
Object[] parameters = null;
|
||||
List<Object> parameters = null;
|
||||
boolean isSuccess = false;
|
||||
String errorCode = null;
|
||||
Exception logEx = null;
|
||||
|
@ -577,7 +577,7 @@ public class SqlInsertImpl implements SqlInsert {
|
|||
ResultSet rs = null;
|
||||
Metric metric = new Metric(log.isLoggable(Level.FINE));
|
||||
String executeSql = sql;
|
||||
Object[] parameters = null;
|
||||
List<Object> parameters = null;
|
||||
boolean isSuccess = false;
|
||||
String errorCode = null;
|
||||
Exception logEx = null;
|
||||
|
|
|
@ -575,11 +575,10 @@ public class SqlSelectImpl implements SqlSelect {
|
|||
}
|
||||
|
||||
private <T> T queryWithTimeout(RowsHandler<T> handler) {
|
||||
assert ps == null;
|
||||
ResultSet rs = null;
|
||||
Metric metric = new Metric(logger.isLoggable(Level.FINE));
|
||||
String executeSql = sql;
|
||||
Object[] parameters = null;
|
||||
List<Object> parameters = null;
|
||||
boolean isWarn = false;
|
||||
boolean isSuccess = false;
|
||||
String errorCode = null;
|
||||
|
|
|
@ -260,8 +260,8 @@ public class SqlUpdateImpl implements SqlUpdate {
|
|||
PreparedStatement ps = null;
|
||||
Metric metric = new Metric(log.isLoggable(Level.FINE));
|
||||
String executeSql = sql;
|
||||
Object[] firstRowParameters = null;
|
||||
List<Object[]> parameters = new ArrayList<>();
|
||||
List<Object> firstRowParameters = null;
|
||||
List<List<Object>> parameters = new ArrayList<>();
|
||||
boolean isSuccess = false;
|
||||
String errorCode = null;
|
||||
Exception logEx = null;
|
||||
|
@ -281,7 +281,7 @@ public class SqlUpdateImpl implements SqlUpdate {
|
|||
}
|
||||
if (connection != null) {
|
||||
ps = connection.prepareStatement(executeSql);
|
||||
for (Object[] params : parameters) {
|
||||
for (List<Object> params : parameters) {
|
||||
adaptor.addParameters(ps, params);
|
||||
ps.addBatch();
|
||||
}
|
||||
|
@ -314,7 +314,7 @@ public class SqlUpdateImpl implements SqlUpdate {
|
|||
PreparedStatement ps = null;
|
||||
Metric metric = new Metric(log.isLoggable(Level.FINE));
|
||||
String executeSql = sql;
|
||||
Object[] parameters = null;
|
||||
List<Object> parameters = null;
|
||||
boolean isSuccess = false;
|
||||
String errorCode = null;
|
||||
Exception logEx = null;
|
||||
|
|
|
@ -15,6 +15,7 @@ import java.time.LocalDate;
|
|||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -32,9 +33,9 @@ public class StatementAdapter {
|
|||
this.options = options;
|
||||
}
|
||||
|
||||
public void addParameters(PreparedStatement ps, Object[] parameters) throws SQLException {
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
Object parameter = parameters[i];
|
||||
public void addParameters(PreparedStatement ps, List<Object> parameters) throws SQLException {
|
||||
for (int i = 0; i < parameters.size(); i++) {
|
||||
Object parameter = parameters.get(i);
|
||||
// Unwrap secret args here so we can use them
|
||||
if (parameter instanceof SecretArg) {
|
||||
parameter = ((SecretArg) parameter).getArg();
|
||||
|
|
|
@ -8,7 +8,8 @@ import java.io.InputStream;
|
|||
import java.io.Reader;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
@ -21,45 +22,45 @@ public class DebugSql {
|
|||
|
||||
public static final String PARAM_SQL_SEPARATOR = "\tParamSql:\t";
|
||||
|
||||
public static void printSql(StringBuilder buf, String sql, Object[] args, Options options) {
|
||||
public static void printSql(StringBuilder buf, String sql, List<Object> args, Options options) {
|
||||
printSql(buf, sql, args, true, options.isLogParameters(), options);
|
||||
}
|
||||
|
||||
public static void printSql(StringBuilder buf, String sql, Object[] args, boolean includeExecSql,
|
||||
public static void printSql(StringBuilder buf, String sql, List<Object> args, boolean includeExecSql,
|
||||
boolean includeParameters, Options options) {
|
||||
Object[] argsToPrint = args;
|
||||
List<Object> argsToPrint = args;
|
||||
if (argsToPrint == null) {
|
||||
argsToPrint = new Object[0];
|
||||
argsToPrint = new ArrayList<>();
|
||||
}
|
||||
int batchSize = -1;
|
||||
if (argsToPrint.length > 0 && argsToPrint instanceof Object[][]) {
|
||||
if (!argsToPrint.isEmpty() && argsToPrint.getFirst() instanceof List) {
|
||||
// The arguments provided were from a batch - just use the first set
|
||||
batchSize = argsToPrint.length;
|
||||
argsToPrint = (Object[]) argsToPrint[0];
|
||||
batchSize = argsToPrint.size();
|
||||
argsToPrint = (List<Object>) argsToPrint.getFirst();
|
||||
}
|
||||
String[] sqlParts = sql.split("\\?");
|
||||
if (sqlParts.length != argsToPrint.length + (sql.endsWith("?") ? 0 : 1)) {
|
||||
if (sqlParts.length != argsToPrint.size() + (sql.endsWith("?") ? 0 : 1)) {
|
||||
buf.append("(wrong # args) query: ");
|
||||
buf.append(sql);
|
||||
if (args != null) {
|
||||
buf.append(" args: ");
|
||||
if (includeParameters) {
|
||||
buf.append(Arrays.toString(argsToPrint));
|
||||
buf.append(argsToPrint);
|
||||
} else {
|
||||
buf.append(argsToPrint.length);
|
||||
buf.append(argsToPrint.size());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (includeExecSql) {
|
||||
buf.append(removeTabs(sql));
|
||||
}
|
||||
if (includeParameters && argsToPrint.length > 0) {
|
||||
if (includeParameters && argsToPrint.size() > 0) {
|
||||
if (includeExecSql) {
|
||||
buf.append(PARAM_SQL_SEPARATOR);
|
||||
}
|
||||
for (int i = 0; i < argsToPrint.length; i++) {
|
||||
for (int i = 0; i < argsToPrint.size(); i++) {
|
||||
buf.append(removeTabs(sqlParts[i]));
|
||||
Object argToPrint = argsToPrint[i];
|
||||
Object argToPrint = argsToPrint.get(i);
|
||||
if (argToPrint instanceof String argToPrintString) {
|
||||
int maxLength = options.maxStringLengthParam();
|
||||
if (argToPrintString.length() > maxLength && maxLength > 0) {
|
||||
|
@ -101,7 +102,7 @@ public class DebugSql {
|
|||
buf.append("<unknown:").append(argToPrint.getClass().getName()).append(">");
|
||||
}
|
||||
}
|
||||
if (sqlParts.length > argsToPrint.length) {
|
||||
if (sqlParts.length > argsToPrint.size()) {
|
||||
buf.append(sqlParts[sqlParts.length - 1]);
|
||||
}
|
||||
}
|
||||
|
@ -121,7 +122,7 @@ public class DebugSql {
|
|||
return s == null ? null : s.replace("'", "''");
|
||||
}
|
||||
|
||||
public static String exceptionMessage(String sql, Object[] parameters, String errorCode, Options options) {
|
||||
public static String exceptionMessage(String sql, List<Object> parameters, String errorCode, Options options) {
|
||||
StringBuilder buf = new StringBuilder("Error executing SQL");
|
||||
if (errorCode != null) {
|
||||
buf.append(" (errorCode=").append(errorCode).append(")");
|
||||
|
@ -133,20 +134,20 @@ public class DebugSql {
|
|||
return buf.toString();
|
||||
}
|
||||
|
||||
public static void logSuccess(String sqlType, Logger log, Metric metric, String sql, Object[] args, Options options) {
|
||||
public static void logSuccess(String sqlType, Logger log, Metric metric, String sql, List<Object> args, Options options) {
|
||||
if (log.isLoggable(Level.FINEST)) {
|
||||
log.log(Level.FINEST, () -> logMiddle('\t', sqlType, metric, null, sql, args, options));
|
||||
}
|
||||
}
|
||||
|
||||
public static void logWarning(String sqlType, Logger log, Metric metric, String errorCode, String sql, Object[] args,
|
||||
public static void logWarning(String sqlType, Logger log, Metric metric, String errorCode, String sql, List<Object> args,
|
||||
Options options, Throwable t) {
|
||||
if (log.isLoggable(Level.WARNING)) {
|
||||
log.log(Level.WARNING, logMiddle(' ', sqlType, metric, errorCode, sql, args, options), t);
|
||||
}
|
||||
}
|
||||
|
||||
public static void logError(String sqlType, Logger log, Metric metric, String errorCode, String sql, Object[] args,
|
||||
public static void logError(String sqlType, Logger log, Metric metric, String errorCode, String sql, List<Object> args,
|
||||
Options options, Throwable t) {
|
||||
if (log.isLoggable(Level.SEVERE)) {
|
||||
log.log(Level.SEVERE, logMiddle(' ', sqlType, metric, errorCode, sql, args, options), t);
|
||||
|
@ -154,7 +155,7 @@ public class DebugSql {
|
|||
}
|
||||
|
||||
private static String logMiddle(char separator, String sqlType, Metric metric,
|
||||
String errorCode, String sql, Object[] args, Options options) {
|
||||
String errorCode, String sql, List<Object> args, Options options) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
if (errorCode != null) {
|
||||
buf.append("errorCode=").append(errorCode).append(" ");
|
||||
|
|
Loading…
Reference in a new issue