use of com.developmentontheedge.sql.format.FilterApplier in project be5 by DevelopmentOnTheEdge.
the class FilterApplierTest method testAddFilterApplierUnion.
@Test
public void testAddFilterApplierUnion() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
AstStart query = SqlQuery.parse("SELECT name FROM bbc b WHERE name LIKE 'Z%' UNION SELECT name FROM actor WHERE name LIKE 'Z%'");
Map<ColumnRef, Object> conditions = Collections.singletonMap(ColumnRef.resolve(query, "bbc.data"), new java.sql.Date(format.parse("01-01-1900").getTime()));
new FilterApplier().addFilter(query, conditions);
assertEquals("SELECT * FROM (SELECT name FROM bbc b WHERE name LIKE 'Z%' UNION SELECT name FROM actor WHERE name LIKE 'Z%') tmp WHERE b.data ='1900-01-01'", new Formatter().format(query, new Context(Dbms.POSTGRESQL), new DefaultParserContext()));
}
use of com.developmentontheedge.sql.format.FilterApplier in project be5 by DevelopmentOnTheEdge.
the class FilterApplierTest method testAddFilterApplier.
@Test
public void testAddFilterApplier() {
AstStart query = SqlQuery.parse("SELECT * FROM games, city WHERE games.city = city.name AND city.country = 'UK'");
Map<ColumnRef, Object> conditions = Collections.singletonMap(ColumnRef.resolve(query, "games.yr"), 2012);
new FilterApplier().addFilter(query, conditions);
assertEquals("SELECT * FROM games, city WHERE games.city = city.name AND city.country = 'UK' AND games.yr = 2012", new Formatter().format(query, new Context(Dbms.POSTGRESQL), new DefaultParserContext()));
query = SqlQuery.parse("SELECT * FROM games RIGHT JOIN city ON (games.city = city.name) WHERE city.country ='UK'");
new FilterApplier().addFilter(query, conditions);
assertEquals("SELECT * FROM games RIGHT JOIN city ON (games.city = city.name) WHERE city.country ='UK' AND games.yr = 2012", new Formatter().format(query, new Context(Dbms.POSTGRESQL), new DefaultParserContext()));
query = SqlQuery.parse("SELECT * FROM games RIGHT JOIN city ON (games.city = city.name) WHERE city.country ='UK' OR city.active = 'yes'");
new FilterApplier().addFilter(query, conditions);
assertEquals("SELECT * FROM games RIGHT JOIN city ON (games.city = city.name) WHERE ( city.country ='UK' OR city.active = 'yes') AND games.yr = 2012", new Formatter().format(query, new Context(Dbms.POSTGRESQL), new DefaultParserContext()));
query = SqlQuery.parse("SELECT * FROM games, city ORDER BY 1");
new FilterApplier().addFilter(query, conditions);
assertEquals("SELECT * FROM games, city WHERE games.yr = 2012 ORDER BY 1", new Formatter().format(query, new Context(Dbms.POSTGRESQL), new DefaultParserContext()));
}
use of com.developmentontheedge.sql.format.FilterApplier in project be5 by DevelopmentOnTheEdge.
the class FilterHelper method applyFilters.
public void applyFilters(AstStart ast, String mainEntityName, Map<String, Object> parameters) {
Set<String> usedParams = ast.tree().select(AstBeParameterTag.class).map(AstBeParameterTag::getName).toSet();
Map<ColumnRef, Object> filters = EntryStream.of(parameters).removeKeys(usedParams::contains).removeKeys(keywords::contains).mapKeys(k -> ColumnRef.resolve(ast, k.contains(".") ? k : mainEntityName + "." + k)).nonNullKeys().toMap();
if (!filters.isEmpty()) {
new FilterApplier().addFilter(ast, filters);
}
}
use of com.developmentontheedge.sql.format.FilterApplier in project be5 by DevelopmentOnTheEdge.
the class FilterApplierTest method testSetFilterApplierUnion.
@Test
public void testSetFilterApplierUnion() {
AstStart query = SqlQuery.parse("SELECT name FROM bbc WHERE name LIKE 'Z%' UNION SELECT name FROM actor WHERE name LIKE 'Z%'");
Map<ColumnRef, Object> conditions = Collections.singletonMap(ColumnRef.resolve(query, "name"), "name");
new FilterApplier().setFilter(query, conditions);
assertEquals("SELECT * FROM (SELECT name FROM bbc UNION SELECT name FROM actor) tmp WHERE name ='name'", new Formatter().format(query, new Context(Dbms.POSTGRESQL), new DefaultParserContext()));
// conditions = Collections.singletonMap( ColumnRef.resolve( query, "name1" ), "name1" );
// new FilterApplier().setFilter( query, conditions );
// assertEquals( "SELECT * FROM (SELECT name FROM bbc UNION SELECT name FROM actor) tmp WHERE name1 ='name1'",
// new Formatter().format( query, new Context( Dbms.POSTGRESQL ), new DefaultParserContext() ) );
}
use of com.developmentontheedge.sql.format.FilterApplier in project be5 by DevelopmentOnTheEdge.
the class FilterApplierTest method testSetFilterApplier.
@Test
public void testSetFilterApplier() {
AstStart query = SqlQuery.parse("SELECT * FROM games g, city WHERE g.city = city.name");
Map<ColumnRef, Object> conditions = EntryStream.<String, Object>of("city.country", "UK", "games.yr", 2012).mapKeys(key -> ColumnRef.resolve(query, key)).toCustomMap(LinkedHashMap::new);
new FilterApplier().setFilter(query, conditions);
assertEquals("SELECT * FROM games g, city WHERE city.country ='UK' AND g.yr = 2012", new Formatter().format(query, new Context(Dbms.POSTGRESQL), new DefaultParserContext()));
AstStart query2 = SqlQuery.parse("SELECT city.name, g.* FROM city INNER JOIN games g ON (g.city = city.name)");
new FilterApplier().setFilter(query2, conditions);
assertEquals("SELECT city.name, g.* FROM city INNER JOIN games g WHERE city.country ='UK' AND g.yr = 2012", new Formatter().format(query2, new Context(Dbms.POSTGRESQL), new DefaultParserContext()));
AstStart query3 = SqlQuery.parse("SELECT * FROM city JOIN games g ON (g.city = city.name) JOIN games gm ON city.country ='UK'");
new FilterApplier().setFilter(query3, conditions);
assertEquals("SELECT * FROM city INNER JOIN games g INNER JOIN games gm WHERE city.country ='UK' AND g.yr = 2012", new Formatter().format(query3, new Context(Dbms.POSTGRESQL), new DefaultParserContext()));
}
Aggregations