Implement JdbcMapper.sqlBuilder() method

This commit is contained in:
Travis Burtrum 2019-02-09 00:06:13 -05:00
parent 26617e0781
commit 664c00a5b2
6 changed files with 81 additions and 8 deletions

View File

@ -3,7 +3,6 @@ package com.moparisthebest.jdbc;
import com.moparisthebest.jdbc.codegen.JdbcMapper; import com.moparisthebest.jdbc.codegen.JdbcMapper;
import com.moparisthebest.jdbc.util.Bindable; import com.moparisthebest.jdbc.util.Bindable;
import com.moparisthebest.jdbc.util.InListUtil; import com.moparisthebest.jdbc.util.InListUtil;
import com.moparisthebest.jdbc.util.PreparedStatementUtil;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.sql.Connection; import java.sql.Connection;

View File

@ -16,10 +16,16 @@ public interface JdbcMapper extends Closeable {
Connection getConnection(); Connection getConnection();
/*IFJAVA6_START
SqlBuilder sqlBuilder();
IFJAVA6_END*/
//IFJAVA8_START //IFJAVA8_START
default SqlBuilder sqlBuilder() { default SqlBuilder sqlBuilder() {
return SqlBuilder.of(getConnection()); // todo: should this use the current inList ? return SqlBuilder.of(getConnection());
} }
//IFJAVA8_END //IFJAVA8_END

View File

@ -230,6 +230,12 @@ public class JdbcMapperProcessor extends AbstractProcessor {
w.write(packageName); w.write(packageName);
w.write(";\n\n"); w.write(";\n\n");
} }
final ExecutableElement sqlBuilderMethod = getSqlBuilderMethod(genClass);
if(sqlBuilderMethod != null) {
w.write("import com.moparisthebest.jdbc.util.SqlBuilder;\n");
}
w.write("import com.moparisthebest.jdbc.Factory;\n\n"); w.write("import com.moparisthebest.jdbc.Factory;\n\n");
w.write("import java.sql.*;\n\n"); w.write("import java.sql.*;\n\n");
w.write("import static com.moparisthebest.jdbc.util.ResultSetUtil.*;\n"); w.write("import static com.moparisthebest.jdbc.util.ResultSetUtil.*;\n");
@ -688,6 +694,30 @@ public class JdbcMapperProcessor extends AbstractProcessor {
} }
// end close method // end close method
if(sqlBuilderMethod != null) {
// we want to generate this method returning proper InList
w.append("\n\t@Override\n\tpublic SqlBuilder sqlBuilder() {\n\t\treturn SqlBuilder.of(conn, com.moparisthebest.jdbc.");
switch(databaseType) {
case ORACLE:
w.append("OracleArrayInList");
break;
case ANY:
w.append("ArrayInList");
break;
case UNNEST:
w.append("UnNestArrayInList");
break;
case BIND:
w.append("BindInList");
break;
default:
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@JdbcMapper.Mapper.databaseType unsupported", element);
continue;
}
w.append(".instance());\n\t}\n");
}
w.write("}\n"); w.write("}\n");
} finally { } finally {
tryClose(w); tryClose(w);
@ -1126,6 +1156,34 @@ public class JdbcMapperProcessor extends AbstractProcessor {
methodElement.getParameters().isEmpty() ? methodElement : null; methodElement.getParameters().isEmpty() ? methodElement : null;
} }
public ExecutableElement getSqlBuilderMethod(final TypeElement genClass) {
ExecutableElement ret = null;
for (final Element methodElement : genClass.getEnclosedElements()) {
if ((ret = getSqlBuilderMethod(methodElement)) != null)
return ret;
}
// superclasses
final TypeMirror superclass = genClass.getSuperclass();
if (superclass.getKind() == TypeKind.DECLARED && (ret = getSqlBuilderMethod((TypeElement) types.asElement(superclass))) != null)
return ret;
// interfaces
for (final TypeMirror iface : genClass.getInterfaces()) {
if (iface.getKind() == TypeKind.DECLARED && (ret = getSqlBuilderMethod((TypeElement) types.asElement(iface))) != null)
return ret;
}
return ret;
}
public static ExecutableElement getSqlBuilderMethod(final Element element) {
return element.getKind() != ElementKind.METHOD ? null : getSqlBuilderMethod((ExecutableElement) element);
}
public static ExecutableElement getSqlBuilderMethod(final ExecutableElement methodElement) {
return methodElement.getReturnType().getKind() == TypeKind.DECLARED &&
methodElement.getSimpleName().toString().equals("sqlBuilder") &&
methodElement.getParameters().isEmpty() ? methodElement : null;
}
public static boolean isPrimitiveInteger(final TypeKind kind) { public static boolean isPrimitiveInteger(final TypeKind kind) {
switch(kind) { switch(kind) {
case BYTE: case BYTE:

View File

@ -179,6 +179,11 @@ public class QueryMapper implements JdbcMapper {
return inListEnabled && sql.contains(inListReplace) ? recursiveReplace(new StringBuilder(sql), bindObjects).toString() : sql; return inListEnabled && sql.contains(inListReplace) ? recursiveReplace(new StringBuilder(sql), bindObjects).toString() : sql;
} }
@Override
public SqlBuilder sqlBuilder() {
return SqlBuilder.of(conn, inList);
}
public <T> InList.InListObject inList(final String columnName, final Collection<T> values) throws SQLException { public <T> InList.InListObject inList(final String columnName, final Collection<T> values) throws SQLException {
this.inListEnabled = true; // worth checking if it's already this or not? this.inListEnabled = true; // worth checking if it's already this or not?
return this.inList.inList(conn, columnName, values); return this.inList.inList(conn, columnName, values);

View File

@ -74,6 +74,11 @@ public class QueryMapperQmDao implements QmDao {
tryClose(qm); tryClose(qm);
} }
@Override
public SqlBuilder sqlBuilder() {
return qm.sqlBuilder();
}
@Override @Override
public FieldPerson getFieldRegularPerson(final long personNo) throws SQLException { public FieldPerson getFieldRegularPerson(final long personNo) throws SQLException {
return qm.toObject(personRegular, FieldPerson.class, personNo); return qm.toObject(personRegular, FieldPerson.class, personNo);

View File

@ -770,12 +770,12 @@ public class QueryMapperTest {
@Test @Test
public void testSelectRandomSqlBuilder() throws Throwable { public void testSelectRandomSqlBuilder() throws Throwable {
final List<Long> arr = Arrays.asList(1L, 2L, 3L); final List<Long> arr = Arrays.asList(1L, 2L, 3L);
assertEquals(arr, qm.selectRandomSqlBuilder(SqlBuilder.of(qm.getConnection()).appendInList("person_no", arr))); assertEquals(arr, qm.selectRandomSqlBuilder(qm.sqlBuilder().appendInList("person_no", arr)));
assertEquals(arr, qm.selectRandomSqlBuilder(SqlBuilder.of(qm.getConnection()).append("person_no = ? OR ", 1L).appendInList("person_no", Arrays.asList(2L, 3L)))); assertEquals(arr, qm.selectRandomSqlBuilder(qm.sqlBuilder().append("person_no = ? OR ", 1L).appendInList("person_no", Arrays.asList(2L, 3L))));
assertEquals(arr, qm.selectRandomSqlBuilder(SqlBuilder.of(qm.getConnection()).append("person_no = 1 OR ").appendInList("person_no", Arrays.asList(2L, 3L)))); assertEquals(arr, qm.selectRandomSqlBuilder(qm.sqlBuilder().append("person_no = 1 OR ").appendInList("person_no", Arrays.asList(2L, 3L))));
assertEquals(arr, qm.selectRandomSqlBuilder(1L, SqlBuilder.of(qm.getConnection()).append(" OR person_no in (2,3)"), "NoNameMatch")); assertEquals(arr, qm.selectRandomSqlBuilder(1L, qm.sqlBuilder().append(" OR person_no in (2,3)"), "NoNameMatch"));
assertEquals(Collections.singletonList(2L), qm.selectRandomSqlBuilder(2L, SqlBuilder.of(qm.getConnection()), "NoNameMatch")); assertEquals(Collections.singletonList(2L), qm.selectRandomSqlBuilder(2L, qm.sqlBuilder(), "NoNameMatch"));
assertEquals(Collections.singletonList(3L), qm.selectRandomSqlBuilder(3L, Bindable.empty, "NoNameMatch")); assertEquals(Collections.singletonList(3L), qm.selectRandomSqlBuilder(3L, Bindable.empty, "NoNameMatch"));
assertEquals(arr, qm.selectRandomSqlBuilder(2L, SqlBuilder.of(qm.getConnection()).append("OR person_no = ? OR ", 1L).appendInList("person_no", Collections.singletonList(3L)), "NoNameMatch")); assertEquals(arr, qm.selectRandomSqlBuilder(2L, qm.sqlBuilder().append("OR person_no = ? OR ", 1L).appendInList("person_no", Collections.singletonList(3L)), "NoNameMatch"));
} }
} }