Task: Reflectoring sql mapper provider to sql mapper package for cubetiq sql core

This commit is contained in:
Sambo Chea 2022-07-13 21:25:03 +07:00
parent dacb3983f2
commit 1924ea9f1e
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
7 changed files with 56 additions and 24 deletions

View File

@ -1,7 +1,7 @@
package com.cubetiqs.sql;
import com.cubetiqs.mapper.MapperProvider;
import com.cubetiqs.mapper.RowMapperProvider;
import com.cubetiqs.sql.mapper.MapperProvider;
import com.cubetiqs.sql.mapper.RowMapperProvider;
import java.util.List;
import java.util.Map;

View File

@ -1,7 +1,7 @@
package com.cubetiqs.sql;
import com.cubetiqs.mapper.MapperProvider;
import com.cubetiqs.mapper.RowMapperProvider;
import com.cubetiqs.sql.mapper.MapperProvider;
import com.cubetiqs.sql.mapper.RowMapperProvider;
import java.util.List;
import java.util.Map;

View File

@ -1,7 +1,7 @@
package com.cubetiqs.sql;
import com.cubetiqs.mapper.MapperProvider;
import com.cubetiqs.mapper.RowMapperProvider;
import com.cubetiqs.sql.mapper.MapperProvider;
import com.cubetiqs.sql.mapper.RowMapperProvider;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

View File

@ -1,6 +1,6 @@
package com.cubetiqs.sql;
import com.cubetiqs.mapper.MapperProvider;
import com.cubetiqs.sql.mapper.MapperProvider;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -122,7 +122,15 @@ public final class ResultSetUtil {
return Pattern.quote(text + "");
}
public static List<String> findValues(String template, CharSequence sequence1, CharSequence sequence2) {
public static List<String> findValues(final String template, final CharSequence sequence) {
return findValues(template, sequence, null);
}
public static List<String> findValues(final String template, final CharSequence sequence1, final CharSequence sequence2) {
return findValues(template, sequence1, sequence2, true);
}
public static List<String> findValues(final String template, final CharSequence sequence1, final CharSequence sequence2, final boolean includeNullOrEmptyMatched) {
List<String> data = new ArrayList<>();
Pattern pattern;
if (sequence1 != null) {
@ -136,7 +144,10 @@ public final class ResultSetUtil {
}
Matcher matcher = pattern.matcher(template);
while (matcher.find()) {
data.add(matcher.group(1));
String matched = matcher.group(1);
if (includeNullOrEmptyMatched || (matched != null && !matched.equals(""))) {
data.add(matched);
}
}
return data;

View File

@ -11,16 +11,25 @@ public class SqlMapParameter implements ISqlMapParameter {
// Replace all ":variableName" with "?"
private String formatSql;
public SqlMapParameter(String sql) {
// Allow to ignore validate the missing key, when the params doesn't exist in sql statement
private final boolean ignoreMissingKey;
public SqlMapParameter(final String sql) {
this.sql = sql;
this.ignoreMissingKey = false;
}
public SqlMapParameter addParam(String key, Object value) {
public SqlMapParameter(final String sql, final boolean ignoreMissingKey) {
this.sql = sql;
this.ignoreMissingKey = ignoreMissingKey;
}
public SqlMapParameter addParam(final String key, final Object value) {
params.put(key, value);
return this;
}
public SqlMapParameter addParams(Map<String, Object> values) {
public SqlMapParameter addParams(final Map<String, Object> values) {
params.putAll(values);
return this;
}
@ -42,19 +51,27 @@ public class SqlMapParameter implements ISqlMapParameter {
@Override
public Object[] getSqlArgs() {
if (params.size() == 0) {
return null;
}
if (sql == null || sql.equals("")) {
return null;
}
List<String> keys = ResultSetUtil.findValues(sql, ":", null);
if (keys.size() > 1 && params.size() == 0) {
System.out.println("Keys = " + keys);
System.out.println("Params = " + params);
if (params.size() == 0) {
return null;
}
List<String> keys = ResultSetUtil.findValues(sql, ":", null, false);
if (keys.size() > 0 && keys.size() > params.size()) {
System.out.println("Found Keys = " + keys);
throw new IllegalArgumentException("Parameter not matched with keys size!");
} else {
if (!ignoreMissingKey) {
// validate the keys
keys.forEach(key -> {
if (!params.containsKey(key)) {
throw new IllegalArgumentException("Missing key '" + key + "' in args!");
}
});
}
}
Object[] args = new Object[keys.size()];
@ -67,12 +84,16 @@ public class SqlMapParameter implements ISqlMapParameter {
for (String key : keys) {
replacement.put(key, "?");
}
formatSql = ResultSetUtil.replacerPrefix(sql, replacement, ":");
formatSql = ResultSetUtil.replacerPrefix(sql, replacement, ":");
return args;
}
public static SqlMapParameter create(String sql) {
public static SqlMapParameter create(final String sql) {
return new SqlMapParameter(sql);
}
public static SqlMapParameter create(final String sql, final boolean ignoreMissingKey) {
return new SqlMapParameter(sql, ignoreMissingKey);
}
}

View File

@ -1,4 +1,4 @@
package com.cubetiqs.mapper;
package com.cubetiqs.sql.mapper;
@FunctionalInterface
public interface MapperProvider<E, R> {

View File

@ -1,4 +1,4 @@
package com.cubetiqs.mapper;
package com.cubetiqs.sql.mapper;
import java.sql.ResultSet;
import java.sql.SQLException;