Clean up SqlBuilder API

This commit is contained in:
Travis Burtrum 2019-03-10 22:02:10 -04:00
parent a535465a2a
commit e47ec84325
2 changed files with 24 additions and 8 deletions

View File

@ -54,24 +54,40 @@ public class SqlBuilder implements Appendable, CharSequence, Collection<Object>,
// start custom SqlBuilder methods
public SqlBuilder append(final String sql, final Object bindObject) {
sb.append(sql);
public SqlBuilder bind(final Object bindObject) {
this.bindObjects.add(bindObject);
return this;
}
public SqlBuilder append(final String sql, final Object... bindObjects) {
return this.append(sql, (Object) bindObjects);
public SqlBuilder bind(final Object... bindObjects) {
return this.bind((Object) bindObjects);
}
public SqlBuilder appendBind(final String sql, final Object bindObject) {
sb.append(sql);
return bind(bindObject);
}
public SqlBuilder appendBind(final String sql, final Object... bindObjects) {
return this.appendBind(sql, (Object) bindObjects);
}
public <T> SqlBuilder appendInList(final String columnName, final Collection<T> values) throws SQLException {
final InList.InListObject inListObject = inList.inList(conn, columnName, values);
return this.append(inListObject.toString(), inListObject.getBindObject());
return this.appendBind(inListObject.toString(), inListObject.getBindObject());
}
public <T> SqlBuilder appendNotInList(final String columnName, final Collection<T> values) throws SQLException {
final InList.InListObject inListObject = inList.notInList(conn, columnName, values);
return this.append(inListObject.toString(), inListObject.getBindObject());
return this.appendBind(inListObject.toString(), inListObject.getBindObject());
}
public SqlBuilder and() {
return append(" AND ");
}
public SqlBuilder or() {
return append(" OR ");
}
public StringBuilder getStringBuilder() {

View File

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