Skip to content

hapreanmanuel/sql-conditions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQL Condition Builder

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.


✨ Features

  • Fluent builder for SQL-like conditions using AND and OR.
  • 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).

📖 Usage / Use Cases

✅ When to Use

  • Unit tests for SQL generation – Verify your application or ORM produces the expected WHERE clauses.
  • 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.

▶ Basic Usage with the Builder

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");
    }
}

📦 Installation

Clone or download the repository and include it in your Java project.

About

Lightweights fluent condition builder

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages