An Java Backend for jQuery QueryBuilder。
Maven:
<dependency>
<groupId>tech.uspa</groupId>
<artifactId>query-builder</artifactId>
<version>2.0.3</version>
</dependency>
- MySQL
- MongoDB
- ArangoDB
public class Test {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private MongoTemplate mongoTemplate;
public void test() throws IOException {
String json = "{\"condition\":\"OR\",\"rules\":[{\"id\":\"name\",\"field\":\"username\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"equal\",\"value\":\"Mistic\"}],\"not\":false,\"valid\":true}";
// ----------------------------------------- SQL -----------------------------------------
// get SqlBuilder
SqlQueryBuilderFactory sqlQueryBuilderFactory = new SqlQueryBuilderFactory();
SqlBuilder sqlBuilder = sqlQueryBuilderFactory.builder();
// build query
SqlQueryResult sqlQueryResult = sqlBuilder.build(json);
// execute
jdbcTemplate.query(new StringBuffer("SELECT * FROM `user` WHERE ").append(sqlQueryResult.getQuery()).toString(), sqlQueryResult.getParams().toArray(), rs -> {
System.out.println(rs.getString("username"));
});
// ----------------------------------------- Mongodb -----------------------------------------
// get MongodbBuilder
MongodbQueryBuilderFactory mongodbQueryBuilderFactory = new MongodbQueryBuilderFactory();
MongodbBuilder mongodbBuilder = mongodbQueryBuilderFactory.builder();
// build query
MongodbQueryResult mongodbQueryResult = mongodbBuilder.build(json);
// execute
DBCursor cursor = mongoTemplate.getCollection("user").find(mongodbQueryResult.getQuery());
while (cursor.hasNext()){
System.out.println(cursor.next().get("username"));
}
// ArangoDb Builder
String jsonRules = "{\"condition\":\"OR\",\"rules\":[{\"id\":\"name\",\"field\":\"username\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"equal\",\"value\":\"Mistic\"}],\"not\":false,\"valid\":true}";
ArangoDbQueryResult result = new ArangoDbBuilderFactory().builder().build(jsonRules);
System.out.println(result.getQuery());
}
}
The project provides custom RuleFilter, RuleParser, and GroupParser features. In the RuleFilter, the validation of rule and data filtering are performed, while RuleParser and GroupParser can be used to resolve the custom rules. Custom RuleFilter and RuleParser can be replaced with factory corresponding addXXX, AddXXXBefore, AddXXXAt, and AddXXXAfter.
Custom RuleFilter Class must be implementIRuleFilter
Provide:
Depending on the database,Custom RuleParser Class must be implementAbstractMongodbRuleParser or AbstractSqlRuleParser
Provide:
operator("equal","not_equal","in","not_in","less","less_or_equal","greater","greater_or_equal","between","not_between","begins_with","not_begins_with","contains","not_contains","ends_with","not_ends_with","is_empty","is_not_empty","is_null","is_not_null")。
Custom RuleFilter Class must be implementIGroupParser
Provide:
default implements has provide "AND,OR,NOT"