Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class SQLBuilder {
// This holds the arguments, in the order of the '?' placeholders in sb
private final List<Object> args = new ArrayList<>();

// This holds the criterias, where nth element corresponds to nth element in args
private final ArrayList<String> crts = new ArrayList<String>();
/**
* Adds a criteria for a SQL WHERE clause.
* All criteria are appended by AND (support for OR, or nesting, can be added when needed).
Expand Down Expand Up @@ -84,6 +86,7 @@ public void addCriteria(String criteria, Object argument) {
}
sb.append(trimmedCriteria);
sb.append(" ?");
crts.add(trimmedCriteria);
args.add(argument);
}

Expand Down Expand Up @@ -118,9 +121,38 @@ public Object[] getArguments() {
/*
* Returns a String representation suitable for debugging and log output.
* This is ONLY intended for debugging in logs, and NEVER for passing to a JDBC database.
*/
@Override
public String toString() {
return "SQLBuilder{..."; // TODO implement this...
StringBuilder whereClause = new StringBuilder("SQLBuilder{");
for (int i=0;i<args.size();i++)
{
if (i!=0)
{
whereClause.append(" AND ");
}
else
{
whereClause.append("WHERE ");
}
Object currentArg = args.get(i);
whereClause.append(crts.get(i));
whereClause.append(" ");
whereClause.append("[");
if(currentArg instanceof String)
{
whereClause.append("'");
whereClause.append(currentArg);
whereClause.append("'");
}else if(currentArg == null)
{
whereClause.append("null");
}else{
whereClause.append(String.valueOf(currentArg));
}
whereClause.append("]");
}
whereClause.append("}");
return whereClause.toString();
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void testEmpty() {
SQLBuilder sqlBuilder = new SQLBuilder();
assertEquals("", sqlBuilder.getSQLTemplate());
assertArrayEquals(new Object[] {}, sqlBuilder.getArguments());
// TODO assertEquals("SQLBuilder{}", sqlBuilder.toString());
assertEquals("SQLBuilder{}", sqlBuilder.toString());
}

@Test
Expand All @@ -47,7 +47,7 @@ public void testUsage() {
sqlBuilder.addCriteria("age < ", 123);
assertEquals(" WHERE name = ? AND hobby LIKE ? AND age < ?", sqlBuilder.getSQLTemplate());
assertArrayEquals(new Object[] { "Michael", "Mifos/Apache Fineract", 123}, sqlBuilder.getArguments());
// TODO assertEquals("SQLBuilder{ WHERE name = ['Michael'] AND hobby = ['Mifos/Apache Fineract'] AND age < [123]}", sqlBuilder.toString());
assertEquals("SQLBuilder{WHERE name = ['Michael'] AND hobby LIKE ['Mifos/Apache Fineract'] AND age < [123]}", sqlBuilder.toString());
}

@Test
Expand All @@ -56,7 +56,7 @@ public void testNullArgument() {
sqlBuilder.addCriteria("ref =", null);
assertEquals(" WHERE ref = ?", sqlBuilder.getSQLTemplate());
assertArrayEquals(new Object[] { null }, sqlBuilder.getArguments());
// TODO assertEquals("SQLBuilder{ WHERE ref = [null] }", sqlBuilder.toString());
assertEquals("SQLBuilder{WHERE ref = [null]}", sqlBuilder.toString());
}

@Test
Expand Down