Search in sources :

Example 1 with DefaultParserContext

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

the class TestMain method testProject.

private static StatContext testProject(Project project, FailData failedQueries) {
    String name = project.getName();
    Rdbms dbms = project.getDatabaseSystem();
    StatContext stat = new StatContext(name + "/" + dbms, "Count", "FMF", "PE", "FE", "PF", "RFE", "RFM", "Success");
    SqlParser parser = new SqlParser();
    parser.setContext(new DefaultParserContext());
    Formatter formatter = new Formatter();
    Context context = new Context(Arrays.stream(Dbms.values()).filter(d -> d.getType() == dbms.getType()).findFirst().get());
    for (String entityName : project.getEntityNames()) {
        Entity entity = project.getEntity(entityName);
        for (Query query : entity.getQueries()) {
            if (!allowedTypes.contains(query.getType()))
                continue;
            if (query.getName().equals("Table definition"))
                continue;
            if (query.getQuery().trim().isEmpty())
                continue;
            stat.inc("Count");
            String queryText;
            try {
                queryText = query.getQueryCompiled().validate();
            } catch (ProjectElementException e) {
                stat.inc("FMF");
                continue;
            }
            parser.parse(queryText);
            if (!parser.getMessages().isEmpty()) {
                if (knownNonExpanded.stream().anyMatch(queryText::contains)) {
                    stat.inc("FMF");
                    continue;
                }
                failedQueries.add(query, queryText);
                // System.out.println("----------------------\nQuery: "+entity.getName()+"."+query.getName()+"\n"+queryText+"\n\n"+String.join("\n", parser.getMessages()));
                stat.inc("PE");
                continue;
            }
            AstStart astStart = parser.getStartNode();
            String format;
            try {
                format = formatter.format(astStart, context, parser.getContext());
                Objects.requireNonNull(format);
            } catch (Exception e) {
                failedQueries.add(query, queryText);
                System.out.println("Err: " + entity.getName() + "." + query.getName() + ": " + e.getMessage());
                stat.inc("FE");
                continue;
            }
            parser.parse(format);
            if (!parser.getMessages().isEmpty()) {
                failedQueries.add(query, queryText);
                // System.out.println("PF: "+queryText+"\n"+"Formatted: "+format+"\n"+parser.getMessages());
                stat.inc("PF");
                continue;
            }
            astStart = parser.getStartNode();
            String format2;
            try {
                format2 = formatter.format(astStart, context, parser.getContext());
                Objects.requireNonNull(format2);
            } catch (Exception e) {
                stat.inc("RFF");
                continue;
            }
            if (!format.equals(format2)) {
                failedQueries.add(query, queryText);
                // System.out.println("Orig : "+queryText);
                // System.out.println("Form1: "+format);
                // System.out.println("Form2: "+format2);
                stat.inc("RFM");
                continue;
            }
            stat.inc("Success");
        }
    }
    return stat;
}
Also used : DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) Context(com.developmentontheedge.sql.format.Context) LoadContext(com.beanexplorer.enterprise.metadata.serialization.LoadContext) Arrays(java.util.Arrays) ModuleUtils(com.beanexplorer.enterprise.metadata.util.ModuleUtils) AstStart(com.developmentontheedge.sql.model.AstStart) ProjectElementException(com.beanexplorer.enterprise.metadata.exception.ProjectElementException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) Formatter(com.developmentontheedge.sql.format.Formatter) Locale(java.util.Locale) Map(java.util.Map) ReadException(com.beanexplorer.enterprise.metadata.exception.ReadException) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) Entity(com.beanexplorer.enterprise.metadata.model.Entity) Project(com.beanexplorer.enterprise.metadata.model.Project) Rdbms(com.beanexplorer.enterprise.metadata.sql.Rdbms) SqlModelReader(com.beanexplorer.enterprise.metadata.sql.SqlModelReader) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) Path(java.nio.file.Path) Context(com.developmentontheedge.sql.format.Context) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) MoreCollectors(one.util.streamex.MoreCollectors) SqlParser(com.developmentontheedge.sql.model.SqlParser) Set(java.util.Set) IOException(java.io.IOException) EntryStream(one.util.streamex.EntryStream) Collectors(java.util.stream.Collectors) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) ProcessInterruptedException(com.beanexplorer.enterprise.metadata.exception.ProcessInterruptedException) Query(com.beanexplorer.enterprise.metadata.model.Query) BeSqlExecutor(com.beanexplorer.enterprise.metadata.sql.BeSqlExecutor) List(java.util.List) TreeMap(java.util.TreeMap) Paths(java.nio.file.Paths) ProjectLoadException(com.beanexplorer.enterprise.metadata.exception.ProjectLoadException) StreamEx(one.util.streamex.StreamEx) LoadContext(com.beanexplorer.enterprise.metadata.serialization.LoadContext) Entry(java.util.Map.Entry) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) Dbms(com.developmentontheedge.sql.format.Dbms) Entity(com.beanexplorer.enterprise.metadata.model.Entity) ProjectElementException(com.beanexplorer.enterprise.metadata.exception.ProjectElementException) Rdbms(com.beanexplorer.enterprise.metadata.sql.Rdbms) Query(com.beanexplorer.enterprise.metadata.model.Query) Formatter(com.developmentontheedge.sql.format.Formatter) SqlParser(com.developmentontheedge.sql.model.SqlParser) ProjectElementException(com.beanexplorer.enterprise.metadata.exception.ProjectElementException) SQLException(java.sql.SQLException) ReadException(com.beanexplorer.enterprise.metadata.exception.ReadException) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ProcessInterruptedException(com.beanexplorer.enterprise.metadata.exception.ProcessInterruptedException) ProjectLoadException(com.beanexplorer.enterprise.metadata.exception.ProjectLoadException) AstStart(com.developmentontheedge.sql.model.AstStart) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext)

Example 2 with DefaultParserContext

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

the class CategoryFilterTest method testCategoryFilter.

@Test
public void testCategoryFilter() {
    AstStart start = SqlQuery.parse("SELECT t.a, q.b FROM myTable t, otherTable q JOIN oneMoreTable a ON (a.ID=q.ID) WHERE t.b > 2");
    new CategoryFilter("myTable", "ID", 123).apply(start);
    assertEquals("SELECT t.a, q.b FROM myTable t " + "INNER JOIN classifications ON classifications.categoryID = 123 " + "AND classifications.recordID = t.ID, otherTable q " + "INNER JOIN oneMoreTable a ON (a.ID = q.ID) " + "WHERE t.b > 2", 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) CategoryFilter(com.developmentontheedge.sql.format.CategoryFilter) Test(org.junit.Test)

Example 3 with DefaultParserContext

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

the class CloneTest method testAstClone.

@Test
public void testAstClone() {
    AstStart start = SqlQuery.parse("SELECT * FROM table1, table2 WHERE col = 'value'");
    AstStart clone = start.clone();
    assertEquals(start.jjtGetNumChildren(), clone.jjtGetNumChildren());
    assertEquals(start.dump(), clone.dump());
    ((AstSelect) clone.getQuery().child(0)).getFrom().removeChild(1);
    // TODO: Current expected value is incorrect and should be fixed when formatter will be improved
    assertEquals("SELECT * FROM table1 WHERE col = 'value'", new Formatter().format(clone, new Context(Dbms.ORACLE), new DefaultParserContext()));
    assertEquals("SELECT * FROM table1, table2 WHERE col = 'value'", new Formatter().format(start, new Context(Dbms.ORACLE), 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) Test(org.junit.Test)

Example 4 with DefaultParserContext

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

use of com.developmentontheedge.sql.model.DefaultParserContext 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)

Aggregations

Context (com.developmentontheedge.sql.format.Context)11 Formatter (com.developmentontheedge.sql.format.Formatter)11 DefaultParserContext (com.developmentontheedge.sql.model.DefaultParserContext)11 AstStart (com.developmentontheedge.sql.model.AstStart)10 Test (org.junit.Test)9 ColumnRef (com.developmentontheedge.sql.format.ColumnRef)4 FilterApplier (com.developmentontheedge.sql.format.FilterApplier)4 ProjectElementException (com.beanexplorer.enterprise.metadata.exception.ProjectElementException)2 Entity (com.beanexplorer.enterprise.metadata.model.Entity)2 Project (com.beanexplorer.enterprise.metadata.model.Project)2 Query (com.beanexplorer.enterprise.metadata.model.Query)2 LoadContext (com.beanexplorer.enterprise.metadata.serialization.LoadContext)2 Rdbms (com.beanexplorer.enterprise.metadata.sql.Rdbms)2 Dbms (com.developmentontheedge.sql.format.Dbms)2 OrderByFilter (com.developmentontheedge.sql.format.OrderByFilter)2 SqlParser (com.developmentontheedge.sql.model.SqlParser)2 SimpleDateFormat (java.text.SimpleDateFormat)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ProcessInterruptedException (com.beanexplorer.enterprise.metadata.exception.ProcessInterruptedException)1