A lightweight Java 8 utility for building SQL WHERE clause regex patterns fluently.
This tool is useful when you need to match SQL conditions in scripts (e.g., testing, linting, code analysis) without external libraries.
- Fluent builder for SQL-like conditions using
ANDandOR. - Automatic handling of parentheses for nested groups.
- Helper methods for common operators like
eq(),lt(),gt(),lte(),gte(). - Generates regex patterns that handle flexible whitespace and multi-line formatting.
- Java 8 compatible, with no external dependencies (only JUnit for tests).
- Unit tests for SQL generation – Verify your application or ORM produces the expected
WHEREclauses. - Static analysis or CI checks – Ensure SQL scripts follow required patterns without adding a full SQL parser.
- Lightweight query validation – Quickly confirm that conditions exist in SQL without executing queries.
Use the static builder API to create conditions fluently:
// A simple POJO for demo
public class Account {
private final String status;
private final BigDecimal balance;
public Account(String status, double balance) {
this.status = status;
this.balance = balance;
}
public String getStatus() {
return status;
}
public double getBalance() {
return balance;
}
}
public class Example {
public static void main(String[] args) {
// Build: STATUS = 'ACTIVE' OR BALANCE < 1000
Condition<Account> condition = SqlConditionBuilder.or(
SqlConditionBuilder.eq("STATUS", "'ACTIVE'"),
SqlConditionBuilder.lt("BALANCE", BigDecimal.valueOf(1000))
);
// Check if an element matches or not the condition
boolean shouldMatch = condition.matches(new Account("ACTIVE", BigDecimal.valueOf(1000)));
boolean shouldNotMatch = condition.matches(new Account("ACTIVE", BigDecimal.valueOf(999)));
// Check if sql matches the condition
boolean sqlShouldMatch = condition.matchesSql("SELECT * FROM accounts where STATUS='ACTIVE' OR BALANCE<1000");
}
}Clone or download the repository and include it in your Java project.