Search in sources :

Example 1 with ColumnRef

use of com.developmentontheedge.sql.format.ColumnRef 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()));
}
Also used : Context(com.developmentontheedge.sql.format.Context) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) AstStart(com.developmentontheedge.sql.model.AstStart) Formatter(com.developmentontheedge.sql.format.Formatter) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) FilterApplier(com.developmentontheedge.sql.format.FilterApplier) ColumnRef(com.developmentontheedge.sql.format.ColumnRef) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 2 with ColumnRef

use of com.developmentontheedge.sql.format.ColumnRef 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()));
}
Also used : Context(com.developmentontheedge.sql.format.Context) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) AstStart(com.developmentontheedge.sql.model.AstStart) Formatter(com.developmentontheedge.sql.format.Formatter) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) FilterApplier(com.developmentontheedge.sql.format.FilterApplier) ColumnRef(com.developmentontheedge.sql.format.ColumnRef) Test(org.junit.Test)

Example 3 with ColumnRef

use of com.developmentontheedge.sql.format.ColumnRef 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);
    }
}
Also used : FilterApplier(com.developmentontheedge.sql.format.FilterApplier) ColumnRef(com.developmentontheedge.sql.format.ColumnRef)

Example 4 with ColumnRef

use of com.developmentontheedge.sql.format.ColumnRef 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() ) );
}
Also used : Context(com.developmentontheedge.sql.format.Context) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) AstStart(com.developmentontheedge.sql.model.AstStart) Formatter(com.developmentontheedge.sql.format.Formatter) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) FilterApplier(com.developmentontheedge.sql.format.FilterApplier) ColumnRef(com.developmentontheedge.sql.format.ColumnRef) Test(org.junit.Test)

Example 5 with ColumnRef

use of com.developmentontheedge.sql.format.ColumnRef 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()));
}
Also used : Context(com.developmentontheedge.sql.format.Context) FilterApplier(com.developmentontheedge.sql.format.FilterApplier) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test) AstStart(com.developmentontheedge.sql.model.AstStart) EntryStream(one.util.streamex.EntryStream) SqlQuery(com.developmentontheedge.sql.model.SqlQuery) LinkedHashMap(java.util.LinkedHashMap) Formatter(com.developmentontheedge.sql.format.Formatter) Map(java.util.Map) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) Assert(org.junit.Assert) ParseException(java.text.ParseException) Collections(java.util.Collections) ColumnRef(com.developmentontheedge.sql.format.ColumnRef) Dbms(com.developmentontheedge.sql.format.Dbms) Context(com.developmentontheedge.sql.format.Context) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) AstStart(com.developmentontheedge.sql.model.AstStart) Formatter(com.developmentontheedge.sql.format.Formatter) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) FilterApplier(com.developmentontheedge.sql.format.FilterApplier) ColumnRef(com.developmentontheedge.sql.format.ColumnRef) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

ColumnRef (com.developmentontheedge.sql.format.ColumnRef)5 FilterApplier (com.developmentontheedge.sql.format.FilterApplier)5 Context (com.developmentontheedge.sql.format.Context)4 Formatter (com.developmentontheedge.sql.format.Formatter)4 AstStart (com.developmentontheedge.sql.model.AstStart)4 DefaultParserContext (com.developmentontheedge.sql.model.DefaultParserContext)4 Test (org.junit.Test)4 SimpleDateFormat (java.text.SimpleDateFormat)2 Dbms (com.developmentontheedge.sql.format.Dbms)1 SqlQuery (com.developmentontheedge.sql.model.SqlQuery)1 ParseException (java.text.ParseException)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 EntryStream (one.util.streamex.EntryStream)1 Assert (org.junit.Assert)1