Fixed the sql params that missing check with key set

This commit is contained in:
Sambo Chea 2023-03-30 17:27:43 +07:00
parent 23c16c35e7
commit 5b0d524cb3
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
2 changed files with 10 additions and 7 deletions

View File

@ -1,8 +1,6 @@
package com.cubetiqs.sql; package com.cubetiqs.sql;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
public class SqlMapParameter implements ISqlMapParameter { public class SqlMapParameter implements ISqlMapParameter {
private final String sql; private final String sql;
@ -11,7 +9,7 @@ public class SqlMapParameter implements ISqlMapParameter {
// Replace all ":variableName" with "?" // Replace all ":variableName" with "?"
private String formatSql; private String formatSql;
// Allow to ignore validate the missing key, when the params doesn't exist in sql statement // Allow ignoring validate the missing key, when the params doesn't exist in sql statement
private final boolean ignoreMissingKey; private final boolean ignoreMissingKey;
public SqlMapParameter(final String sql) { public SqlMapParameter(final String sql) {
@ -60,8 +58,9 @@ public class SqlMapParameter implements ISqlMapParameter {
} }
List<String> keys = ResultSetUtil.findValues(sql, ":", null, false); List<String> keys = ResultSetUtil.findValues(sql, ":", null, false);
if (keys.size() > 0 && keys.size() > params.size()) { Set<String> keySet = new HashSet<>(keys);
System.out.println("Found Keys = " + keys); if (keySet.size() > 0 && keySet.size() > params.size()) {
System.out.println("Found Keys = " + keys + ", KeySet = " + keySet + ", Params = " + params);
throw new IllegalArgumentException("Parameter not matched with keys size!"); throw new IllegalArgumentException("Parameter not matched with keys size!");
} else { } else {
if (!ignoreMissingKey) { if (!ignoreMissingKey) {

View File

@ -18,7 +18,11 @@ public class ConnectionTests {
JdbcDataFactory factory = ConnectionFactory JdbcDataFactory factory = ConnectionFactory
.createDataFactory(builder); .createDataFactory(builder);
IExecuteResult<List<Object>> result = factory.queryForList("select Status as gg from um"); IExecuteResult<List<Object>> result = factory.queryForList(
SqlMapParameter.create("select UMID, Status from um where UMID = :umid or UMID = :umid")
.addParam("umid", "12")
.addParam("umid", "11")
);
System.out.println(result.getData()); System.out.println(result.getData());
factory.tryClose(); factory.tryClose();