Search in sources :

Example 6 with AstStart

use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.

the class ContextApplierApplyParametersTest method number.

@Test
public void number() {
    AstStart start = SqlQuery.parse("SELECT * FROM table WHERE totalSize = <parameter:totalSize />");
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().parameter("totalSize", "123").build());
    contextApplier.applyContext(start);
    assertEquals("SELECT * FROM table WHERE totalSize = 123", start.getQuery().toString());
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) BasicQueryContext(com.developmentontheedge.sql.format.BasicQueryContext) Test(org.junit.Test)

Example 7 with AstStart

use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.

the class ContextApplierApplyParametersTest method string.

@Test
public void string() {
    AstStart start = SqlQuery.parse("SELECT * FROM table WHERE name = '<parameter:name />'");
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().parameter("name", "test").build());
    contextApplier.applyContext(start);
    assertEquals("SELECT * FROM table WHERE name = 'test'", start.getQuery().toString());
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) BasicQueryContext(com.developmentontheedge.sql.format.BasicQueryContext) Test(org.junit.Test)

Example 8 with AstStart

use of com.developmentontheedge.sql.model.AstStart 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 9 with AstStart

use of com.developmentontheedge.sql.model.AstStart 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 10 with AstStart

use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.

the class OrderByFilterTest method testOrderByFilterUnion.

@Test
public void testOrderByFilterUnion() {
    AstStart start = SqlQuery.parse("SELECT name FROM bbc WHERE name LIKE 'Z%' UNION SELECT name FROM actor WHERE name LIKE 'Z%'");
    Map<String, String> columns = Collections.singletonMap("name", "DESC");
    new OrderByFilter().apply(start, columns);
    assertEquals("SELECT * FROM (SELECT name FROM bbc WHERE name LIKE 'Z%' UNION SELECT name FROM actor WHERE name LIKE 'Z%') " + "tmp ORDER BY 1 DESC", new Formatter().format(start, new Context(Dbms.MYSQL), 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) OrderByFilter(com.developmentontheedge.sql.format.OrderByFilter) Test(org.junit.Test)

Aggregations

AstStart (com.developmentontheedge.sql.model.AstStart)34 Test (org.junit.Test)26 ContextApplier (com.developmentontheedge.sql.format.ContextApplier)16 Context (com.developmentontheedge.sql.format.Context)13 Formatter (com.developmentontheedge.sql.format.Formatter)13 DefaultParserContext (com.developmentontheedge.sql.model.DefaultParserContext)11 BasicQueryContext (com.developmentontheedge.sql.format.BasicQueryContext)10 AstBeSqlSubQuery (com.developmentontheedge.sql.model.AstBeSqlSubQuery)8 HashMap (java.util.HashMap)8 Map (java.util.Map)7 SqlQuery (com.developmentontheedge.sql.model.SqlQuery)6 Collections (java.util.Collections)6 Assert (org.junit.Assert)5 QueryResolver (com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver)4 ColumnRef (com.developmentontheedge.sql.format.ColumnRef)4 FilterApplier (com.developmentontheedge.sql.format.FilterApplier)4 Ignore (org.junit.Ignore)4 ColumnAdder (com.developmentontheedge.sql.format.ColumnAdder)3 Dbms (com.developmentontheedge.sql.format.Dbms)3 LimitsApplier (com.developmentontheedge.sql.format.LimitsApplier)3